简体   繁体   中英

Get laravel route {slug} in middleware

I am implementing role based access system in laravel, i want to validate each request thorugh moddileware. So i want to my route {slug} in my middleware, based on that slug i want to check that user has permission for access that request. How can i achieve that?

My route code:

Route::group(['middleware' => 'testmiddleware'], function ($router) {
        Route::get('{slug}', 'App\Http\Controllers\UserController@getAuthenticatedUser');
});

如果您想在testmiddleware中的路由中获取{slug}参数的值,您可以按如下方式实现:

$request->route('slug')

Add prefix to group

Route::group(['prefix' => "{slug}", 'middleware' => 'testmiddleware'], function () {
    Route::get('/', 'App\Http\Controllers\UserController@getAuthenticatedUser'); // it will geneate url {slug}
    Route::get('blog', 'App\Http\Controllers\Blog@getBlogs');// it will geneate url {slug}/blog
    Route::post('user', 'App\Http\Controllers\Users@getUsers'); // it will geneate url {slug}/user
});

then in middleware you will get

$request->slug
  • in this way you will get slug to all group no need to add all the routes

ref link https://laravel.com/docs/8.x/routing#route-group-prefixes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM