Rumah pembangunan bahagian belakang tutorial php 这是我的MVC框架ActionController的封装

这是我的MVC框架ActionController的封装

Jul 25, 2016 am 09:10 AM

这是我的MVC框架ActionController的封装
  1. /*
  2. * MST_Library v3.1
  3. * @autohr Janpoem
  4. */
  5. if (!defined('IN_MST_CORE'))
  6. exit('MST_ActionController Can\'t be include single!');
  7. if (!defined(MST_Core::LITE_MODE)) {
  8. MST_Core::import(array(
  9. 'MST/ActionController/Request',
  10. 'MST/ActionController/Router',
  11. ), MST_Core::P_LIB);
  12. }
  13. abstract class MST_ActionController {
  14. const
  15. NO_RENDER = false,
  16. IS_RENDER = 'CONTROLLER_IS_RENDER',
  17. PF_CONTROLLER = 'Controller',
  18. PF_ACTION = 'Action',
  19. FILE = 'file',
  20. VIEW = 'view',
  21. TEXT = 'text',
  22. ACTION = 'action',
  23. WIDGET = 'widget',
  24. CUSTOM_VIEW = 'custom_view';
  25. private static
  26. $_request = null,
  27. $_instance = null,
  28. $_currentView = null;
  29. # @todo 此处的处理应该放到controller被实例化以后, dispatch应该有一个具体含义
  30. final static public function dispatch(array $config = null, $beforeDispatch = null) {
  31. if (self::$_instance == null) {
  32. $request = new MST_ActionController_Request($config['request']);
  33. $router = new MST_ActionController_Router($config['routes']);
  34. $router->routing($request);
  35. $controller = $request['controller'];
  36. $controller = MST_String::camelize2($controller) . static::PF_CONTROLLER;
  37. if ($request['module'] != null) {
  38. $module = $request['module'];
  39. if (strpos($module, '/') !== false)
  40. $module = str_replace('/', '_', $module);
  41. $controller = $module . '_' . $controller;
  42. }
  43. if (is_callable($beforeDispatch)) {
  44. call_user_func_array($beforeDispatch, array($request, & $controller));
  45. }
  46. $GLOBALS['DATA_CACHE']['request'] = & $request;
  47. if (!class_exists($controller))
  48. MST_Core::error(202, $controller);
  49. else
  50. self::$_instance = new $controller();
  51. }
  52. }
  53. public
  54. $layout = false,
  55. $format = 'html',
  56. $params = null,
  57. $autoLoadHelper = false;
  58. protected
  59. $comet = 0,
  60. $viewPath = null,
  61. $defaultRender = self::VIEW;
  62. abstract public function application();
  63. private function __construct()
  64. {
  65. if ($this->comet ob_start();
  66. $this->params = & $GLOBALS['DATA_CACHE']['request'];
  67. $this->viewPath = trim(
  68. $this->params['module'] . '/' . $this->params['controller'], '/');
  69. if ($this->application() !== self::NO_RENDER)
  70. $this->action($this->params['action']);
  71. }
  72. public function __destruct() {
  73. if (!defined(self::IS_RENDER) && self::$_currentView != null) {
  74. switch ($this->defaultRender) {
  75. case self::VIEW :
  76. case self::TEXT :
  77. case self::ACTION :
  78. case self::WIDGET :
  79. #$this->defaultRender = $mode;
  80. break;
  81. default :
  82. $this->defaultRender = self::VIEW;
  83. }
  84. $this->render(
  85. $this->defaultRender,
  86. self::$_currentView
  87. );
  88. }
  89. if (self::$_instance != null)
  90. self::$_instance = null;
  91. if (self::$_request != null)
  92. self::$_request = null;
  93. }
  94. protected function action($action) {
  95. $name = MST_String::camelize($action);
  96. $actName = $name . self::PF_ACTION;
  97. if (!method_exists($this, $actName))
  98. MST_Core::error(203, $actName);
  99. $actRef = new ReflectionMethod($this, $actName);
  100. if ($actRef->isPrivate() || $actRef->isProtected()
  101. && !constant(MST_ActionController_Router::IS_MAP))
  102. MST_Core::error(203, $actName);
  103. if ($this->$actName() !== self::NO_RENDER && self::$_currentView == null)
  104. self::$_currentView = $action;
  105. return $this;
  106. }
  107. /**
  108. * 输出,url跳转
  109. */
  110. protected function redirect($url) {
  111. if (defined(self::IS_RENDER)) return self::NO_RENDER;
  112. define(self::IS_RENDER, true);
  113. header('Location:'.linkUri($url));
  114. return $this;
  115. }
  116. // render XML
  117. // render JAVASCRIPT
  118. protected function render(
  119. $mode = null,
  120. $content = null,
  121. array $options = null)
  122. {
  123. if (defined(self::IS_RENDER)) return self::NO_RENDER;
  124. define(self::IS_RENDER, true);
  125. if ($mode == null) $mode = $this->defaultRender;
  126. if ($mode == self::VIEW)
  127. $content = $this->viewPath . '/' . $content;
  128. MST_ActionView::instance()
  129. ->assign($this)
  130. ->setOptions($options)
  131. ->render($mode, $content);
  132. return $this;
  133. }
  134. protected function customRender($file, $path, array $options = null) {
  135. return $this->render(self::CUSTOM_VIEW, array($file, $path), $options);
  136. }
  137. protected function setView($val) {
  138. self::$_currentView = $val;
  139. }
  140. protected function setViewOption($key, $val) {
  141. MST_ActionView::instance()->setOption($key, $val);
  142. return $this;
  143. }
  144. protected function getViewOption($key) {
  145. return MST_ActionView::instance()->getOption($key);
  146. }
  147. protected function setViewOptions(array $options) {
  148. MST_ActionView::instance()->setOptions($options);
  149. return $this;
  150. }
  151. protected function getViewOptions() {
  152. return MST_ActionView::instance()->getOptions();
  153. }
  154. protected function doComet(Closure $fn) {
  155. $times = 0;
  156. set_time_limit(0);
  157. while(true) {
  158. ob_flush();
  159. flush();
  160. $times++;
  161. $result = call_user_func($fn, $times, $this);
  162. if ($result === false) {
  163. break;
  164. }
  165. usleep(10000);
  166. sleep($this->comet);
  167. }
  168. }
  169. }
复制代码


Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Tag artikel panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

11 skrip pemendek URL terbaik PHP (percuma dan premium) 11 skrip pemendek URL terbaik PHP (percuma dan premium) Mar 03, 2025 am 10:49 AM

11 skrip pemendek URL terbaik PHP (percuma dan premium)

Bekerja dengan Data Sesi Flash di Laravel Bekerja dengan Data Sesi Flash di Laravel Mar 12, 2025 pm 05:08 PM

Bekerja dengan Data Sesi Flash di Laravel

Respons HTTP yang dipermudahkan dalam ujian Laravel Respons HTTP yang dipermudahkan dalam ujian Laravel Mar 12, 2025 pm 05:09 PM

Respons HTTP yang dipermudahkan dalam ujian Laravel

Pengenalan kepada API Instagram Pengenalan kepada API Instagram Mar 02, 2025 am 09:32 AM

Pengenalan kepada API Instagram

Bina aplikasi React dengan hujung belakang Laravel: Bahagian 2, React Bina aplikasi React dengan hujung belakang Laravel: Bahagian 2, React Mar 04, 2025 am 09:33 AM

Bina aplikasi React dengan hujung belakang Laravel: Bahagian 2, React

Curl dalam PHP: Cara Menggunakan Pelanjutan PHP Curl dalam API REST Curl dalam PHP: Cara Menggunakan Pelanjutan PHP Curl dalam API REST Mar 14, 2025 am 11:42 AM

Curl dalam PHP: Cara Menggunakan Pelanjutan PHP Curl dalam API REST

12 skrip sembang php terbaik di codecanyon 12 skrip sembang php terbaik di codecanyon Mar 13, 2025 pm 12:08 PM

12 skrip sembang php terbaik di codecanyon

Pengumuman Penyiasatan Situasi PHP 2025 Pengumuman Penyiasatan Situasi PHP 2025 Mar 03, 2025 pm 04:20 PM

Pengumuman Penyiasatan Situasi PHP 2025

See all articles