欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-04-02 12:11:16
404页面即系统在找不到请求的操作方法和找不到请求的控制器名称时的一种报错行为的优化。 在很多网站中都会有使用404页面的时候,在thinkphp框架中该如何设置呢,接下...

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程序设计有所帮助。