Home > PHP Framework > Laravel > body text

What is the use of laravel's route naming?

青灯夜游
Release: 2022-02-23 18:06:25
Original
3844 people have browsed it

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.

What is the use of laravel's route naming?

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');
Copy after login

You can also specify a route name for a controller method:

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

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');
Copy after login

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]);
Copy after login

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);
}
Copy after login

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');
});
Copy after login

resource route naming

resource route

Route::resource('users', 'UsersController');
Copy after login

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');
Copy after login

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
Copy after login

resource Routing prefix:

Route::resource('products',  'ProductsController',  ['as'  =>  'admin']);
route('admin.products.index'); // http://your.website/products
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!