dongzhuo3376 2016-06-29 10:19
浏览 96
已采纳

自定义路由器PHP中的参数

I am trying to create a custom router. This is what I have so far:

<?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 "
";  

        $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();
            }
        }
    }

}

}

This allows me to say in my index.php something like:

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

What I want to do now is to create an individual page for every item in the portfolio and give it a unique URL. For example, if I have the item 'dinosaur' in my portfolio, I want to be able to say: $router->get('/portfolio/dinosaur', 'MainController', 'searchIfDinosaurExistsInDatabase');

So generally, I would like to have in my index sth like: $router->get('/portfolio/{item}', 'MainController', 'searchInDbForItem');

How can I modify my router in order to achieve this?

  • 写回答

1条回答 默认 最新

  • dongya6381 2016-06-29 10:54
    关注

    You need to add an regular expression to route. For example like this (it's a very simple example):

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

    And you need use preg_match for comparison a route and an url.

    It's a very simple example of router https://github.com/newage/example/blob/master/core/Route/HttpRoute.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。