Home >PHP Framework >Laravel >Detailed explanation of Laravel's request method and routing parameters
The following tutorial column of Laravel will introduce you to Laravel's routing request method and routing parameters. I hope it will be helpful to friends in need!
##1. Routing request method____File path app->routes->web.php//get请求方式 Route::get('user/show',function(){ return '世间安得两全法,不负如来不负卿;万般皆是命,半点不由人';}); //post请求方式 Route::post('edit',function(){ return '万般皆是命,半点不由人';}); //多请求路由 Route::match(['get','post'],'user/register',function(){ return '多请求路由register';}); //任意请求 Route::any('user/wall'function(){ return '任意请求';});
//路由传参,可传多个参数 Route::get('user/{id}/{name}',function($id,$name){ return '路由传参————'.$id.$name;}); //路由可选参数 Route::get('page/{page?}',function($page=1){ return 'page'.$page;});
//参数类型限制Route::get('choice/{id}/{name}',function($id,$name){ return 'choice参数类型限制'.$id.$name;})->where(['id'=>'\d+','name'=>'[a-zA-Z]+']);
modify the virtual host configuration file and add the following code to apache:
<VirtualHost *:80>DocumentRoot "D:/xampp/htdocs/<project>/public"ServerName ddd.com</VirtualHost>host文件 127.0.0.1 ddd.comChapter 2 RouterIntroduction to routing
1. Simply put, it forwards the user's request to the corresponding program for processing
2. It is used to establish the mapping between the URL and the program
3. Request types get, put, post, patch, delete, etc.
No framework can be separated from the router. TP is generated through address bar rules, such as: xxx.com/home/user/add;
2.1 How to call the router Controller
The relationship between laravel's router and controller needs to be clearly defined in the /routes/web.php
file.
The format is as follows:
基础路由/* 当用 GET 方式访问 xx.com/yy 这个地址的时候用匿名函数去响应 . */Route::get('/yy', function(){return '123';});/* 当用 POST 方式访问 xx.com/zz 这个地址时,用 匿名函数去响应 . */Route::post('/zz', function(){return '123';});/* 当 GET 访问网站根目录 "/" 时,用第2个参数的匿名函数去响应 . */Route::get('/', function () {return 'hello';})多请求路由/* 不管是GET还是POST方法,访问 xx.com/user 时,都用 XxController 中的 method() 方法去响应 . */Route::match(['get','post'] , '/user' , 'XxController@method')/* GET,POST,PUT,DELETE.. 任何方法访问 xx.com/test, 都用第2个参数中的匿名函数去响应 . */Route::any('/test', function () {return 'Hello World';});注意: 如果同一个路由被写了2次 则以最后一次路由为准!2.2 Router and passing parameters
/* 下例是指 xx.com/user/123 这样的 URL,user 后面的值将会捕捉到, 并自动传递给控制器的方法或匿名函数 */Route::get('user/{id}', function ($id) {return 'User '.$id;});/* 下例是指 xx.com/user/{name}/{id} 这样的 URL,user 后的参数, 会被捕捉到 , 并自动传递给控制器的方法或匿名函数 */Route::get('user/{name}/{id}', function ($name, $id) {return 'user_'.$name.$id;});如果没有传递参数,则会报错;2.3 传递可选参数 在路由 参数 的花括号最后 加上 ?(问号) 即可 Route::get('user/{name?}', function ($name = null) {return $name;});Route::get('user/{name?}', function ($name = 'John') {return $name;});
在 TP 中,自动验证写在 Model 里,不够灵活. laravel把参数限制写在方法或者路由中.普通形式:->where('要限制的参数名','限制规则(正则,不用斜线//)');数组形式:->where(['要限制的参数名1'=>'限制规则1(正则,不用斜线//)','要限制的参数名2'=>'限制规则2(正则,不用斜线//)']);Route::get('user/{name}', function ($name) {//})->where('name', '[A-Za-z]+');Route::get('user/{id}', function ($id) {//})->where('id', '[0-9]+');Route::get('user/{id}/{name}', function ($id, $name) {//})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);注意 : 路由参数不能包含中横线 "-",参数会被理解为变量名,所以不能有'-',下划线是可以滴;
//get请求方式Route::get('user/show',function(){ return '世间安得两全法,不负如来不负卿;万般皆是命,半点不由人';});//post请求方式 Route::post('edit',function(){ return '万般皆是命,半点不由人';}); //多请求路由Route::match(['get','post'],'user/register',function(){ return '多请求路由register';});//任意请求Route::any('user/wall'function(){ return '任意请求';});2. Routing parameter transfer app->routes->web.php
//路由传参,可传多个参数Route::get('user/{id}/{name}',function($id,$name){ return '路由传参————'.$id.$name;});//路由可选参数Route::get('page/{page?}',function($page=1){ return 'page'.$page;});
//参数类型限制Route::get('choice/{id}/{name}',function($id,$name){ return 'choice参数类型限制'.$id.$name;})->where(['id'=>'\d+','name'=>'[a-zA-Z]+']);
modify the virtual host configuration file and add the following code to apache:
367e5423ee61f30ef297d66282a28f6fDocumentRoot "D:/xampp/htdocs/c70346ba2d7be186a93fef826469bd2f/public"ServerName ddd.comee672f0beb03b42be69279368a66a410host文件 127.0.0.1 ddd.comChapter 2 RouterRouting Introduction
1. Simply speaking, it is to forward the user's request to the corresponding program for processing
2. It is used to establish the mapping between the URL and the program
3. The request type is get, put, post, patch, delete, etc.
No framework can be separated from the router. TP is generated through address bar rules, such as: xxx.com/home/user/add;
2.1 How the router calls the controller
Laravel's router and control The relationship between routers needs to be clearly defined in the /routes/web.php
file.
The format is as follows:
基础路由/* 当用 GET 方式访问 xx.com/yy 这个地址的时候用匿名函数去响应 . */Route::get('/yy', function(){return '123';});/* 当用 POST 方式访问 xx.com/zz 这个地址时,用 匿名函数去响应 . */Route::post('/zz', function(){return '123';});/* 当 GET 访问网站根目录 "/" 时,用第2个参数的匿名函数去响应 . */Route::get('/', function () {return 'hello';})多请求路由/* 不管是GET还是POST方法,访问 xx.com/user 时,都用 XxController 中的 method() 方法去响应 . */Route::match(['get','post'] , '/user' , 'XxController@method')/* GET,POST,PUT,DELETE.. 任何方法访问 xx.com/test, 都用第2个参数中的匿名函数去响应 . */Route::any('/test', function () {return 'Hello World';});注意: 如果同一个路由被写了2次 则以最后一次路由为准!2.2 Router and passing parameters
/* 下例是指 xx.com/user/123 这样的 URL,user 后面的值将会捕捉到, 并自动传递给控制器的方法或匿名函数 */Route::get('user/{id}', function ($id) {return 'User '.$id;});/* 下例是指 xx.com/user/{name}/{id} 这样的 URL,user 后的参数, 会被捕捉到 , 并自动传递给控制器的方法或匿名函数 */Route::get('user/{name}/{id}', function ($name, $id) {return 'user_'.$name.$id;});如果没有传递参数,则会报错;2.3 传递可选参数 在路由 参数 的花括号最后 加上 ?(问号) 即可 Route::get('user/{name?}', function ($name = null) {return $name;});Route::get('user/{name?}', function ($name = 'John') {return $name;});
在 TP 中,自动验证写在 Model 里,不够灵活. laravel把参数限制写在方法或者路由中.普通形式:->where('要限制的参数名','限制规则(正则,不用斜线//)');数组形式:->where(['要限制的参数名1'=>'限制规则1(正则,不用斜线//)','要限制的参数名2'=>'限制规则2(正则,不用斜线//)']);Route::get('user/{name}', function ($name) {//})->where('name', '[A-Za-z]+');Route::get('user/{id}', function ($id) {//})->where('id', '[0-9]+');Route::get('user/{id}/{name}', function ($id, $name) {//})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);注意 : 路由参数不能包含中横线 "-",参数会被理解为变量名,所以不能有'-',下划线是可以滴;
The above is the detailed content of Detailed explanation of Laravel's request method and routing parameters. For more information, please follow other related articles on the PHP Chinese website!