controller.php 11 KB

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