自定义路由器中的参数 PHP

Parameters in Custom Router PHP

我正在尝试创建自定义路由器。 这是我目前所拥有的:

<?php
/**
 * Created by PhpStorm.
 * User: antony
 * Date: 5/30/16
 * Time: 3:31 PM
 */
namespace Fab\Router;

class Router
{
    private $_getUri = array();
    private $_getController = array();
    private $_getMethod = array();
    private $_postUri = array();
    private $_postController = array();
    private $_postMethod = array();

public function __construct()
{
}

/**
 * Build a collection of internal GET URLs to look for
 * @param $uri - The url that the user types in the browser
 * @param $controller - The controller that will handle the url
 * @param $method - The method of the controller that will run
 */
public function get($uri, $controller, $method)
{
    $this->_getUri[] = $uri;
    $this->_getController[] = $controller;
    $this->_getMethod[] = $method;
}

/**
 * Build a collection of internal POST URLs to look for
 * @param $uri - The url that the user types in the browser
 * @param $controller - The controller that will handle the url
 * @param $method - The method of the controller that will run
 */
public function post($uri, $controller, $method)
{
    $this->_postUri[] = $uri;
    $this->_postController[] = $controller;
    $this->_postMethod[] = $method;
}

public function submit()
{

    if ($_SERVER['REQUEST_METHOD'] === 'GET') {

        var_dump($_SERVER['REQUEST_URI']);
        echo "\n";  

        $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); //get the url

        var_dump($path);

        foreach ($this->_getUri as $key => $value)
        {
            if (preg_match("#^$value$#", $path))
            {
 //                    echo $key . ' => ' . $value; //See what the $path returns

                //Instantiate Controller
                $controller = 'Fab\Controllers\' . $this->_getController[$key];
                $controller = new $controller();

                //Call the appropriate method
                $method = $this->_getMethod[$key];
                $controller->$method();
            }
        }
    } elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {

        $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); //get the url

        foreach ($this->_postUri as $key => $value)
        {
            if (preg_match("#^$value$#", $path))
            {
                //echo $key . ' => ' . $value; //See what the $path returns

                //Instantiate Controller
                $controller = 'Fab\Controllers\' . $this->_postController[$key];
                $controller = new $controller();

                //Call the appropriate method
                $method = $this->_postMethod[$key];
                $controller->$method();
            }
        }
    }

}

}

这让我可以在我的 index.php 中说:

$router->get('/portfolio', 'MainController', 'portfolio');

我现在想做的是为投资组合中的每个项目创建一个单独的页面并赋予它唯一性 URL。 例如,如果我的投资组合中有项目 'dinosaur',我希望能够说: $router->get('/portfolio/dinosaur', 'MainController', 'searchIfDinosaurExistsInDatabase');

所以一般来说,我希望在我的索引中有这样的东西: $router->get('/portfolio/{item}', 'MainController', 'searchInDbForItem');

如何修改我的路由器以实现此目的?

您需要为路由添加正则表达式。例如像这样(这是一个非常简单的例子):

$router->get('/portfolio/[\w\d]+', 'MainController', 'searchInDbForItem');`

并且您需要使用 preg_match 来比较路线和 url。

这是路由器的一个非常简单的例子https://github.com/newage/example/blob/master/core/Route/HttpRoute.php