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 '任意请求';});
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;});
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]+']);
4. File1.2 Configure virtual hostNote 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.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;});
2.4 Parameter restrictions在 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]+']);注意 : 路由参数不能包含中横线 "-",参数会被理解为变量名,所以不能有'-',下划线是可以滴;
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 '任意请求';});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;});
3. Parameter type restrictions app->routes->web.php//参数类型限制Route::get('choice/{id}/{name}',function($id,$name){
return 'choice参数类型限制'.$id.$name;})->where(['id'=>'\d+','name'=>'[a-zA-Z]+']);
4. File 1.2 Configure the virtual hostNote that under the project path public modify the virtual host configuration file and add the following code to apache:
<virtualhost>DocumentRoot "D:/xampp/htdocs/<project>/public"ServerName ddd.com</project></virtualhost>host文件 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;});
2.4 Parameter restrictions 在 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!

Laravel is suitable for building web applications quickly, while Python is suitable for a wider range of application scenarios. 1.Laravel provides EloquentORM, Blade template engine and Artisan tools to simplify web development. 2. Python is known for its dynamic types, rich standard library and third-party ecosystem, and is suitable for Web development, data science and other fields.

Laravel and Python each have their own advantages: Laravel is suitable for quickly building feature-rich web applications, and Python performs well in the fields of data science and general programming. 1.Laravel provides EloquentORM and Blade template engines, suitable for building modern web applications. 2. Python has a rich standard library and third-party library, and Django and Flask frameworks meet different development needs.

Laravel is worth choosing because it can make the code structure clear and the development process more artistic. 1) Laravel is based on PHP, follows the MVC architecture, and simplifies web development. 2) Its core functions such as EloquentORM, Artisan tools and Blade templates enhance the elegance and robustness of development. 3) Through routing, controllers, models and views, developers can efficiently build applications. 4) Advanced functions such as queue and event monitoring further improve application performance.

Laravel is not only a back-end framework, but also a complete web development solution. It provides powerful back-end functions, such as routing, database operations, user authentication, etc., and supports front-end development, improving the development efficiency of the entire web application.

Laravel is suitable for web development, Python is suitable for data science and rapid prototyping. 1.Laravel is based on PHP and provides elegant syntax and rich functions, such as EloquentORM. 2. Python is known for its simplicity, widely used in Web development and data science, and has a rich library ecosystem.

Laravelcanbeeffectivelyusedinreal-worldapplicationsforbuildingscalablewebsolutions.1)ItsimplifiesCRUDoperationsinRESTfulAPIsusingEloquentORM.2)Laravel'secosystem,includingtoolslikeNova,enhancesdevelopment.3)Itaddressesperformancewithcachingsystems,en

Laravel's core functions in back-end development include routing system, EloquentORM, migration function, cache system and queue system. 1. The routing system simplifies URL mapping and improves code organization and maintenance. 2.EloquentORM provides object-oriented data operations to improve development efficiency. 3. The migration function manages the database structure through version control to ensure consistency. 4. The cache system reduces database queries and improves response speed. 5. The queue system effectively processes large-scale data, avoid blocking user requests, and improve overall performance.

Laravel performs strongly in back-end development, simplifying database operations through EloquentORM, controllers and service classes handle business logic, and providing queues, events and other functions. 1) EloquentORM maps database tables through the model to simplify query. 2) Business logic is processed in controllers and service classes to improve modularity and maintainability. 3) Other functions such as queue systems help to handle complex needs.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment