2013-02-27 30 views
1

我从https://github.com/zendframework/ZendSkeletonApplication下载了骷髅,它工作正常。一切都很清楚。如何路由到许多模块?

但是当我添加另一个模块会发生什么?我复制模块从骨架,我改了个名字,然后加入我的新模块application.config.php:

return array(
    'modules' => array(
     'Application', 
     'Api', 
    ), 
[...] 

,并更改module.config.php路线:

return array(
    'router' => array(
     'routes' => array(
      'api' => array(
       'type' => 'Literal', 
       'options' => array(
        'route' => 'api/', 
        'defaults' => array(
         '__NAMESPACE__' => 'Api\Controller', 
         'controller' => 'Api', 
         'action'  => 'api', 
        ), 
       ), 
       'may_terminate' => true, 
       'child_routes' => array(
        'default' => array(
         'type' => 'Segment', 
         'options' => array(
          'route' => '/api[/:action]]', 
          'constraints' => array(
           'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
           'action'  => '[a-zA-Z][a-zA-Z0-9_-]*', 
          ), 
          'defaults' => array(
          ), 
         ), 
        ), 
       ), 
      ), 
     ), 
    ), 
    'service_manager' => array(
     'factories' => array(
      'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory', 
     ), 
    ), 
    'translator' => array(
     'locale' => 'en_US', 
     'translation_file_patterns' => array(
      array(
       'type'  => 'gettext', 
       'base_dir' => __DIR__ . '/../language', 
       'pattern' => '%s.mo', 
      ), 
     ), 
    ), 
    'controllers' => array(
     'invokables' => array(
      'Application\Controller\Index' => 'Application\Controller\IndexController', 
      'Application\Controller\Auth' => 'Application\Controller\AuthController' 
     ), 
    ), 
    'view_manager' => array(
     'display_not_found_reason' => true, 
     'display_exceptions'  => true, 
     'doctype'     => 'HTML5', 
     'not_found_template'  => 'error/404', 
     'exception_template'  => 'error/index', 
     'template_map' => array(
      'layout/layout'   => __DIR__ . '/../view/layout/layout.phtml', 
      'application/index/index' => __DIR__ . '/../view/application/index/index.phtml', 
      'error/404'    => __DIR__ . '/../view/error/404.phtml', 
      'error/index'    => __DIR__ . '/../view/error/index.phtml', 
     ), 
     'template_path_stack' => array(
      __DIR__ . '/../view', 
     ), 
    ), 
); 

但现在我明白了从api甚至从骨架位置的布局。怎么做?

+0

模块是_NOT_独立的容器。他们是交叉关注的问题,而不是你的应用程序的子部分。 – Ocramius 2013-02-27 20:09:47

回答

2

你所有的module.config.php文件将被合并在一起形成一个大的配置数组。

究竟发生了什么,取决于您在module.config.phproutes阵列中使用的名称。

如果您重复使用与已经提供的名称相同的名称(例如,您的Application模块中已经有一条名为api的路线),则旧条目将为overriden

哪条路径将被用于重定向到正确的控制器,动作和参数也取决于匹配。所有定义的路由(通过所有模块)将按顺序进行检查。第一个匹配你当前的URL将被执行。

在你的情况下,似乎没有任何含糊之处,因为你把所有的东西都改名为api(路由名称+路由前缀),所以它可以很好地工作。当然你也可能想在其他模块中定义完全不同的较短路径,那么你必须确保它们不会获取应该被后面的模块匹配的URL。