MohsenAB's avatar

Redirect doesn't work in the Middleware

Hi everyone,

I have a question. I created a middleware to check if the user has an image. Otherwise he goes to the image upload page and he has to upload an image. Unfortunately in my Middleware Redirect Function does not! What is the resone of that?

Instead I have to use Response Function and it works great! How can I send a flash message with a Response function? Hopefully you can help me with it.

public function handle($request, Closure $next) {

    // Checking through relations if the User has already an image
    if (empty(auth()->user()->profile->image)) {
        return response()
            ->view('pages.image.create', [], 200)
            ->header('Content-Type', $type = '');

        // return redirect()
        //     ->route('image.create')
        //     ->with('info', trans('To complete your profile, we request you to upload a clear photo of yourself.'));
    }

    return $next($request);
}
0 likes
7 replies
Snapey's avatar
Snapey
Best Answer
Level 122

probably because middleware's are not really good for this.

You redirect to a new route for image upload. First thing that happens on that route is that the middleware checks if you have an image, then redirects you to the same route.

To work with redirect, you have to check if they are actually headed to any of the image routes, and if so, leave them alone.

Personally, I would just put up a nagging banner.

bobbybouwmann's avatar

You return a view and don't do a real redirect. You have to redirect to the actual route

public function handle($request, Closure $next) 
{
    if (empty(auth()->user()->profile->image)) {
        return response()->route('images.upload');
    }

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

I have tried it bt it still does not work

  • In my middleware, it gives error if I use route() after response. Instead I have to use redirectToRoute(). But it still does not work!

public function handle($request, Closure $next) {

    // Checking through relations if the User has already an image
    if (empty(auth()->user()->profile->image)) {
        return response()->redirectToRoute('image.create');
    }

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

Can you please explain more about this methode. I am new in Laravel :-) Can you please send a tutorial link or give some keywords which I can use to search on Google? Thx

bobbybouwmann's avatar

Aah sorry I made a type. It should be this

return redirect()->route('images.upload'); // Using route name

// Or

return redirect('images/upload'); // Using URL
MohsenAB's avatar

Thanks for your hint!

I have solved the problem as follows. I don't know if it is safe but it works for me.

public function handle($request, Closure $next)
{

    // Checking through relations if the User has already an image

    if (empty(auth()->user()->profile->image)) {
        if (!$request->routeIs('image.create')) {
            if (!$request->isMethod('post')) {
                return redirect()
                    ->route('image.create')
                    ->with('info', trans('To complete your profile, we request you to upload a clear photo of yourself.'));
            }
        }
    }
    return $next($request);
}
1 like

Please or to participate in this conversation.