我正在使用Yii2休息服务.我想将所有控制器都包含在rest url manager中.现在我有如下配置main.php:
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => false,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
['class' => 'yii\rest\UrlRule', 'controller' => 'usera'],
['class' => 'yii\rest\UrlRule', 'controller' => 'userb'],
['class' => 'yii\rest\UrlRule', 'controller' => 'userc'],
['class' => 'yii\rest\UrlRule', 'controller' => 'userd'],
],
],
Run Code Online (Sandbox Code Playgroud)
我可以使用这样的东西,所以我不必在配置文件中包含每个控制器吗?
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => false,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => '*'],
],
],
Run Code Online (Sandbox Code Playgroud)
您可以使用两种方法:1)在controller属性中使用数组
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => ['user', 'usera', 'userb'....]],
],
Run Code Online (Sandbox Code Playgroud)
但是你必须设置所有控制器.
2)创建新类并从中扩展yii\rest\UrlRule.
namespace app\rest;
class UrlRuleCustom extends UrlRule {
$path = '@app/controllers';
public function init()
{
$d = dir(Yii::getAlias($this->path));
$arr = [];
while (false !== ($entry = $d->read())) {
if (strpos($entry, 'Controller.php') !== false) {
$arr[] = lcfirst(str_replace(['Controller.php'], '', $entry));
}
}
$this->controller = $arr;
parent::init();
}
}
Run Code Online (Sandbox Code Playgroud)
在urlManager中:
'rules' => [
['class' => 'app\rest\UrlRuleCustom', 'path' => '@app/controllers'],
],
Run Code Online (Sandbox Code Playgroud)
但我不知道有没有工作Yii::getAlias过UrlRuleCustom.如果没有工作设置而不是绝对路径或相对.
| 归档时间: |
|
| 查看次数: |
1233 次 |
| 最近记录: |