In laravel, the role of route naming is to generate routes to facilitate redirection; route naming allows specific routes to easily generate URLs or redirects. Developers can link to routes using the name method to define the name of a specific route.
The operating environment of this tutorial: Windows 7 system, Laravel 6 version, Dell G3 computer.
Laravel’s route naming
The role of laravel route naming: generate routes to facilitate redirection.
Route naming allows specific routes to easily generate URLs or redirects. You can link to a route using the name method to define the name of a specified route:
Route::get('user/profile', function () { // })->name('profile');
You can also specify a route name for a controller method:
Route::get('user/profile', 'UserProfileController@show')->name('profile');
Generate a link to a named route URL
Once a name is assigned to a given route, the route's name can be used when generating URLs or redirects via the global route function:
// 生成 URL... $url = route('profile'); // 重定向... return redirect()->route('profile');
if If the named route defines parameters, you can pass the parameters as the second parameter to the route function. The given parameters will automatically be inserted into the URL at their 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 the current request Whether to route to a given named route, you can use the named method on the route 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 give the route group Adds a given string to each route name. For example, you might want to prefix the names of all group routes with "admin." The given string is exactly the same as the specified route name prefix, so we will make sure to provide the trailing . character in the prefix:
Route::name('admin.')->group(function () { Route::get('users', function () { // 指定路由名为 "admin.users"... })->name('users'); });
resource route naming
resource route
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');
resource route naming
Route::resource('foo', 'ProductsController'); route('foo.index'); // http://your.website/foo Route::resource('products', 'ProductsController', ['names' => 'foo']); route('foo.index'); // http://your.website/products Route::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'); // http://your.website/products
[Related recommendations: laravel video tutorial]
The above is the detailed content of What is the use of laravel's route naming?. For more information, please follow other related articles on the PHP Chinese website!