当前位置:首页 > PHP教程 > PHP高级教程

thinkphp框架下404页面设置 仅三步

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

在很多网站中都会有使用404页面的时候,在thinkphp框架中该如何设置呢,接下来我介绍其中一种方法,具体内容如下

第一步:在thinkphp框架中的home/comtroller中建一个emptycontroller.class.php,其代码如下:

<?php
namespace homecontroller;
use thinkcontroller;
class emptycontroller extends controller{
  
  //空操作_empty()方法
  function _empty(){
    header("http/1.0 404 not found");
    $this -> display("public:404");
  }
  
  function index(){
    header("http/1.0 404 not found");
    $this -> dislay("public:404");
  }
}
?>

注意:其中 header("http/1.0 404 not found")是定义此状态码未404。

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

<?php
namespace homecontroller;
use thinkcontroller;
class publiccontroller extends controller{
  function _empty(){
    header("location:/bbs/thinkphp/404.html");
  }
}
?> 

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

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

<?php
namespace homecontroller;
// use thinkcontroller;
class indexcontroller extends publiccontroller {
  public function index(){
  
    *
    *
    *
     }
}
?>

注意:将use thinkcontroller;注释掉

以上就是thinkphp 404页面设置的全部内容,希望对大家学习php程序设计有所帮助。


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!