Laravel Framework Routing configuration summary, setting skill Daquan _php Example

Source: Internet
Author: User
Tags auth closure

Basic routing

The vast majority of your application's routes 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

Copy Code code as follows:

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

Basic POST Route
Copy Code code as follows:

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

registers a route in response to all HTTP methods
Copy Code code as follows:

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

force a route to be accessed through HTTPS
Copy Code code as follows:

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

Often you need to generate URLs based on routing, and you can use the Url::to method:
Copy Code code as follows:
$url = url::to (' foo ');

Routing parameters

Copy Code code as follows:

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

Optional Routing parameters
Copy Code code as follows:

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

Optional route parameters with default values
Copy Code code as follows:

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

routing with regular expression constraints
Copy Code code 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

Routing filters provide a simple way to restrict access to a specified route, which is useful when you need to create a certification area for your site. The Laravel framework includes 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 routing filter

Copy Code code as follows:

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

If a response is returned from a routing filter, the response is considered to be the response of 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

Copy Code code as follows:

Route::get (' user ', Array (' Before ' => ' old ', function ()
{
Return ' to are over years old! ';
}));

Specify multiple routing filters for a route

Copy Code code as follows:

Route::get (' user ', Array (' Before ' => ' Auth|old ', function ()
{
Return ' to are authenticated and over years old! ';
}));

Specify route filter Parameters
Copy Code code as follows:

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

When the routing filter receives a response $response as a third parameter:
Copy Code code as follows:

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

mode of basic routing Filters

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

Copy Code code as follows:

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

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

You can also constrain the mode filter by specifying the HTTP method:

Copy Code code 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 located outside the application, you can use dependency injection in the filter to make it easier to test.

Define a filter class

Copy Code code as follows:

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

registering a class-based filter
Copy Code code 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:

Copy Code code as follows:

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

You can also specify a route name for the controller's methods:
Copy Code code as follows:

Route::get (' User/profile ', Array (' as ' => ' profile ', ' uses ' =>
' Usercontroller@showprofile '));

Now you use the name of the route when you generate the URL or jump:

Copy Code code as follows:

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

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

Copy Code code as follows:

$name = Route::currentroutename ();

Routing Group

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

Copy Code code as follows:

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

Sub-domain name routing

The Laravel route can also handle the subdomain of the wildcard character and get the wildcard argument from the domain name:

Registering child domain name routing

Copy Code code as follows:

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

Route prefix

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

Add a prefix to a routing group

Copy Code code 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, if you inject a user ID, you can inject an entire user model instance based on the specified ID. First use the Route::model method to specify the desired model:

To bind a variable to a model

Copy Code code as follows:

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

Then, define a route that contains the {user} parameter:
Copy Code code 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 an instance of User with ID 1.

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

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

Copy Code code 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:
Copy Code code as follows:

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

Throw 404 Error

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

Copy Code code as follows:

App::abort (404);

Second, you can throw an instance of a 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 Controller

Laravel not only allows you to route to closures, you can also route to controller classes, and even allow the creation of resource controllers.

For more information, visit the controller documentation.

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.