For my application I am having multiple user roles and a custom maintenance mode. If the site is in maintenance mode, then depending on the user's role will limit their access to certain pages.
So when "normal" users access the forums, they should instead see a different view saying that the site is in maintenance mode; whereas when "admin" users access the forums they should be able to see the forums.
public function handle($request, Closure $next) {
if(Auth::user()->role->maintenance_mode != 1) {
// They do not have access during maintenance mode,
// so change the response to show a different view.
}
// They do have access during maintenance mode,
// so continue the request.
return $next($request);
}
Is it possible to have the same route (e.g. /forums) but to show a different view, changed by the middleware.
It is possible, yes, however each middleware calls $next($request)
which may point to another middleware before the route, therefore returning a view may not be the best idea. If you do want to do it, then you need to do something like: return new Response(view('maintanance'));
and don't forget to include use Illuminate\Http\Response;
in the header of your Middleware class.
In your case, what I would do is have a route that returns a view such as /maintainance
(preferably with a name) and then in the middleware, return a redirect to the route return redirect()->route('maintanance');
within the if
.
You could also throw a HttpException
with the status code of 503
to have the application invoke Laravel's built in maintenance mode within that first if
.