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);
}
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.
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);
}
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);
}
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
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);
}