gpt4 book ai didi

php - 如何在 Phalcon 中设置 404 页面

转载 作者:IT王子 更新时间:2023-10-29 00:04:50 25 4
gpt4 key购买 nike

如何在 Phalcon 中设置 404 页面以在 Controller / Action 不存在时显示?

最佳答案

您可以设置调度员为您执行此操作。

当您引导您的应用程序时,您可以这样做($di 是您的 DI 工厂):

use \Phalcon\Mvc\Dispatcher as PhDispatcher;

$di->set(
'dispatcher',
function() use ($di) {

$evManager = $di->getShared('eventsManager');

$evManager->attach(
"dispatch:beforeException",
function($event, $dispatcher, $exception)
{
switch ($exception->getCode()) {
case PhDispatcher::EXCEPTION_HANDLER_NOT_FOUND:
case PhDispatcher::EXCEPTION_ACTION_NOT_FOUND:
$dispatcher->forward(
array(
'controller' => 'error',
'action' => 'show404',
)
);
return false;
}
}
);
$dispatcher = new PhDispatcher();
$dispatcher->setEventsManager($evManager);
return $dispatcher;
},
true
);

创建一个ErrorController

<?php

/**
* ErrorController
*/
class ErrorController extends \Phalcon\Mvc\Controller
{
public function show404Action()
{
$this->response->setStatusCode(404, 'Not Found');
$this->view->pick('404/404');
}
}

和一个 404 View (/views/404/404.volt)

<div align="center" id="fourohfour">
<div class="sub-content">
<strong>ERROR 404</strong>
<br />
<br />
You have tried to access a page which does not exist or has been moved.
<br />
<br />
Please click the links at the top navigation bar to
navigate to other parts of the site, or
if you wish to contact us, there is information in the About page.
<br />
<br />
[ERROR]
</div>
</div>

关于php - 如何在 Phalcon 中设置 404 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14071261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com