feat: module routing for Auto Routing Improved

This commit is contained in:
kenjis 2023-04-08 12:48:08 +09:00
parent 34120ad470
commit d02735ea6c
No known key found for this signature in database
GPG Key ID: BD254878922AF198
3 changed files with 47 additions and 2 deletions

View File

@ -97,4 +97,17 @@ class Routing extends BaseRouting
* Default: false
*/
public bool $prioritize = false;
/**
* Map of URI segments and namespaces. For Auto Routing (Improved).
*
* The key is the first URI segment. The value is the controller namespace.
* E.g.,
* [
* 'blog' => 'Acme\Blog\Controllers',
* ]
*
* @var array [ uri_segment => namespace ]
*/
public array $moduleRoutes = [];
}

View File

@ -13,6 +13,7 @@ namespace CodeIgniter\Router;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\Router\Exceptions\MethodNotFoundException;
use Config\Routing;
use ReflectionClass;
use ReflectionException;
@ -112,6 +113,15 @@ final class AutoRouterImproved implements AutoRouterInterface
{
$segments = explode('/', $uri);
// Check for Module Routes.
if ($routingConfig = config(Routing::class)) {
if (array_key_exists($segments[0], $routingConfig->moduleRoutes)) {
$uriSegment = array_shift($segments);
$this->namespace = rtrim($routingConfig->moduleRoutes[$uriSegment], '\\') . '\\';
}
}
// WARNING: Directories get shifted out of the segments array.
$nonDirSegments = $this->scanControllers($segments);

View File

@ -11,6 +11,7 @@
namespace CodeIgniter\Router;
use CodeIgniter\Config\Factories;
use CodeIgniter\Config\Services;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\Router\Controllers\Dash_folder\Dash_controller;
@ -39,11 +40,11 @@ final class AutoRouterImprovedTest extends CIUnitTestCase
$this->collection = new RouteCollection(Services::locator(), $moduleConfig, new Routing());
}
private function createNewAutoRouter(string $httpVerb = 'get'): AutoRouterImproved
private function createNewAutoRouter(string $httpVerb = 'get', $namespace = 'CodeIgniter\Router\Controllers'): AutoRouterImproved
{
return new AutoRouterImproved(
[],
'CodeIgniter\Router\Controllers',
$namespace,
$this->collection->getDefaultController(),
$this->collection->getDefaultMethod(),
true,
@ -66,6 +67,27 @@ final class AutoRouterImprovedTest extends CIUnitTestCase
$this->assertSame([], $params);
}
public function testAutoRouteFindsModuleDefaultControllerAndMethodGet()
{
$config = config(Routing::class);
$config->moduleRoutes = [
'test' => 'CodeIgniter\Router\Controllers',
];
Factories::injectMock('config', Routing::class, $config);
$this->collection->setDefaultController('Index');
$router = $this->createNewAutoRouter('get', 'App/Controllers');
[$directory, $controller, $method, $params]
= $router->getRoute('test');
$this->assertNull($directory);
$this->assertSame('\\' . Index::class, $controller);
$this->assertSame('getIndex', $method);
$this->assertSame([], $params);
}
public function testAutoRouteFindsDefaultControllerAndMethodPost()
{
$this->collection->setDefaultController('Index');