当前位置:网站首页>Routing in laravel framework

Routing in laravel framework

2022-07-05 13:49:00 Weichi Begonia

Common methods in routing

laravel The routes in the framework are defined in routes/web.php in , Commonly used Route Class get/ any/ match Method to define the route , among any/match All support multiple request modes ,group Method can be used to define routing groups , such as :

Route::get('integration', function () {
    return "hello world";
});

In the above code get Method requires two parameters , One is url Address (integration The front frame will be automatically added and defined in xampp Medium url Address ), One is function, Inside is the value we need to return or view.

If it is view, It can be defined as this :

Route::get('/', function () {
    return view('welcome');
});

laravel Common methods defined by the middle road :

1. get Method : ditto

2. any Method ( Multi route request ): That is, this route can be triggered in any way , such as :

Route::any('multi2', function(){
   return "multi2";
});

3. match Method ( Multi route request ):

Route::match(['get', 'post'], 'multi1', function(){
    return 'multi1';   //  This route supports get/post Way to trigger 
});

3. routing group

Route::group(['prefix'=>"member"], function(){
    //  The following routes will all have member Prefix 
    Route::get('user/center', ['as'=>'center', function(){
        return route('center');
    }]);
    Route::any('multy2', function(){
        return "group-multi2";
    });
});

How to add parameters to a route ?- The wonderful use of braces {}

Route::get("user/{id}/{name?}", function($id,$name=null){
   return "User-yingkun-".$id."-".$name;
});

How to add some parameter restrictions to the route ? -- Use where Method

Route::get("user/{id}/{name}", function($id, $name){
   return "User-" . $id . "-name-" . $name;
})->where(["id"=>"\d+", "name"=>"[A-Za-z]+"]);

  Routing alias

Route::get("user/member-center", ["as"=>"center", function(){
//    return "member-center";
    return route('center');   // route The function is used to generate the specified route url Address of the 
}]);

 

原网站

版权声明
本文为[Weichi Begonia]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140519440800.html