在PhalconPHP中重定向到404路由会导致空白页 [英] Redirecting to 404 route in PhalconPHP results in Blank Page
问题描述
我已经设置了一个路由器,并在其中定义了404路由:
I have setup a router, and in it, defined a route for 404s:
<?php
use Phalcon\Mvc\Router;
$router = new Router(FALSE);
$router->removeExtraSlashes(true);
$route = $router->add('/', ['controller' => 'index', 'action' => 'index']);
$route->setName("index");
// other routes defined here...
$router->notFound([
"controller" => "index",
"action" => "route404"
]);
?>
我的IndexController:
My IndexController:
<?php
class IndexController extends ControllerBase
{
public function indexAction()
{
// code removd for berevity
}
public function route404Action() {
// no code here, I just need to show the view.
}
}
?>
我有一个@@ c0>视图,其中只包含一点HTML,我什至尝试将其设置为.volt
文件,没有运气.
And I have a view @ /app/views/index/route404.phtml
that just has a bit of HTML in it, I even tried making it a .volt
file, no luck.
当我转到与任何路线都不匹配的页面时,它可以正常工作.但是,如果我尝试重定向到它,我只会得到一个空白页.例如,在我的一个控制器中,我有这个:
When I go to a page that doesn't match any routes, it works fine. But, if I try to redirect to it, I just get a blank page. For example, in one of my controllers I have this:
if (!$category) {
// show 404
//Tried this next line to test, and it indeed does what you'd expect, I see "Not Found".
// echo "Not Found"; exit;
$response = new \Phalcon\Http\Response();
$response->redirect([
"for" => "index",
"controller" => "index",
"action" => "route404"]
);
return; // i return here so it won't run the code after this if statement.
}
有什么想法吗?该页面完全空白(源代码中没有任何内容),并且我的apache日志中没有错误.
Any ideas? The page is completely blank (nothing in source) and there are no errors in my apache logs.
推荐答案
尝试返回响应对象,而不仅仅是返回空白.示例:
Try returning the response object, not just a blank return. Example:
return $this->response->redirect(...);
但是,我建议使用从调度员转发来显示404页.这样,用户将停留在相同的URL上,浏览器将收到正确的状态码(404).也是这样的SEO友好:)
However I would recommend to use Forward from dispatcher to show 404 pages. This way user will stay on same url and browser will receive the correct status code (404). Also it's SEO friendly this way :)
示例:
if ($somethingFailed) {
return $this->dispatcher->forward(['controller' => 'index', 'action' => 'error404']);
}
// Controller method
function error404Action()
{
$this->response->setStatusCode(404, "Not Found");
$this->view->pick(['_layouts/error-404']);
$this->response->send();
}
这篇关于在PhalconPHP中重定向到404路由会导致空白页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!