Home  >  Article  >  Backend Development  >  An introduction to routing in the Laravel framework

An introduction to routing in the Laravel framework

黄舟
黄舟Original
2017-09-09 09:31:531439browse

Finally decided to learn one more heavyweight framework. Of course, the first choice is Laravel, which is known as the most elegant web development framework--Laravel

For getting started with the framework, first understand its routing rules: Previously necessary, the following are several common basic routing rules in laravel

//基础路由
//GET
Route::get('basic',function (){

    return 'Im,GET';

});

//POST
Route::post('basic2',function (){

    return 'Im,Post';

});
//多请求路由(两种方式:match &  any   match:需指定请求方式  any:无需指定)

Route::match(['get','post'],'match',function (){

    return  'Im,match';

});

Route::any('any',function (){

    return  'Im,any';
});

// 路由参数
Route::get('user/{id}',function ($id){

     return  'User-id-'.$id;

});

//可选值
Route::get('user/{name?}',function ($name = null){

    return  'User-name-'.$name;
});

//默认值
Route::get('user/{name?}',function ($name = 'koala'){

    return  'User-name-'.$name;
});

//加入正则表达式

Route::get('user/{name?}',function ($name = 'koala'){
    return  'User-name-'.$name;
})->where('name','[A-Za-z]+');

##

//多参数 加正则验证
Route::get('user/{id}/{name?}',function ($id,$name='koala'){
    return 'User-id-'.$id . '-name-' . $name;
})->where(['id'=>'[0-9]+','name'=>'[A-Za-z]+']);

##

//路由别名  (路由别名的作用是为了方便在模板中的调用,日后就算路由有修改,只要别名不变还是可以访问)
Route::get('user/member-center',['as'=>'center',function(){

        return  route('center'); //显示路由的路径规则

}]);

//路由群组  (将路由整合到群组中 ps:prefix为路由的前缀名称)  

Route::group(['prefix'=> 'member'],function (){

    Route::get('user/member-center',['as'=>'center',function(){

        return  route('center');

    }]);


    Route::any('any',function (){

        return  'Im,member-any';
    });


});

// The following is how we combine routing and controller to access the controller through routing

First we need to create a controller

Next we configure the routing rules

//路由与控制器关联(以 GET 为例)

//第一种方法

//Route::get('member/info','MemberController@info');

//第二种方法
Route::get('member/info',['uses'=>'MemberController@info']);

The above is the detailed content of An introduction to routing in the Laravel framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn