我现在只是在抨击 Phalcon,试图在我考虑在继续开发之前将一个大项目从 CodeIgniter 移植到 Phalcon 或其他框架时了解它。
我正在使用路由器 try catch 404 错误,但这并没有按预期工作。它自己的路由器似乎正在工作,因为如果未定义 index.php,我也有 $router->add("/"
来获取站点索引 Controller ,并且如果我将其注释掉,则继续到家/然后抛出 404!但是,输入一些不存在的 Controller 名称,例如测试,给我错误“PhalconException:无法加载 TestController 处理程序类”。我的 htaccess 是按照 Phalcon 文档教程。
在 Bootstrap (index.php)中我有;
$di->set('路由器', function() {
需要 __DIR__ 。 '/../app/config/routes.php';
返回$路由器;
});
我的 routes.php 看起来像这样;
$router = new \Phalcon\Mvc\Router();
// default
$router->add("/", array(
'controller' => 'index',
'action' => 'index'
));
//Set 404 paths
$router->notFound(array(
"controller" => "group",
"action" => "index"
));
$router->handle();
return $router;
该网站有用户提交的组,可以通过 domain.com/group-name/在 CI 中使用 404 路由来加载组 Controller ,这将激活对该组的搜索。如果找到它会显示该组,如果找不到它会抛出一个带有 404 标题的 404 页面。很可能在 Phalcon 中有更好的方法来做到这一点,但我至少很想知道为什么我的 notFound rout 失败了;)
编辑:不确定这是否改变了什么,但我使用的是所有主 Controller 都扩展的基本 Controller “controllerBase”。它只包含一些变量和一个初始化函数以及另一个可以从 Controller 调用的函数。我不认为这会影响 notFound 路由,但我认为我最好以任何方式提及它。
最佳答案
\Phalcon\Mvc\Router 的默认行为不使用 notFound() 方法。
如果您想使用 notFound(),您需要先禁用默认路由,如下所述:http://docs.phalconphp.com/en/latest/reference/routing.html#default-behavior
在您的示例中,您只需更改:
$router = new \Phalcon\Mvc\Router();
以下内容:
$router = new \Phalcon\Mvc\Router(false);
关于php - Phalcon php 路由器 notFound 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23795417/