diff --git a/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php b/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php index 6b865226d626..a077633f7011 100644 --- a/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php +++ b/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php @@ -11,6 +11,7 @@ namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved; +use Config\Routing; use ReflectionClass; use ReflectionMethod; @@ -31,6 +32,8 @@ final class ControllerMethodReader */ private array $httpMethods; + private bool $translateURIDashes; + /** * @param string $namespace the default namespace */ @@ -38,6 +41,9 @@ public function __construct(string $namespace, array $httpMethods) { $this->namespace = $namespace; $this->httpMethods = $httpMethods; + + $config = config(Routing::class); + $this->translateURIDashes = $config->translateURIDashes; } /** @@ -67,7 +73,7 @@ public function read(string $class, string $defaultController = 'Home', string $ foreach ($this->httpMethods as $httpVerb) { if (strpos($methodName, $httpVerb) === 0) { // Remove HTTP verb prefix. - $methodInUri = lcfirst(substr($methodName, strlen($httpVerb))); + $methodInUri = $this->getUriByMethod($httpVerb, $methodName); // Check if it is the default method. if ($methodInUri === $defaultMethod) { @@ -171,7 +177,27 @@ private function getUriByClass(string $classname): string $classPath .= lcfirst($part) . '/'; } - return rtrim($classPath, '/'); + $classUri = rtrim($classPath, '/'); + + if ($this->translateURIDashes) { + $classUri = str_replace('_', '-', $classUri); + } + + return $classUri; + } + + /** + * @return string URI path part from the method + */ + private function getUriByMethod(string $httpVerb, string $methodName): string + { + $methodUri = lcfirst(substr($methodName, strlen($httpVerb))); + + if ($this->translateURIDashes) { + $methodUri = str_replace('_', '-', $methodUri); + } + + return $methodUri; } /** diff --git a/tests/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReaderTest.php b/tests/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReaderTest.php index 133d0feff781..6d4a0a5917cc 100644 --- a/tests/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReaderTest.php +++ b/tests/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReaderTest.php @@ -11,8 +11,11 @@ namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved; +use CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Dash_folder\Dash_controller; use CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Home; +use CodeIgniter\Config\Factories; use CodeIgniter\Test\CIUnitTestCase; +use Config\Routing; use Tests\Support\Controllers\Newautorouting; use Tests\Support\Controllers\Remap; @@ -66,6 +69,43 @@ public function testRead(): void $this->assertSame($expected, $routes); } + public function testReadTranslateURIDashes(): void + { + $config = config(Routing::class); + $config->translateURIDashes = true; + Factories::injectMock('config', Routing::class, $config); + + $reader = $this->createControllerMethodReader( + 'CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers' + ); + + $routes = $reader->read(Dash_controller::class); + + $expected = [ + 0 => [ + 'method' => 'get', + 'route' => 'dash-folder/dash-controller/somemethod', + 'route_params' => '[/..]', + 'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Dash_folder\Dash_controller::getSomemethod', + 'params' => [ + 'p1' => false, + ], + ], + [ + 'method' => 'get', + 'route' => 'dash-folder/dash-controller/dash-method', + 'route_params' => '/..[/..]', + 'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Dash_folder\Dash_controller::getDash_method', + 'params' => [ + 'p1' => true, + 'p2' => false, + ], + ], + ]; + + $this->assertSame($expected, $routes); + } + public function testReadDefaultController(): void { $reader = $this->createControllerMethodReader( diff --git a/tests/system/Commands/Utilities/Routes/AutoRouterImproved/Controllers/Dash_folder/Dash_controller.php b/tests/system/Commands/Utilities/Routes/AutoRouterImproved/Controllers/Dash_folder/Dash_controller.php new file mode 100644 index 000000000000..486c8529d082 --- /dev/null +++ b/tests/system/Commands/Utilities/Routes/AutoRouterImproved/Controllers/Dash_folder/Dash_controller.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Dash_folder; + +use CodeIgniter\Controller; + +class Dash_controller extends Controller +{ + public function getSomemethod($p1 = ''): void + { + } + + public function getDash_method($p1, $p2 = ''): void + { + } +}