我正在尝试在Phalcon 1.3.4
中创建动态路由,但如果缺少参数(例如:action
或:params
),则路由不匹配。
这是(工作)代码:
$router = new Phalcon\Mvc\Router(TRUE);
$group = new Phalcon\Mvc\Router\Group([
'namespace' => 'App\\Backoffice',
'controller' => 'Index',
]);
// All the routes start with /group
$group->setPrefix('/backoffice');
// Adding route to group
$group->add('', ['action' => 'index']); // matches /backoffice
$group->add('/:controller', ['controller' => 1]); // matches /backoffice/moderate
$group->add('/:controller/:action', ['controller' => 1, 'action' => 2]);
$group->add('/:controller/:action/:params', ['controller' => 1, 'action' => 2, 'params' => 3]);
$router->mount($group);
是否可以删除冗余的前三条路线并仅保留第四条路线?通过指定默认值来匹配/backoffice
或/backoffice/moderate
。
答案 0 :(得分:0)
这是我初始化路由器的方式:
$router = new \Phalcon\Mvc\Router(false);
$router->removeExtraSlashes(true);
$router->notFound([
"module" => "page",
"controller" => 'index',
"action" => 'index',
]);
文档中还有一个setDefaults()方法:http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Router.html
这有用吗?