ZF2访问路由器配置中的配置

时间:2015-07-07 07:09:27

标签: zend-framework zend-framework2

我按主机名路由,我想通过zf2

中config / autoload中的local.php配置文件获取我的域名

我知道如何在控制器中获得配置,  但我不知道如何在我的路由器配置文件中获取它

我在我的代码中评论我想要的内容

  'router' => array(
             'routes' => array(

                 'advertise' => array(
                     'type'    => 'Hostname',
                     'options' => array(
                         'route' =>  'www.myhome.com', // here i want to get my domain by config 
                     'defaults'  => array(

                            'controller'    => 'Advertise\Controller\Advertise',
                            'action'        => 'index',
                        ),
                    ), 
                    .............

1 个答案:

答案 0 :(得分:1)

您可以使用“路由器”服务的API(Zend\Mvc\Router\Http\TreeRouteStack)和add a route dynamically的实例。

如何将路由附加到路由堆栈取决于您,您可以使用自己的路由从config扩展默认路由器工厂Zend\Mvc\Service\RouterFactory

use MyModule\Mvc\Service;

use Zend\Mvc\Service\RouterFactory;
use Zend\ServiceManager\ServiceLocatorInterface;

class MyRouterFactory extends RouterFactory
{
    public function createService(ServiceLocatorInterface $serviceLocator, $cName = null, $rName = null)
    {
        $serviceManager = $serviceLocator->getServiceLocator();

        $router = parent::createService($serviceLocator, $cName, $rName);
        $config = $serviceManager->get('config');

        $router->addRoute('advertise', [
            'type'    => 'hostname',
            'options' => [
                'route'    => $config['some_other_config_key'],
                'defaults' => [
                    'controller' => 'Advertise\Controller\Advertise',
                    'action'     => 'index'
                ]
            ],
            'priority' => 123
        ]);

        return $router;
    }
}

请务必在Router中使用module.config.php名称注册,以替换默认实施。

'service_manager' => [
    'factories' => [
        'Router' => 'MyModule\Mvc\Service\MyCustomRouterFactory',
    ],
],

这种方法的好处是路由器结构全部保存在一个地方;当你用工厂类添加它时也是如此 如果需要,您可以访问其他服务。

或者,您可以通过事件添加它,虽然通过事件管理器触发,但这种方法可能会占用大量资源。

namespace MyModule;

use Zend\ModuleManager\InitProviderInterface;
use Zend\ModuleManager\ModuleManagerInterface;
use Zend\Mvc\Application;
use Zend\Mvc\MvcEvent;

class Module implements InitProviderInterface
{
    // init is called when the module is initilised, we can use this to add a listener to the
    // 'bootstrap' event
    public function init(ModuleManagerInterface $manager)
    {
        $eventManager = $manager->getEventManager()->getSharedManager();

        $eventManager->attach(Application::class, MvcEvent::EVENT_BOOTSTRAP, [$this, 'addRoutes']);
    }

    public function addRoutes(MvcEvent $event)
    {
        $serviceManager = $event->getApplication()->getServiceManager();

        $router = $serviceManager->get('Router');
        $config = $serviceManager->get('Config');

        $router->addRoute('advertise', [
            'type'    => 'hostname',
            'options' => [
                'route'    => $config['some_other_config_key'],
                'defaults' => [
                    'controller' => 'Advertise\Controller\Advertise',
                    'action'     => 'index'
                ]
            ],
            'priority' => 123
        ]);
    }
}
相关问题