-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrollers.html
255 lines (190 loc) · 8.65 KB
/
controllers.html
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Controllers</title>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/ubuntu-font.css">
<script src="vendor/modernizr-2.6.2.min.js"></script>
<script type="text/javascript" src="vendor/google-code-prettify/prettify.js"></script>
</head><body><section class="docs-content"><div class="container">
<article class="docs-body"><h1>Controllers</h1>
<p><a name="basic-controllers"></a></p>
<h2>Basic Controllers</h2>
<p>Instead of defining all of your route-level logic in a single <code>routes.php</code> file, you may wish to organize this behavior using Controller classes. Controllers can group related route logic into a class, as well as take advantage of more advanced framework features such as automatic <a href="/docs/ioc">dependency injection</a>.</p>
<p>Controllers are typically stored in the <code>app/controllers</code> directory, and this directory is registered in the <code>classmap</code> option of your <code>composer.json</code> file by default.</p>
<p>Here is an example of a basic controller class:</p>
<pre class="prettyprint"><code>class UserController extends BaseController {
/**
* Show the profile for the given user.
*/
public function showProfile($id)
{
$user = User::find($id);
return View::make('user.profile', array('user' => $user));
}
}
</code></pre>
<p>All controllers should extend the <code>BaseController</code> class. The <code>BaseController</code> is also stored in the <code>app/controllers</code> directory, and may be used as a place to put shared controller logic. The <code>BaseController</code> extends the framework's <code>Controller</code> class. Now, We can route to this controller action like so:</p>
<pre class="prettyprint"><code>Route::get('user/{id}', 'UserController@showProfile');
</code></pre>
<p>If you choose to nest or organize your controller using PHP namespaces, simply use the fully qualified class name when defining the route:</p>
<pre class="prettyprint"><code>Route::get('foo', 'Namespace\FooController@method');
</code></pre>
<p>You may also specify names on controller routes:</p>
<pre class="prettyprint"><code>Route::get('foo', array('uses' => 'FooController@method',
'as' => 'name'));
</code></pre>
<p>To generate a URL to a controller action, you may use the <code>URL::action</code> method:</p>
<pre class="prettyprint"><code>$url = URL::action('FooController@method');
</code></pre>
<p>You may access the name of the controller action being run using the <code>currentRouteAction</code> method:</p>
<pre class="prettyprint"><code>$action = Route::currentRouteAction();
</code></pre>
<p><a name="controller-filters"></a></p>
<h2>Controller Filters</h2>
<p><a href="/docs/routing#route-filters">Filters</a> may be specified on controller routes similar to "regular" routes:</p>
<pre class="prettyprint"><code>Route::get('profile', array('before' => 'auth',
'uses' => 'UserController@showProfile'));
</code></pre>
<p>However, you may also specify filters from within your controller:</p>
<pre class="prettyprint"><code>class UserController extends BaseController {
/**
* Instantiate a new UserController instance.
*/
public function __construct()
{
$this->beforeFilter('auth');
$this->beforeFilter('csrf', array('on' => 'post'));
$this->afterFilter('log', array('only' =>
array('fooAction', 'barAction')));
}
}
</code></pre>
<p>You may also specify controller filters inline using a Closure:</p>
<pre class="prettyprint"><code>class UserController extends BaseController {
/**
* Instantiate a new UserController instance.
*/
public function __construct()
{
$this->beforeFilter(function()
{
//
});
}
}
</code></pre>
<p><a name="restful-controllers"></a></p>
<h2>RESTful Controllers</h2>
<p>Laravel allows you to easily define a single route to handle every action in a controller using simple, REST naming conventions. First, define the route using the <code>Route::controller</code> method:</p>
<p><strong>Defining A RESTful Controller</strong></p>
<pre class="prettyprint"><code>Route::controller('users', 'UserController');
</code></pre>
<p>The <code>controller</code> method accepts two arguments. The first is the base URI the controller handles, while the second is the class name of the controller. Next, just add methods to your controller, prefixed with the HTTP verb they respond to:</p>
<pre class="prettyprint"><code>class UserController extends BaseController {
public function getIndex()
{
//
}
public function postProfile()
{
//
}
}
</code></pre>
<p>The <code>index</code> methods will respond to the root URI handled by the controller, which, in this case, is <code>users</code>.</p>
<p>If your controller action contains multiple words, you may access the action using "dash" syntax in the URI. For example, the following controller action on our <code>UserController</code> would respond to the <code>users/admin-profile</code> URI:</p>
<pre class="prettyprint"><code>public function getAdminProfile() {}
</code></pre>
<p><a name="resource-controllers"></a></p>
<h2>Resource Controllers</h2>
<p>Resource controllers make it easier to build RESTful controllers around resources. For example, you may wish to create a controller that manages "photos" stored by your application. Using the <code>controller:make</code> command via the Artisan CLI and the <code>Route::resource</code> method, we can quickly create such a controller.</p>
<p>To create the controller via the command line, execute the following command:</p>
<pre class="prettyprint"><code>php artisan controller:make PhotoController
</code></pre>
<p>Now we can register a resourceful route to the controller:</p>
<pre class="prettyprint"><code>Route::resource('photo', 'PhotoController');
</code></pre>
<p>This single route declaration creates multiple routes to handle a variety of RESTful actions on the photo resource. Likewise, the generated controller will already have stubbed methods for each of these actions with notes informing you which URIs and verbs they handle.</p>
<p><strong>Actions Handled By Resource Controller</strong></p>
<table>
<thead><tr>
<th>Verb</th>
<th>Path</th>
<th>Action</th>
<th>Route Name</th>
</tr></thead>
<tbody>
<tr>
<td>GET</td>
<td>/resource</td>
<td>index</td>
<td>resource.index</td>
</tr>
<tr>
<td>GET</td>
<td>/resource/create</td>
<td>create</td>
<td>resource.create</td>
</tr>
<tr>
<td>POST</td>
<td>/resource</td>
<td>store</td>
<td>resource.store</td>
</tr>
<tr>
<td>GET</td>
<td>/resource/{id}</td>
<td>show</td>
<td>resource.show</td>
</tr>
<tr>
<td>GET</td>
<td>/resource/{id}/edit</td>
<td>edit</td>
<td>resource.edit</td>
</tr>
<tr>
<td>PUT/PATCH</td>
<td>/resource/{id}</td>
<td>update</td>
<td>resource.update</td>
</tr>
<tr>
<td>DELETE</td>
<td>/resource/{id}</td>
<td>destroy</td>
<td>resource.destroy</td>
</tr>
</tbody>
</table>
<p>Sometimes you may only need to handle a subset of the resource actions:</p>
<pre class="prettyprint"><code>php artisan controller:make PhotoController --only=index,show
php artisan controller:make PhotoController --except=index
</code></pre>
<p>And, you may also specify a subset of actions to handle on the route:</p>
<pre class="prettyprint"><code>Route::resource('photo', 'PhotoController',
array('only' => array('index', 'show')));
</code></pre>
<p><a name="handling-missing-methods"></a></p>
<h2>Handling Missing Methods</h2>
<p>A catch-all method may be defined which will be called when no other matching method is found on a given controller. The method should be named <code>missingMethod</code>, and receives the parameter array for the request as its only argument:</p>
<p><strong>Defining A Catch-All Method</strong></p>
<pre class="prettyprint"><code>public function missingMethod($parameters)
{
//
}
</code></pre>
</article><div class="clearfix"></div>
</div>
</section>
<script src="js/jquery.min.js"></script>
<script src="js/plugins.js"></script>
<script type="text/javascript" src="vendor/google-code-prettify/run_prettify.js"></script>
</body></html>