M
M
Maria_Gavrilova2019-08-17 09:59:21
Laravel
Maria_Gavrilova, 2019-08-17 09:59:21

Separate folder in Controllers for each role?

I have Admin, Owner, Client, User roles in the project... In the administrative interface, Admin and Owner differ quite a bit (but there are still differences). Is it normal to make a separate folder in Controllers for each role and almost duplicate all controllers, slightly modifying them? It also turns out that routes are duplicated ... and views. Please advise to organize structures. There is just an idea that I duplicate a lot of code, is this normal? For roles and permissions I use "laravel spatie permission".

Answer the question

In order to leave comments, you need to log in

5 answer(s)
E
Eugene Wolf, 2019-08-17
@Wolfnsex

Is it normal to make a separate folder in Controllers for each role and almost duplicate all controllers, slightly modifying them?
No. Even if we discard all other "buts", imagine what it will cost you to introduce a new role into the system?! This will be a complete ahtung!
It also turns out that routes are duplicated ... and views.
Especially.
There is just an idea that I duplicate a lot of code, is this normal?
No, not normal. To be honest, I don't understand how you could learn Laravel while ignoring one of the fundamental principles of DRY development .
For roles and permissions I use "laravel spatie permission".
To be honest, I don’t know what it is, but if the current module for servicing rights / privileges / roles / access control (etc.) - something does not suit you or makes (?) You violate the fundamental principles of development - you just need to find another more suitable for your specific needs.

J
jazzus, 2019-08-18
@jazzus

Duplicate code is not normal. An attempt to refactor duplicate code, when ideas can change on the go, and fix in 10 files, will convince you of this. Regarding the access check in Laravel, everything has been invented for a long time. Add to this the relationships between the tables roles, perms and users (many-to-many), add roles and permissions to the table and can be used without modules. And even here, checks should be made not by roles, but by access (perms table). Roles may have overlapping access. Ready-made descriptions of how to do it exactly on the Internet are full. It's elementary there. You can also add an active boolean to user_role so that you can quickly deactivate a user role (a user can have multiple roles) with history preserved for instant recovery. This can be used when moderating users with a minimum of code and without logging (you get inactive user roles and make active true)

D
Dmitry, 2019-08-22
@dlnsk

You need to understand the principle of using roles and permissions. Perhaps this article will help you:
Laravel 5. Hierarchical RBAC for the smallest

S
Sanes, 2019-08-17
@Sanes

I have done so myself. Arranged views and controllers in different directories.
Only I actually have 2 independent applications, admin and user.
Those. not roles, but applications divided. Perhaps not your case.

A
Alex Wells, 2019-08-17
@Alex_Wells

It is normal if there is a significant difference between the portals. More than normal. But it's best if the separation is only on the frontend, with shared components where necessary, and on the backend - normally designed Rest, without separation into roles in urls.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question