搜尋
首頁後端開發php教程這是我的MVC框架ActionController的封裝

這是我的 MVC 框架 ActionController 的封裝
  1. /*
  2. * MST_Library v3.1
  3. * @autohr Janpoem
  4. */
  5. (!define('IN_MST_CORE'))
  6. exit('MST_ActionController 不能包含單一!');
  7. if (!defined(MST_Core::LITE_MODE)) {
  8. MST_Core: :import(Core::LITE_MODE)) {
  9. MST_Corearray:import(Corearray (
  10. 'MST/ActionController/Request',
  11. 'MST/ActionController/Router',
  12. ), MST_Core::P_LIB);
  13. }
  14. 抽象類別MST_ActionController {}
  15. 抽象類別MST_ActionController {
  16. const
  17. NO_RENDER = false,
  18. IS_RENDER = 'CONTROLLER_IS_RENDER',
  19. PF_CONTROLLER = '控制器',
  20. PF_ACTION = '操作🎜> VIEW = '視圖',
  21. TEXT = '文本',
  22. ACTION = '操作',
  23. WIDGET = '小部件',
  24. CUSTOM_VIEW = '自訂視圖';
  25. private static
  26. $_request = null,
  27. $_instance = null,
  28. $_currentView = null;
  29. # @todo 此處的處理應該放在controller被實例化以後,dispatch 應該有一個具體的意義
  30. 最終靜態公共函數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. } ']['request'] = & $request;
  46. if (!class_exists($controller))
  47. MST_Core::error(202, $controller);
  48. else
  49. self::$_instance = new $controller();
  50. }
  51. }
  52. public
  53. $layout = false,
  54. $format = 'html',
  55. $params = null,
  56. $format = 'html',
  57. $params = null,
  58. $format = 'html',
  59. $params = null,
  60. $format = 'html',
  61. $params = null,
  62. $format = 'html',
  63. $params = null,
  64. $format = 'html',
  65. $params = null,
  66. $format = 'html',
  67. $params = null,
  68. $autoLoadHelper = false;
  69. protected
  70. $comet = 0,
  71. $viewPath = null,
  72. $defaultRender = self::VIEW;
  73. 抽象公共函數應用();
  74. 私有函數__construct()
  75. {
  76. if ($this->comet ob_start();
  77. $this->params = & $GLOBALS['DATA_CACHE']['request'];
  78. $this->viewPath = trim(
  79. $this->params['module'] . '/' . $this-> ;params[' controller'], '/');
  80. if ($this->application() !== self::NO_RENDER)
  81. $this->action($this->params ['action']);
  82. }
  83. public function __destruct() {
  84. if (!define(self::IS_RENDER) && self::$_currentView != null) {
  85. switch ($this->defaultRender ) {
  86. case self::VIEW :
  87. case self::TEXT :
  88. case self::ACTION :
  89. case self::WIDGET :
  90. # $this->defaultRender = $modeaultRender = $mode ;
  91. break;
  92. 預設:
  93. $this->defaultRender = self::VIEW;
  94. }
  95. $this->render(
  96. $this->defaultRender,
  97. self::$_currentView
  98. );
  99. }
  100. if (self::$_instance != null)
  101. self::$_instance = null ;
  102. if (self::$_request ! = null)
  103. self::$_request = null;
  104. }
  105. protected function action($action) {
  106. $name = MST_String::camelize($action);
  107. $actName = $name 。 self::PF_ACTION;
  108. if (!method_exists($this, $actName))
  109. MST_Core::error(203, $actName);
  110. $actRef = new ReflectionMethod($this, $actName);
  111. if ($actRef->isPrivate() || $actRef->isProtected()
  112. && !constant(MST_ActionController_Router::IS_MAP))
  113. MST_Core::error(203, $actName) if ($this->$actName() !== self::NO_RENDER && self::$_currentView == null)
  114. self::$_currentView = $action;
  115. return $this;
  116. }
  117. /**
  118. * 輸出,url跳轉
  119. */
  120. protected 函數重定向($url) {
  121. if (define(self::IS_RENDER)) return self::NO_RENDER;
  122. Define (self::IS_RENDER, true);
  123. header('Location:'.linkUri($url));
  124. return $this;
  125. }
  126. // 渲染XML
  127. // 渲染JAVASCRIPT
  128. protected 函數render(
  129. $mode = null,
  130. $content = null,
  131. array $options = null)
  132. {
  133. if (define(self) : :IS_RENDER)) return self::NO_RENDER; Define(self::IS_RENDER, true); if ($mode == null) $mode = $this->defaultRender; if ( $momode == self::VIEW) $content = $this->viewPath 。 '/' 。 $content; MST_ActionView::instance() ->分配($this) ->setOptions($options) ->render($mode, $content); 回傳$this; }
  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. lush(true);
  160. ob_flush();
  161. lush( ); $times++;
  162. $result = call_user_func($fn, $times, $this);
  163. if ($result === false) {
  164. break;
  165. }
  166. usleep(10000) ;
  167. sleep($this->comet);
  168. }
  169. }
}
複製程式碼
複製程式碼


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP依賴注入容器:快速啟動PHP依賴注入容器:快速啟動May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依賴注入與服務定位器PHP中的依賴注入與服務定位器May 13, 2025 am 12:10 AM

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

PHP性能優化策略。PHP性能優化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP電子郵件驗證:確保正確發送電子郵件PHP電子郵件驗證:確保正確發送電子郵件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

如何使PHP應用程序更快如何使PHP應用程序更快May 12, 2025 am 12:12 AM

tomakephpapplicationsfaster,關注台詞:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

PHP性能優化清單:立即提高速度PHP性能優化清單:立即提高速度May 12, 2025 am 12:07 AM

到ImprovephPapplicationspeed,關注台詞:1)啟用opcodeCachingwithapCutoredUcescriptexecutiontime.2)實現databasequerycachingingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandreduceconnection.4 limitesclection.4.4

PHP依賴注入:提高代碼可檢驗性PHP依賴注入:提高代碼可檢驗性May 12, 2025 am 12:03 AM

依赖注入(DI)通过显式传递依赖关系,显著提升了PHP代码的可测试性。1)DI解耦类与具体实现,使测试和维护更灵活。2)三种类型中,构造函数注入明确表达依赖,保持状态一致。3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

PHP性能優化:數據庫查詢優化PHP性能優化:數據庫查詢優化May 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。