- ThinkPHP實戰
- 夏磊
- 162字
- 2020-11-28 18:26:55
4.11 空操作
當ThinkPHP找不到請求的操作時,會執行_empty方法,利用該機制,可以實現錯誤頁面和一些URL優化。
本例使用空操作來實現一個用戶預覽的功能,新建Application/Home/Controller/UserController.class.php,代碼如下:
<? php /** * Project: thinkphp-inaction * User: xialeistudio<1065890063@qq.com> * Date: 2016-02-18 */ namespace Home\Controller; use Think\Controller; class UserController extends Controller { public function _empty($name) { $this->view($name); } private function view($name) { echo 'name:'.$name; } }
瀏覽器訪問http://localhost/chapter-4/Home/User/zhangsan,輸出“name:zhangsan”。
執行流程如下:
(1)準備執行Home/Controller/UserController.class.php的zhangsan方法;
(2)對應zhangsan方法不存在,執行UserController.class.php的_empty方法,并將zhangsan作為$name傳入;
(3)調用UserController的view方法,輸出“name:zhangsan”。
推薦閱讀
- HornetQ Messaging Developer’s Guide
- Learning Laravel 4 Application Development
- Mastering Rust
- Building RESTful Python Web Services
- SSM開發實戰教程(Spring+Spring MVC+MyBatis)
- Raspberry Pi Robotic Projects(Third Edition)
- Python Interviews
- IoT Projects with Bluetooth Low Energy
- Apache Solr PHP Integration
- Visual C++從入門到精通(第2版)
- SCRATCH編程課:我的游戲我做主
- Apache Solr for Indexing Data
- 高質量程序設計指南:C++/C語言
- C語言編程魔法書:基于C11標準
- Learning Rust