controller.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php namespace Laravel\Routing;
  2. use Laravel\IoC;
  3. use Laravel\Response;
  4. abstract class Controller {
  5. /**
  6. * The "before" filters defined for the controller.
  7. *
  8. * @var array
  9. */
  10. public $before = array();
  11. /**
  12. * The "after" filters defined for the controller.
  13. *
  14. * @var array
  15. */
  16. public $after = array();
  17. /**
  18. * Handle the delegation of a route to a controller method.
  19. *
  20. * The controller destination should follow a {controller}@{method} convention.
  21. * Nested controllers may be delegated to using dot syntax.
  22. *
  23. * For example, a destination of "user.profile@show" would call the User_Profile
  24. * controller's show method with the given parameters.
  25. *
  26. * @param string $destination
  27. * @param array $parameters
  28. * @return mixed
  29. */
  30. public static function call($destination, $parameters = array())
  31. {
  32. if (strpos($destination, '@') === false)
  33. {
  34. throw new \Exception("Route delegate [{$destination}] has an invalid format.");
  35. }
  36. list($controller, $method) = explode('@', $destination);
  37. $controller = static::resolve($controller);
  38. if (is_null($controller) or static::hidden($method))
  39. {
  40. return Response::error('404');
  41. }
  42. // Again, as was the case with route closures, if the controller
  43. // "before" filters return a response, it will be considered the
  44. // response to the request and the controller method will not be
  45. // used to handle the request to the application.
  46. $response = Filter::run($controller->filters('before'), array(), true);
  47. if (is_null($response))
  48. {
  49. $response = call_user_func_array(array($controller, $method), $parameters);
  50. }
  51. // The after filter and the framework expects all responses to
  52. // be instances of the Response class. If the route did not
  53. // return an instsance of Response, we will make on now.
  54. if ( ! $response instanceof Response) $response = new Response($response);
  55. $filters = array_merge($controller->filters('after'), array('after'));
  56. Filter::run($filters, array($response));
  57. return $response;
  58. }
  59. /**
  60. * Resolve a controller name to a controller instance.
  61. *
  62. * @param Container $container
  63. * @param string $controller
  64. * @return Controller
  65. */
  66. public static function resolve($controller)
  67. {
  68. if ( ! static::load($controller)) return;
  69. // If the controller is registered in the IoC container, we will
  70. // resolve it out of the container. Using constructor injection
  71. // on controllers via the container allows more flexible and
  72. // testable development of applications.
  73. if (IoC::container()->registered('controllers.'.$controller))
  74. {
  75. return IoC::container()->resolve('controllers.'.$controller);
  76. }
  77. $controller = str_replace(' ', '_', ucwords(str_replace('.', ' ', $controller))).'_Controller';
  78. return new $controller;
  79. }
  80. /**
  81. * Load the file for a given controller.
  82. *
  83. * @param string $controller
  84. * @return bool
  85. */
  86. protected static function load($controller)
  87. {
  88. $controller = strtolower(str_replace('.', '/', $controller));
  89. if (file_exists($path = CONTROLLER_PATH.$controller.EXT))
  90. {
  91. require $path;
  92. return true;
  93. }
  94. return false;
  95. }
  96. /**
  97. * Determine if a given controller method is callable.
  98. *
  99. * @param string $method
  100. * @return bool
  101. */
  102. protected static function hidden($method)
  103. {
  104. return $method == 'before' or $method == 'after' or strncmp($method, '_', 1) == 0;
  105. }
  106. /**
  107. * Get an array of filter names defined for the destination.
  108. *
  109. * @param string $name
  110. * @return array
  111. */
  112. public function filters($name)
  113. {
  114. return (array) $this->$name;
  115. }
  116. /**
  117. * Magic Method to handle calls to undefined functions on the controller.
  118. *
  119. * By default, the 404 response will be returned for an calls to undefined
  120. * methods on the controller. However, this method may also be overridden
  121. * and used as a pseudo-router by the controller.
  122. */
  123. public function __call($method, $parameters)
  124. {
  125. return Response::error('404');
  126. }
  127. /**
  128. * Dynamically resolve items from the application IoC container.
  129. *
  130. * <code>
  131. * // Retrieve an object registered in the container as "mailer"
  132. * $mailer = $this->mailer;
  133. *
  134. * // Equivalent call using the IoC container instance
  135. * $mailer = IoC::container()->resolve('mailer');
  136. * </code>
  137. */
  138. public function __get($key)
  139. {
  140. if (IoC::container()->registered($key))
  141. {
  142. return IoC::container()->resolve($key);
  143. }
  144. throw new \Exception("Attempting to access undefined property [$key] on controller.");
  145. }
  146. }