controller.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 method did not
  53. // return an instsance of Response, we will make on now.
  54. if ( ! $response instanceof Response) $response = new Response($response);
  55. Filter::run($controller->filters('after'), array($response));
  56. return $response;
  57. }
  58. /**
  59. * Resolve a controller name to a controller instance.
  60. *
  61. * @param Container $container
  62. * @param string $controller
  63. * @return Controller
  64. */
  65. public static function resolve($controller)
  66. {
  67. if ( ! static::load($controller)) return;
  68. // If the controller is registered in the IoC container, we will
  69. // resolve it out of the container. Using constructor injection
  70. // on controllers via the container allows more flexible and
  71. // testable development of applications.
  72. if (IoC::container()->registered('controllers.'.$controller))
  73. {
  74. return IoC::container()->resolve('controllers.'.$controller);
  75. }
  76. $controller = str_replace(' ', '_', ucwords(str_replace('.', ' ', $controller))).'_Controller';
  77. return new $controller;
  78. }
  79. /**
  80. * Load the file for a given controller.
  81. *
  82. * @param string $controller
  83. * @return bool
  84. */
  85. protected static function load($controller)
  86. {
  87. $controller = strtolower(str_replace('.', '/', $controller));
  88. if (file_exists($path = CONTROLLER_PATH.$controller.EXT))
  89. {
  90. require $path;
  91. return true;
  92. }
  93. return false;
  94. }
  95. /**
  96. * Determine if a given controller method is callable.
  97. *
  98. * @param string $method
  99. * @return bool
  100. */
  101. protected static function hidden($method)
  102. {
  103. return $method == 'before' or $method == 'after' or strncmp($method, '_', 1) == 0;
  104. }
  105. /**
  106. * Get an array of filter names defined for the destination.
  107. *
  108. * @param string $name
  109. * @return array
  110. */
  111. public function filters($name)
  112. {
  113. return (array) $this->$name;
  114. }
  115. /**
  116. * Magic Method to handle calls to undefined functions on the controller.
  117. *
  118. * By default, the 404 response will be returned for an calls to undefined
  119. * methods on the controller. However, this method may also be overridden
  120. * and used as a pseudo-router by the controller.
  121. */
  122. public function __call($method, $parameters)
  123. {
  124. return Response::error('404');
  125. }
  126. /**
  127. * Dynamically resolve items from the application IoC container.
  128. *
  129. * <code>
  130. * // Retrieve an object registered in the container as "mailer"
  131. * $mailer = $this->mailer;
  132. *
  133. * // Equivalent call using the IoC container instance
  134. * $mailer = IoC::container()->resolve('mailer');
  135. * </code>
  136. */
  137. public function __get($key)
  138. {
  139. if (IoC::container()->registered($key))
  140. {
  141. return IoC::container()->resolve($key);
  142. }
  143. throw new \Exception("Attempting to access undefined property [$key] on controller.");
  144. }
  145. }