Table of Contents
在 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]+']);注意 : 路由参数不能包含中横线 "-",参数会被理解为变量名,所以不能有'-',下划线是可以滴;
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]+']);注意 : 路由参数不能包含中横线 "-",参数会被理解为变量名,所以不能有'-',下划线是可以滴;
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]+']);注意 : 路由参数不能包含中横线 "-",参数会被理解为变量名,所以不能有'-',下划线是可以滴;
Copy after login
Home PHP Framework Laravel Detailed explanation of Laravel's request method and routing parameters

Detailed explanation of Laravel's request method and routing parameters

Feb 10, 2021 am 09:16 AM
laravel

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the return code when email sending fails in Laravel? How to get the return code when email sending fails in Laravel? Apr 01, 2025 pm 02:45 PM

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...

Laravel schedule task is not executed: What should I do if the task is not running after schedule: run command? Laravel schedule task is not executed: What should I do if the task is not running after schedule: run command? Mar 31, 2025 pm 11:24 PM

Laravel schedule task run unresponsive troubleshooting When using Laravel's schedule task scheduling, many developers will encounter this problem: schedule:run...

In Laravel, how to deal with the situation where verification codes are failed to be sent by email? In Laravel, how to deal with the situation where verification codes are failed to be sent by email? Mar 31, 2025 pm 11:48 PM

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

How to implement the custom table function of clicking to add data in dcat admin? How to implement the custom table function of clicking to add data in dcat admin? Apr 01, 2025 am 07:09 AM

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

Laravel - Dump Server Laravel - Dump Server Aug 27, 2024 am 10:51 AM

Laravel - Dump Server - Laravel dump server comes with the version of Laravel 5.7. The previous versions do not include any dump server. Dump server will be a development dependency in laravel/laravel composer file.

Laravel Redis connection sharing: Why does the select method affect other connections? Laravel Redis connection sharing: Why does the select method affect other connections? Apr 01, 2025 am 07:45 AM

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...

Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Apr 01, 2025 am 09:09 AM

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,...

Laravel - Action URL Laravel - Action URL Aug 27, 2024 am 10:51 AM

Laravel - Action URL - Laravel 5.7 introduces a new feature called “callable action URL”. This feature is similar to the one in Laravel 5.6 which accepts string in action method. The main purpose of the new syntax introduced Laravel 5.7 is to directl

See all articles