Files
framework/library/think/controller.php
2015-12-16 14:45:07 +08:00

96 lines
2.8 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think;
T('controller/view');
class Controller
{
use \traits\controller\view;
/**
* 控制器参数
* @var config
* @access protected
*/
protected $config = [
// 前置操作方法
'before_action_list' => [],
];
/**
* 架构函数 初始化视图类 并采用内置模板引擎
* @access public
* @param array $config
*/
public function __construct($config = [])
{
// 模板引擎参数
$this->config(empty($config) ? Config::get() : $config);
// 控制器初始化
if (method_exists($this, '_initialize')) {
$this->_initialize();
}
// 前置操作方法
// 支持 ['action1','action2'] 或者 ['action1'=>['only'=>'index'],'action2'=>['except'=>'login']]
$list = $this->config['before_action_list'];
if ($list) {
foreach ($list as $method => $options) {
is_numeric($method) ?
$this->beforeAction($options) :
$this->beforeAction($method, $options);
}
}
}
/**
* 设置控制器参数
* @access public
* @param array $config 视图参数
* @return Think\Controller
*/
public function config($config = [])
{
if (is_array($config)) {
foreach ($this->config as $key => $val) {
if (isset($config[$key])) {
$this->config[$key] = $config[$key];
}
}
}
return $this;
}
/**
* 前置操作
* @access protected
* @param string $method 前置操作方法名
* @param array $options 调用参数 ['only'=>[...]] 或者['except'=>[...]]
*/
protected function beforeAction($method, $options = [])
{
if (isset($options['only'])) {
if (!in_array(ACTION_NAME, $options['only'])) {
return;
}
} elseif (isset($options['except'])) {
if (in_array(ACTION_NAME, $options['except'])) {
return;
}
}
if (method_exists($this, $method)) {
call_user_func([$this, $method]);
}
}
}