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

Route setting, Route parameters and Route naming methods in laravel Framework

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "routing settings, routing parameters and routing naming methods in the laravel framework", with detailed content, clear steps and proper handling of details. I hope that this article "routing settings, routing parameters and routing naming methods in the laravel framework" can help you solve your doubts.

Routing must be configured in laravel before it can be used. Unlike in tp, it can be used without configuration, because tp can be parsed automatically through pathinfo.

First, simple routing settings

We generally configure web-side routing in the routes/web.php file.

/ / Parameter 1, indicating the uri path / / Parameter 2, closure function, processing response Route::get ('/ test', function () {return 'Test';})

Second, routing methods to handle specific http requests

Route::get ('/', function () {}); Route::post ('/', function () {}); Route::delete ('/', function () {})

You can also capture any request method through Route::any ()

Route::any ('/', function () {})

You can also use Route::match () to handle the specified request method

Route::match (['get',' post'],'/', function () {})

Third, map routes to controller methods

Route::get ('/ hello', 'HelloController@index')

Map the processing of / hello to the index method of app/Http/Controllers/HelloController.php.

If we create a multi-tier directory under the Controllers directory, we can use the (directory\...\ controller @ method):

Route::get ('/ hello', 'Hello\ HelloController@index')

IV. Routing parameters

Sometimes you need to pass parameters on the route, just identify it in the routing path.

Route::get ('/ list/ {page}', function ($page) {return "current pages {$page}";})

However, the above page parameter is required. If it is not passed, the error will not be found. At this point, you can add a? after the identity to indicate optional, and give the function parameter a default value.

Route::get ('/ list/ {page?}', function ($page = 1) {return "current pages {$page}";})

We can also set regular rules for routing parameters to ensure the correctness of the parameters.

Route::get ('/ search/ {key?} / {page?}', function ($key ='', $page = 1) {return "search {$key} pages {$page}";})-> where (['key' = >' [A-Za-z] +', 'page' = >' [0-9] +'])

Get routing parameters

Route::get ('/ search/ {key?} / {page?}', function (Request $req) {/ / get a single routing parameter var_dump ($req::route ('key')); / / get all routing parameters var_dump ($req::route ()-> parameters ());})

Get the common parameters through Request::all (), similar to? a=a&b=b&c=c

Route::get ('/ search/ {key?} / {page?}', function (Request $req, $key ='', $page = 1) {var_dump ($key); var_dump ($page); var_dump ($req::all ());})

Fifth, route naming

We can set a name for the route to facilitate use in the view

Route::get ('/ list/ {page?}', function ($page = 1) {return view ('list', [' page' = > $page]);})-> name ('list.page')

In the resources/views/list.blade.php view, we display the routing address through the route () method

{{route ('list.page', [' page' = > $page])}} read here, this article "routing settings, routing parameters and routing naming methods in the laravel framework" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it to understand it. If you want to know more about related articles, please follow 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