Hello. I have 2 modules: user and application. They have same IndexController.
I wrote common route in application.config
router' => [
'routes' => [
'common' => [
'type' => Segment::class,
'options' => [
'route' => '[/:module/:controller/:action[/:id]]',
'constraints' => [
'module' => '[a-zA-Z][a-zA-Z0-9_-]*',
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'action' => 'index',
],
],
],
],
],
Router match this route. But nowadays
When uri is domain.com/application/index/index
then It was calling IndexController in User module. But I expect for IndexController in Application module
How to reach that IndexController will be call in different modules with this route config?
I setted alias for 2 IndexControllers as index. As i understand just last alias used.
The question is different: Where did you get the description that there is a parameter “module”?
Thanks for answer! I understand why just have called IndexController in user module - because its alias was last determined. But how possible call needed controller in needed module with common route like that
‘route’ => ‘[/:module/:controller/:action[/:id]]’,
parameter “module” does not use for some logic. Its a example how i would like
So:
application/index/index - would be call IndexController in Application module
user/index/index - would be call IndexController in User module
Or i should write specific route for each controller?
laminas-mvc does not use a parameter module
, only controller
and action.
So I wanted to know where you got this information with the parameter module
.
The parameter controller
points to a class name or to a registered alias of a class name, this contains the namespace and so also the module name. Example: Album\Controller\IndexController::class
– Album
is here the module.
My target was generate common route like: module / controller / action
I have not find right solution for that in docs or something yet. That’s why I wrote a little function in Module.php in each module that implements that.
#here it is
public function loadCommonRoutes(array $config): array
{
$routes = $config['router']['routes'];
$templateRoute = strtolower(__NAMESPACE__).'-common-';
$controllersFiles = array_diff(scandir(__DIR__ . '/../src/Controller'), array('.', '..'));
foreach ($controllersFiles as $ControllerFileName) {
$controllerName = strtolower(explode('Controller', $ControllerFileName)[0]);
$routeName = $templateRoute.$controllerName;
$routes[$routeName] = [
'type' => Segment::class,
'options' => [
'route' => '/'.strtolower(__NAMESPACE__).'/'.$controllerName.'[/:action[/:id]][/]',
'constraints' => [
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'action' => 'index',
'controller' => __NAMESPACE__.'\\Controller\\'.ucfirst($controllerName).'Controller',
],
],
];
}
$config['router']['routes'] = $routes;
return $config;
}
This function does not exist, so you will not find anything about it.
As I have already described above, a different approach is used in laminas-mvc.
Personally, I don’t want to see anything about modules or controllers in the URL. If the controller changes or is dropped because a request handler or something else is now used, then the URL also changes.
But good if you have found a solution for yourself and share it here. 