php控制c,C:(Controller.php)应用控制器

[TOC]

* * * * *

## 1 控制器基类文件分析(thinkphp/library/think/Controller.php)

~~~

protected $view = null;

protected $beforeActionList = [];

~~~

~~~

public function __construct()

{

$this->view = \think\View::instance(Config::get());

if (method_exists($this, '_initialize')) {

$this->_initialize();

}

if ($this->beforeActionList) {

foreach ($this->beforeActionList as $method => $options) {

is_numeric($method) ?

$this->beforeAction($options) :

$this->beforeAction($method, $options);

}

}

}

~~~

~~~

protected function beforeAction($method, $options = [])

{

if (isset($options['only'])) {

if (is_string($options['only'])) {

$options['only'] = explode(',', $options['only']);

}

if (!in_array(ACTION_NAME, $options['only'])) {

return;

}

} elseif (isset($options['except'])) {

if (is_string($options['except'])) {

$options['except'] = explode(',', $options['except']);

}

if (in_array(ACTION_NAME, $options['except'])) {

return;

}

}

if (method_exists($this, $method)) {

call_user_func([$this, $method]);

}

}

~~~

~~~

public function fetch($template = '', $vars = [], $cache_id = '')

{

return $this->view->fetch($template, $vars, $cache_id);

}

public function show($content, $vars = [])

{

return $this->view->show($content, $vars);

}

public function assign($name, $value = '')

{

$this->view->assign($name, $value);

}

public function engine($engine, $config = [])

{

$this->view->engine($engine, $config);

}

~~~

Controller.php定义了控制的基类操作,大部分是视图的操作调用

`1 public function __construct(){} `

构造函数创建视图对象,并调用控制器实例化方法$this->_initialize(),

最后调用前置操作方法$this->beforeAction()

`2 public function fetch($template = '', $vars = [], $cache_id = ''){}`

`public function show($content, $vars = []){}`

编译模板文件并输出。

`3 public function assign($name, $value = ''){}`

控制器变量传递到模板中

`4 public function engine($engine, $config = []){}`

设置视图的模板渲染引擎

* * * * *

## 2 应用入口分析

应用目录的入口在App::run()的调度类型的module调度中

其分析思路见 [应用启动文件](http://www.kancloud.cn/zmwtp/tp5/119426)与[应用调度分析](http://www.kancloud.cn/zmwtp/tp5/119428)两节。

访问localhost/tp5/public/index.php 时。

根据全局配置文件/thinkphp/convention.php中的配置可知

default_module 默认模块为 index

default_controller 默认控制器 Index,

default_action 默认操作 index,

因此App::run()的module调度类型中会自动访问/applicaiton/index模块

自动加载器的Loader::controller()会自动访问/application/index模块的index控制器。

Index控制器在/applicaiton/index/controller的index.php文件

所以最后调用/application/index/controller/index.php文件的index()。

* * * * *

## 3 应用默认目录(/application/)

~~~

/application

/index

/controller

config.php

databse.php

route.php

~~~

其中/index 应用的index模块

/controller Index模块下的控制器目录

config.php 应用的配置文件

databse.php 应用的数据库配置文件

route.php 应用的路由配置文件

## 4 应用控制器开发

应用控制器是应用调度的入口文件,通常的开发就是在/application/index/controller目录下添加控制器,并调用框架模型和视图等实现业务逻辑。

控制器controller是框架MVC的入口。通常在controller类的方法中调用模型的操作与视图渲染方法。

模型操作见 数据模型文件

视图渲染见 视图渲染文件

其具体开发见 使用范例 [应用控制器操作](http://www.kancloud.cn/zmwtp/tp5/119443)。

进一步的可将前台与后台进行分模块开发。模块开发见 使用范例的[模块开发](http://www.kancloud.cn/zmwtp/tp5/119528)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值