I am trying to route in my own mvc framework. Simple route like /controller/action is working properly with following: index.php
$router = new Core\Router();
//Add the routes
$router->add('{controller}/{action}');
Router:
public function add($route, $params = [])
{
// Convert the route to a regular expression: escape forward slashes
$route = preg_replace('/\//', '\\/', $route);
// Convert variables e.g. {controller}
$route = preg_replace('/\{([a-z]+)\}/', '(?P<\1>[a-z-]+)', $route);
// Convert variables with custom regular expressions e.g. {id:\d+}
$route = preg_replace('/\{([a-z]+):([^\}]+)\}/', '(?P<\1>\2)', $route);
// Add start and end delimiters, and case insensitive flag
$route = '/^' . $route . '$/i';
$this->routes[$route] = $params;
}
/**
* Get all the routes from the routing table
*
* @return array
*/
public function getRoutes()
{
return $this->routes;
}
/**
* Match the route to the routes in the routing table, setting the $params
* property if a route is found.
*
* @param string $url The route URL
*
* @return boolean true if a match found, false otherwise
*/
public function match($url)
{
foreach ($this->routes as $route => $params) {
if (preg_match($route, $url, $matches)) {
// Get named capture group values
foreach ($matches as $key => $match) {
if (is_string($key)) {
$params[$key] = $match;
}
}
$this->params = $params;
return true;
}
}
return false;
}
/**
* Get the currently matched parameters
*
* @return array
*/
public function getParams()
{
return $this->params;
}
/**
* Dispatch the route, creating the controller object and running the
* action method
*
* @param string $url The route URL
*
* @return void
*/
public function dispatch($url)
{
$url = $this->removeQueryStringVariables($url);
if ($this->match($url)) {
$controller = $this->params['controller'];
$controller = $this->convertToStudlyCaps($controller);
$controller = $this->getNamespace() . $controller;
if (class_exists($controller)) {
$controller_object = new $controller($this->params);
$action = $this->params['action'];
$action = $this->convertToCamelCase($action);
if (is_callable([$controller_object, $action])) {
$controller_object->$action();
} else {
throw new \Exception("Method $action (in controller $controller) not found");
}
} else {
throw new \Exception("Controller class $controller not found");
}
} else {
throw new \Exception('No route matched.', 404);
}
}
But while i try to add another route like: /controller/id/action in my index.php with:
$router->add('{controller}/{id:\d}/{action}');
It is showing route not found exception. What wrong am i doing here. Can anyone tell me how can i solve this problem?
class Router {
protected $routes = [];
protected $params = [];
public function add($route,$action) {
$route = preg_replace('/^\//','',$route);
$route = preg_replace('/\//','\\/',$route);
$route = preg_replace('/\{([a-z]+)\}/','(?<\1>[a-z0-9-]+)',$route);
$route = '/^'. $route .'\/?$/i';
$action = is_array($action) ? $action ['uses'] : $action;
list($params['controller'] , $params['method']) = explode('@',$action);
$this->routes[$route] = $params;
}
public function match($url) {
foreach ($this->routes as $route => $param) {
if(preg_match($route , $url , $matchess)) {
foreach ($matchess as $key => $match){
if(!is_numeric($key)) {
$param [$key] = $match;
}
}
$this->params = $param;
return true;
}
}
return false;
}
public function getRoutes(): array {
return $this->routes;
}
public function getParams() {
return $this->params;
}
}
我有同样的问题,正在使用 symfony,我已经添加了另一条路线,但所有路线都打开了家庭控制器,请帮忙?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.