sheershoff / yii2-module-urlrules
支持Yii2模块URL规则的扩展。
1.0.2
2016-05-26 07:03 UTC
Requires
- php: >=5.3.0
- yiisoft/yii2: ~2.0.0
Requires (Dev)
- phpunit/phpunit: 4.*
This package is auto-updated.
Last update: 2024-09-29 04:54:55 UTC
README
此扩展允许在模块内部声明URL规则,便于管理模块URL规则,例如API模块。
本模块采用MIT许可证(MIT)分发。
要求
Yii >=2.0.0
安装
安装此扩展的首选方式是通过composer。
运行以下命令之一:
php composer.phar require --prefer-dist sheershoff/yii2-module-urlrules
或将以下内容添加到composer.json的require部分:
"sheershoff/yii2-module-urlrules": "~1.0.0"
配置
要使用此扩展,您需要在应用程序配置中配置组件部分,并在模块中添加getUrlRules或urlRules。
在所需的app的main.php
中添加moduleUrlRules
组件
'components' => [ // ... 'moduleUrlRules' => [ 'class' => '\sheershoff\ModuleUrlRules\ModuleUrlRules', // allowed modules lists the modules that affect the url rules 'allowedModules' => ['v1'], ], // ...
检查模块是否已声明,并查看API应用的urlManager
设置或至少启用enablePrettyUrl
选项。例如:
return [ 'modules' => [ 'v1' => [ 'basePath' => '@api/modules/v1', 'class' => 'api\modules\v1\Module' ] ], 'components' => [ // ... // this config is suitable for an API app 'urlManager' => [ 'enablePrettyUrl' => true, 'enableStrictParsing' => true, 'showScriptName' => false, 'rules' => [], ], ], ];
在模块的Module.php
中添加getUrlRules
,如下所示
<?php namespace api\modules\v1; class Module extends \yii\base\Module { public $controllerNamespace = 'api\modules\v1\controllers'; public function getUrlRules() { return [ [ 'class' => 'yii\rest\UrlRule', 'controller' => [self::getUniqueId().'/city'], ], ]; } }
祝您使用愉快 :-)
待办事项
- 使用不同的php和yii版本进行测试。
- 查看引导过程是否可以优化,使其不会绑定到没有
moduleUrlRules
组件的应用。 - 检查别名映射是否正常工作,以将模块移动到另一个slug或在此扩展中实现一个。