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

An introduction to routing in the Laravel framework

黄舟
Release: 2023-03-15 20:54:02
Original
1520 people have browsed it

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';

});
Copy after login

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

    return 'Im,Post';

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

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

    return  'Im,match';

});
Copy after login

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

    return  'Im,any';
});
Copy after login

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

     return  'User-id-'.$id;

});
Copy after login

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

    return  'User-name-'.$name;
});
Copy after login

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

    return  'User-name-'.$name;
});
Copy after login

//加入正则表达式

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

##

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

##

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

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

}]);
Copy after login

//路由群组  (将路由整合到群组中 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';
    });


});
Copy after login

// 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']);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template