3
votes

I have an application with 3 modules and route configs like below:

  • admin.domain.tld/[:controller[:/action]] => Admin
  • rest.domain.tld/[:controller[:/id]] => Rest
  • domain.tld/[:controller[:/action]] => Site

and set DI alias for all controller in each modules

REST Module DI Alias:

'alias' => array(
    'index' => 'Rest\Controller\IndexController',
    ...
),

Admin Module DI Alias:

'alias' => array(
    'index' => 'Admin\Controller\IndexController',
    ...
),

Site Module DI Alias:

'alias' => array(
    'index' => 'Site\Controller\IndexController',
    ...
),

As you see, some controllers has same name (eg: IndexController), but since zf2 merged config with LIFO behaviour, 'index' alias always from the last added module.

Application Config

'modules' => array('Rest','Admin', 'Site'),

when i access http://admin.domain.tld/ I expect index alias gives Admin\Controller\IndexController but since Site Module (registered last) has same alias for index it gives Site\Controller\IndexController

How to use different DI alias to match same controller name?

1
May you post some of your configs/DI?Gabriel Santos
I update the question to show you DI alias configurations for each module and application modules config registration.Komang
See the configs from Akrabat website: akrabat.com/zend-framework-2/… he generate routers for each module, not alias.Gabriel Santos
@Gabriel thanks, what i want to achieve here is using catchall routing instead of add individual route for each controller, but according to Matthew, they working on per-module solutions until then the best solutions is using specific router for eachKomang
Akrabat do a per-module work..Gabriel Santos

1 Answers

3
votes

Before the new view layer was merged into master, it was required to have aliases for controllers to behave correctly when resolving view scripts. Now this is not required anymore, it is even not recommended anymore to use aliases for controllers. The problem with aliasing is there is one alias for one FQCN, so your problem is directly related to this.

What you need to do is to remove aliases from the DI configuration and use explicit routes instead. The "magic" route [:controller[/:action]] is a bad practice and results in more problems than it can help you. So write some explicit routes and remove the aliases.