Controller.php 3.7 KB

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