thinkphp 访问不存在url怎么设置404

如题所述

空控制器的概念是指当系统找不到请求的控制器名称的时候,系统会尝试定位空控制器(EmptyController),利用这个机制我们可以用来定制错误页面和进行URL的优化。

/**
 * 空模块,主要用于显示404页面,请不要删除
 */
class EmptyController extends HomeController {
 // 没有任何方法,直接执行HomeController的_empty方法
 // 请不要删除该控制器
     function _empty() {
          header ( "HTTP/1.0 404 Not Found" ); // 使HTTP返回404状态码
          $this->display ( "Public:404" );
     }
     function index() {
          header ( " HTTP/1.0  404  Not Found" );
          $this->display ( "Public:404" );
     }
}

空操作是指系统在找不到请求的操作方法的时候,会定位到空操作(_empty)方法来执行,利用这个机制,我们可以实现错误页面和一些URL的优化。

/* 空操作,用于输出404页面 */
 public function _empty() {
      header ( "HTTP/1.0 404 Not Found" ); // 使HTTP返回404状态码
      $this->display ( "Public:404" );
      exit;
 }

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-06-05
404 页面代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>很抱歉,您搜索的书搬家啦-言书库!</title>
<style type="text/css">
body {margin: 0px; padding:0px; font-family:"微软雅黑", Arial, "Trebuchet MS", Verdana, Georgia,Baskerville,Palatino,Times; font-size:16px;}
div{margin-left:auto; margin-right:auto;}
a {text-decoration: none; color: #1064A0;}
a:hover {color: #0078D2;}
img { border:none; }
h1,h2,h3,h4 {
/* display:block;*/
margin:0;
font-weight:normal;
font-family: "微软雅黑", Arial, "Trebuchet MS", Helvetica, Verdana ;
}
h1{font-size:44px; color:#0188DE; padding:20px 0px 10px 0px;}
h2{color:#0188DE; font-size:16px; padding:10px 0px 40px 0px;}
#page{width:910px; padding:20px 20px 40px 20px; margin-top:80px;}
.button{width:180px; height:28px; margin-left:0px; margin-top:10px; background:#009CFF; border-bottom:4px solid #0188DE; text-align:center;}
.button a{width:180px; height:28px; display:block; font-size:14px; color:#fff; }
.button a:hover{ background:#5BBFFF;}
</style>
</head>
<body>
<div id="page" style="border-style:dashed;border-color:#e4e4e4;line-height:30px;background:url(sorry.png) no-repeat right;">
<h1>腹有诗书气自华~</h1>
<h2>言书库,完本小说在线阅读网址 </h2>
<meta http-equiv="refresh" content="1.5;url=http://www.yanshuku.com">
<font color="#666666">2秒内,若网页未能自动跳转,请点击下面按钮进行跳转!</font><br /><br />
<div class="button">
<a href="http://www.yanshuku.com" title="进入首页">立即进入首页</a>
</div>
</div>
</body>
</html>

TP配置:
无法加载模板跳向404页面
/thinkphp/library/think/Dispatcher.class.php中176行
// 加载模块的扩展配置文件
load_ext_file(MODULE_PATH);
}else{
header("Location:/404.html");die;
// E(L(‘_MODULE_NOT_EXIST_‘).‘:‘.MODULE_NAME);
}
加上header跳转页面,404.html放在跟下

无法加载控制器跳向404页面
创建一个EmptyController.class.php 代码如下

namespace Home\Controller;
use Think\Controller;
class EmptyController extends Controller
{
public function _empty(){
$this->display(‘Error/404‘);//在Home/view中Error文件夹中
}
}
这样就行
无法加载方法跳向404页面
在/thinkphp/library/think/Controller.class.php在170行加上重跳转404页面
public function __call($method,$args) {
if( 0 === strcasecmp($method,ACTION_NAME.C(‘ACTION_SUFFIX‘))) {
if(method_exists($this,‘_empty‘)) {
// 如果定义了_empty操作 则调用
$this->_empty($method,$args);
}elseif(file_exists_case($this->view->parseTemplate())){
// 检查是否存在默认模版 如果有直接输出模版
$this->display();
}else{
$this->display(‘Error/404‘);
// E(L(‘_ERROR_ACTION_‘).‘:‘.ACTION_NAME);
}
}else{
E(__CLASS__.‘:‘.$method.L(‘_METHOD_NOT_EXIST_‘));
    return;
  }
}
相似回答