简体   繁体   English

从ZF2模块打印所有路线

[英]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. 我正在尝试使用var_dump()或任何调试功能在“某些页面”上打印模块中的所有路由。

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: 如果要仅出于调试目的查看所有路由,则可以在路由器对象上使用var_dump或类似的路由:

// $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 模块/应用程序/源代码/应用程序/控制器/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 . 当我们将路由数组传递给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 模块/应用程序/源代码/应用程序/控制器/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. 我们的控制器无法作为invokables因为我们知道我们已经将参数传递给了它的构造函数。 So we need to configure that in factories key under controllers key of our module.config.php 因此,我们需要配置在factories键下controllers我们的关键module.config.php

module/Application/config/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! 已按照@ av3的建议对此答案进行了良好实践的编辑!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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