我正在嘗試創建一個自定義路由器。 這是我到目前爲止有:自定義路由器中的參數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');
如何修改我的路由器以實現此目的?
這已經這麼久了偉大的工作,一個很簡單的例子!現在,我需要有希臘字符的UR;所以我需要添加像[[α-ωA-Ω]]。你知道我在這裏能做到嗎? – padawanTony
@padawanTony你可以嘗試'{希臘語}'字符集或'[α-ωA-Ω]'。全字母解決方案'[A-ZA-zA-Ωα-ωίϊlόάέύϋΰήώ]' – newage
由於這些不起作用,我創建了一個新問題,我可以更好地解釋這種情況。看看你是否想要:http://stackoverflow.com/questions/39076407/regex-greek-characters-in-url – padawanTony