Auth role based authentications

  Fri 09 / 02 / 2024

  Posted by: Vanita saini

Category : Laravel

Tags : laravel , php

Description

 //Route::resource('/nwqs', NationalWaterQualityController::class);  This route is not working if i add this code in below section

Route::prefix('auth')->middleware(['auth','isAdmin','isUser'])->group(function(){

    Route::resource('/nwqs', NationalWaterQualityController::class);

 });


I just want to access this route in both admin as well as user section. 




View Code
                        Route::prefix('auth')->middleware(['auth','isAdmin'])->group(function(){
   
    Route::get('/dashboard', [DashboardController::class, 'dashboard'])->name('dashboard');
    Route::resource('/posts', PostController::class);
    Route::resource('/categories', CategoryController::class);
    Route::resource('/stations', StationController::class);

    Route::get('/airqualities/import',[AirqualityController::class, 'import'])->name('airqualities.import');
    Route::post('/airqualities/import',[AirqualityController::class, 'importSave'])->name('airqualities.importSave');
    Route::resource('/airqualities', AirqualityController::class);

    Route::get('/categories/download/{filename}',[CategoryController::class, 'download'])->name('categories.download');
    Route::get('/categories/remove/{id}',[CategoryController::class, 'remove'])->name('categories.remove');
    Route::resource('/modules', ModuleController::class);
    Route::get('/modules/download/{filename}/{filetype?}',[ModuleController::class, 'download'])->name('modules.download');
    Route::post('/posts/get-additional-fields', [PostController::class, 'getAdditionalFields'])->name('posts.get-additional-fields');

    //Route::resource('/nwqs', NationalWaterQualityController::class);
    Route::resource('/stps', StpController::class);
    Route::post('/stps/checkSubRegion', [StpController::class, 'checkSubRegion'])->name('stps.checkSubRegion');
    Route::post('/stps/dropdownStationCode', [StpController::class, 'dropdownStationCode'])->name('stps.dropdownStationCode');
    Route::post('/stps/dropdownStationCodeAddress', [StpController::class, 'dropdownStationCodeAddress'])->name('stps.dropdownStationCodeAddress');
    Route::resource('/users', UserDashboardController::class);

});


Route::prefix('auth')->middleware(['auth','isAdmin','isUser'])->group(function(){
    Route::resource('/nwqs', NationalWaterQualityController::class);
 });


Route::middleware(['isUser','auth'])->group(function(){
    Route::get('/userdashboard', [HomeController::class, 'home'])->name('userdashboard');
    Route::get('/user/profile', [UserDashboardController::class, 'profile'])->name('user.profile');
    Route::post('/user/save', [UserDashboardController::class, 'save'])->name('user.save');
 });
                
  Answered by CDL

Your code looks fine but I think the prefix doesn't apply on the resource route, as you mention the prefix at the top so define it like this


~~~

Route::resource('/auth/nwqs', NationalWaterQualityController::class);

~~~


This will work for you. Let me know if you still have any issues.

Solved
  Comment   Share
0 Likes   1 Comment


comment-author
Vanita saini Author

still not working. i used this code like this: Route::prefix('auth')->middleware(['auth','isAdmin'])->group(function(){ Route::resource('/nwqs', NationalWaterQualityController::class); Route::resource('/stps', StpController::class); Route::post('/stps/checkSubRegion', [StpController::class, 'checkSubRegion'])->name('stps.checkSubRegion'); Route::post('/stps/dropdownStationCode', [StpController::class, 'dropdownStationCode'])->name('stps.dropdownStationCode'); Route::post('/stps/dropdownStationCodeAddress', [StpController::class, 'dropdownStationCodeAddress'])->name('stps.dropdownStationCodeAddress'); Route::resource('/users', UserDashboardController::class); }); /*Route::middleware(['auth','isAdmin','isUser'])->group(function(){ Route::resource('/auth/nwqs', NationalWaterQualityController::class); });*/ Route::middleware(['isUser','auth'])->group(function(){ Route::get('/userdashboard', [HomeController::class, 'home'])->name('userdashboard'); Route::resource('/auth/nwqs', NationalWaterQualityController::class); Route::get('/user/profile', [UserDashboardController::class, 'profile'])->name('user.profile'); Route::post('/user/save', [UserDashboardController::class, 'save'])->name('user.save'); }); Now it works only in the user section, not for admin.



Reply


commented 8 months ago
comment-author
Hadayat Niazi Super Admin

Was your problem resolved?



comment-author
Hadayat Niazi Super Admin

@vanita Let's understand the issue first: Basically, the issue is, one route overriding another route, so if you want to see the list of your routes execute this command: "php artisan route:list" After that you will be able to see all the routes so make sure to remove collision, for user you can use Route::('user/nwqs'); and for the administrator, you can use Route::('admin/nwqs'); Let me know still you have any issues.