在 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]+']);注意 : 路由参数不能包含中横线 "-",参数会被理解为变量名,所以不能有'-',下划线是可以滴;
在 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]+']);注意 : 路由参数不能包含中横线 "-",参数会被理解为变量名,所以不能有'-',下划线是可以滴;
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 '任意请求';});
Copy after login
2. Route parameter passing 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;});
Copy after login
3. Parameter type restriction 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]+']);
Copy after login
4. File1.2 Configure virtual hostNote that under the project path public//参数类型限制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.com
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次 则以最后一次路由为准!
/*
下例是指 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;});
Copy after login
2.4 Parameter restrictions/* 下例是指 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]+']);注意 : 路由参数不能包含中横线 "-",参数会被理解为变量名,所以不能有'-',下划线是可以滴;
Copy after login
1. Routing request method____File path app->routes->web.php在 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 '任意请求';});
//路由传参,可传多个参数Route::get('user/{id}/{name}',function($id,$name){
return '路由传参————'.$id.$name;});//路由可选参数Route::get('page/{page?}',function($page=1){
return 'page'.$page;});
Copy after login
3. Parameter type restrictions 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]+']);
Copy after login
4. File 1.2 Configure the virtual hostNote that under the project path public//参数类型限制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.com
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次 则以最后一次路由为准!
/*
下例是指 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;});
Copy after login
2.4 Parameter restrictions /* 下例是指 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]+']);注意 : 路由参数不能包含中横线 "-",参数会被理解为变量名,所以不能有'-',下划线是可以滴;
Copy after login
在 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Method for obtaining the return code when Laravel email sending fails. When using Laravel to develop applications, you often encounter situations where you need to send verification codes. And in reality...

The method of handling Laravel's email failure to send verification code is to use Laravel...

How to implement the table function of custom click to add data in dcatadmin (laravel-admin) When using dcat...

The impact of sharing of Redis connections in Laravel framework and select methods When using Laravel framework and Redis, developers may encounter a problem: through configuration...

Custom tenant database connection in Laravel multi-tenant extension package stancl/tenancy When building multi-tenant applications using Laravel multi-tenant extension package stancl/tenancy,...

LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

How to check the validity of Redis connections in Laravel6 projects is a common problem, especially when projects rely on Redis for business processing. The following is...

A problem of duplicate class definition during Laravel database migration occurs. When using the Laravel framework for database migration, developers may encounter "classes have been used...
