controller.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php namespace Laravel\Routing;
  2. use Laravel\IoC;
  3. use Laravel\View;
  4. use Laravel\Request;
  5. use Laravel\Redirect;
  6. use Laravel\Response;
  7. abstract class Controller {
  8. /**
  9. * The layout being used by the controller.
  10. *
  11. * @var string
  12. */
  13. public $layout;
  14. /**
  15. * The filters assigned to the controller.
  16. *
  17. * @var array
  18. */
  19. protected $filters = array();
  20. /**
  21. * Handle the delegation of a route to a controller method.
  22. *
  23. * The controller destination should follow a {controller}@{method} convention.
  24. * Nested controllers may be delegated to using dot syntax.
  25. *
  26. * For example, a destination of "user.profile@show" would call the User_Profile
  27. * controller's show method with the given parameters.
  28. *
  29. * @param string $destination
  30. * @param array $parameters
  31. * @return Response
  32. */
  33. public static function call($destination, $parameters = array())
  34. {
  35. if (strpos($destination, '@') === false)
  36. {
  37. throw new \Exception("Route delegate [{$destination}] has an invalid format.");
  38. }
  39. list($controller, $method) = explode('@', $destination);
  40. $controller = static::resolve($controller);
  41. if (is_null($controller))
  42. {
  43. return Response::error('404');
  44. }
  45. return $controller->execute($method, $parameters);
  46. }
  47. /**
  48. * Resolve a controller name to a controller instance.
  49. *
  50. * @param Container $container
  51. * @param string $controller
  52. * @return Controller
  53. */
  54. public static function resolve($controller)
  55. {
  56. if ( ! static::load($controller)) return;
  57. // If the controller is registered in the IoC container, we will resolve
  58. // it out of the container. Using constructor injection on controllers
  59. // via the container allows more flexible and testable applications.
  60. if (IoC::registered('controllers.'.$controller))
  61. {
  62. return IoC::resolve('controllers.'.$controller);
  63. }
  64. $controller = str_replace(' ', '_', ucwords(str_replace('.', ' ', $controller))).'_Controller';
  65. $controller = new $controller;
  66. // If the controller has specified a layout to be used when rendering
  67. // views, we will instantiate the layout instance and set it to the
  68. // layout property, replacing the string layout name.
  69. if ( ! is_null($controller->layout))
  70. {
  71. $controller->layout = View::make($controller->layout);
  72. }
  73. return $controller;
  74. }
  75. /**
  76. * Load the file for a given controller.
  77. *
  78. * @param string $controller
  79. * @return bool
  80. */
  81. protected static function load($controller)
  82. {
  83. $controller = strtolower(str_replace('.', '/', $controller));
  84. if (file_exists($path = CONTROLLER_PATH.$controller.EXT))
  85. {
  86. require_once $path;
  87. return true;
  88. }
  89. return false;
  90. }
  91. /**
  92. * Execute a controller method with the given parameters.
  93. *
  94. * @param string $method
  95. * @param array $parameters
  96. * @return Response
  97. */
  98. public function execute($method, $parameters = array())
  99. {
  100. // Again, as was the case with route closures, if the controller
  101. // "before" filters return a response, it will be considered the
  102. // response to the request and the controller method will not be
  103. // used to handle the request to the application.
  104. $response = Filter::run($this->filters('before', $method), array(), true);
  105. if (is_null($response))
  106. {
  107. $response = call_user_func_array(array($this, "action_{$method}"), $parameters);
  108. // If the controller has specified a layout view. The response
  109. // returned by the controller method will be bound to that view
  110. // and the layout will be considered the response.
  111. if (is_null($response) and ! is_null($this->layout))
  112. {
  113. $response = $this->layout;
  114. }
  115. }
  116. // The after filter and the framework expects all responses to
  117. // be instances of the Response class. If the method did not
  118. // return an instsance of Response, we will make on now.
  119. if ( ! $response instanceof Response)
  120. {
  121. $response = new Response($response);
  122. }
  123. Filter::run($this->filters('after', $method), array($response));
  124. return $response;
  125. }
  126. /**
  127. * Register filters on the controller's methods.
  128. *
  129. * Generally, this method will be used in the controller's constructor.
  130. *
  131. * <code>
  132. * // Set a "foo" after filter on the controller
  133. * $this->filter('before', 'foo');
  134. *
  135. * // Set several filters on an explicit group of methods
  136. * $this->filter('after', 'foo|bar')->only(array('user', 'profile'));
  137. * </code>
  138. *
  139. * @param string|array $filters
  140. * @return Filter_Collection
  141. */
  142. protected function filter($name, $filters)
  143. {
  144. $this->filters[$name][] = new Filter_Collection($name, $filters);
  145. return $this->filters[$name][count($this->filters[$name]) - 1];
  146. }
  147. /**
  148. * Get an array of filter names defined for the destination.
  149. *
  150. * @param string $name
  151. * @param string $method
  152. * @return array
  153. */
  154. protected function filters($name, $method)
  155. {
  156. if ( ! isset($this->filters[$name])) return array();
  157. $filters = array();
  158. foreach ($this->filters[$name] as $filter)
  159. {
  160. if ($filter->applies($method))
  161. {
  162. $filters = array_merge($filters, $filter->filters);
  163. }
  164. }
  165. return array_unique($filters);
  166. }
  167. /**
  168. * Magic Method to handle calls to undefined functions on the controller.
  169. *
  170. * By default, the 404 response will be returned for an calls to undefined
  171. * methods on the controller. However, this method may also be overridden
  172. * and used as a pseudo-router by the controller.
  173. */
  174. public function __call($method, $parameters)
  175. {
  176. return Response::error('404');
  177. }
  178. /**
  179. * Dynamically resolve items from the application IoC container.
  180. *
  181. * <code>
  182. * // Retrieve an object registered in the container as "mailer"
  183. * $mailer = $this->mailer;
  184. *
  185. * // Equivalent call using the IoC container instance
  186. * $mailer = IoC::resolve('mailer');
  187. * </code>
  188. */
  189. public function __get($key)
  190. {
  191. if (IoC::registered($key))
  192. {
  193. return IoC::resolve($key);
  194. }
  195. throw new \Exception("Attempting to access undefined property [$key] on controller.");
  196. }
  197. }