Answer the question
In order to leave comments, you need to log in
Yii2. Organizing output with different roles in the same way?
Good afternoon!
There are several roles.
When users with different roles enter the site, all of them should see the same layout, and, for example, following the same path, they should have different content of this layout.
For example,
there are 3 roles: role_1, role_2, role_3.
Users follow the url "/".
And each of them sees its content inside this layout'a.
those. user with role_1 sees view_1, user with role_2 sees view_2, etc.
The logic for view's is also different.
Please advise how to organize it more competently? So that the controllers do not bloat and there is no switch by role in each method (controller).
The problem is that the logic for each view render is different for each role, and for each role in the controller method you will have to write something like:
switch($role) {
case 'role_1':
.....
$params = arr1;
break;
case 'role_2':
.....
$params = arr2;
break;
case 'role_3':
.....
$params = arr2;
break;
}
return $this->render('index-' . $role, $params);
Answer the question
In order to leave comments, you need to log in
Create your own controller for each role: Role1Controller, Role2Controller, Role3Controller.
And create a routing rule that will select a controller depending on the role.
If the matter is only in different views, then you can do this:return $this->render('index_'.$user->role);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question