Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在自定义PHP框架中将GET变量传递给控制器?_Php - Fatal编程技术网

如何在自定义PHP框架中将GET变量传递给控制器?

如何在自定义PHP框架中将GET变量传递给控制器?,php,Php,这是控制器的路由器。现在我希望能够通过路由器将ID或任何数据传递给控制器,这样我就可以根据收到的数据执行操作,就像在Laravel中一样 旧的(我现在拥有的) 新的(我正在寻找的) 这是一个请求类型的路由器文件,它将请求直接发送到路由器,或者是POST/GET <?php Class Router { public $routes = [ 'GET' => [], 'POST' => [] ]; public stat

这是控制器的路由器。现在我希望能够通过路由器将ID或任何数据传递给控制器,这样我就可以根据收到的数据执行操作,就像在Laravel中一样

旧的(我现在拥有的)

新的(我正在寻找的)

这是一个请求类型的路由器文件,它将请求直接发送到路由器,或者是POST/GET

<?php
Class Router {

    public $routes = [
        'GET' => [],
        'POST' => []
    ];

    public static function load($file)
    {
        $router = new static;
        require $file;
        return $router;
    }

    public function get($uri,  $controller)
    {
        $this->routes['GET'][$uri] = $controller;
    }

    public function post($uri,  $controller)
    {
        $this->routes['POST'][$uri] = $controller;
    }

    public function direct($uri, $requestType)
    {
        if(array_key_exists($uri, $this->routes[$requestType]))
        {
            return $this->callAction(
            ...explode('@', $this->routes[$requestType][$uri])
            );
        }

        throw new Exception('No routes defined for this URL.');
    }

    protected function callAction($controller, $action)
    {
        $controller = new $controller;

        if(! method_exists($controller, $action))
        {
            throw new Exception(
                "{$controller}  does not respond to the {$action} action."
            );
        }

        return $controller->$action();
    }

}

由于某种原因,这段代码的间距太大了。我不明白你的意思。空白。线。之间每一个线路。属于代码。这是一件小事,但它使代码占用的空间是它应该占用的两倍。非常感谢,我将处理它ASP:)我已经重新格式化了代码。。。但就目前而言,没有任何东西可以阻止您使用
$router->get('user-account/{id}','PagesController@account');
(尽管您想要
标记,如果您试图插入
$id
变量),因为
get()
方法所做的只是将索引添加到关联数组中。。。
$router->get('user-account/{id}','PagesController@account');
<?php
Class Router {

    public $routes = [
        'GET' => [],
        'POST' => []
    ];

    public static function load($file)
    {
        $router = new static;
        require $file;
        return $router;
    }

    public function get($uri,  $controller)
    {
        $this->routes['GET'][$uri] = $controller;
    }

    public function post($uri,  $controller)
    {
        $this->routes['POST'][$uri] = $controller;
    }

    public function direct($uri, $requestType)
    {
        if(array_key_exists($uri, $this->routes[$requestType]))
        {
            return $this->callAction(
            ...explode('@', $this->routes[$requestType][$uri])
            );
        }

        throw new Exception('No routes defined for this URL.');
    }

    protected function callAction($controller, $action)
    {
        $controller = new $controller;

        if(! method_exists($controller, $action))
        {
            throw new Exception(
                "{$controller}  does not respond to the {$action} action."
            );
        }

        return $controller->$action();
    }

}