13
13
14
14
namespace CodeIgniter \Commands \Utilities \Routes \AutoRouterImproved ;
15
15
16
+ use Config \Routing ;
16
17
use ReflectionClass ;
17
18
use ReflectionMethod ;
18
19
@@ -33,13 +34,18 @@ final class ControllerMethodReader
33
34
*/
34
35
private array $ httpMethods ;
35
36
37
+ private bool $ translateURIDashes ;
38
+
36
39
/**
37
40
* @param string $namespace the default namespace
38
41
*/
39
42
public function __construct (string $ namespace , array $ httpMethods )
40
43
{
41
44
$ this ->namespace = $ namespace ;
42
45
$ this ->httpMethods = $ httpMethods ;
46
+
47
+ $ config = config (Routing::class);
48
+ $ this ->translateURIDashes = $ config ->translateURIDashes ;
43
49
}
44
50
45
51
/**
@@ -69,7 +75,7 @@ public function read(string $class, string $defaultController = 'Home', string $
69
75
foreach ($ this ->httpMethods as $ httpVerb ) {
70
76
if (strpos ($ methodName , strtolower ($ httpVerb )) === 0 ) {
71
77
// Remove HTTP verb prefix.
72
- $ methodInUri = lcfirst ( substr ( $ methodName , strlen ( $ httpVerb )) );
78
+ $ methodInUri = $ this -> getUriByMethod ( $ httpVerb , $ methodName );
73
79
74
80
// Check if it is the default method.
75
81
if ($ methodInUri === $ defaultMethod ) {
@@ -173,7 +179,27 @@ private function getUriByClass(string $classname): string
173
179
$ classPath .= lcfirst ($ part ) . '/ ' ;
174
180
}
175
181
176
- return rtrim ($ classPath , '/ ' );
182
+ $ classUri = rtrim ($ classPath , '/ ' );
183
+
184
+ if ($ this ->translateURIDashes ) {
185
+ $ classUri = str_replace ('_ ' , '- ' , $ classUri );
186
+ }
187
+
188
+ return $ classUri ;
189
+ }
190
+
191
+ /**
192
+ * @return string URI path part from the method
193
+ */
194
+ private function getUriByMethod (string $ httpVerb , string $ methodName ): string
195
+ {
196
+ $ methodUri = lcfirst (substr ($ methodName , strlen ($ httpVerb )));
197
+
198
+ if ($ this ->translateURIDashes ) {
199
+ $ methodUri = str_replace ('_ ' , '- ' , $ methodUri );
200
+ }
201
+
202
+ return $ methodUri ;
177
203
}
178
204
179
205
/**
0 commit comments