利用kohana框架自定义404页面
创建一个自定义的 404 页面
为了在你的 Kohana 应用程序中有一个自定义的 404 页面。你需要试着捕捉所有的无效路由幵将它们转
到一个显示 404 信息的特殊控制器/动作。该路由就是下面例子这样。
大多数定制路由应被定义在你的默认路由之前,而捕捉无效部分的路由应该放在你的默认路由之后。
(*注意:你的默认路由必项比未编辑的 bootstrap 文件里的默认路由杢的更特别)
(这个注意点非常吨糊,也没给出什举例子戒者引用杢表述如何编辑默认路由配置到凼数属性,因此让用
户比较混乱,不知道该如何去做)
Route::set('catch_all', '<path>', array('path' => '.+'))
->defaults(array(
'controller' => 'errors',
'action' => '404',
));
不要忘记创建控制器和相应的视图文件
/kohana/application/classes/controller/errors.php
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Errors extends Controller {
public function action_404()
{
$this->request->status = 404;
$view=View::factory('errors/404');
$this->request->response=$view;
$this->response->body($view);
}
还有视图文件
/kohana/application/views/errors/404.php
<h1>404 Not Found</h1>
修改.htaccess文件,加入以下内容
ErrorDocument 404 /errors/404
前提在apache配置,允许可以使用.htaccess文件生效
首先找到apache的配置文件,httpd.conf,用编辑工具打开,或者文本形式打开。
开启rewrite_module模块(将前面的#号去掉)。
修改Directory的AllowOverride为all,注意配置文件中有很多Directory,不要该错了,否则不会生效的,Directory一定是你apache服务的根目录。下图可做参考。
重启apache服务,接下来访问下吧,试试是否设置成功。
访问随机输入的路径,提示404报错