Hi! I'm making a multi module application and I've finally managed to get the default routing to work. So when I provide a route with :module/:controller/:action/:params everything works as expected and it finds the right controller and action.
I'm using this generic solution in routes.php:
<?php
$router = $di->getRouter();
foreach ($application->getModules() as $key => $module) {
$namespace = preg_replace('/Module$/', 'Controllers', $module["className"]);
$router->add('/'.$key.'/:params', [
'namespace' => $namespace,
'module' => $key,
'controller' => 'index',
'action' => 'index',
'params' => 1
])->setName($key);
$router->add('/'.$key.'/:controller/:params', [
'namespace' => $namespace,
'module' => $key,
'controller' => 1,
'action' => 'index',
'params' => 2
]);
$router->add('/'.$key.'/:controller/:action', [
'namespace' => $namespace,
'module' => $key,
'controller' => 1,
'action' => 2
]);
$router->add('/'.$key.'/:controller/:action/:params', [
'namespace' => $namespace,
'module' => $key,
'controller' => 1,
'action' => 2,
'params' => 3
]);
}
I've also set default namespace, module, controller and action.
$router->setDefaultNamespace("Modules\Frontend\Controllers");
$router->setDefaultModule('frontend');
$router->setDefaultController('index');
$router->setDefaultAction('index');
What happens is when I enter a route that has :module/:controller/:params (no action) it gives me an error that what I supplied in params (a word) - that action is not found on controller handler. I thought this case would fall under this route:
$router->add('/'.$key.'/:controller/:params', [
'namespace' => $namespace,
'module' => $key,
'controller' => 1,
'action' => 'index',
'params' => 2
]);
I wanted to get the index action without explicitly writing controller/index/param, and it worked before in my simple applications. So why am I not getting the default behaviour of index action that the route defines?
Also, when I add a specific route to these generic ones, the router doesn't recognize it. Is this possible with this generic code or is any other route going to be overwritten by these default ones?
Any help is appreciated! Thank you.