Is creating different controllers for different roles is a good way?
Hello,
I would like to know if creating controllers for different roles - for example DashboardAdministratorController is a good way of doing things? I've noticted that in my DashboardController I have few if instructions for different roles. In each if instruction I have about 150 lines of code. I think that putting this in separate controllers will make the code cleaner.
If I am thinking good, please tell me if then the DashboardController should only check roles and then return the index from another controller - for example check if role is administrator then return index from DashboardAdministratorController? Or maybe there is a better way of doing it?
You should always separate admin and user code as much as possible, mainly to avoid accidental interference etc.
You could do what I do, which is have subfolders for controllers.
In app\Http\Controllers you create a folder called Administrator, or admin, or adminarea etc. And within this you have all your admin only controllers etc.
You can create new controllers in here automatically too, by using
Thanks, that's nice suggestion! Okay, but then what about the main DashboardController? I should check the roles there and then return the method from custom controller? Because I read somewhere that redirecting from controller method to another controller method is not a good way of doing things.
I mean something like that: User has logged into the app, then he is redirected to the DashboardController, and in the controller I am checking if the user has role admin or some other role and then I am returning method from AdminDashboardController.