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