Customized 404 page in PHP Phalcon framework
IPFS
在一般狀況下其實不需要自訂404 page也不會怎麼樣,但是如果用 phalcon dev tools 做 CRUD 頁面出來,有些狀況可能是不需要 create 資料的,比方說對 log 只需要看,沒有道理從頁面上手動 create
在 app/controllers/ 下開一個 ErrorController.php
<?php /** * ErrorController */ class ErrorController extends \Phalcon\Mvc\Controller { public function show404Action() { $this->response->setStatusCode(404, 'Not Found'); error_log("test"); $this->view->pick('404/404'); } }
在 app/config/services.php 新增
$di->set( 'dispatcher', function() use ($di) { $evManager = $di->getShared('eventsManager'); $evManager->attach( "dispatch:beforeException", function($event, $dispatcher, $exception) { switch ($exception->getCode()) { case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: case Dispatcher::EXCEPTION_ACTION_NOT_FOUND: error_log("testssss"); $dispatcher->forward( array( 'controller' => 'Error', 'action' => 'show404', ) ); return false; } } ); $dispatcher = new Dispatcher(); $dispatcher->setEventsManager($evManager); return $dispatcher; }, true );
然後在 app/views/ 新增 404/404.volt
ERROR 404
You have tried to access a page which does not exist or has been moved.
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.
[ERROR]
然後在要關掉功能的 action,例如 LogController 內關掉 newAction()
<?php use Phalcon\Mvc\Model\Criteria; use Phalcon\Paginator\Adapter\Model as Paginator; class LogController extends ControllerBase { /* skip some code */ public function newAction() { $e = new Exception("", Phalcon\Mvc\Dispatcher::EXCEPTION_ACTION_NOT_FOUND); throw $e; } /* skip some code */ }
Original link: Phanix's Blog
喜欢我的作品吗?别忘了给予支持与赞赏,让我知道在创作的路上有你陪伴,一起延续这份热忱!