Home >php教程 >php手册 >thinkphp框架下404页面设置,thinkphp404

thinkphp框架下404页面设置,thinkphp404

WBOY
WBOYOriginal
2016-06-13 08:40:281110browse

thinkphp框架下404页面设置,thinkphp404

404页面即系统在找不到请求的操作方法和找不到请求的控制器名称时的一种报错行为的优化。

第一步:在thinkphp框架中的Home/Comtroller中建一个EmptyController.class.php,其代码如下:

<span><?php<br />namespace Home\Controller;<br />use Think\Controller;<br />class EmptyController extends Controller{<br />    <br />  //空操作_empty()方法<br />    function _empty(){<br />        header("HTTP/1.0 404 Not Found");<br />        $this -> display("Public:404");<br />    }<br />    <br />    function index(){<br />        header("HTTP/1.0 404 Not Found");<br />        $this -> dislay("Public:404");<br />    }<br />}<br />?></span><span><br /></span>

注意:其中 header("HTTP/1.0 404 Not Found")是定义此状态码未404。

第二步:在thinkphp框架中的Home/Comtroller中建一个公共的类PublicController.class.php,其代码如下:

 

<?<span>php
namespace Home\Controller;
</span><span>use</span><span> Think\Controller;
</span><span>class</span> PublicController <span>extends</span><span> Controller{
    </span><span>function</span><span> _empty(){
        </span><span>header</span>("Location:/bbs/thinkphp/404.html"<span>);
    }
}
</span>?>

 

注意:其中 header("Location:/bbs/thinkphp/404.html")中的/bbs/thinkphp/404.html是你出现404后页面跳转的地址,需和自己的404.html页面放置位对应。

第三步:让其他控制器全部继承 第二步中的PublicController.class.php,比如:

 

<?<span>php
namespace Home\Controller;
</span><span>//</span><span> use Think\Controller;</span>
<span>class</span> IndexController <span>extends</span><span> PublicController {
    </span><span>public</span> <span>function</span><span> index(){
    
        </span>*
        *
        *<span>
         }
}
?</span>>

 

注意:将use Think\Controller;注释掉

(完成)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn