This is the package of my MVC framework Actioncontroller

Source: Internet
Author: User
This is the package of my MVC framework 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= ' Widgets ',
  24. custom_view= ' Custom_view ';
  25. private static
  26. $_request = NULL,
  27. $_instance = NULL,
  28. $_currentview = null;
  29. # @todo The processing here should be placed after the controller is instantiated, dispatch should have a specific meaning
  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::P F_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 <= 0)
  66. Ob_start ();
  67. $this->params = & $GLOBALS [' Data_cache '] [' request '];
  68. $this->viewpath = Trim (
  69. $this->params[' module '). '/' . $this->params[' controller '], '/');
  70. if ($this->application ()!== Self::no_render)
  71. $this->action ($this->params[' action ');
  72. }
  73. Public Function __destruct () {
  74. if (!defined (self::is_render) && Self::$_currentview! = null) {
  75. Switch ($this->defaultrender) {
  76. Case Self::view:
  77. Case Self::text:
  78. Case Self::action:
  79. Case Self::widget:
  80. # $this->defaultrender = $mode;
  81. Break
  82. Default:
  83. $this->defaultrender = Self::view;
  84. }
  85. $this->render (
  86. $this->defaultrender,
  87. Self::$_currentview
  88. );
  89. }
  90. if (self::$_instance! = null)
  91. Self::$_instance = null;
  92. if (self::$_request! = null)
  93. Self::$_request = null;
  94. }
  95. protected function Action ($action) {
  96. $name = Mst_string::camelize ($action);
  97. $actName = $name. Self::P f_action;
  98. if (!method_exists ($this, $actName))
  99. Mst_core::error (203, $actName);
  100. $actRef = new Reflectionmethod ($this, $actName);
  101. if ($actRef->isprivate () | | $actRef->isprotected ()
  102. &&!constant (Mst_actioncontroller_router::is_map))
  103. Mst_core::error (203, $actName);
  104. if ($this, $actName ()!== self::no_render && Self::$_currentview = = null)
  105. Self::$_currentview = $action;
  106. return $this;
  107. }
  108. /**
  109. * Output, URL jump
  110. */
  111. protected function Redirect ($url) {
  112. if (defined (Self::is_render)) return self::no_render;
  113. Define (Self::is_render, true);
  114. Header (' Location: '. Linkuri ($url));
  115. return $this;
  116. }
  117. Render XML
  118. Render JAVASCRIPT
  119. protected function Render (
  120. $mode = NULL,
  121. $content = NULL,
  122. Array $options = null)
  123. {
  124. if (defined (Self::is_render)) return self::no_render;
  125. Define (Self::is_render, true);
  126. if ($mode = = null) $mode = $this->defaultrender;
  127. if ($mode = = Self::view)
  128. $content = $this->viewpath. '/' . $content;
  129. Mst_actionview::instance ()
  130. ->assign ($this)
  131. ->setoptions ($options)
  132. ->render ($mode, $content);
  133. return $this;
  134. }
  135. protected function Customrender ($file, $path, array $options = null) {
  136. return $this->render (Self::custom_view, Array ($file, $path), $options);
  137. }
  138. protected function Setview ($val) {
  139. Self::$_currentview = $val;
  140. }
  141. protected function Setviewoption ($key, $val) {
  142. Mst_actionview::instance ()->setoption ($key, $val);
  143. return $this;
  144. }
  145. protected function Getviewoption ($key) {
  146. Return Mst_actionview::instance ()->getoption ($key);
  147. }
  148. protected function setviewoptions (array $options) {
  149. Mst_actionview::instance ()->setoptions ($options);
  150. return $this;
  151. }
  152. protected function getviewoptions () {
  153. Return Mst_actionview::instance ()->getoptions ();
  154. }
  155. protected function Docomet (Closure $fn) {
  156. $times = 0;
  157. Set_time_limit (0);
  158. while (true) {
  159. Ob_flush ();
  160. Flush ();
  161. $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. }
  170. }
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.