Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the purpose of laravel routing

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge about the role of laravel routing. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

In laravel, the role of routing is to forward users' different url requests to the corresponding programs for processing; routing is the path for external access to laravel applications, routing defines the specific way in which Laravel applications provide services to the outside world, and laravel routes are defined in the routes folder.

This article operating environment: Windows10 system, Laravel6 version, Dell G3 computer.

What is the purpose of laravel routing

The function of routing is to forward the user's different url requests to the corresponding program for processing. The route of laravel is defined in the routes folder, and four routing files are provided by default, in which the web.php file defines the basic page request.

In laravel, routing is a way for outsiders to access Laravel applications, or routes define how Laravel applications provide services to the outside world. The route will submit the user's request to the specified controller and method for processing according to the pre-planned scheme.

Basic routing

The most basic routing requests are get and post requests, and laravel defines different request methods through the Route object. For example, define a get request whose url is' req' 'and return the string' get response':

Route::get ('req',function () {undefinedreturn' get response';})

When I request http://localhost/Laravel/laravel52/public/req in the form of get, the response is as follows:

Similarly, when defining a post request, use Route::post (url,function () {})

Multi-request routing

If you want to handle multiple request methods in the same way, you can use match or any:

Use match to match the corresponding request method, for example, when a req2 is requested with get or post, match response is returned:

Route::match (['get','post'],' req2',function () {undefinedreturn 'match response';})

Any will match any request method, for example, request req3 in any way and return any response:

Route::any ('req3',function () {undefinedreturn' any response';})

Request parameter

Required parameters: when a request is sent with parameters, it can be received in the route, enclosing the parameters in curly braces and dividing them with /, for example:

Route::get ('req4/ {name} / {age}', function ($name, $age) {undefinedreturn "name}, {$age} years old.";})

Optional parameters: the above parameters are required. If a parameter is missing, an error will be reported. If you want a parameter to be optional, you can add one to it and set the default value. The default parameter must be the last parameter, otherwise it cannot be identified in the middle:

Route::get ('req4/ {name} / {age?}', function ($name, $age=0) {undefinedreturn "name}, {$age} years old.";})

Regular check: the parameters in the request can be verified through where

Route::get ('req4/ {name} / {age?}', function ($name, $age=0) {undefinedreturn "name}, {$age} years old.";})-> where (['name'= >' [A-Za-z] +', 'age'= >' [0-9] +'])

Routing group

Sometimes our routing may have multiple levels, such as defining a first-level routing home, under which there is a secondary routing article,comment, etc., so we need to put article and comment into the home group. Add the prefix home to the routing article through the array key prefix:

Route::group (['prefix' = >' home'], function () {undefinedRoute::get ('article', function () {undefinedreturn' home/article';});})

This way the route can be accessed through home/article.

Route naming

Sometimes you need to give a name to a route, and you need to use the as array key to specify the route name when defining the route. For example, if you name the route home/comment comment, you can use the name comment of the route when generating url and redirection:

Route::get ('home/comment', [' as'= > 'comment',function () {undefinedreturn route (' comment'); / / generate url} corresponding to comment through the route function])

Export to http://localhost/Laravel/laravel52/public/home/comment

These are all the contents of the article "what is the purpose of laravel routing?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report