404頁面即系統在找不到請求的操作方法和找不到請求的控制器名稱時的一種報錯行為的優化。

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

<?php
namespace Home\Controller;
use Think\Controller;
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 Home\Controller;
use Think\Controller;
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 Home\Controller;
// use Think\Controller;
class IndexController extends PublicController {
public function index(){

*
*
*
}
}
?>

注意:將use Think\Controller;注釋掉