首頁  >  文章  >  php教程  >  thinkphp框架下404页面设置 - 申文哲

thinkphp框架下404页面设置 - 申文哲

WBOY
WBOY原創
2016-05-20 10:13:141140瀏覽

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

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

<span style="color: #000000;"><?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 style="color: #000000;"><br></span>

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

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

 

<span style="color: #000000;">php
namespace Home\Controller;
</span><span style="color: #0000ff;">use</span><span style="color: #000000;"> Think\Controller;
</span><span style="color: #0000ff;">class</span> PublicController <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Controller{
    </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> _empty(){
        </span><span style="color: #008080;">header</span>("Location:/bbs/thinkphp/404.html"<span style="color: #000000;">);
    }
}
</span>?>

 

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

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

 

<span style="color: #000000;">php
namespace Home\Controller;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> use Think\Controller;</span>
<span style="color: #0000ff;">class</span> IndexController <span style="color: #0000ff;">extends</span><span style="color: #000000;"> PublicController {
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> index(){
    
        </span>*
        *
        *<span style="color: #000000;">
         }
}
?</span>>

 

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

(完成)

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn