在路由器中调用未命名的控制器和方法
Calling unnamed controller and method in a router
查看 altorouter 问答,我找到了这个示例 here 关于在使用 altorouter 时如何调用命名控制器和方法。
$router->map('GET','/content/[:parent]/?[:child]?', 'content_controller#display_item', 'content');
语法
$router(method, route, target, name(optional));
我知道如何映射方法、路由,但不明白如何将目标映射到 map/call 未命名的控制器或方法?
我试过了,它似乎有效,但我还需要确认我们是否应该这样做
$router->map('GET', '/[a:controller]/[a:action]?', function ($controller, $action = null) {
if ($action=='') {$action = 'index';}
if (method_exists($controller, $action)) {
$controller::$action();
} else {
echo 'missing';
}
});
这里的路由是/[a:controller]/[a:action]?
,目标是一个函数
function ($controller, $action = null) {
if ($action=='') {$action = 'index';}
if (method_exists($controller, $action)) {
$controller::$action();
} else {
echo 'missing';
}
});
获取未命名的控制器并检查 method/action。如果 method/action 为 null,则将索引分配给变量 $action 然后进行映射。
请确认调用未定义控制器和方法是否正确war
查看 altorouter 问答,我找到了这个示例 here 关于在使用 altorouter 时如何调用命名控制器和方法。
$router->map('GET','/content/[:parent]/?[:child]?', 'content_controller#display_item', 'content');
语法
$router(method, route, target, name(optional));
我知道如何映射方法、路由,但不明白如何将目标映射到 map/call 未命名的控制器或方法?
我试过了,它似乎有效,但我还需要确认我们是否应该这样做
$router->map('GET', '/[a:controller]/[a:action]?', function ($controller, $action = null) {
if ($action=='') {$action = 'index';}
if (method_exists($controller, $action)) {
$controller::$action();
} else {
echo 'missing';
}
});
这里的路由是/[a:controller]/[a:action]?
,目标是一个函数
function ($controller, $action = null) {
if ($action=='') {$action = 'index';}
if (method_exists($controller, $action)) {
$controller::$action();
} else {
echo 'missing';
}
});
获取未命名的控制器并检查 method/action。如果 method/action 为 null,则将索引分配给变量 $action 然后进行映射。
请确认调用未定义控制器和方法是否正确war