controller.php 10 KB

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