controller.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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($name, $method) = explode('@', $destination);
  122. $controller = static::resolve($bundle, $name);
  123. // For convenience we will set the current controller and action on the
  124. // Request's route instance so they can be easily accessed from the
  125. // application. This is sometimes useful for dynamic situations.
  126. if ( ! is_null($route = Request::route()))
  127. {
  128. $route->controller = $name;
  129. $route->controller_action = $method;
  130. }
  131. // If the controller could not be resolved, we're out of options and
  132. // will return the 404 error response. If we found the controller,
  133. // we can execute the requested method on the instance.
  134. if (is_null($controller))
  135. {
  136. return Event::first('404');
  137. }
  138. return $controller->execute($method, $parameters);
  139. }
  140. /**
  141. * Replace all back-references on the given destination.
  142. *
  143. * @param string $destination
  144. * @param array $parameters
  145. * @return array
  146. */
  147. protected static function references(&$destination, &$parameters)
  148. {
  149. // Controller delegates may use back-references to the action parameters,
  150. // which allows the developer to setup more flexible routes to various
  151. // controllers with much less code than would be usual.
  152. foreach ($parameters as $key => $value)
  153. {
  154. $search = '(:'.($key + 1).')';
  155. $destination = str_replace($search, $value, $destination, $count);
  156. if ($count > 0) unset($parameters[$key]);
  157. }
  158. return array($destination, $parameters);
  159. }
  160. /**
  161. * Resolve a bundle and controller name to a controller instance.
  162. *
  163. * @param string $bundle
  164. * @param string $controller
  165. * @return Controller
  166. */
  167. public static function resolve($bundle, $controller)
  168. {
  169. if ( ! static::load($bundle, $controller)) return;
  170. $identifier = Bundle::identifier($bundle, $controller);
  171. // If the controller is registered in the IoC container, we will resolve
  172. // it out of the container. Using constructor injection on controllers
  173. // via the container allows more flexible applications.
  174. $resolver = 'controller: '.$identifier;
  175. if (IoC::registered($resolver))
  176. {
  177. return IoC::resolve($resolver);
  178. }
  179. $controller = static::format($bundle, $controller);
  180. // If we couldn't resolve the controller out of the IoC container we'll
  181. // format the controller name into its proper class name and load it
  182. // by convention out of the bundle's controller directory.
  183. if (Event::listeners(static::factory))
  184. {
  185. return Event::first(static::factory, $controller);
  186. }
  187. else
  188. {
  189. return new $controller;
  190. }
  191. }
  192. /**
  193. * Load the file for a given controller.
  194. *
  195. * @param string $bundle
  196. * @param string $controller
  197. * @return bool
  198. */
  199. protected static function load($bundle, $controller)
  200. {
  201. $controller = strtolower(str_replace('.', '/', $controller));
  202. if (file_exists($path = Bundle::path($bundle).'controllers/'.$controller.EXT))
  203. {
  204. require_once $path;
  205. return true;
  206. }
  207. return false;
  208. }
  209. /**
  210. * Format a bundle and controller identifier into the controller's class name.
  211. *
  212. * @param string $bundle
  213. * @param string $controller
  214. * @return string
  215. */
  216. protected static function format($bundle, $controller)
  217. {
  218. return Bundle::class_prefix($bundle).Str::classify($controller).'_Controller';
  219. }
  220. /**
  221. * Execute a controller method with the given parameters.
  222. *
  223. * @param string $method
  224. * @param array $parameters
  225. * @return Response
  226. */
  227. public function execute($method, $parameters = array())
  228. {
  229. $filters = $this->filters('before', $method);
  230. // Again, as was the case with route closures, if the controller "before"
  231. // filters return a response, it will be considered the response to the
  232. // request and the controller method will not be used.
  233. $response = Filter::run($filters, array(), true);
  234. if (is_null($response))
  235. {
  236. $this->before();
  237. $response = $this->response($method, $parameters);
  238. }
  239. $response = Response::prepare($response);
  240. // The "after" function on the controller is simply a convenient hook
  241. // so the developer can work on the response before it's returned to
  242. // the browser. This is useful for templating, etc.
  243. $this->after($response);
  244. Filter::run($this->filters('after', $method), array($response));
  245. return $response;
  246. }
  247. /**
  248. * Execute a controller action and return the response.
  249. *
  250. * Unlike the "execute" method, no filters will be run and the response
  251. * from the controller action will not be changed in any way before it
  252. * is returned to the consumer.
  253. *
  254. * @param string $method
  255. * @param array $parameters
  256. * @return mixed
  257. */
  258. public function response($method, $parameters = array())
  259. {
  260. // The developer may mark the controller as being "RESTful" which
  261. // indicates that the controller actions are prefixed with the
  262. // HTTP verb they respond to rather than the word "action".
  263. if ($this->restful)
  264. {
  265. $action = strtolower(Request::method()).'_'.$method;
  266. }
  267. else
  268. {
  269. $action = "action_{$method}";
  270. }
  271. $response = call_user_func_array(array($this, $action), $parameters);
  272. // If the controller has specified a layout view the response
  273. // returned by the controller method will be bound to that
  274. // view and the layout will be considered the response.
  275. if (is_null($response) and ! is_null($this->layout))
  276. {
  277. $response = $this->layout;
  278. }
  279. return $response;
  280. }
  281. /**
  282. * Register filters on the controller's methods.
  283. *
  284. * <code>
  285. * // Set a "foo" after filter on the controller
  286. * $this->filter('before', 'foo');
  287. *
  288. * // Set several filters on an explicit group of methods
  289. * $this->filter('after', 'foo|bar')->only(array('user', 'profile'));
  290. * </code>
  291. *
  292. * @param string $event
  293. * @param string|array $filters
  294. * @param mixed $parameters
  295. * @return Filter_Collection
  296. */
  297. protected function filter($event, $filters, $parameters = null)
  298. {
  299. $this->filters[$event][] = new Filter_Collection($filters, $parameters);
  300. return $this->filters[$event][count($this->filters[$event]) - 1];
  301. }
  302. /**
  303. * Get an array of filter names defined for the destination.
  304. *
  305. * @param string $event
  306. * @param string $method
  307. * @return array
  308. */
  309. protected function filters($event, $method)
  310. {
  311. if ( ! isset($this->filters[$event])) return array();
  312. $filters = array();
  313. foreach ($this->filters[$event] as $collection)
  314. {
  315. if ($collection->applies($method))
  316. {
  317. $filters[] = $collection;
  318. }
  319. }
  320. return $filters;
  321. }
  322. /**
  323. * Create the layout that is assigned to the controller.
  324. *
  325. * @return View
  326. */
  327. public function layout()
  328. {
  329. if (starts_with($this->layout, 'name: '))
  330. {
  331. return View::of(substr($this->layout, 6));
  332. }
  333. return View::make($this->layout);
  334. }
  335. /**
  336. * This function is called before the action is executed.
  337. *
  338. * @return void
  339. */
  340. public function before() {}
  341. /**
  342. * This function is called after the action is executed.
  343. *
  344. * @param Response $response
  345. * @return void
  346. */
  347. public function after($response) {}
  348. /**
  349. * Magic Method to handle calls to undefined controller functions.
  350. */
  351. public function __call($method, $parameters)
  352. {
  353. return Response::error('404');
  354. }
  355. /**
  356. * Dynamically resolve items from the application IoC container.
  357. *
  358. * <code>
  359. * // Retrieve an object registered in the container
  360. * $mailer = $this->mailer;
  361. *
  362. * // Equivalent call using the IoC container instance
  363. * $mailer = IoC::resolve('mailer');
  364. * </code>
  365. */
  366. public function __get($key)
  367. {
  368. if (IoC::registered($key))
  369. {
  370. return IoC::resolve($key);
  371. }
  372. }
  373. }