Controller.php 7.2 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();
  86. }
  87. if (!$this->beforeRun()) {
  88. exit();
  89. }
  90. }
  91. /**
  92. * 启用view后调用初始化View方法
  93. */
  94. protected function initView(){}
  95. /**
  96. * 设置view
  97. *
  98. * @param string $engine
  99. * @param array $policy
  100. * @return mixed
  101. */
  102. public function setView($engine = 'smarty', $policy = array())
  103. {
  104. $this->view = \Qii::getInstance()->setView($engine, $policy)->getView();
  105. $this->initView();
  106. $this->response->setRender($this->view);
  107. return $this->view;
  108. }
  109. /**
  110. * 设置缓存
  111. *
  112. * @param string $engine 缓存方法
  113. * @param array $policy 缓存策略
  114. */
  115. public function setCache($engine = '', $policy = array())
  116. {
  117. return $this->cache->$engine = \Qii::getInstance()->setCache($engine, $policy);
  118. }
  119. /**
  120. * 开启数据库操作
  121. */
  122. final public function enableDB()
  123. {
  124. return $this->db = new Model();
  125. }
  126. /**
  127. * 获取view
  128. *
  129. * @return mixed
  130. */
  131. public function getView()
  132. {
  133. return $this->view;
  134. }
  135. /**
  136. * view assign
  137. * @param string | array $key
  138. * @param mix $val 值
  139. * @return void
  140. */
  141. public function assign($key, $val = null)
  142. {
  143. if(!$this->view && !($this->view instanceof viewIntf)) {
  144. throw new \Exception(_i(6001), __LINE__);
  145. }
  146. if(is_array($key)) {
  147. $this->view->assign($key);
  148. }else{
  149. $this->view->assign($key, $val);
  150. }
  151. }
  152. /**
  153. * 渲染
  154. * @param string $tpl 模板路径
  155. * @param array $arr 需要替换的变量
  156. */
  157. public function render($tpl, $arr = [])
  158. {
  159. if(!$this->view && !($this->view instanceof viewIntf)) {
  160. throw new \Exception(_i(6001), __LINE__);
  161. }
  162. $this->view->assign($arr);
  163. $this->view->display($tpl);
  164. }
  165. /**
  166. * 设置 response
  167. * @param $request
  168. */
  169. public function setResponse($response)
  170. {
  171. if (!($response instanceof Response)) {
  172. return;
  173. }
  174. return $this->response = $response;
  175. }
  176. /**
  177. * 设置request
  178. * @param $request
  179. */
  180. public function setRequest(Request $request)
  181. {
  182. return $this->request = $request;
  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. if ($this->request && $this->request->isForward()) {
  273. $this->request->setForward(false);
  274. \Qii::getInstance()->dispatcher->setRequest($this->request);
  275. \Qii::getInstance()->dispatcher->dispatch();
  276. $this->request->setDispatched(true);
  277. }
  278. }
  279. }