List all routes from the application using Zend Framework 2

How easy is it to list all the routes that we defined in our application using Zend Framework 2?

By "routes" I mean those defined in:

module/[moduleName]/config/module.config.php

under

 'router' => array( 'routes' => array( ... ) ) 

I need to list them all, but I can’t figure out how to do this easily, and neither the documentation nor the forums helped me.

+4
source share
3 answers

You can find the complete (integrated) configuration or reset the router itself. It is not possible to export all the route objects, so I must disappoint you.

To get the full configuration, enter it from the service locator:

 // $sl instanceof Zend\ServiceManager\ServiceManager $config = $sl->get('COnfig'); $routes = $config['router']['routes']; 

If you want to view all routes for debugging purposes only, you can use var_dump or the like on the router object:

 // $sl instanceof Zend\ServiceManager\ServiceManager $router = $sl->get('Router'); var_dump($router); 

To get route instances, you can build routes yourself using the route plugin manager, but I'm not sure if you want to go ...

+8
source

To get all routes, I use ZFTool

And the console command to get the route dump:

 php vendor/bin/zf.php config list | grep routes 

For Windows users (not tested):

 php vendor/bin/zf.php config list | findstr /R /C:"[routes]" 
+2
source

I needed to distinguish routes per module so that we could use the BjyAuthorize ACL setup accordingly in different situations.

Although there are several ways to do this, for example, Jurian Sluiman is shown to read all routes (modified variables):

 // $this->services instanceof Zend\ServiceManager\ServiceManager $config = $this->services->get('Config'); $routes = $config['router']['routes']; 

You can get them as follows for future differentiation into a module in a function :

 // $this->services instanceof Zend\ServiceManager\ServiceManager /** * Load the Application active modules. * @note May need to specify additional modules that may not be * loaded at this runtime. */ $moduleManager = $this->services->get('ModuleManager'); $moduleManager->loadModules(); // Retrieve array of module names. $modules = $moduleManager->getModules(); // Setup a container for all active routes. $routes = []; // Build array of all active routes. foreach ($modules as $moduleName) { $module = $moduleManager->getModule($moduleName); $routes[$moduleName] = array_keys($module->getConfig()['router']['routes']); } // Whatever you care to do with them. echo $routes; 
0
source

Source: https://habr.com/ru/post/1496677/


All Articles