Controller.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Abstract controller class. Controllers should only be created using a [Request].
  4. *
  5. * Controllers methods will be automatically called in the following order by
  6. * the request:
  7. *
  8. * $controller = new Controller_Foo($request);
  9. * $controller->before();
  10. * $controller->action_bar();
  11. * $controller->after();
  12. *
  13. * The controller action should add the output it creates to
  14. * `$this->response->body($output)`, typically in the form of a [View], during the
  15. * "action" part of execution.
  16. *
  17. * @package Kohana
  18. * @category Controller
  19. * @author Kohana Team
  20. * @copyright (c) Kohana Team
  21. * @license https://koseven.ga/LICENSE.md
  22. */
  23. abstract class Kohana_Controller {
  24. /**
  25. * @var Request Request that created the controller
  26. */
  27. public $request;
  28. /**
  29. * @var Response The response that will be returned from controller
  30. */
  31. public $response;
  32. /**
  33. * Creates a new controller instance. Each controller must be constructed
  34. * with the request object that created it.
  35. *
  36. * @param Request $request Request that created the controller
  37. * @param Response $response The request's response
  38. * @return void
  39. */
  40. public function __construct(Request $request, Response $response)
  41. {
  42. // Assign the request to the controller
  43. $this->request = $request;
  44. // Assign a response to the controller
  45. $this->response = $response;
  46. }
  47. /**
  48. * Executes the given action and calls the [Controller::before] and [Controller::after] methods.
  49. *
  50. * Can also be used to catch exceptions from actions in a single place.
  51. *
  52. * 1. Before the controller action is called, the [Controller::before] method
  53. * will be called.
  54. * 2. Next the controller action will be called.
  55. * 3. After the controller action is called, the [Controller::after] method
  56. * will be called.
  57. *
  58. * @throws HTTP_Exception_404
  59. * @return Response
  60. */
  61. public function execute()
  62. {
  63. // Execute the "before action" method
  64. $this->before();
  65. // Determine the action to use
  66. $action = 'action_'.$this->request->action();
  67. // If the action doesn't exist, it's a 404
  68. if ( ! method_exists($this, $action))
  69. {
  70. throw HTTP_Exception::factory(404,
  71. 'The requested URL :uri was not found on this server.',
  72. [':uri' => $this->request->uri()]
  73. )->request($this->request);
  74. }
  75. // Execute the action itself
  76. $this->{$action}();
  77. // Execute the "after action" method
  78. $this->after();
  79. // Return the response
  80. return $this->response;
  81. }
  82. /**
  83. * Automatically executed before the controller action. Can be used to set
  84. * class properties, do authorization checks, and execute other custom code.
  85. *
  86. * @return void
  87. */
  88. public function before()
  89. {
  90. // Nothing by default
  91. }
  92. /**
  93. * Automatically executed after the controller action. Can be used to apply
  94. * transformation to the response, add extra output, and execute
  95. * other custom code.
  96. *
  97. * @return void
  98. */
  99. public function after()
  100. {
  101. // Nothing by default
  102. }
  103. /**
  104. * Issues a HTTP redirect.
  105. *
  106. * Proxies to the [HTTP::redirect] method.
  107. *
  108. * @param string $uri URI to redirect to
  109. * @param int $code HTTP Status code to use for the redirect
  110. * @throws HTTP_Exception
  111. */
  112. public static function redirect($uri = '', $code = 302)
  113. {
  114. return HTTP::redirect( (string) $uri, $code);
  115. }
  116. /**
  117. * Checks the browser cache to see the response needs to be returned,
  118. * execution will halt and a 304 Not Modified will be sent if the
  119. * browser cache is up to date.
  120. *
  121. * $this->check_cache(sha1($content));
  122. *
  123. * @param string $etag Resource Etag
  124. * @return Response
  125. */
  126. protected function check_cache($etag = NULL)
  127. {
  128. return HTTP::check_cache($this->request, $this->response, $etag);
  129. }
  130. }