簡體   English   中英

自定義路由器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。 例如,如果我的投資組合中有項目“恐龍”,我想說: $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的一個非常簡單的示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM