创建一个自定义的 404 页面–kohana3使用手册
为了在你的 Kohana 应用程序中有一个自定义的 404 页面。你需要试着捕捉所有的无效路由并将它们转到一个显示404信息的特殊控制器/动作。该路由就是下面例子这样。
大多数定制路由应被定义在你的默认路由之前,而捕捉无效部分的路由应该放在你的默认路由之后。
(*注意:你的默认路由必须比未编辑的bootstrap文件里的默认路由来的更特别)
(这个注意点非常含糊,也没给出什么例子或者引用来表述如何编辑默认路由配置到函数属性,因此让用户比较混乱,不知道该如何去做)
Route::set(‘catch_all’, ‘<path>’, array(‘path’ => ‘.+’))
->defaults(array(
‘controller’ => ‘errors’,
‘action’ => ‘404’
));
不要忘记创建控制器和相应的视图文件
/home/kerkness/kohana/application/classes/controller/errors.php
class Controller_Errors extends Controller { public function action_404() { $this->request->status = 404; $this->request->response = View::factory('errors/404'); } }
还有视图文件
/home/kerkness/kohana/application/views/errors/404.php
<h1>404 Not Found</h1>
示例:
// 允许 http://test/security/login, http://test/security/logout, http://test/security/register, http://test/security/verifyemail Route::set('security', 'security/', array('action' => 'login|logout|register|verifyemail')) ->defaults(array( 'controller' => 'security', )); // 允许 http://test/, http://test/index, http://test/restricted Route::set('home', '((/))', array('action' => 'index|restricted', 'ignore' => '.+')) ->defaults(array( 'controller' => 'home', 'action' => 'index', )); // 发送一切到404 Route::set('catch_all', ' ', array('path' => '.+')) ->defaults(array( 'controller' => 'errors', 'action' => '404', ));