tp3.23路由优化(一)错误页面之空操作,空控制器,修改tp原生错误页面,开启子域名

这里简单讲下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'] ;?> &#12288;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'=> '模块名[/控制器名]'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值