Same route for different controllers depending on the logged user #964
Replies: 3 comments 1 reply
-
@realfabiopio hey there. I think you are approaching the problem from the wrong perspective. I think it would be easier and more correct if the route always resolve to a "BaseController", in that "BaseController" you can check the authenticated user and call the appropriate functions. // App\Actions\C001Action.php
public function __invoke() {
// if you need crud here, you can resolve it from the container
$crud = app('crud');
}
// App\Actions\C0012Action.php
public function __invoke() {
// ...
}
// and your base controller
public function endpoint()
{
return match($user->role) {
'admin' => new \App\Actions\C001Action(),
'user' => new \App\Actions\C0012Action()
} If you are talking only about permissions, you should do that check in your controller, using a Gate or whatever you feel better using. Like I proposed is more common to see, but usually there is no need to do that in CRUD controllers as actions are more suited for single action controllers. Cheers |
Beta Was this translation helpful? Give feedback.
-
I didn't test this, but in theory. Create a Middleware
on routes
Check is that works for you. Cheers. |
Beta Was this translation helpful? Give feedback.
-
Due to not activity i will close this issue, but please feel free to re-open or create a new one if needed. Cheers. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I need to implement a solution that allows the same route for different controllers depending on the logged user.
Some users have customizations in their controllers, those who do not must use the route's default controller.
In LoginController I store the conditional in the session.
Example:
Controllers:
and so on...
route:
Route::crud('abc', 'App\Http\Controllers\abcController');
I tried to use a middleware but any attempt to change the controller caused the object to need to be instantiated and thus lost all references (e.g. in setupListOperation() $this->crud was null)
Is there a way to solve this?
Beta Was this translation helpful? Give feedback.
All reactions