这里简单讲下tp3.23路由错误时页面的优化
1,空操作最好写在公共控制器CommonController 里,然后其他控制器继承它
<?php
namespace Admin\Controller;
use Think\Controller;
class CommonController extends Controller
{
public function _initialize(){
if(!isset($_SESSION['admin_id']) || !isset($_SESSION['admin_username'])){
$this->redirect("Admin/account/login");
}
}
public function _empty($name){
//把不存在的操作方法解析到error方法
$this->error('页面不存在');
}
protected function error($name){
header("content-type:text/html;charset=utf-8"); //设置编码
echo $name;
}
}
当访问非控制器时,如域名/admin/index/没有的操作方法
就会输出’页面不存在‘,当然error操作你可以找个好看404错误页面模板来替换’页面不存在‘。
2,空控制器,建立个EmptyController.class.php文件
<?php
namespace Admin\Controller;
use Think\Controller;
class EmptyController extends Controller{
public function index(){
$this->error('页面不存在');
}
protected function error($name){
header("content-type:text/html;charset=utf-8"); //设置编码
echo $name;
}
}
3,以上两种都不太好,当模块不存在时还是出现tp原生的错误页面,所以回到头来不如修改它。从ThinkPHP->Tpl->think_exception.tpl页面(tp原生的错误页面),改成你的404错误页面模板,考虑到练习用还是先改成这样,比较出现错误没有显示就不好找错误处
<?php
if(C('LAYOUT_ON')) {
echo '{__NOLAYOUT__}';
}
?>
<!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 content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>系统发生错误</title>
<style type="text/css">
*{ padding: 0; margin: 0; }
html{ overflow-y: scroll; }
body{ background: #fff; font-family: '微软雅黑'; color: #333; font-size: 16px; }
img{ border: 0; }
.error{ padding: 24px 48px; }
.face{ font-size: 100px; font-weight: normal; line-height: 120px; margin-bottom: 12px; }
h1{ font-size: 32px; line-height: 48px; }
.error .content{ padding-top: 10px}
.error .info{ margin-bottom: 12px; }
.error .info .title{ margin-bottom: 3px; }
.error .info .title h3{ color: #000; font-weight: 700; font-size: 16px; }
.error .info .text{ line-height: 24px; }
.copyright{ padding: 12px 48px; color: #999; }
.copyright a{ color: #000; text-decoration: none; }
</style>
</head>
<body>
<div class="error">
<p class="face" align="center">:(</p>
<h1 align="center">页面不存在</h1>
<div class="content" style="display:none;">
<?php if(isset($e['file'])) {?>
<div class="info">
<div class="title">
<h3>错误位置</h3>
</div>
<div class="text">
<p>FILE: <?php echo $e['file'] ;?>  LINE: <?php echo $e['line'];?></p>
</div>
</div>
<?php }?>
<?php if(isset($e['trace'])) {?>
<div class="info">
<div class="title">
<h3>TRACE</h3>
</div>
<div class="text">
<p><?php echo nl2br($e['trace']);?></p>
</div>
</div>
<?php }?>
</div>
</div>
</body>
</html>
当想要显示错误时,
<div class="content" style="display:none;">改为<div class="content" >
'DEFAULT_MODULE' => 'Home',//设置默认模块
'APP_SUB_DOMAIN_DEPLOY' => 1, // 开启子域名或IP配置
'APP_SUB_DOMAIN_RULES' => array(
'demo.com' => 'Home', //完整域名www.demo.com
), //域名部署配置 ,格式1: '子域名或泛域名或IP'=> '模块名[/控制器名]'