通过RouteProviderInterface进行ZF2路由配置

时间:2013-10-02 09:31:49

标签: php zend-framework2

我一直在尝试将Module.php配置为使用the Module Manager Listeners进行配置(即Zend\ModuleManager\Feature\*下可用的接口)。具体来说,我希望能够配置主module.config.php的模块的路由。我还没有找到任何实际的例子。

我发现,如果我已正确阅读文档,方法getRouteConfig()应该在我的路由中合并到getConfig()提供的数组中?

Module.php

class Module implements Feature\RouteProviderInterface
{
//...
  public function getRouteConfig()
  {
    return include __DIR__ . '/config/route.config.php';
  }
  //...
}

/config/route.config.php

return array(

  'route_manager' => array(
    'router' => array (
      'routes' => array(
         //.. routes that were working correctly when added to module.config.php
      ),
     ),
   ),
 );

我可以看到通过getRouteConfig()返回的数组,所以我知道正确调用了该方法。

也许我误解了上述界面的目的,或者我没有提供正确的“密钥”(route_manager),因为我正在为我的路线获得404。

任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:3)

我还没有按照您提到的方式执行此操作,但route_manager方法中不需要键getRouteConfig()

这是因为所有get{$specificManager}Config() - 方法都是直接从各自的Manager-Classes调用的。因此,不需要初始密钥。使用其他术语时,使用getRouteConfig()时,您已经在route_manager的范围内。与使用getServiceConfig()时相同,您已经在service_manager的范围内。但是getConfig()在应用程序范围内,因此访问应用程序部分的配置,您需要特定地解决这个问题。

需要注意的一点是:getConfig()的配置可以缓存以提高性能,而所有其他get{$specificManager}Config()方法则不会。特别是在RouteConfiguration的情况下,我强烈建议您使用RouteConfig的getConfig() - 方法。

如果你真的需要分开配置,那么我会建议@Hendriq为你显示的方式。

答案 1 :(得分:2)

我有它工作,但我只使用getConfig()。我在array_merge中使用getConfig()做了什么。

public function getConfig()
{
    return array_merge(
        require_once 'path_to_config/module.config.php',
        require_once 'path_to_config/routes.config.php'
    );
}

我的router.config.php看起来像:

return [
    'router' => [
        'routes' => [
            // routes
        ]
    ]
];

这样我也得到了一些其他配置文件分离(ACL)。

编辑 感谢文章Understanding ZF2-Configuration,我有了一个主意。我认为你的阵列不是

return array(
    'route_manager' => array(
        'router' => array (
            'routes' => array(
                //.. routes that were working correctly when added to module.config.php
            )
        )
    )
);

宁愿

return array(
    'router' => array (
        'routes' => array(
            //.. routes that were working correctly when added to module.config.php
        ),
    ),
);

答案 2 :(得分:2)

getRouteConfig与其他提供商类似,因此您可以创建一些自定义路由。我想你想要做的是通过亨德里克的方法最合适的。

可以在http://zf2cheatsheet.com/

找到getRouteConfig的示例
public function getRouteConfig()
{
    return array(
        'factories' => array(
            'pageRoute' => function ($routePluginManager) {
                $locator = $routePluginManager->getServiceLocator();
                $params = array('defaults' => array('controller' => 'routeTest','action' => 'page','id' => 'pages'));
                $route = Route\PageRoute::factory($params);
                $route->setServiceManager($locator);
                return $route;
            },
        ),
    );
}

在我们的Module\Route命名空间中,我们创建实现PageRoute的类Zend\Mvc\Http\RouteInterface,在我们的示例中为Zend\ServiceManager\ServiceManagerAwareInterface。现在只需实现接口的功能......在示例中,他使用Doctrine从数据库加载页面。

最后,我们可以将新的自定义路线添加到module.config.php,以便可以使用:

'page' => array(
    'type' => 'pageRoute',
),

正如您在最后一步中所看到的,我们回到Hendriq的解决方案,因为预期用途不是将路由加载到路由器中,而是创建自定义路由。

希望这有帮助

相关问题