// +---------------------------------------------------------------------- namespace think; T('controller/Jump'); class Controller { use \traits\controller\Jump; // 视图类实例 protected $view = null; /** * 前置操作方法列表 * @var array $beforeActionList * @access protected */ protected $beforeActionList = []; /** * 架构函数 * @access public */ 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); } } } /** * 前置操作 * @access protected * @param string $method 前置操作方法名 * @param array $options 调用参数 ['only'=>[...]] 或者['except'=>[...]] */ 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]); } } /** * 加载模板和页面输出 可以返回输出内容 * @access public * @param string $template 模板文件名 * @param array $vars 模板输出变量 * @param array $config 模板参数 * @return mixed */ public function fetch($template = '', $vars = [], $config = []) { return $this->view->fetch($template, $vars, $config); } /** * 渲染内容输出 * @access public * @param string $content 内容 * @param array $vars 模板输出变量 * @return mixed */ public function show($content, $vars = []) { return $this->view->show($content, $vars); } /** * 模板变量赋值 * @access protected * @param mixed $name 要显示的模板变量 * @param mixed $value 变量的值 * @return void */ public function assign($name, $value = '') { $this->view->assign($name, $value); } /** * 初始化模板引擎 * @access protected * @param string $engine 引擎名称 * @param array $config 引擎参数 * @return void */ public function engine($engine, $config = []) { $this->view->engine($engine, $config); } }