Laravel Routing Notes

Source: Internet
Author: User

All route is defined in the app/http/routes. php file, the file contains the most basic route and group. The default group is the group that provides the Web middleware. Only group within the Web middleware can access session and CSRF protection.

Router The route types allowed for registration include:

Route::get ($uri$callback); Route::p ost ($uri$callback); Route::p ut ($uri$callback); Route::p atch ($uri$callback); Route::d elete ($uri$callback); Route: Options ($uri$callback);

If you need to satisfy multiple request types at the same time, you need to use the match method, and if you need to satisfy all the request types, use the Any method:

Route::match ([' Get ', ' post '], '/', function () {    //}); Route::any (' foo ', function () {    //});

The route can get parameters from the URL that is accessed:

function ($id) {    return ' User '.  $id;}); Routefunction ($postId$commentId) {    //});

Parameters are not allowed----only use _ instead of

We can also name the route, make it easy to use in URLs, and redirect. Define using ' as ' as the key for the array:

function  () {    //}]);

A route using a controller can also be defined as follows:

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

You can also call the method setting name when you define the route:

Route::get (' User/profile ', ' [email protected] ')->name (' profile ');

If you use group, you can use as to declare a route name prefix for the route in all group:

function () {    routefunction  () {        //  route named "admin::d ashboard"     }]);});

After defining the name, you can use it conveniently:

// Generating URLs ... $url = route (' profile '); // Generating redirects ... return redirect ()->route (' profile ');

If you need to pass a parameter, you just need to do the same as calling the function:

function ($id) {    //}]); $url = route (' profile ', [' id ' = + 1]);

Parameters are automatically inserted into the corresponding location of the URL.

Route Group:

The route group allows you to share attributes to a set of route, such as middleware or namespace. Examples are as follows:

Route::group ([' middleware ' = ' auth '),function() {Route:: Get ('/',function ()    {        //Uses Auth Middleware    }); Route:: Get (' User/profile ',function () {        //Uses Auth Middleware    });}); Route:: Group ([' namespace ' = ' Admin '),function(){    //Controllers within the "App\http\controllers\admin" NamespaceRoute:: Group ([' namespace ' = ' User '),function() {        //Controllers within the "App\http\controllers\admin\user" Namespace    });});

The default namespace directory is app\Http\controllers.

You can also use the route group to match the two-level domain name and URL prefixes:

function () {    Routefunction ($account$id) {        //    });}); Routefunction  () {    routefunction  ()    {        //  Matches the "/admin/users" URL    }) ;

You can even add parameters to the URL prefix:

function () {    Routefunction ($accountId)    {        //  Matches the "/ Accounts/{account_id}/detail "URL    });});

Laravel Routing Notes

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.