Yii2 routes definition in modules(模块中的 Yii2 路由定义)
问题描述
有没有办法从模块配置中添加路由?
Is there any solution to add routes from module configuration?
示例.我们有描述
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => require(FILE_PATH_CONFIG_ENV . '_routes.php') // return array
],
]
在每个模块中,我们加载带有私有参数的自定义配置文件
in each module we load custom config file with private parameters
public function loadConfig($sFileName = 'main', $sFolderName = 'config')
{
Yii::configure($this, require($this->getBasePath() . DS . $sFolderName . DS . $sFileName . '.php'));
return $this;
}
配置文件
return [
'components' => [
'urlManager' => [
'class' => 'yiiwebUrlManager',
'rules' => [
'/admin' => '/admin/index/index',
]
]
]
];
现在我需要以某种方式将当前配置(主要用于 Web 应用程序)与从模块加载的配置合并.最后,我只想在模块配置路由中描述这个模块,并将它们用于漂亮的 url(用户友好的 url).我怎样才能做到这一点?这个例子在我创建 url /admin/index/index 时不起作用,它显示了我 /admin/index/index 但我想要 /admin如模块规则中所述.
And now I need somehow merge current config (main for web application) with config loaded from module. In end I want describe in module config routes only for this module and use them for pretty urls (user friendly url). How can I do this one? This examples not working when I create url /admin/index/index, it shows me /admin/index/index but I want /admin as mentioned in module rules.
推荐答案
模块的分隔 url 规则在官方文档中提到 这里.
Separating url rules for modules is mentioned in official documentation here.
而且我认为这是一种更优化的方法,与在一个配置文件中合并和声明所有规则不同.
And I think this is more optimum approach unlike merging and declaring all rules in one config file.
规则按照声明的顺序进行解析(这在 此处),因此通过分离,您可以跳过其他模块的 url.在大量规则的情况下,它可以提高性能.
The rules are parsed in order they are declared (this is mentioned here), so with the separation you can skip other modules urls. In case of large amount of rules it can give perfomance boost.
这篇关于模块中的 Yii2 路由定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:模块中的 Yii2 路由定义
基础教程推荐
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
