PHP自定义URL路由
我正在开发一个定制的PHP URL路由类,但是我需要一些关于正则表达式的帮助。 我希望用户添加如下路由:PHP自定义URL路由,php,regex,url,router,Php,Regex,Url,Router,我正在开发一个定制的PHP URL路由类,但是我需要一些关于正则表达式的帮助。 我希望用户添加如下路由: $router->addRoute('users/:id', 'users/view/'); public function addRoute($url, $target) { $this->routes[] = ['url' => $url, 'target' => $target]; } public function routes() { f
$router->addRoute('users/:id', 'users/view/');
public function addRoute($url, $target)
{
$this->routes[] = ['url' => $url, 'target' => $target];
}
public function routes()
{
foreach($this->routes as $route) {
$pattern = $route['url'];
// Check if the route url contains :id
if (strpos($route['url'], ':id'))
{
// Build the pattern
$pattern = str_replace(':id','(\d+)', $pattern);
}
echo $pattern . '<br />' . $this->_url;
if (preg_match_all('~' . $pattern . '~u', $this->_url, $matches))
{
$this->url_parts = explode('/', $route['target']);
$this->_params = $matches;
}
}
}
private function routes()
{
// Loop through the routes
foreach($this->routes as $route)
{
// Set the pattern to the matching url
$pattern = $route['url'];
// Check if the pattern contains :id
if (strpos($route['url'], ':id'))
{
// Build the pattern
$pattern = str_replace(':id','([0-9]+)', $pattern);
}
// Check if the requested url matches the pattern
if (preg_match_all('~^' . $pattern . '+$~', $this->_url, $matches))
{
// If so, set the url_parts var
$this->url_parts = explode('/', $route['target']);
// Remove the first index of the matches array
array_shift($matches);
// Set the params var
$this->_params = $matches;
}
}
}
添加路由后,脚本需要检查请求的URL是否与定义的格式(users/:id)匹配,并调用users控制器的view方法。它还需要将id作为参数传递给view方法
我的addRoute方法如下所示:
$router->addRoute('users/:id', 'users/view/');
public function addRoute($url, $target)
{
$this->routes[] = ['url' => $url, 'target' => $target];
}
public function routes()
{
foreach($this->routes as $route) {
$pattern = $route['url'];
// Check if the route url contains :id
if (strpos($route['url'], ':id'))
{
// Build the pattern
$pattern = str_replace(':id','(\d+)', $pattern);
}
echo $pattern . '<br />' . $this->_url;
if (preg_match_all('~' . $pattern . '~u', $this->_url, $matches))
{
$this->url_parts = explode('/', $route['target']);
$this->_params = $matches;
}
}
}
private function routes()
{
// Loop through the routes
foreach($this->routes as $route)
{
// Set the pattern to the matching url
$pattern = $route['url'];
// Check if the pattern contains :id
if (strpos($route['url'], ':id'))
{
// Build the pattern
$pattern = str_replace(':id','([0-9]+)', $pattern);
}
// Check if the requested url matches the pattern
if (preg_match_all('~^' . $pattern . '+$~', $this->_url, $matches))
{
// If so, set the url_parts var
$this->url_parts = explode('/', $route['target']);
// Remove the first index of the matches array
array_shift($matches);
// Set the params var
$this->_params = $matches;
}
}
}
处理路由的方法如下所示:
$router->addRoute('users/:id', 'users/view/');
public function addRoute($url, $target)
{
$this->routes[] = ['url' => $url, 'target' => $target];
}
public function routes()
{
foreach($this->routes as $route) {
$pattern = $route['url'];
// Check if the route url contains :id
if (strpos($route['url'], ':id'))
{
// Build the pattern
$pattern = str_replace(':id','(\d+)', $pattern);
}
echo $pattern . '<br />' . $this->_url;
if (preg_match_all('~' . $pattern . '~u', $this->_url, $matches))
{
$this->url_parts = explode('/', $route['target']);
$this->_params = $matches;
}
}
}
private function routes()
{
// Loop through the routes
foreach($this->routes as $route)
{
// Set the pattern to the matching url
$pattern = $route['url'];
// Check if the pattern contains :id
if (strpos($route['url'], ':id'))
{
// Build the pattern
$pattern = str_replace(':id','([0-9]+)', $pattern);
}
// Check if the requested url matches the pattern
if (preg_match_all('~^' . $pattern . '+$~', $this->_url, $matches))
{
// If so, set the url_parts var
$this->url_parts = explode('/', $route['target']);
// Remove the first index of the matches array
array_shift($matches);
// Set the params var
$this->_params = $matches;
}
}
}
公共功能路由()
{
foreach($this->routes as$route){
$pattern=$route['url'];
//检查路由url是否包含:id
if(strpos($route['url'],':id'))
{
//构建模式
$pattern=str_replace(':id','(\d+),$pattern);
}
回显$pattern.“
”。$this->\u url;
if(preg_match_all(“~.”.$pattern.'~u',$this->\u url,$matches))
{
$this->url_parts=explode('/',$route['target']);
$this->_params=$matches;
}
}
}
当前,脚本将遍历路由并检查url是否包含:id
。如果是这样,它将替换为(\d+)
然后脚本检查请求的url是否与模式匹配,并设置一些变量
到目前为止,一切正常,但经过一些测试后,在匹配url时出现了一些问题
我希望脚本只允许采用/users/:id
格式的url,但当我调用以下url时,它将传递给/users/1/test
如何防止脚本允许此URL,并且只允许它与定义的格式匹配?请尝试以下操作:
$router->addRoute('users/(\d+)$', 'users/view/');
我自己解决了这个问题。 我必须在表达式前加一个^,在表达式后加一个+$。 因此函数如下所示:
$router->addRoute('users/:id', 'users/view/');
public function addRoute($url, $target)
{
$this->routes[] = ['url' => $url, 'target' => $target];
}
public function routes()
{
foreach($this->routes as $route) {
$pattern = $route['url'];
// Check if the route url contains :id
if (strpos($route['url'], ':id'))
{
// Build the pattern
$pattern = str_replace(':id','(\d+)', $pattern);
}
echo $pattern . '<br />' . $this->_url;
if (preg_match_all('~' . $pattern . '~u', $this->_url, $matches))
{
$this->url_parts = explode('/', $route['target']);
$this->_params = $matches;
}
}
}
private function routes()
{
// Loop through the routes
foreach($this->routes as $route)
{
// Set the pattern to the matching url
$pattern = $route['url'];
// Check if the pattern contains :id
if (strpos($route['url'], ':id'))
{
// Build the pattern
$pattern = str_replace(':id','([0-9]+)', $pattern);
}
// Check if the requested url matches the pattern
if (preg_match_all('~^' . $pattern . '+$~', $this->_url, $matches))
{
// If so, set the url_parts var
$this->url_parts = explode('/', $route['target']);
// Remove the first index of the matches array
array_shift($matches);
// Set the params var
$this->_params = $matches;
}
}
}