zend-framework3zend-router

Zend framework 3 routers in separate file (not in config array tree)


I am using this tutorial https://docs.zendframework.com/tutorials/getting-started/overview/ for creating album module. It works for me.

Inside project there is /module/Album/config/module.config.php file which contains routes. Routers are located inside an array tree. As my previous experience shows I can have in the future dozens of routes per a project (even per a module).

On this documentation page https://docs.zendframework.com/zend-router/routing/ I found another way to add routers to the module.

// One at a time:
$route = Literal::factory([
    'route' => '/foo',
    'defaults' => [
        'controller' => 'foo-index',
        'action'     => 'index',
    ],
]);
$router->addRoute('foo', $route);

Such a way is preferred for me than storing routes in a very deep config array tree.

So, my question is: where I can put php routers code outside a config tree as I have mentioned earlier? Where in the module should be such a routers-file located at?


Solution

  • Next to module.config.php in the modules config/ folder it's common to create a routes.config.php.

    I split it further by doing something like user.routes.config.php with roles.routes.config.php. Possibly you'd like front.routes.config.php with admin.routes.config.php.

    In the end, it's up to you. For colleagues and future sanity, make sure you do it consistently though.


    As an example, the config in a project of mine for the User module:

    config files

    It's a module that handles anything directly User related, so it's all in there. Should probably split it up more, but for now, that would be unnecessary.

    You'd then load all of this config like so in your Module.php:

    use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
    use Zend\ModuleManager\Feature\ConfigProviderInterface;
    
    class Module implements ConfigProviderInterface, AutoloaderProviderInterface
    {
    
        /**
         * @return array
         */
        public function getConfig()
        {
            $config = [];
    
            $path = __DIR__
                . DIRECTORY_SEPARATOR . '..'
                . DIRECTORY_SEPARATOR . 'config'
                . DIRECTORY_SEPARATOR . '*.php';
    
            foreach (glob($path) as $filename) {
                $config = array_merge_recursive($config, include $filename);
            }
    
            return $config;
        }
    
        /**
         * @return array
         */
        public function getAutoloaderConfig()
        {
            return [
                'Zend\Loader\StandardAutoloader' => [
                    'namespaces' => [
                        __NAMESPACE__ => __DIR__ . DIRECTORY_SEPARATOR . 'src',
                    ],
                ],
            ];
        }
    }
    

    Remember, eventual implementation in your project(s) is up to you. However, work out a standard and stick to it. You'll go insane if you have different standards everywhere you go.