controller.php 8.9 KB

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