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 route naming

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

Share

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

Most people do not understand the knowledge points of this article "what is the role of laravel routing naming", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the role of laravel routing naming" article.

In laravel, the role of route naming is to generate routes and facilitate redirection; route naming allows specific routes to easily generate URL or redirection. Developers can use the name method to link to a route to define the name of the specified route.

The operating environment of this tutorial: windows7 system, Laravel6 version, Dell G3 computer.

Route naming for laravel

The role of laravel route naming: generate routes for easy redirection.

Route naming allows specific routes to easily generate URL or redirects. You can use the name method to link to a route to define the name of the specified route:

Route::get ('user/profile', function () {/ /})-> name (' profile')

You can also specify a route name for the controller's method:

Route::get ('user/profile',' UserProfileController@show')-> name ('profile')

Generate a URL that points to a named route

Once a name has been assigned to a given route, the name of the route can be used when generating URL or redirection through the global route function:

/ / generate URL...$url = route ('profile'); / / redirect. Return redirect ()-> route (' profile')

If a named route defines a parameter, you can pass the parameter to the route function as the second parameter. The given parameter is automatically inserted into the URL in its correct location:

Route::get ('user/ {id} / profile', function ($id) {/ /})-> name (' profile'); $url = route ('profile', [' id' = > 1])

Check the current route

If you want to determine whether the current request is routed to a given named route, you can use the named method on the routing instance. For example, you can check the current route name in the routing middleware:

/ * Handle an incoming request. * * @ param\ Illuminate\ Http\ Request $request * @ param\ Closure $next * @ return mixed * / public function handle ($request, Closure $next) {if ($request- > route ()-> named ('profile')) {/ /} return $next ($request);}

Route name prefix

The name method can be used to add a given string to each route name in a route group. For example, you might want to prefix the names of all packet routes with "admin". The given string is exactly the same as the specified route name prefix, so we will ensure that the trailing. Characters:

Route::name ('admin.')-> group (function () {Route::get (' users', function () {/ / specify the route name as "admin.users"... })-> name ('users');})

Resource route naming

Resource routing

Route::resource ('users',' UsersController')

The above code will be equivalent to:

Route::get ('/ users', 'UsersController@index')-> name (' users.index'); Route::get ('/ users/ {user}', 'UsersController@show')-> name (' users.show'); Route::get ('/ users/create', 'UsersController@create')-> name (' users.create'); Route::post ('/ users', 'UsersController@store')-> name (' users.store') Route::get ('/ users/ {user} / edit', 'UsersController@edit')-> name (' users.edit'); Route::patch ('/ users/ {user}', 'UsersController@update')-> name (' users.update'); Route::delete ('/ users/ {user}', 'UsersController@destroy')-> name (' users.destroy')

Naming of resource rout

Route::resource ('foo',' ProductsController'); route ('foo.index'); / / http://your.website/fooRoute::resource('products',' ProductsController', ['names' = >' foo']); route ('foo.index'); / / http://your.website/productsRoute::resource('products',' ProductsController', ['names' = >' admin.products']); route ('admin.products.index') / / http://your.website/products

Resource routing prefix:

Route::resource ('products',' ProductsController', ['as' = >' admin']); route ('admin.products.index'); / / the above is about "what is the purpose of routing naming in laravel". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, 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