Answer the question
In order to leave comments, you need to log in
How to make friend urlManager in config with urlManager module in Yii2?
Hello. Do not tell me how to make urlManager friends with the rules in the config:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => true,
'rules' => [
'/' => 'site/index',
'/admin' => 'admin/default/index',
'<action>'=>'site/<action>',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'<module:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>' => '<module>/<controller>/<action>',
'<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
'<module:\w+>/<controller:\w+>' => '<module>/<controller>/index',
],
],
/** @var array The rules to be used in URL management. */
public $urlRules = [
'<id:\d+>' => 'profile/show',
'<action:(login|logout)>' => 'security/<action>',
'<action:(register|resend)>' => 'registration/<action>',
'confirm/<id:\d+>/<code:\w+>' => 'registration/confirm',
'forgot' => 'recovery/request',
'recover/<id:\d+>/<code:\w+>' => 'recovery/reset',
'settings/<action:\w+>' => 'settings/<action>'
];
Answer the question
In order to leave comments, you need to log in
There is a solution for Yii 1:
I found the code itself on the net, partially completed it, in terms of caching, and in terms of nested modules.
Perhaps you can finish it for Yii2
1. In the config
/* Правила роутинга для модулей */
'onBeginRequest' => array('ModuleUrlManager', 'collectRules'),
/**
* Класс менеджер путей модулей
* Активирует urlRules параметр активного модуля
* Работает с многоуровневой вложеностью модулей
* Реализовано кеширование urlRules модулей
*/
class ModuleUrlManager {
static function collectRules() {
if (!empty(Yii::app()->modules)) {
$cache = Yii::app()->getCache();
$route = Yii::app()->getRequest()->getPathInfo();
$patharray = explode('/', $route);
$moduleName = substr($route, 0, strpos($route, '/'));
/* Если роут имеет вид module.html */
if (empty($moduleName)) {
$moduleName = substr($route, 0, strpos($route, '.'));
}
if (Yii::app()->hasModule($moduleName)) {
$urlRules = false;
if ($cache)
$urlRules = $cache->get('module.url.rules.' . $moduleName);
if ($urlRules === false) {
$module = Yii::app()->getModule($moduleName);
if (isset($module->urlRules)) {
$urlRules = $module->urlRules;
if ($cache && !empty($urlRules))
$cache->set('module.url.rules.' . $module->id, $urlRules);
}
}
if (!empty($urlRules))
Yii::app()->getUrlManager()->addRules($urlRules);
if (!empty($patharray)) {
foreach ($patharray as $pathUnit) {
$position = strpos($pathUnit, '.html');
if ($position) {
$pathUnit = substr($pathUnit, 0, $position);
}
$urlRules = false;
if ($cache)
$urlRules = $cache->get('module.url.rules.' . $moduleName . '/' . $pathUnit);
if ($urlRules === false) {
$module = Yii::app()->getModule($moduleName);
if ($module->hasModule($pathUnit)) {
$submodule = $module->getModule($pathUnit);
if (isset($submodule->urlRules)) {
$urlRules = $submodule->urlRules;
if ($cache && !empty($urlRules))
$cache->set('module.url.rules.' . $submodule->id, $urlRules);
}
}
}
if (!empty($urlRules))
Yii::app()->getUrlManager()->addRules($urlRules);
}
}
}
return true;
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question