本文實例講述了thinkphp3.2.3框架實現(xiàn)的空模塊、空控制器、空操作,跳轉到錯誤404頁面。分享給大家供大家參考,具體如下:
【演示準備】
首先下載了一個thinkphp3.2.3,目錄結構如下:(只有home模塊、index控制器、index操作方法)
然后找了一個簡易的錯誤404頁面404.html放到了根目錄:
【空模塊】
訪問一個不存在的模塊admin,錯誤提示:
修改根目錄下 thinkphp/library/think/dispatcher.class.php 178行代碼:
1
2
3
|
// e(l('_module_not_exist_').':'.module_name); header( "location: /404.html" ); exit (); |
【空控制器】
訪問home模塊不存在的控制器user,錯誤提示:
查看根目錄下 thinkphp/library/think/app.class.php 101行前后代碼:
通常的處理方法是:在當前模塊下新建一個empty控制器,在里面做404跳轉(a方法實例化empty控制器)。
emptycontroller.class.php:
1
2
3
4
5
6
7
8
9
|
<?php namespace home\controller; use think\controller; class emptycontroller extends controller { public function index() { header( "location:/404.html" ); exit (); } } |
再次訪問空控制器:
【空操作】
訪問home模塊下index控制器不存在的test操作方法,錯誤提示:
查看根目錄下 thinkphp/library/think/controller.class.php 170行前后代碼:
通常的處理方法是:在當前模塊下新建一個public控制器(繼承controller),在里面定義_empty方法跳轉404頁面, 然后其他控制器再繼承public。
publiccontroller.class.php:
1
2
3
4
5
6
7
8
9
|
<?php namespace home\controller; use think\controller; class publiccontroller extends controller { public function _empty() { header( "location:/404.html" ); exit (); } } |
index控制器繼承puclic:
再次訪問空操作方法:
希望本文所述對大家基于thinkphp框架的php程序設計有所幫助。
原文鏈接:https://blog.csdn.net/msllws/article/details/82932615