Answer the question
In order to leave comments, you need to log in
How to make dynamic routing in Yii2?
Let's say we have 3 user types and 3 module types, respectively:
modules
-> cleaner
-> admin
-> client
Each module has an Order controller
How to make it possible to determine which module controller to run based on the user role?
For example, if the user is cleaner, then at url: "order/list" the order controller of the cleaner module should be launched.
Thanks to user matperez for his answer.
Yes, you can work with UrlRule. But what if there are 1000 controllers? Under each url to set the controller? Is it possible to somehow tell the framework which module to work with? For example, if the user is admin, then you need to tell the framework to work only with the admin module.
Decision:
namespace app\components;
use yii\web\UrlRule;
class RoleUrlRule extends UrlRule
{
public function parseRequest($manager, $request)
{
$pathInfo = $request->getPathInfo();
if (\Yii::$app->user->identity->role == 'cleaner')
return ['cleaner/' . $pathInfo, array()];
}
}
Answer the question
In order to leave comments, you need to log in
I think it's possible to create your own \yii\web\UrlRule class. In principle, only the parseRequest method should suffice. The route needs to be returned depending on the current user. Then register it in the routing rules:
'rules' => [
['class' => 'MyUrlRule', 'pattern' => '/order/list', 'route' => 'order/list', ...],
...,
]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question