0

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?

webprogrammer
  • 2,393
  • 3
  • 21
  • 27

1 Answers1

2

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.

rkeet
  • 3,406
  • 2
  • 23
  • 49
  • I meant something else. I was talking about plain routers, not about array tree, but your answer is helpful because it seems I will use array tree instead plain routers and planning create separated files like you shows me on the picture. – webprogrammer Apr 19 '19 at 16:23
  • 1
    Well, if you look in [here in zend-router](https://github.com/zendframework/zend-router/blob/5ce5ff9630c4467e3eaf7cf06d78dbb2296a41b4/src/ConfigProvider.php#L41)'s ConfigProvider class, you'll find the available routers.Looking around in the module, there's also a few interfaces, so technically, you could create your own. You could also replace theirs with your own, or another which implements those interfaces needed. Up to you really. Personnally, find it best to stick with what's provided by a framework. If the framework doesn't match the needs, maybe time for another framework ;-) – rkeet Apr 19 '19 at 21:59
  • I have forgotten to upvote your answer. Also, I keep my routers within separate file like you suggested. So, I can mark your answer as accepted. Thank you! – webprogrammer Jan 23 '20 at 11:10