2016-06-29 27 views
0

我正在嘗試創建一個自定義路由器。 這是我到目前爲止有:自定義路由器中的參數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');

如何修改我的路由器以實現此目的?

回答

1

您需要添加一個正則表達式來路由。例如像這樣(這是一個非常簡單的例子):

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

而你需要使用preg_match比較路由和URL。

這是路由器https://github.com/newage/example/blob/master/core/Route/HttpRoute.php

+0

這已經這麼久了偉大的工作,一個很簡單的例子!現在,我需要有希臘字符的UR;所以我需要添加像[[α-ωA-Ω]]。你知道我在這裏能做到嗎? – padawanTony

+0

@padawanTony你可以嘗試'{希臘語}'字符集或'[α-ωA-Ω]'。全字母解決方案'[A-ZA-zA-Ωα-ωίϊlόάέύϋΰήώ]' – newage

+0

由於這些不起作用,我創建了一個新問題,我可以更好地解釋這種情況。看看你是否想要:http://stackoverflow.com/questions/39076407/regex-greek-characters-in-url – padawanTony