Home > PHP Framework > Laravel > body text

Detailed explanation of Laravel's request method and routing parameters

藏色散人
Release: 2021-02-20 09:05:06
forward
5130 people have browsed it

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

//路由传参,可传多个参数
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('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 virtual host

Note that under the project path public

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
Copy after login

Chapter 2 Router

Introduction 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(&#39;/yy&#39;, function(){return &#39;123&#39;;});/*
当用 POST 方式访问 xx.com/zz 这个地址时,用 匿名函数去响应 .
*/Route::post(&#39;/zz&#39;, function(){return &#39;123&#39;;});/*
当 GET 访问网站根目录 "/" 时,用第2个参数的匿名函数去响应 .
*/Route::get(&#39;/&#39;, function () {return &#39;hello&#39;;})多请求路由/*
不管是GET还是POST方法,访问 xx.com/user 时,都用 XxController 中的 method() 方法去响应 .
*/Route::match([&#39;get&#39;,&#39;post&#39;] , &#39;/user&#39; , &#39;XxController@method&#39;)/*
GET,POST,PUT,DELETE.. 任何方法访问 xx.com/test, 都用第2个参数中的匿名函数去响应 .
*/Route::any(&#39;/test&#39;, function () {return &#39;Hello World&#39;;});注意: 如果同一个路由被写了2次
则以最后一次路由为准!
Copy after login

2.2 Router and passing parameters

/*
下例是指 xx.com/user/123 这样的 URL,user 后面的值将会捕捉到,
并自动传递给控制器的方法或匿名函数
*/Route::get(&#39;user/{id}&#39;, function ($id) {return &#39;User &#39;.$id;});/*
下例是指 xx.com/user/{name}/{id} 这样的 URL,user 后的参数,
会被捕捉到 , 并自动传递给控制器的方法或匿名函数
*/Route::get(&#39;user/{name}/{id}&#39;, function ($name, $id) {return &#39;user_&#39;.$name.$id;});如果没有传递参数,则会报错;2.3 传递可选参数
在路由 参数 的花括号最后 加上 ?(问号) 即可
Route::get(&#39;user/{name?}&#39;, function ($name = null) {return $name;});Route::get(&#39;user/{name?}&#39;, function ($name = &#39;John&#39;) {return $name;});
Copy after login

2.4 Parameter restrictions

在 TP 中,自动验证写在 Model 里,不够灵活. laravel把参数限制写在方法或者路由中.普通形式:->where(&#39;要限制的参数名&#39;,&#39;限制规则(正则,不用斜线//)&#39;);数组形式:->where([&#39;要限制的参数名1&#39;=>&#39;限制规则1(正则,不用斜线//)&#39;,&#39;要限制的参数名2&#39;=>&#39;限制规则2(正则,不用斜线//)&#39;]);Route::get(&#39;user/{name}&#39;, function ($name) {//})->where(&#39;name&#39;, &#39;[A-Za-z]+&#39;);Route::get(&#39;user/{id}&#39;, function ($id) {//})->where(&#39;id&#39;, &#39;[0-9]+&#39;);Route::get(&#39;user/{id}/{name}&#39;, function ($id, $name) {//})->where([&#39;id&#39; => &#39;[0-9]+&#39;, &#39;name&#39; => &#39;[a-z]+&#39;]);注意 : 路由参数不能包含中横线 "-",参数会被理解为变量名,所以不能有&#39;-&#39;,下划线是可以滴;
Copy after login

              1. Routing request method____File path app->routes->web.php

//get请求方式Route::get(&#39;user/show&#39;,function(){
    return &#39;世间安得两全法,不负如来不负卿;万般皆是命,半点不由人&#39;;});//post请求方式
 Route::post(&#39;edit&#39;,function(){
    return &#39;万般皆是命,半点不由人&#39;;}); //多请求路由Route::match([&#39;get&#39;,&#39;post&#39;],&#39;user/register&#39;,function(){
    return &#39;多请求路由register&#39;;});//任意请求Route::any(&#39;user/wall&#39;function(){
    return &#39;任意请求&#39;;});
Copy after login

2. Routing parameter transfer app->routes->web.php

//路由传参,可传多个参数Route::get(&#39;user/{id}/{name}&#39;,function($id,$name){
    return &#39;路由传参————&#39;.$id.$name;});//路由可选参数Route::get(&#39;page/{page?}&#39;,function($page=1){
    return &#39;page&#39;.$page;});
Copy after login

3. Parameter type restrictions app->routes->web.php

//参数类型限制Route::get(&#39;choice/{id}/{name}&#39;,function($id,$name){
    return &#39;choice参数类型限制&#39;.$id.$name;})->where([&#39;id&#39;=>&#39;\d+&#39;,&#39;name&#39;=>&#39;[a-zA-Z]+&#39;]);
Copy after login

4. File

1.2 Configure the virtual host

Note that under the project path public

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
Copy after login

Chapter 2 Router

Routing 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(&#39;/yy&#39;, function(){return &#39;123&#39;;});/*
当用 POST 方式访问 xx.com/zz 这个地址时,用 匿名函数去响应 .
*/Route::post(&#39;/zz&#39;, function(){return &#39;123&#39;;});/*
当 GET 访问网站根目录 "/" 时,用第2个参数的匿名函数去响应 .
*/Route::get(&#39;/&#39;, function () {return &#39;hello&#39;;})多请求路由/*
不管是GET还是POST方法,访问 xx.com/user 时,都用 XxController 中的 method() 方法去响应 .
*/Route::match([&#39;get&#39;,&#39;post&#39;] , &#39;/user&#39; , &#39;XxController@method&#39;)/*
GET,POST,PUT,DELETE.. 任何方法访问 xx.com/test, 都用第2个参数中的匿名函数去响应 .
*/Route::any(&#39;/test&#39;, function () {return &#39;Hello World&#39;;});注意: 如果同一个路由被写了2次
则以最后一次路由为准!
Copy after login

2.2 Router and passing parameters

/*
下例是指 xx.com/user/123 这样的 URL,user 后面的值将会捕捉到,
并自动传递给控制器的方法或匿名函数
*/Route::get(&#39;user/{id}&#39;, function ($id) {return &#39;User &#39;.$id;});/*
下例是指 xx.com/user/{name}/{id} 这样的 URL,user 后的参数,
会被捕捉到 , 并自动传递给控制器的方法或匿名函数
*/Route::get(&#39;user/{name}/{id}&#39;, function ($name, $id) {return &#39;user_&#39;.$name.$id;});如果没有传递参数,则会报错;2.3 传递可选参数
在路由 参数 的花括号最后 加上 ?(问号) 即可
Route::get(&#39;user/{name?}&#39;, function ($name = null) {return $name;});Route::get(&#39;user/{name?}&#39;, function ($name = &#39;John&#39;) {return $name;});
Copy after login

2.4 Parameter restrictions

在 TP 中,自动验证写在 Model 里,不够灵活. laravel把参数限制写在方法或者路由中.普通形式:->where(&#39;要限制的参数名&#39;,&#39;限制规则(正则,不用斜线//)&#39;);数组形式:->where([&#39;要限制的参数名1&#39;=>&#39;限制规则1(正则,不用斜线//)&#39;,&#39;要限制的参数名2&#39;=>&#39;限制规则2(正则,不用斜线//)&#39;]);Route::get(&#39;user/{name}&#39;, function ($name) {//})->where(&#39;name&#39;, &#39;[A-Za-z]+&#39;);Route::get(&#39;user/{id}&#39;, function ($id) {//})->where(&#39;id&#39;, &#39;[0-9]+&#39;);Route::get(&#39;user/{id}/{name}&#39;, function ($id, $name) {//})->where([&#39;id&#39; => &#39;[0-9]+&#39;, &#39;name&#39; => &#39;[a-z]+&#39;]);注意 : 路由参数不能包含中横线 "-",参数会被理解为变量名,所以不能有&#39;-&#39;,下划线是可以滴;
Copy after login

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!

Related labels:
source:csdn.net
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