Summary of the "Laravel5.0 Framework" routing method

Source: Internet
Author: User

The route in Laravel, like other PHP frameworks, is to offload various requests to each controller.

The simplest route consists of a URI and a closure call.

Routing files in: ' learnlaravel5/app/http/routes.php ':

Basic GET Route

Route::get ('/', function () {return ' Hello World ';});

Basic POST Routing

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

Register a route in response to all HTTP methods

Route::any (' foo ', function () {   return ' Hello World ';});

Forcing a route to be accessed over HTTPS

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

Route parameters

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

Optional routing parameters

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

Optional route parameters with default values

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

Routing with regular expression constraints

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

Route::filter (' old ', function () {  if (input::get (' age ') < $)  {    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

Route::get (' user ', Array (' before ' = ' old ', function () {  return ' is over years old! ';}
));

Specify multiple route filters for a route

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

Specifying route filter Parameters

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:

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.

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:

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

Using Filter Classes

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

Class Foofilter {public Function filter () {//Filter logic ...}}

Registering a class-based filter

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:

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

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

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

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

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

Use the Currentroutename method to get the name of a route:

$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:

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

Route::group (' 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

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

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

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

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:

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

To handle routing parameters in your own way, you can use the Route::bind method:

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:

App::abort (404);

Controller Routing

Is it tiring to define a route for each address rule?

Laravel gave us a compromise solution--Controller routing:

Route::controller ('/', ' HomeController ');

The way the controller is written is also subject to change:

<?php namespace App\http\controllers;use app\http\controllers\controller;class HomeController extends Controller {    /**     * Show home page.     *     * @return Response     *    /Public Function GetIndex ()    {        return view (' home ');    }    /**     * Show about interface     *     * @return Response     *    /Public Function getabout ()    {        return view (' About ');}    }

In accordance with the above example,

If we access http://yourdomain/ the address it will show HomeController the getIndex content that the method produces,

Access http://yourdomain/about , getAbout the content generated by the method is displayed.

In addition to use such as get{Method} This format, can also have, and so on, as for post{Method} delete{Method} get the prefix, post and so on behalf of the meaning, you understand!

Resource Controller

Resource controllers have a great connection to restful, and to understand resource controllers, you must first understand restful. Do not understand their own Baidu!

The Laravel resource controller has native support for restful architectures. In fact, there is no direct difference between the Laravel resource controller and other controllers, but the method and structure of the Controller class is slightly stipulated.

To define a resource controller in a route, you need only one:

Route::resource (' article ', ' Articlecontroller ');

Address rules for access:

Request Method request URI the corresponding controller method meaning of the representative
GET /article Index Index/List
GET /article/create Create Create (show form)
POST /article Store Save the data you created
GET /article/{id} Show Display the contents of the corresponding ID
GET /article/{id}/edit Edit Edit (Show form)
Put/patch /article/{id} Save Save the data you edit
GET /article/{id} Destroy Delete

Pick two explanations.

I have defined a resource route Route::resource(‘article‘, ‘ArticleController‘); .

When I access the address http://yourdomain/article , it is equivalent to accessing ArticleController the controller's index method.

When I access the address, I get http://yourdomain/article/create access to the create method.

When I submit data to an address via post http://yourdomain/article , the equivalent is handled by the store method.

--------------------extension------------------------------

To generate URLs based on routing, you can use the Url::to method:

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

Remark ****************

This article refers to the Phphub community's chongyi,jb51.com Junjie.

Summary of the "Laravel5.0 Framework" routing method

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.