Controller应用说明:
- 结合之前的Hello World继续Controller应用
- Controller层是业务逻辑层和模板显示层的中间层,主要职责是获取数据和输出数据
- Controller层的接口函数可以使用:$this->controller和$this->view,具体可以查看相关的API文档详细信息。
- 所有的Controller需要继承InitPHP框架提供的框架基类Controller
- 建议使用Zend Studio工具开发,有代码提示功能,能将所有的API提示显示,方便您的开发速度
API使用:
- 具体的API可以参考Controller中的API文档
- Controller API:$this->controller->
- View API:$this->view->
class indexController extends Controller {
public $initphp_list = array('test');
public function run() {
$username = $this->controller->get_gp('username'); //【$this->controller 使用】获取GET或者POST的username值
$this->view->display(); //【$this->view 使用】模板显示
}
public function test() {
$code = $this->getLibrary('code');
$code->getcode();
}
/**
* @return testService
*/
private function getTestService() {
return InitPHP::getService('test','test');
}
}
配置参考:
- 自定义Controller,则需要修改initphp框架中的initphp.conf.php配置文件
/**
* Controller控制器配置参数
* 1. 你可以配置控制器默认的文件夹,默认的后缀,Action默认后缀,默认执行的Action和Controller
* 2. 一般情况下,你可以不需要修改该配置参数
* 3. $InitPHP_conf['ismodule']参数,当你的项目比较大的时候,可以选用module方式,
* 开启module后,你的URL种需要带m的参数,原始:index.php?c=index&a=run, 加module:
* index.php?m=user&c=index&a=run , module就是$InitPHP_conf['controller']['path']目录下的
* 一个文件夹名称,请用小写文件夹名称
*/
$InitPHP_conf['ismodule'] = false; //开启module方式
$InitPHP_conf['controller']['path'] = 'web/controller/';
$InitPHP_conf['controller']['controller_postfix'] = 'Controller'; //控制器文件后缀名
$InitPHP_conf['controller']['action_postfix'] = ''; //Action函数名称后缀
$InitPHP_conf['controller']['default_controller'] = 'index'; //默认执行的控制器名称
$InitPHP_conf['controller']['default_action'] = 'run'; //默认执行的Action函数
$InitPHP_conf['controller']['module_list'] = array('test', 'index'); //module白名单
$InitPHP_conf['controller']['default_module'] = 'index'; //默认执行module
$InitPHP_conf['controller']['default_before_action'] = 'before'; //默认前置的ACTION名称
$InitPHP_conf['controller']['default_after_action'] = 'after'; //默认后置ACTION名称
Action实例:
- 默认的缺省Controller为indexController,默认缺省Action为run()
- $initphp_list是Action的白名单,只有在白名单中才可以被URL访问
- before()是前置的Action,会在正常Action运行之前提前运行
- after()是后置的Action,会在正常Action运行之后运行
class indexController extends Controller {
public $initphp_list = array('test');
public function run() {
$username = $this->controller->get_gp('username'); //获取GET或者POST的username值
echo '默认执行';
$this->view->display(); //模板显示
}
public function test() {
echo 'index.php?c=index&a=test 才会执行';
$code = $this->getLibrary('code');
$code->getcode();
}
public function before() {
echo '前置执行Action';
}
public function after() {
echo '后置执行Action';
}
/**
* @return testService
*/
private function getTestService() {
return InitPHP::getService('test','test');
}
}