0

It is known that ZF2 has no Router. Every route is a router:

In the new routing system we don’t have a router as such, as every route can match and assemble URIs by themselves, which makes them routers, too.

And ZF2 now force you to put your routes in every module's config:

The mapping of a URL to a particular action is done using routes that are defined in the module’s module.config.php file. Open your config/module.config.php file, and modify it to add to the “routes” and “controller” parameters.

For me it's not convenient to have one solid thing be scattered over application directories. Is it possible somehow to have all my routes in one place (routes.php file, for example)? Like this, for example:

routes.php
return array (
    'module_1' => array( ... its route/routes ...),
    'module_2' => array( ... its route/routes ...), etc.
);

How to achieve it in ZF2?

Just to link with my question: ZF2 Routing as in ZF1

Community
  • 1
  • 1
Green
  • 28,742
  • 61
  • 158
  • 247

1 Answers1

3

In ZF2 modules tend to provide configuration, this configuration is merged into a single application configuration. So I don't see any reasons why the router configuration couldn't be stored in it's own configuration file.

By default ZF2 will include configuration files that reside in config/autoload that match either *global.php or *local.php - so having a file named routes.global.php should work.

This default behavior is defined in /config/application.config.php:

'config_glob_paths'    => array(
    'config/autoload/{,*.}{global,local}.php',
),
DrBeza
  • 2,241
  • 1
  • 16
  • 18
  • Thank you. But how router (which is absent in ZF2) can understand that `routes.global.php` is the file with my application-wide routes? Whom and how may I inform? `router => routes.global.php` ? Or some other way? I can't understand – Green Nov 12 '12 at 21:25
  • In module based configuration you place your routes under the router array key - so to keep it simple your file should start with return array('router' => ...) etc. The key point to understand, however you provide configuration it is all merged together. So having an auto loaded file will still achieve the same results. – DrBeza Nov 12 '12 at 21:39