我正在为我的PHP MVC应用程序编写一个路由器,而且我目前需要找到一种方法来将路由中的匹配用作控制器和操作的变量。从另一个字符串中的一个字符串替换匹配项
举例来说,如果我有以下途径:/users/qub1/home
我想用正则表达式与此类似:\/users\/(?!/).*\/(?!/).*
然后我想说明这样的动作:$2
(在例如,这将是家)
而参数传递到这样的动作:$1
(在这个例子中,这将是qub1)。
这将随后执行类似的代码这样:
$controller = new UsersController();
$controller->$2($1);
配置的路由被存储为,例如:
public function setRoute($route, $regex = false, $controller = 'Index', $action = 'index', $parameters = array()) {
if(!$regex) {
$route = preg_quote($route, '/');
}
$this->routes[] = [
'route' => $route,
'controller' => $controller,
'action' => $action,
'parameters' => $parameters
];
}
当上面的例子将被存储这样的:$router->setRoute('\/users\/(?!/).*\/(?!/).*', true, 'User', '$2', [$1]);
所以基本上,我想用一个正则表达式中的匹配组作为变量来替换另一个正则表达式(如果有意义的话)。
我希望我已经足够准确地描述了我的问题。谢谢您的帮助。
编辑:
我目前使用解析路径(这是行不通的,但它应该说明我想要实现)代码:
public function executeRoute($route) {
// Loop over available routes
foreach($this->routes as $currentRoute) {
// Check if the current route matches the provided route
if(preg_match('/^' . $currentRoute['route'] . '$/', '/' . $route, $matches)) {
// If it matches, perform the current route's action
// Define names
$controllerClass = preg_replace('\$.*\d', $matches[str_replace('$', '', '$1')], ucfirst($currentRoute['controller'] . 'Controller'));
$actionMethod = preg_replace('\$.*\d', $matches[str_replace('$', '', '$1')], strtolower($currentRoute['action']) . 'Action');
$parameters = preg_replace('\$.*\d', $matches[str_replace('$', '', '$1')], join(', ', $currentRoute['parameters']));
// Create the controller
$controller = new $controllerClass();
$controller->$actionMethod($parameters);
// Return
return;
}
}
}
嗨,你可以尝试strstr();在PHP http://php.net/strstr –
路由的目的是为了隐藏内部的实现,所以很容易添加新的路由。你认真做它更复杂,它需要是 – Yang
@KarthikKeyan我已经使用strtr,它的工作原理。我首先必须将匹配数组转换为关联数组(将索引预置为'$'),然后将其用作针参数。我现在可以使用如下方式轻松设置路由:'$ router-> setRoute('\/users \ /([^ \ /] *)',true,'Users','index',['$ 1'] );''会在幕后执行这样的事情:'UsersController-> indexAction($ 1)',其中$ 1将是用户名。 – Qub1