简体   繁体   中英

Print all routes from ZF2 modules

I'm trying to print all routes from my modules on "some page" with var_dump() or whatever debug function.

I have found lots of posts and samples but I can't get them printed and most examples fail in my code.

So far I think this is the best way to do so but where to use this code ?

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

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

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

You may print all routes from in your controller's method. Look at the following example

module/Application/src/Application/Controller/IndexController.php

<?php 
namespace Application\Controller;

use Zend\View\Model\ViewModel;
use Zend\Mvc\Controller\AbstractActionController;

class IndexController extends AbstractActionController
{
    /**
     * @var array
     */
    protected $routes;

    /**
     * @param array $routes
     */
    public function __construct(array $routes)
    {
        // Here is the catch
        $this->routes = $routes;
    }

    public function indexAction()
    {
        // Thus you may print all routes
        $routes = $this->routes;

        echo '<pre>';
        print_r($routes);
        echo '</pre>';
        exit;

        return new ViewModel();
    }
}

As we passed an array of routes to the constructor of IndexController . We need to make an factory of this controller. A factory is a class that creates instances of other classes.

module/Application/src/Application/Controller/IndexControllerFactory.php

<?php 
namespace Application\Controller;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class IndexControllerFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $serviceManager = $serviceLocator->getServiceLocator();
        $config = $serviceManager->get('Config');
        $routes = $config['router'];

        return new IndexController($routes);
    }
}

A invokable class can not be constructed with arguments. Our controller would not work as invokables because we know we already passed an argument to its constructor. So we need to configure that in factories key under controllers key of our module.config.php

module/Application/config/module.config.php

'controllers' => [
    'invokables' => [
        // This would not work any more as we created a factory of it
        // 'Application\Controller\Index' => 'Application\Controller\IndexController',
    ],

    // We should do it thus  
    'factories' => [
        'Application\Controller\Index' => 'Application\Controller\IndexControllerFactory',
    ],
],

This answer has been edited for good practice as @av3 suggested!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM