
最基本的控制器:
<?phpnamespace HomeController;use ThinkController;class IndexController extends Controller {public function index(){}public function hello(){echo "hello";}}控制器的名称采用驼峰法命名(首字母大写),控制器文件位于 Application/Home/Controller/IndexController.class.php<?phpnamespace HomeController;use ThinkController;class IndexController extends Controller {public function _before_index(){echo "index.before<br>";}public function index(){echo "index<br>";}public function _after_index(){echo "index.after<br>";}}配置ACTION_SUFFIX改变操作方法书写方式:<?phpnamespace HomeController;use ThinkController;class IndexController extends Controller {public function listAction(){echo "list";}public function helloAction(){echo "hello";}public function testAction(){echo "test";}}空控制器和空操作方法:
上图所示,当访问:
http://serverName/index.php/Home/City/beijing/
由于City控制器并没有定义beijing、shanghai或者shenzhen操作方法,因此系统会定位到空操作方法 _empty中去解析,_empty方法的参数就是当前URL里面的操作名,因此会看到依次输出的结果是:
你是怎么找到我的?
操作绑定到类: (作用:可以实现为每个操作方法定义一个类,而不是控制器类的一个方法)
以URL访问为 http://serverName/Home/Index/index为例,
原来的控制器文件定义位置为:Application/Home/Controller/IndexController.class.php
控制器类的定义如下:
namespace HomeController;use ThinkController;class IndexController extends Controller{public function index(){echo "执行Index控制器的index操作";}}可以看到,实际上我们调用的是 HomeControllerIndexController 类的index方法。namespace HomeControllerIndex;use ThinkController;class index extends Controller{public function run(){echo "执行Index控制器的index操作";}}现在,我们调用的其实是 HomeControllerIndexindex 类的run方法。