Skip to content

Commit 9e435ca

Browse files
committed
TranslateURIDashes fix. See #1564
1 parent 4576ad5 commit 9e435ca

File tree

2 files changed

+61
-14
lines changed

2 files changed

+61
-14
lines changed

system/Router/Router.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ public function __construct(RouteCollectionInterface $routes)
160160
* @param string $uri
161161
*
162162
* @return mixed
163+
* @throws \CodeIgniter\Router\RedirectException
163164
*/
164165
public function handle(string $uri = null)
165166
{
@@ -192,7 +193,7 @@ public function handle(string $uri = null)
192193

193194
$this->autoRoute($uri);
194195

195-
return $this->controller;
196+
return $this->controllerName();
196197
}
197198

198199
//--------------------------------------------------------------------
@@ -216,7 +217,9 @@ public function getFilter()
216217
*/
217218
public function controllerName()
218219
{
219-
return $this->controller;
220+
return $this->translateURIDashes
221+
? str_replace('-', '_', $this->controller)
222+
: $this->controller;
220223
}
221224

222225
//--------------------------------------------------------------------
@@ -229,7 +232,9 @@ public function controllerName()
229232
*/
230233
public function methodName(): string
231234
{
232-
return $this->method;
235+
return $this->translateURIDashes
236+
? str_replace('-', '_', $this->method)
237+
: $this->method;
233238
}
234239

235240
//--------------------------------------------------------------------
@@ -529,7 +534,7 @@ public function autoRoute(string $uri)
529534
}
530535

531536
// Load the file so that it's available for CodeIgniter.
532-
$file = APPPATH . 'Controllers/' . $this->directory . $this->controller . '.php';
537+
$file = APPPATH . 'Controllers/' . $this->directory . $this->controllerName() . '.php';
533538
if (is_file($file))
534539
{
535540
include_once $file;
@@ -539,7 +544,7 @@ public function autoRoute(string $uri)
539544
// We have to check for a length over 1, since by default it will be '\'
540545
if (strpos($this->controller, '\\') === false && strlen($this->collection->getDefaultNamespace()) > 1)
541546
{
542-
$this->controller = str_replace('/', '\\', $this->collection->getDefaultNamespace() . $this->directory . $this->controller);
547+
$this->controller = str_replace('/', '\\', $this->collection->getDefaultNamespace() . $this->directory . $this->controllerName());
543548
}
544549
}
545550

@@ -620,15 +625,6 @@ protected function setRequest(array $segments = [])
620625
return;
621626
}
622627

623-
if ($this->translateURIDashes === true)
624-
{
625-
$segments[0] = str_replace('-', '_', $segments[0]);
626-
if (isset($segments[1]))
627-
{
628-
$segments[1] = str_replace('-', '_', $segments[1]);
629-
}
630-
}
631-
632628
list($controller, $method) = array_pad(explode('::', $segments[0]), 2, null);
633629

634630
$this->controller = $controller;

tests/system/Router/RouterTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ protected function setUp()
2828

2929
$routes = [
3030
'users' => 'Users::index',
31+
'user-setting/show-list' => 'User_setting::show_list',
32+
'user-setting/(:segment)' => 'User_setting::detail/$1',
3133
'posts' => 'Blog::posts',
3234
'pages' => 'App\Pages::list_all',
3335
'posts/(:num)' => 'Blog::show/$1',
@@ -439,4 +441,53 @@ public function testRouteOrder()
439441
$this->assertEquals('auth_post', $router->methodName());
440442
}
441443

444+
/**
445+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1564
446+
*/
447+
public function testTranslateURIDashes()
448+
{
449+
$router = new Router($this->collection);
450+
451+
$router->handle('user-setting/show-list');
452+
453+
$router->setTranslateURIDashes(true);
454+
455+
$this->assertEquals('\User_setting', $router->controllerName());
456+
$this->assertEquals('show_list', $router->methodName());
457+
}
458+
459+
//--------------------------------------------------------------------
460+
461+
/**
462+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1564
463+
*/
464+
public function testTranslateURIDashesForParams()
465+
{
466+
$router = new Router($this->collection);
467+
$router->setTranslateURIDashes(true);
468+
469+
$router->handle('user-setting/2018-12-02');
470+
471+
$this->assertEquals('\User_setting', $router->controllerName());
472+
$this->assertEquals('detail', $router->methodName());
473+
$this->assertEquals(['2018-12-02'], $router->params());
474+
}
475+
476+
//--------------------------------------------------------------------
477+
478+
/**
479+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1564
480+
*/
481+
public function testTranslateURIDashesForAutoRoute()
482+
{
483+
$router = new Router($this->collection);
484+
$router->setTranslateURIDashes(true);
485+
486+
$router->autoRoute('admin-user/show-list');
487+
488+
$this->assertEquals('Admin_user', $router->controllerName());
489+
$this->assertEquals('show_list', $router->methodName());
490+
}
491+
492+
//--------------------------------------------------------------------
442493
}

0 commit comments

Comments
 (0)