John Davidson

php - Laravel routes prefix for protected and unprotected routes

0 comments
Message:


I have two types of routes.



  1. For guests and authenticated users

  2. For authenticated users only


For authenticated users only, i am using a middleware and i need to have a route prefix. My code look like this


Route::prefix('guests')->group(function () {
Route::get('/user_landing', [EngineController::class, 'user_landing'])->name('user_landing');
});

Route::middleware(['auth', 'role:user,admin'])->group(function () {
Route::get('/user_landing', [EngineController::class, 'user_landing'])->name('user_landing');
});

How can i have a route prefix if i already have a middleware in


Route::middleware(['auth', 'role:user,admin'])->group(function () {
Route::get('/user_landing', [EngineController::class, 'user_landing'])->name('user_landing');
});


My Answer:
> For guests and authenticated users

php
Route::get('/home', 'HomeController@index')->name('home');

// For authenticated users only
Route::middleware('auth')->group(function () {
Route::get('/dashboard', 'DashboardController@index')->name('dashboard');
});


> For authenticated users only

php
Route::middleware('auth')->prefix('protected')->group(function () {
Route::get('/profile', 'ProfileController@index')->name('profile');
});


In the above code, the routes with the prefix 'protected' will only be accessible to authenticated users due to the 'auth' middleware. The routes without a prefix will be accessible to both guests and authenticated users.

Rate this post

4 of 5 based on 1915 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us