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

时间:2017-07-19 15:30:38

标签: php

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

OLD (我现在拥有的)

$router->get('user-account', 'PagesController@account');

(我在寻找什么)

$router->get('user-account/{id}','PagesController@account');

这是请求类型的路由器文件,它将请求路由到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();
    }

}

我希望能够通过路由器将数据传递给页面控制器中的函数,就像Laravel一样。

0 个答案:

没有答案