controller.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php namespace Laravel\Routing;
  2. use Laravel\IoC;
  3. use Laravel\Str;
  4. use Laravel\View;
  5. use Laravel\Event;
  6. use Laravel\Bundle;
  7. use Laravel\Request;
  8. use Laravel\Redirect;
  9. use Laravel\Response;
  10. abstract class Controller {
  11. /**
  12. * The layout being used by the controller.
  13. *
  14. * @var string
  15. */
  16. public $layout;
  17. /**
  18. * The bundle the controller belongs to.
  19. *
  20. * @var string
  21. */
  22. public $bundle;
  23. /**
  24. * Indicates if the controller uses RESTful routing.
  25. *
  26. * @var bool
  27. */
  28. public $restful = false;
  29. /**
  30. * The filters assigned to the controller.
  31. *
  32. * @var array
  33. */
  34. protected $filters = array();
  35. /**
  36. * Call an action method on a controller.
  37. *
  38. * <code>
  39. * // Call the "show" method on the "user" controller
  40. * $response = Controller::call('user@show');
  41. *
  42. * // Call the "user/admin" controller and pass parameters
  43. * $response = Controller::call('user.admin@profile', array($username));
  44. * </code>
  45. *
  46. * @param string $destination
  47. * @param array $parameters
  48. * @return Response
  49. */
  50. public static function call($destination, $parameters = array())
  51. {
  52. static::references($destination, $parameters);
  53. list($bundle, $destination) = Bundle::parse($destination);
  54. // We will always start the bundle, just in case the developer is pointing
  55. // a route to another bundle. This allows us to lazy load the bundle and
  56. // improve speed since the bundle is not loaded on every request.
  57. Bundle::start($bundle);
  58. list($controller, $method) = explode('@', $destination);
  59. $controller = static::resolve($bundle, $controller);
  60. // If the controller could not be resolved, we're out of options and
  61. // will return the 404 error response. If we found the controller,
  62. // we can execute the requested method on the instance.
  63. if (is_null($controller))
  64. {
  65. return Event::first('404');
  66. }
  67. return $controller->execute($method, $parameters);
  68. }
  69. /**
  70. * Replace all back-references on the given destination.
  71. *
  72. * @param string $destination
  73. * @param array $parameters
  74. * @return array
  75. */
  76. protected static function references(&$destination, &$parameters)
  77. {
  78. // Controller delegates may use back-references to the action parameters,
  79. // which allows the developer to setup more flexible routes to various
  80. // controllers with much less code than would be usual.
  81. foreach ($parameters as $key => $value)
  82. {
  83. $search = '(:'.($key + 1).')';
  84. $destination = str_replace($search, $value, $destination, $count);
  85. if ($count > 0) unset($parameters[$key]);
  86. }
  87. return array($destination, $parameters);
  88. }
  89. /**
  90. * Resolve a bundle and controller name to a controller instance.
  91. *
  92. * @param string $bundle
  93. * @param string $controller
  94. * @return Controller
  95. */
  96. public static function resolve($bundle, $controller)
  97. {
  98. if ( ! static::load($bundle, $controller)) return;
  99. $identifier = Bundle::identifier($bundle, $controller);
  100. // If the controller is registered in the IoC container, we will resolve
  101. // it out of the container. Using constructor injection on controllers
  102. // via the container allows more flexible applications.
  103. $resolver = 'controller: '.$identifier;
  104. if (IoC::registered($resolver))
  105. {
  106. return IoC::resolve($resolver);
  107. }
  108. // If we couldn't resolve the controller out of the IoC container we'll
  109. // format the controller name into its proper class name and load it
  110. // by convention out of the bundle's controller directory.
  111. $controller = static::format($bundle, $controller);
  112. $controller = new $controller;
  113. // If the controller has specified a layout to be used when rendering
  114. // views, we will instantiate the layout instance and set it to the
  115. // layout property, replacing the string layout name.
  116. if ( ! is_null($controller->layout))
  117. {
  118. $controller->layout = $controller->layout();
  119. }
  120. return $controller;
  121. }
  122. /**
  123. * Load the file for a given controller.
  124. *
  125. * @param string $bundle
  126. * @param string $controller
  127. * @return bool
  128. */
  129. protected static function load($bundle, $controller)
  130. {
  131. $controller = strtolower(str_replace('.', '/', $controller));
  132. if (file_exists($path = Bundle::path($bundle).'controllers/'.$controller.EXT))
  133. {
  134. require_once $path;
  135. return true;
  136. }
  137. return false;
  138. }
  139. /**
  140. * Format a bundle and controller identifier into the controller's class name.
  141. *
  142. * @param string $bundle
  143. * @param string $controller
  144. * @return string
  145. */
  146. protected static function format($bundle, $controller)
  147. {
  148. return Bundle::class_prefix($bundle).Str::classify($controller).'_Controller';
  149. }
  150. /**
  151. * Execute a controller method with the given parameters.
  152. *
  153. * @param string $method
  154. * @param array $parameters
  155. * @return Response
  156. */
  157. public function execute($method, $parameters = array())
  158. {
  159. $filters = $this->filters('before', $method);
  160. // Again, as was the case with route closures, if the controller "before"
  161. // filters return a response, it will be considered the response to the
  162. // request and the controller method will not be used.
  163. $response = Filter::run($filters, array(), true);
  164. if (is_null($response))
  165. {
  166. $this->before();
  167. $response = $this->response($method, $parameters);
  168. }
  169. $response = Response::prepare($response);
  170. // The "after" function on the controller is simply a convenient hook
  171. // so the developer can work on the response before it's returned to
  172. // the browser. This is useful for templating, etc.
  173. $this->after($response);
  174. Filter::run($this->filters('after', $method), array($response));
  175. return $response;
  176. }
  177. /**
  178. * Execute a controller action and return the response.
  179. *
  180. * Unlike the "execute" method, no filters will be run and the response
  181. * from the controller action will not be changed in any way before it
  182. * is returned to the consumer.
  183. *
  184. * @param string $method
  185. * @param array $parameters
  186. * @return mixed
  187. */
  188. public function response($method, $parameters = array())
  189. {
  190. // The developer may mark the controller as being "RESTful" which
  191. // indicates that the controller actions are prefixed with the
  192. // HTTP verb they respond to rather than the word "action".
  193. if ($this->restful)
  194. {
  195. $action = strtolower(Request::method()).'_'.$method;
  196. }
  197. else
  198. {
  199. $action = "action_{$method}";
  200. }
  201. $response = call_user_func_array(array($this, $action), $parameters);
  202. // If the controller has specified a layout view. The response
  203. // returned by the controller method will be bound to that
  204. // view and the layout will be considered the response.
  205. if (is_null($response) and ! is_null($this->layout))
  206. {
  207. $response = $this->layout;
  208. }
  209. return $response;
  210. }
  211. /**
  212. * Register filters on the controller's methods.
  213. *
  214. * <code>
  215. * // Set a "foo" after filter on the controller
  216. * $this->filter('before', 'foo');
  217. *
  218. * // Set several filters on an explicit group of methods
  219. * $this->filter('after', 'foo|bar')->only(array('user', 'profile'));
  220. * </code>
  221. *
  222. * @param string $event
  223. * @param string|array $filters
  224. * @param mixed $parameters
  225. * @return Filter_Collection
  226. */
  227. protected function filter($event, $filters, $parameters = null)
  228. {
  229. $this->filters[$event][] = new Filter_Collection($filters, $parameters);
  230. return $this->filters[$event][count($this->filters[$event]) - 1];
  231. }
  232. /**
  233. * Get an array of filter names defined for the destination.
  234. *
  235. * @param string $event
  236. * @param string $method
  237. * @return array
  238. */
  239. protected function filters($event, $method)
  240. {
  241. if ( ! isset($this->filters[$event])) return array();
  242. $filters = array();
  243. foreach ($this->filters[$event] as $collection)
  244. {
  245. if ($collection->applies($method))
  246. {
  247. $filters[] = $collection;
  248. }
  249. }
  250. return $filters;
  251. }
  252. /**
  253. * Create the layout that is assigned to the controller.
  254. *
  255. * @return View
  256. */
  257. public function layout()
  258. {
  259. if (starts_with($this->layout, 'name: '))
  260. {
  261. return View::of(substr($this->layout, 6));
  262. }
  263. return View::make($this->layout);
  264. }
  265. /**
  266. * This function is called before the action is executed.
  267. *
  268. * @return void
  269. */
  270. public function before() {}
  271. /**
  272. * This function is called after the action is executed.
  273. *
  274. * @param Response $response
  275. * @return void
  276. */
  277. public function after($response) {}
  278. /**
  279. * Magic Method to handle calls to undefined controller functions.
  280. */
  281. public function __call($method, $parameters)
  282. {
  283. return Response::error('404');
  284. }
  285. /**
  286. * Dynamically resolve items from the application IoC container.
  287. *
  288. * <code>
  289. * // Retrieve an object registered in the container
  290. * $mailer = $this->mailer;
  291. *
  292. * // Equivalent call using the IoC container instance
  293. * $mailer = IoC::resolve('mailer');
  294. * </code>
  295. */
  296. public function __get($key)
  297. {
  298. if (IoC::registered($key))
  299. {
  300. return IoC::resolve($key);
  301. }
  302. }
  303. }