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报错
在这里插入图片描述

转于:http://www.bsdcn.com/ Kohana 中文手册[情人节专版] 本手册为 Kohana Docs v2.2 版本。 本手册制作日期:2009年02月10日 本手册由 icyleaf 制作 --- 参考 常规(General) Kohana 文件系统(Filesystem) - 汉化度 100% 配置(Configuration) - 汉化度 100% URLs - 汉化度 100% 路由(Routing) - 汉化度 99% 加载资源(Loading) - 汉化度 100% 控制器(Controllers) - 汉化度 100% 库(Libraries) - 汉化度 100% 辅助函数(Helpers) - 汉化度 100% 视图(Views) - 汉化度 100% 模型(Models) - 汉化度 100% 事件(Events) - 汉化度 85% 钩子(Hooks) - 汉化度 100% 错误处理(Error Handling) - 汉化度 100% 模块(Modules) - 汉化度 100% 国际化(i18n) - 汉化度 100% 日志(Logging) - 汉化度 100% 核心类(Core) 基准测试类(Benchmark Class) - 汉化度 100% 事件类(Event Class) - 汉化度 100% Kohana 类 - 汉化度 100% Unicode 类 - 汉化度 100% 视图库(View Class) - 汉化度 100% 核心库(Libraries) 缓存库(Cache Library) - 汉化度 100% 日历库(Calendar Library) - 汉化度 95% 验证库(Captcha Library) - 汉化度 99% 数据库库(Database Library) - 汉化度 40% 加密库(Encrypt Library) - 汉化度 100% 图像库(Image Library) - 汉化度 20% 输入库(Input Library) - 汉化度 0% ORM 库 - 汉化度 100% 分页库(Pagination Library) - 汉化度 99% 分析库(Profiler Library) - 汉化度 100% Session 库 - 汉化度 100% URI 库 - 汉化度 99% 校验库(Validation Library) - 汉化度 99% 辅助函数(Helpers) 数组辅助函数 - 汉化度 100% Cookie 辅助函数 - 汉化度 98% 日期辅助函数 - 汉化度 100% 下载辅助函数 - 汉化度 100% Email 辅助函数 - 汉化度 100% Expires Helper - 汉化度 0% Feed 辅助函数 - 汉化度 100% 文件辅助函数 - 汉化度 100% 表单辅助函数 - 汉化度 45% HTML 辅助函数 - 汉化度 100% Inflector Helper - 汉化度 0% 数字辅助函数 - 汉化度 100% 请求辅助函数 - 汉化度 80% 安全性辅助函数 - 汉化度 100% 文本辅助函数 - 汉化度 20% 上传辅助函数 - 汉化度 100% URL 辅助函数 - 汉化度 100% 校验辅助函数 - 汉化度 8% 附加模块(Addons) Archive 扩展 - 汉化度 99% Auth 扩展 - 汉化度 100% Gmaps 扩展 - 汉化度 100% Kodoc 扩展 - 汉化度 100% Payment 扩展 - 汉化度 99%
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值