Skip to content

Commit 9701230

Browse files
committed
Fix issue #39
1 parent 0abba24 commit 9701230

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/lib/generators/ControllersGenerator.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Laminas\Code\Generator\ValueGenerator;
1919
use Yii;
2020
use yii\gii\CodeFile;
21-
use yii\helpers\ArrayHelper;
2221
use yii\helpers\Inflector;
2322

2423
class ControllersGenerator
@@ -38,7 +37,9 @@ class ControllersGenerator
3837
public function __construct(Config $config, array $actions = [])
3938
{
4039
$this->config = $config;
41-
$this->controllers = ArrayHelper::index($actions, null, 'controllerId');
40+
foreach ($actions as $action) {
41+
$this->controllers[$action->prefix . '/' . $action->controllerId][] = $action;
42+
}
4243
$this->files = new CodeFiles([]);
4344
}
4445

@@ -54,7 +55,7 @@ public function generate():CodeFiles
5455
$path = $this->config->getPathFromNamespace($namespace);
5556
$templateName = $this->config->useJsonApi ? 'controller_jsonapi.php' : 'controller.php';
5657

57-
foreach ($this->controllers as $controller => $actions) {
58+
foreach ($this->controllers as $controllerWithPrefix => $actions) {
5859
$controllerNamespace = $namespace;
5960
$controllerPath = $path;
6061
/**
@@ -66,7 +67,10 @@ public function generate():CodeFiles
6667
$controllerPath = $action->prefixSettings['path']
6768
?? $this->config->getPathFromNamespace($controllerNamespace);
6869
}
69-
$className = Inflector::id2camel($controller) . 'Controller';
70+
71+
$routeParts = explode('/', $controllerWithPrefix);
72+
73+
$className = Inflector::id2camel(end($routeParts)) . 'Controller';
7074
$this->files->add(new CodeFile(
7175
Yii::getAlias($controllerPath . "/base/$className.php"),
7276
$this->config->render(

tests/specs/issue_fix/controller_namespace_issue_for_modules_in_urlprefixes/index.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
'/info' => ['module' =>'petinfo','namespace' => '\app\modules\petinfo\controllers'],
1616
'/fgh' => ['namespace' => '\app\modules\fgh\controllers'],
1717
'/fgh2' => ['path' => '@app/modules/fgh2/controllers', 'namespace' => '\app\fgh2\controllers'],
18-
'/api/v1' => ['path' => '@app/modules/api/v1/controllers', 'namespace' => '\app\api\v1\controllers']
18+
'/api/v1' => ['path' => '@app/modules/api/v1/controllers', 'namespace' => '\app\api\v1\controllers'],
19+
'theprefix' => ['module' => 'themodule', 'namespace' => '\app\modules\themodule\controllers']
1920
]
2021
];

tests/specs/issue_fix/controller_namespace_issue_for_modules_in_urlprefixes/index.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ paths:
124124
responses:
125125
'200':
126126
description: list of details
127+
/theprefix/ctrl:
128+
get:
129+
description: list all pet details
130+
responses:
131+
'200':
132+
description: list of details
127133

128134
components:
129135
schemas:

0 commit comments

Comments
 (0)