Anda di halaman 1dari 17

2017516 HTTPRoutingLaravelThePHPFrameworkForWebArtisans

SEARCH 5.2

HTTP Routing
# Basic Routing
# Route Parameters
# Required Parameters
# Optional Parameters
# Regular Expression Constraints
# Named Routes
# Route Groups
# Middleware
# Namespaces
# Sub-Domain Routing
# Route Prefixes
# CSRF Protection
# Introduction
# Excluding URIs
# X-CSRF-Token
# X-XSRF-Token
# Route Model Binding
# Form Method Spoofing
# Accessing The Current Route

# Basic Routing
All Laravel routes are defined in the app/Http/routes.php file, which is automatically loaded by the framework.
The most basic Laravel routes simply accept a URI and a Closure , providing a very simple and expressive
method of defining routes:

Route::get('foo',function(){

return'HelloWorld';

});

https://laravel.com/docs/5.2/routing 1/17
2017516 HTTPRoutingLaravelThePHPFrameworkForWebArtisans

The Default Routes File


The default routes.php file is loaded by the RouteServiceProvider and is automatically included in the web

middleware group, which provides access to session state and CSRF protection. Most of the routes for your
application will be defined within this file.

Available Router Methods


The router allows you to register routes that respond to any HTTP verb:

Route::get($uri,$callback);

Route::post($uri,$callback);
Route::put($uri,$callback);

Route::patch($uri,$callback);

Route::delete($uri,$callback);

Route::options($uri,$callback);

Sometimes you may need to register a route that responds to multiple HTTP verbs. You may do so using the
match method. Or, you may even register a route that responds to all HTTP verbs using the any method:

Route::match(['get','post'],'/',function(){

//
});

Route::any('foo',function(){

//
});

# Route Parameters

Required Parameters
Of course, sometimes you will need to capture segments of the URI within your route. For example, you may
need to capture a user's ID from the URL. You may do so by defining route parameters:

Route::get('user/{id}',function($id){

return'User'.$id;

https://laravel.com/docs/5.2/routing 2/17
2017516 HTTPRoutingLaravelThePHPFrameworkForWebArtisans

});

You may define as many route parameters as required by your route:

Route::get('posts/{post}/comments/{comment}',function($postId,$commentId){
//

});

Route parameters are always encased within "curly" braces. The parameters will be passed into your route's
Closure when the route is executed.

Note: Route parameters cannot contain the character. Use an underscore ( _ ) instead.

Optional Parameters
Occasionally you may need to specify a route parameter, but make the presence of that route parameter
optional. You may do so by placing a ? mark aer the parameter name. Make sure to give the route's
corresponding variable a default value:

Route::get('user/{name?}',function($name=null){
return$name;

});

Route::get('user/{name?}',function($name='John'){
return$name;

});

Regular Expression Constraints


You may constrain the format of your route parameters using the where method on a route instance. The where

method accepts the name of the parameter and a regular expression defining how the parameter should be
constrained:

Route::get('user/{name}',function($name){
//

})
>where('name','[AZaz]+');

Route::get('user/{id}',function($id){
https://laravel.com/docs/5.2/routing 3/17
2017516 HTTPRoutingLaravelThePHPFrameworkForWebArtisans
Route::get('user/{id}',function($id){
//

})
>where('id','[09]+');

Route::get('user/{id}/{name}',function($id,$name){
//

})
>where(['id'=>'[09]+','name'=>'[az]+']);

Global Constraints
If you would like a route parameter to always be constrained by a given regular expression, you may use the
pattern method. You should define these patterns in the boot method of your RouteServiceProvider :

/**
*Defineyourroutemodelbindings,patternfilters,etc.
*
*@param\Illuminate\Routing\Router$router
*@returnvoid

*/
publicfunctionboot(Router$router)
{
$router>pattern('id','[09]+');

parent::boot($router);
}

Once the pattern has been defined, it is automatically applied to all routes using that parameter name:

Route::get('user/{id}',function($id){

//Onlycalledif{id}isnumeric.
});

# Named Routes
Named routes allow the convenient generation of URLs or redirects for specific routes. You may specify a name
for a route using the as array key when defining the route:

Route::get('user/profile',['as'=>'profile',function(){

https://laravel.com/docs/5.2/routing 4/17
2017516 HTTPRoutingLaravelThePHPFrameworkForWebArtisans

//
}]);

You may also specify route names for controller actions:

Route::get('user/profile',[
'as'=>'profile','uses'=>'UserController@showProfile'

]);

Alternatively, instead of specifying the route name in the route array definition, you may chain the name method
onto the end of the route definition:

Route::get('user/profile','UserController@showProfile')>name('profile');

Route Groups & Named Routes


If you are using route groups, you may specify an as keyword in the route group attribute array, allowing you to
set a common route name prefix for all routes within the group:

Route::group(['as'=>'admin::'],function(){
Route::get('dashboard',['as'=>'dashboard',function(){
//Routenamed"admin::dashboard"
}]);
});

Generating URLs To Named Routes


Once you have assigned a name to a given route, you may use the route's name when generating URLs or
redirects via the global route function:

//GeneratingURLs...
$url=route('profile');

//GeneratingRedirects...
returnredirect()>route('profile');

If the named route defines parameters, you may pass the parameters as the second argument to the route

function. The given parameters will automatically be inserted into the URL in their correct positions:

Route::get('user/{id}/profile',['as'=>'profile',function($id){
https://laravel.com/docs/5.2/routing 5/17
2017516 HTTPRoutingLaravelThePHPFrameworkForWebArtisans

//

}]);

$url=route('profile',['id'=>1]);

# Route Groups
Route groups allow you to share route attributes, such as middleware or namespaces, across a large number of
routes without needing to define those attributes on each individual route. Shared attributes are specified in an
array format as the first parameter to the Route::group method.

To learn more about route groups, we'll walk through several common use-cases for the feature.

Middleware
To assign middleware to all routes within a group, you may use the middleware key in the group attribute array.
Middleware will be executed in the order you define this array:

Route::group(['middleware'=>'auth'],function(){
Route::get('/',function(){

//UsesAuthMiddleware
});

Route::get('user/profile',function(){
//UsesAuthMiddleware
});

});

Namespaces
Another common use-case for route groups is assigning the same PHP namespace to a group of controllers. You
may use the namespace parameter in your group attribute array to specify the namespace for all controllers
within the group:

Route::group(['namespace'=>'Admin'],function()

{
//ControllersWithinThe"App\Http\Controllers\Admin"Namespace

https://laravel.com/docs/5.2/routing 6/17
2017516 HTTPRoutingLaravelThePHPFrameworkForWebArtisans

Route::group(['namespace'=>'User'],function(){
//ControllersWithinThe"App\Http\Controllers\Admin\User"Namespace
});
});

Remember, by default, the RouteServiceProvider includes your routes.php file within a namespace group,
allowing you to register controller routes without specifying the full App\Http\Controllers namespace prefix. So,
we only need to specify the portion of the namespace that comes aer the base App\Http\Controllers

namespace.

Sub-Domain Routing
Route groups may also be used to route wildcard sub-domains. Sub-domains may be assigned route parameters
just like route URIs, allowing you to capture a portion of the sub-domain for usage in your route or controller.
The sub-domain may be specified using the domain key on the group attribute array:

Route::group(['domain'=>'{account}.myapp.com'],function(){
Route::get('user/{id}',function($account,$id){
//
});
});

Route Prefixes
The prefix group attribute may be used to prefix each route in the group with a given URI. For example, you
may want to prefix all route URIs within the group with admin :

Route::group(['prefix'=>'admin'],function(){
Route::get('users',function(){
//MatchesThe"/admin/users"URL
});

});

You may also use the prefix parameter to specify common parameters for your grouped routes:

Route::group(['prefix'=>'accounts/{account_id}'],function(){
Route::get('detail',function($accountId){

//MatchesThe"/accounts/{account_id}/detail"URL
});

https://laravel.com/docs/5.2/routing 7/17
2017516 HTTPRoutingLaravelThePHPFrameworkForWebArtisans

});

# CSRF Protection

Introduction
Laravel makes it easy to protect your application from cross-site request forgery (CSRF) attacks. Cross-site
request forgeries are a type of malicious exploit whereby unauthorized commands are performed on behalf of
an authenticated user.

Laravel automatically generates a CSRF "token" for each active user session managed by the application. This
token is used to verify that the authenticated user is the one actually making the requests to the application.

Anytime you define a HTML form in your application, you should include a hidden CSRF token field in the form so
that the CSRF protection middleware will be able to validate the request. To generate a hidden input field
_token containing the CSRF token, you may use the csrf_field helper function:

//VanillaPHP
<?phpechocsrf_field();?>

//BladeTemplateSyntax
{{csrf_field()}}

The csrf_field helper function generates the following HTML:

<inputtype="hidden"name="_token"value="<?phpechocsrf_token();?>">

You do not need to manually verify the CSRF token on POST, PUT, or DELETE requests. The VerifyCsrfToken

middleware, which is included in the web middleware group, will automatically verify that the token in the
request input matches the token stored in the session.

Excluding URIs From CSRF Protection


Sometimes you may wish to exclude a set of URIs from CSRF protection. For example, if you are using Stripe to
process payments and are utilizing their webhook system, you will need to exclude your webhook handler route
from Laravel's CSRF protection.

https://laravel.com/docs/5.2/routing 8/17
2017516 HTTPRoutingLaravelThePHPFrameworkForWebArtisans

You may exclude URIs by defining their routes outside of the web middleware group that is included in the
default routes.php file, or by adding the URIs to the $except property of the VerifyCsrfToken middleware:

<?php

namespaceApp\Http\Middleware;

useIlluminate\Foundation\Http\Middleware\VerifyCsrfTokenasBaseVerifier;

classVerifyCsrfTokenextendsBaseVerifier
{
/**
*TheURIsthatshouldbeexcludedfromCSRFverification.

*
*@vararray
*/
protected$except=[
'stripe/*',
];
}

X-CSRF-TOKEN
In addition to checking for the CSRF token as a POST parameter, the Laravel VerifyCsrfToken middleware will
also check for the XCSRFTOKEN request header. You could, for example, store the token in a "meta" tag:

<metaname="csrftoken"content="{{csrf_token()}}">

Once you have created the meta tag, you can instruct a library like jQuery to add the token to all request
headers. This provides simple, convenient CSRF protection for your AJAX based applications:

$.ajaxSetup({
headers:{
'XCSRFTOKEN':$('meta[name="csrftoken"]').attr('content')
}
});

X-XSRF-TOKEN
https://laravel.com/docs/5.2/routing 9/17
2017516 HTTPRoutingLaravelThePHPFrameworkForWebArtisans

Laravel also stores the CSRF token in a XSRFTOKEN cookie. You can use the cookie value to set the XXSRFTOKEN

request header. Some JavaScript frameworks, like Angular, do this automatically for you. It is unlikely that you
will need to use this value manually.

# Route Model Binding


Laravel route model binding provides a convenient way to inject model instances into your routes. For example,
instead of injecting a user's ID, you can inject the entire User model instance that matches the given ID.

Implicit Binding
Laravel will automatically resolve type-hinted Eloquent models defined in routes or controller actions whose
variable names match a route segment name. For example:

Route::get('api/users/{user}',function(App\User$user){
return$user>email;
});

In this example, since the Eloquent type-hinted $user variable defined on the route matches the {user}

segment in the route's URI, Laravel will automatically inject the model instance that has an ID matching the
corresponding value from the request URI.

If a matching model instance is not found in the database, a 404 HTTP response will be automatically generated.

Customizing The Key Name


If you would like the implicit model binding to use a database column other than id when retrieving models,
you may override the getRouteKeyName method on your Eloquent model:

/**
*Gettheroutekeyforthemodel.
*
*@returnstring

*/
publicfunctiongetRouteKeyName()
{
return'slug';
}

https://laravel.com/docs/5.2/routing 10/17
2017516 HTTPRoutingLaravelThePHPFrameworkForWebArtisans

Explicit Binding
To register an explicit binding, use the router's model method to specify the class for a given parameter. You
should define your model bindings in the RouteServiceProvider::boot method:

Binding A Parameter To A Model

publicfunctionboot(Router$router)
{
parent::boot($router);

$router>model('user','App\User');
}

Next, define a route that contains a {user} parameter:

$router>get('profile/{user}',function(App\User$user){
//
});

Since we have bound the {user} parameter to the App\User model, a User instance will be injected into the
route. So, for example, a request to profile/1 will inject the User instance which has an ID of 1.

If a matching model instance is not found in the database, a 404 HTTP response will be automatically generated.

Customizing The Resolution Logic


If you wish to use your own resolution logic, you should use the Route::bind method. The Closure you pass to
the bind method will receive the value of the URI segment, and should return an instance of the class you want
to be injected into the route:

$router>bind('user',function($value){
returnApp\User::where('name',$value)>first();
});

Customizing The "Not Found" Behavior


If you wish to specify your own "not found" behavior, pass a Closure as the third argument to the model

method:

$router>model('user','App\User',function(){
https://laravel.com/docs/5.2/routing 11/17
2017516 HTTPRoutingLaravelThePHPFrameworkForWebArtisans
$router>model('user','App\User',function(){
thrownewNotFoundHttpException;
});

# Form Method Spoofing


HTML forms do not support PUT , PATCH or DELETE actions. So, when defining PUT , PATCH or DELETE routes
that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with
the _method field will be used as the HTTP request method:

<formaction="/foo/bar"method="POST">
<inputtype="hidden"name="_method"value="PUT">
<inputtype="hidden"name="_token"value="{{csrf_token()}}">
</form>

To generate the hidden input field _method , you may also use the method_field helper function:

<?phpechomethod_field('PUT');?>

Of course, using the Blade templating engine:

{{method_field('PUT')}}

# Accessing The Current Route


The Route::current() method will return the route handling the current HTTP request, allowing you to inspect
the full Illuminate\Routing\Route instance:

$route=Route::current();

$name=$route>getName();

$actionName=$route>getActionName();

You may also use the currentRouteName and currentRouteAction helper methods on the Route facade to access
the current route's name or action:

https://laravel.com/docs/5.2/routing 12/17
2017516 HTTPRoutingLaravelThePHPFrameworkForWebArtisans

$name=Route::currentRouteName();

$action=Route::currentRouteAction();

Please refer to the API documentation for both the underlying class of the Route facade and Route instance to
review all accessible methods.

L A R A V E L I S A T R A D E M A R K O F T AY L O R O T W E L L . C O P Y R I G H T T AY L O R O T W E L L .

DESIGNED BY

https://laravel.com/docs/5.2/routing 13/17
2017516 HTTPRoutingLaravelThePHPFrameworkForWebArtisans

https://laravel.com/docs/5.2/routing 14/17
2017516 HTTPRoutingLaravelThePHPFrameworkForWebArtisans

https://laravel.com/docs/5.2/routing 15/17
2017516 HTTPRoutingLaravelThePHPFrameworkForWebArtisans

https://laravel.com/docs/5.2/routing 16/17
2017516 HTTPRoutingLaravelThePHPFrameworkForWebArtisans

https://laravel.com/docs/5.2/routing 17/17

Anda mungkin juga menyukai