controller.php 9.0 KB

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