Controller.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /**
  3. * 控制器基类
  4. */
  5. namespace Qii\Base;
  6. use Qii\Autoloader\Factory;
  7. use \Qii\Autoloader\Psr4;
  8. use Qii\Driver\Model;
  9. use \Qii\View\Intf as viewIntf;
  10. /**
  11. * Qii\Controller\Abstract class
  12. * @author Zhu Jinhui
  13. */
  14. abstract class Controller
  15. {
  16. /**
  17. * @var array $actions action对应的方法列表,设置了的就转发
  18. */
  19. public $actions = array();
  20. /**
  21. * @var \Qii\Autoloader\Psr4::getInstance() $load
  22. */
  23. public $load;
  24. /**
  25. * @var \Qii\Language\Loader $language
  26. */
  27. public $language;
  28. /**
  29. * @var \Qii\Controller\Base $controller
  30. */
  31. public $controller;
  32. /**
  33. * @var string $controllerId controller名
  34. */
  35. public $controllerId = 'index';
  36. /**
  37. * @var string $actionId action名
  38. */
  39. public $actionId = 'index';
  40. /**
  41. * @var Qii\Request\Base $request
  42. */
  43. public $request;
  44. /**
  45. * @var Response $response
  46. */
  47. public $response;
  48. /**
  49. * @var \Qii\Driver\xx\Connection
  50. */
  51. public $db;
  52. /**
  53. * @var mixed
  54. */
  55. public $view;
  56. /**
  57. * @var \Qii\Cache\Abslute $cache
  58. */
  59. public $cache;
  60. /**
  61. * 是否启用view模块
  62. * @var bool
  63. */
  64. public $enableView = false;
  65. /**
  66. * 是否启用Model
  67. * @var bool
  68. */
  69. public $enableDB = false;
  70. public function __construct()
  71. {
  72. $this->load = Psr4::getInstance()->loadClass('\Qii\Autoloader\Loader');
  73. $this->request = Psr4::getInstance()->loadClass('\Qii\Request\Http');
  74. $this->controllerId = $this->request->controller;
  75. $this->actionId = $this->request->action;
  76. $this->language = Factory::getInstance('\Qii\Language\Loader');
  77. $this->response = Factory::getInstance('\Qii\Response\Http');
  78. $this->cache = new \stdClass();
  79. //载入model
  80. if ($this->enableDB) {
  81. $this->enableDB();
  82. }
  83. //载入view
  84. if ($this->enableView) {
  85. $this->view = $this->setView(\Qii::getInstance()->viewEngine);
  86. }
  87. }
  88. /**
  89. * 启用view后调用初始化View方法
  90. */
  91. protected function initView(){}
  92. /**
  93. * 设置view
  94. *
  95. * @param string $engine
  96. * @param array $policy
  97. * @return mixed
  98. */
  99. public function setView($engine = 'smarty', $policy = array())
  100. {
  101. $this->view = \Qii::getInstance()->setView($engine, $policy)->getView();
  102. $this->initView();
  103. $this->response->setRender($this->view);
  104. return $this->view;
  105. }
  106. /**
  107. * 设置缓存
  108. *
  109. * @param string $engine 缓存方法
  110. * @param array $policy 缓存策略
  111. */
  112. public function setCache($engine = '', $policy = array())
  113. {
  114. return $this->cache->$engine = \Qii::getInstance()->setCache($engine, $policy);
  115. }
  116. /**
  117. * 开启数据库操作
  118. */
  119. final public function enableDB()
  120. {
  121. return $this->db = _loadClass('\Qii\Driver\Model');
  122. }
  123. /**
  124. * 获取view
  125. *
  126. * @return mixed
  127. */
  128. public function getView()
  129. {
  130. return $this->view;
  131. }
  132. /**
  133. * view assign
  134. * @param string | array $key
  135. * @param mix $val 值
  136. * @return void
  137. */
  138. public function assign($key, $val = null)
  139. {
  140. if(!$this->view && !($this->view instanceof viewIntf)) {
  141. throw new \Exception(_i(6001), __LINE__);
  142. }
  143. if(is_array($key)) {
  144. $this->view->assign($key);
  145. }else{
  146. $this->view->assign($key, $val);
  147. }
  148. }
  149. /**
  150. * 渲染
  151. * @param string $tpl 模板路径
  152. * @param array $arr 需要替换的变量
  153. */
  154. public function render($tpl, $arr = [])
  155. {
  156. if(!$this->view && !($this->view instanceof viewIntf)) {
  157. throw new \Exception(_i(6001), __LINE__);
  158. }
  159. $this->view->assign($arr);
  160. $this->view->display($tpl);
  161. }
  162. /**
  163. * 设置 response
  164. * @param $request
  165. */
  166. public function setResponse($response)
  167. {
  168. if (!($response instanceof Response)) {
  169. return;
  170. }
  171. //将之前设置的header赋给新的response
  172. $response->setAllHeaders($this->response->getHeader());
  173. return $this->response = $response;
  174. }
  175. /**
  176. * 设置request
  177. * @param $request
  178. */
  179. public function setRequest(Request $request)
  180. {
  181. $this->request = $request;
  182. return $this;
  183. }
  184. /**
  185. * 只要继承的方法调用parent::__construct()就开始执行
  186. * 此方法如果返回false,将不再往下继续执行
  187. */
  188. protected function beforeRun()
  189. {
  190. return true;
  191. }
  192. /**
  193. * 获取 controller middleware
  194. * @return array
  195. */
  196. public function getMiddleware() {
  197. return array();
  198. }
  199. /**
  200. * 执行完dispatch后调用
  201. */
  202. protected function afterRun()
  203. {
  204. if (!$this->response || !is_object($this->response)) {
  205. return;
  206. }
  207. if ($this->response instanceof Response) {
  208. if ($this->response->needRender() && $this->view && $this->view instanceof viewIntf) {
  209. $this->response->setRender($this->view);
  210. }
  211. $this->response->response();
  212. }
  213. }
  214. /**
  215. * 转发
  216. * @param String $controller
  217. * @param String $action
  218. * @return mixed
  219. */
  220. final public function dispatch($controller, $action)
  221. {
  222. if($this->request == null) {
  223. throw new \Exception(_i(1104, "\Qii\Request\Http"), __LINE__);
  224. }
  225. $this->request->setControllerName($controller);
  226. $this->request->setActionName($action);
  227. \Qii::getInstance()->dispatcher->setRequest($this->request);
  228. return call_user_func_array(array(\Qii::getInstance()->dispatcher, 'dispatch'), func_get_args());
  229. }
  230. /**
  231. * 获取当前使用的controller
  232. *
  233. * @return string
  234. */
  235. final public function getCurrentController()
  236. {
  237. return get_called_class();
  238. }
  239. /**
  240. * 获取 request 方法
  241. * @return Qii\Request\Http
  242. */
  243. final public function getRequest()
  244. {
  245. return $this->request;
  246. }
  247. /**
  248. * 获取response类
  249. * @return mixed
  250. */
  251. final public function getResponse()
  252. {
  253. return $this->response;
  254. }
  255. /**
  256. * 设置forward
  257. * @param string $controller controller名
  258. * @param string $action action名
  259. */
  260. public function setForward($controller, $action)
  261. {
  262. $this->request->setControllerName($controller);
  263. $this->request->setActionName($action);
  264. $this->request->setForward(true);
  265. }
  266. /**
  267. * afterRun 和 forward 执行
  268. */
  269. public function __destruct()
  270. {
  271. $this->afterRun();
  272. \Qii::getInstance()->runComplete($this);
  273. if ($this->request && $this->request->isForward()) {
  274. $this->request->setForward(false);
  275. \Qii::getInstance()->dispatcher->setRequest($this->request);
  276. \Qii::getInstance()->dispatcher->dispatch();
  277. $this->request->setDispatched(true);
  278. }
  279. }
  280. }