Laravel Route Route

Source: Internet
Author: User

Basic routing

Most of the routes for your application will be defined in the app/routes.php file. The simplest route in Laravel consists of a URI and a closure call.

Basic GET Route

The code is as follows:


Route::get ('/', function ()
{
Return ' Hello world ';
});


Basic POST Routing

The code is as follows:


Route::p ost (' Foo/bar ', function ()
{
Return ' Hello world ';
});


Register a route in response to all HTTP methods

The code is as follows:


Route::any (' foo ', function ()
{
Return ' Hello world ';
});


Forcing a route to be accessed over HTTPS

The code is as follows:


Route::get (' foo ', Array (' HTTPS ', function ()
{
Return ' must is over HTTPS ';
}));


Often you need to generate URLs based on routes that you can use by using the Url::to method:

The code is as follows:

$url = url::to (' foo ');

Route parameters

The code is as follows:


Route::get (' User/{id} ', function ($id)
{
Return ' User '. $id;
});


Optional routing parameters

The code is as follows:


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


Optional route parameters with default values

The code is as follows:


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


Routing with regular expression constraints

The code is as follows:


Route::get (' user/{name} ', function ($name)
{
//
})
->where (' name ', ' [a-za-z]+ ');
Route::get (' User/{id} ', function ($id)
{
//
})
->where (' id ', ' [0-9]+ ');

Routing Filters

Route filters provide a simple way to restrict access to a specified route, which is useful when you need to create an authentication zone for your site. The Laravel framework contains a number of routing filters, such as auth filters, Auth.basic filters, guest filters, and CSRF filters. They are stored in the app/filters.php file.

Define a route filter

The code is as follows:


Route::filter (' old ', function ()
{
if (Input::get (' age ') < 200)
{
Return redirect::to (' home ');
}
});


If a response is returned from a route filter, the response is considered a response to the request, the route will not be executed, and any after filter on the route will be canceled.

Specify a route filter for a route

The code is as follows:


Route::get (' user ', Array (' before ' = ' old ', function ()
{
Return ' You were over years old! ';
}));


Specify multiple route filters for a route

The code is as follows:


Route::get (' user ', Array (' before ' = ' auth|old ', function ()
{
Return ' You were authenticated and over years old! ';
}));


Specifying route filter Parameters

The code is as follows:


Route::filter (' Age ', function ($route, $request, $value)
{
//
});
Route::get (' user ', Array (' before ' = ' age:200 ', function ()
{
Return ' Hello world ';
}));


When the route filter receives the response as a third parameter $response:

The code is as follows:


Route::filter (' Log ', function ($route, $request, $response, $value)
{
//
});


Patterns for basic route filters

You may want to specify a filter based on a URI for a set of routes.

The code is as follows:


Route::filter (' admin ', function ()
{
//
});
Route::when (' admin/* ', ' admin ');


In the example above, the admin filter will be applied with all routes beginning with admin/. The asterisk, as a wildcard, is adapted to the combination of all characters.

You can also constrain a pattern filter by specifying an HTTP method:

The code is as follows:


Route::when (' admin/* ', ' admin ', array (' post '));

Filter class

For advanced filters, you can use a class instead of a closure function. Because the filter class is an IoC container that is outside the application, you can use dependency injection in the filter to make it easier to test.

Define a filter class

The code is as follows:


Class Foofilter {
Public Function Filter ()
{
Filter logic ...
}
}


Registering a class-based filter

The code is as follows:


Route::filter (' foo ', ' Foofilter ');

Named routes

Named routes make it easier to specify routes when generating jumps or URLs. You can specify a name for the route like this:

The code is as follows:


Route::get (' User/profile ', Array (' as ' = ' profile ', function ()
{
//
}));


You can also specify the route name for the controller's methods:

The code is as follows:


Route::get (' User/profile ', Array (' as ' = ' profile ', ' uses ' =
' [email protected] ');


Now you use the name of the route when generating URLs or jumps:

The code is as follows:


$url = Url::route (' profile ');
$redirect = Redirect::route (' profile ');


You can use the Currentroutename method to get the name of a route:

The code is as follows:


$name = Route::currentroutename ();

Routing groups

There are times when you might want to apply filters to a set of routes. You do not need to specify a filter for each route, you can use routing groups:

The code is as follows:


Route::group (Array (' before ' = ' auth '), function ()
{
Route::get ('/', function ()
{
Has Auth Filter
});
Route::get (' User/profile ', function ()
{
Has Auth Filter
});
});

Sub-domain Routing

The Laravel route can also handle wildcard subdomains and get wildcard parameters from the domain name:

Registering sub-domain Routing

The code is as follows:


Route::group (Array (' domain ' = ' = ' {account}.myapp.com '), function ()
{
Route::get (' User/{id} ', function ($account, $id)
{
//
});
});

Route prefixes

A set of routes can be prefixed to a routing group by using the prefix option in an attribute array:

To add a prefix to a routing group

The code is as follows:


Route::group (Array (' prefix ' = ' admin '), function ()
{
Route::get (' User ', function ()
{
//
});
});

Routing model bindings

Model binding provides a simple way to inject a model into a route. For example, not only is the ID of a user injected, you can inject the entire user model instance according to the specified ID. First use the Route::model method to specify the desired model:

Binding a variable to a model

The code is as follows:


Route::model (' user ', ' user ');


Then, define a route that contains the {user} parameter:

The code is as follows:


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

Because we have bound the {user} parameter to the user model, a user instance is injected into the route. So, for example, a PROFILE/1 request will inject a User instance with ID 1.

Note: If the model instance is not found in the database, a 404 error will be thrown.

If you want to specify a behavior that you define that you do not find, you can pass a closure as the third parameter for the model method:

The code is as follows:


Route::model (' user ', ' user ', function ()
{
throw new Notfoundexception;
});


Sometimes you want to use your own method to handle routing parameters, you can use the Route::bind method:

The code is as follows:


Route::bind (' User ', function ($value, $route)
{
Return User::where (' name ', $value)->first ();
});


404 Error raised

There are two ways to manually trigger a 404 error in a route. First, you can use the App::abort method:

The code is as follows:


App::abort (404);

Second, you can throw an instance of symfony\component\httpkernel\exception\notfoundhttpexception.

More information about handling 404 exceptions and using custom responses for these errors can be found in the error section.

Routing to a controller

Laravel not only allows you to route to closures, it can also be routed to the Controller class, and even allows the creation of resource controllers.

For more information, please visit the controller documentation.

Laravel Route Route

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.