I use backpack 4 for laravel(laravel 8)and can't write a route for the user page. After login user has to get /user/dashboard or /dashboard, but the user is redirected to /admin/dashboard. How can I solve this problem?
web.php
Route::get('/', function() {
return redirect()->route('backpack.auth.login');
});
Route::get('/user/dashboard', [DashboardUserController::class, 'index'])->middleware(['web','admin']);
CodePudding user response:
If your users won't ever need to use Backpack and should only able to view the dashboard, you could add a new middleware to Backpack's set of middlewares that checks if your user is an admin or a normal user to redirect them if they are not an admin.
Because Backpack registers the login routes itself, you can't just add your middleware to the route on registration. Instead you'll need to add your middleware to Backpack's admin
middleware group. That's the one that gets added to all admin routes.
Steps:
- Create the new middleware:
php artisan make:middleware RedirectToDashboard
- Open the new middleware at
app/Http/Middleware/RedirectToDashboard.php
- Change the
handle
method to look something like this:
public function handle(Request $request, Closure $next)
{
if($request->user()->isNormalUser()) { // You need to change this to the check if the user is a normal user, that's specific to your application
return redirect()->to('dashboard'); // 'dashboard' is the name of your route, you may need to change it.
}
return $next($request);
}
- Add the new middleware to Backpack's default set of middlewares in
config/backpack/base.php
. Open the file and look for themiddleware_class
entry (most likely somewhere around line 220). Add\App\Http\Middleware\RedirectToDashboard::class
to the array. - Log into Backpack as a normal user and check if you get redirected.
CodePudding user response:
/admin
prefix comes from config/backpack/base.php remove route_preffix
from the file.
Your users will be redirected to /dashboard
.