如何像 Zend Framework 一样读取/呈现 URL

2022-01-16 00:00:00 url frameworks php zend-framework

我正在努力在 php 中创建自己的框架.
除了根据 URL 运行框架外,一切都按计划进行.
我无法理解:
www.mydomain.com/folderone/foldertwo/index.php
加载正常 URL从同一 URL 加载 Zend Framework URL 将是
www.mydomain.com/folderone(controller)/folder2(action)/variables

I am working on creating my own framework in php.
Everything is going as planned except working the framework according to the URL.
I cannot understand this:
Normal URL loading from www.mydomain.com/folderone/foldertwo/index.php
Zend Framework URL loading from the same URL would be
www.mydomain.com/folderone(controller)/folder2(action)/variables

我怎样才能创建这种逻辑?
我错过了什么?
我真的致力于创建这个框架.

how can i create that logic?
What am i missing?
I am really dedicated to create this framework.

推荐答案

我在设置框架时有同样的任务.这对我来说是一个解决方案.

I had the same task as I setup my framework. This is a solution work for me.

首先创建您的 .htaccess 文件.设置您的重写条件并排除您的模板路径.你可以这样做(只需复制和粘贴):

Create first your .htaccess file. Set up your rewrite conditions and exclude your template path. You can do it like that (just copy & paste):

RewriteEngine On

Options +Indexes
Options +FollowSymLinks

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/index.php -f
RewriteCond %{DOCUMENT_ROOT}/template -d

RewriteRule ^(.*)$  index.php?$1 [QSA]

在我继续之前,我们至少要创建五个目录:

Before I go ahead, we have to create five directories at least:

/var/www/models/
/var/www/controllers/
/var/www/classes/
/var/www/system/
/var/www/template/

现在,我在 index.php 中添加了一个自动加载功能:

Now, I added an auto loading in my index.php:

<?php
error_reporting(E_ALL ^ E_NOTICE);
session_start();

function autoload($class_name)
{
    $autoloadDirs = array('models', 'classes', 'controllers');

    foreach($autoloadDirs as $dir)
    {
        if(file_exists($dir.'/'.$class_name.'.php'))
        {
            require_once($dir.'/'.$class_name.'.php');
        }
    }
}

spl_autoload_register('autoload');

require_once('system/Calling.php');

Calling::Run();

?>

在上面的脚本中,您会在 Calling.php 中看到 require_once

At the script above you'll see that require_once within Calling.php

class Calling
{
    public static function Run($querystring = null)
    {
        //1. Parameter = Contollername
        //2. Parameter = Action
        $qString = preg_replace('/(/$|^/)/','',$querystring === null ? $_SERVER['QUERY_STRING'] : $querystring);
        $callParam = !empty($qString) ? explode('/', $qString) : array();

        $controllerName = count($callParam) > 0 ?     (class_exists(ucfirst($callParam[0]).'Controller') ? ucfirst(array_shift($callParam)) : 'Error') : 'Main';
        //All controllers have suffix "Controller" -> NameController.php
        //and class name ike NameController
        //If no controller name given, use MainController.php
        $controllerClassName = $controllerName.'Controller';
        //All public methods have suffix "Action" -> myMethodAction
        //If there is no method named, use DefaultAction
        $actionName = count($callParam) > 0 && method_exists($controllerClassName, ucfirst($callParam[0]).'Action') ? ucfirst(array_shift($callParam)) : 'Default';
        $actionFunctionName = $actionName.'Action';

        //Fetch the params
        $param = new stdClass();
        for($i = 0; $i < count($callParam); $i += 2)
        {
            $param->{$callParam[$i]} = isset($callParam[$i + 1]) ? $callParam[$i+1] : null;
        }

        ////////////////////////////////////////////////////////////
        //Init the Controller
        $controller = new $controllerClassName($controllerName, $actionName);
        $controller->$actionFunctionName($param);
        ////////////////////////////////////////////////////////////
        //If you adapt this code: Is up to you to extends your controller  
        //from an internal controller which has the method Display(); 
        $controller->Display();
    }
}

此外,在您的控制器目录中添加您的第一个控制器名称和 MainController.php

Further, in your controller directory add your first controller namend MainController.php

//--> just better if you have also an internal controller with your global stuff 
//--> class MainController extends Controller
class MainController
{

    /** This is the default action
     * @param $params
     * @access public
     * @return
     */
    public function DefaultAction(stdClass $params)
    {
            //-> Do your staff here
    }

     /** This is the second action
     * @param $params
     * @access public
     * @return
     */
    public function SecondAction(stdClass $params)
    {
            //-> Do your staff here
    }

    /** This is the view handling method which has to run at least
     * and I recommend to set up an internal controller and to extend
     * all other controller from it and include this method in your
     * internal controller
     * @param
     * @access
     * @return
     */
    public function Display()
    {
        //-> Run your template here
    }
?>

现在,您可以像这样调用控制器、方法和参数:

Now, you can call the controller, methods and params like that:

//-> Load the main controller with default action
www.example.com/ 
//-> Load the main controller with default action
www.example.com/main/default/ 
//-> Load the main controller with second action
www.example.com/main/second/ 
//-> Load the main controller with second action and gives two params
www.example.com/main/second/key1/value1/key2/value2/ 

现在您将拥有以下文件和目录来启动您自己的框架.

And now you'll have the following files and directories to start up your own framework.

/var/www/.htaccess
/var/www/index.php
/var/www/controllers/MainController.php
/var/www/system/Calling.php

/var/www/models/
/var/www/classes/
/var/www/template/

享受您的新基本框架套件

Enjoy your new basic framework kit

相关文章