In the constructor method of my controllers I have a line similar to the following;
AdminHelper::userHasPermission($controller, $action, $redirect); AdminHelper::userHasPermission('user', 'view', true);
This executes a help method which pulls the users id from the session and checks to make sure that the user has the necessary permissions to use this controller.
If $redirect = true, and the user doesn't have permissions to view the resource, I want to redirect to an error page. The issue I have is that I don't necessarily want to return a request/response. I simply want to redirect and exit. However I can't seem to redirect from within this method;
I've tried...
if ($hasPermission == false) { header('location: /admin/error'); exit; }
if ($hasPermission == false) { redirect()->to('/admin/error); exit; }
Neither of the above work. I know I could perform this check in every method in the controller and return it, but I don't want to place the same bit of code in every method of every controller.
I know, I don't want to as it's the controllers constructor method that's calling in the userHasPermission method;
public function __construct() { AdminHelper::userHasPermission('user', 'view', true); }
The idea is the constructor method checks the user can access this controller, and if the user doesn't have permissions, the method should perform a redirect.
I figured it out;
if ($hasPermissions != true) { redirect()->to('/admin/error')->send(); }
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community