Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Waik's avatar
Level 1

redirect to route in blade

I am using spatie and standard auth. I need to redirect the user to different pages during authorization. I do this in the blade template. I know it's wrong, but the other options don't work.

blade

    @hasanyrole('user|admin')
        {{ route('test') }}
    @endhasanyrole

web.php

Route::get('/testpage', function () {
    return view('test.page');
})->name('test');

when i load the template i just see the generated route text on it, how do i load the route?

0 likes
14 replies
tykus's avatar

Why are you doing this in the view; and not in a middleware?

Waik's avatar
Level 1

@tykus ok. Example

I have three roles

  • user
  • administrator
  • preuser(is an unconfirmed user).

I want to configure the logic:

  • If the user is not authorized, the user always receives the Login page
  • If the admin or user is authorized, the LoginController sends it to "/home "and the web downloads the code. route.
  • If an preuser user is logged in, he receives a separate route when loading "/home" ,for example" confim register "

My option:

Route::group(['middleware' => 'auth'], function(){

  Route::group(['middleware' => ['role:admin|user']], function () {
    Route::get('/home', 'App\Http\Controllers\PostController@index');
  });

  Route::group(['middleware' => ['role:preuser']], function () {
    Route::get('/home', function () {
        return view("confim.register");
    });
  });
});

Auth::routes();

I get an error on the"/home" "403 USER DOES NOT HAVE THE RIGHT ROLES." when I authorize under the user with the admin role

p/s i execute "php artisan optimize" for Route cache cleared!

tykus's avatar

@Waik

Two routes defined for /home URL; how does that work?

If the user is not authorized, the user always receives the Login page

Are you confusing authentication and authorization; or do you really want to log out an unauthorized User?

Waik's avatar
Level 1

@tykus

  1. Is that a problem? How to be?
  2. Mistake. Authentication.
parthjani7's avatar
  1. rename middleware to verified, and apply it to all user types. 'middleware' => ['auth', 'verified']

  2. remove the 2nd /home route & create a separate route for preuser (i.e. user-confirmation)

  3. Your middleware should be something like this:


    public function handle(Request $request, Closure $next)
    {
        if($request->user() === 'preuser') { // put your logic to idetify user type
            return redirect()->route('user-confirmation');
        }

        return $next($request);
    }
parthjani7's avatar

@waik If it is a custom middleware then you can rename it, if not create a new one.

lortschi's avatar

I love users like Tykus. He likes to give his smart-a** expert advice on the topic, but he doesn't like to answer questions.

Snapey's avatar

@lortschi Thats why he has so few 'best replies' (not)! Remember these are awarded by the person asking the question and are a mark of how well the question was answered.

In this case, the whole approach of the OP is wrong, so a lot of back and forth is just to establish the actual issue.

If the OP just walks away and does not keep up the converation then the question fails to reach a conclusion.

Tell you what, if you are looking for a solution, why not ask your own question.

Please or to participate in this conversation.