06.10.2016 Views

laravel-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

HTTP Routing 123<br />

5 });<br />

Route Prefixes<br />

The prefix group attribute may be used to prefix each route in the group with a given URI. For<br />

example, you may want to prefix all route URIs within the group with admin:<br />

1 Route::group(['prefix' => 'admin'], function () {<br />

2 Route::get('users', function () {<br />

3 // Matches The "/admin/users" URL<br />

4 });<br />

5 });<br />

Route Model Binding<br />

When injecting a model ID to a route or controller action, you will often query to retrieve the model<br />

that corresponds to that ID. Laravel route model binding provides a convenient way to automatically<br />

inject the model instances directly into your routes. For example, instead of injecting a user’s ID,<br />

you can inject the entire User model instance that matches the given ID.<br />

Implicit Binding<br />

Laravel automatically resolves type-hinted Eloquent models defined in routes or controller actions<br />

whose variable names match a route segment name. For example:<br />

1 Route::get('api/users/{user}', function (App\User $user) {<br />

2 return $user->email;<br />

3 });<br />

In this example, since the Eloquent type-hinted $user variable defined on the route matches the<br />

{user} segment in the route’s URI, Laravel will automatically inject the model instance that has<br />

an ID matching the corresponding value from the request URI. If a matching model instance is not<br />

found in the database, a 404 HTTP response will be automatically generated.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!