2013-06-05 44 views
0

这样的路由我实际上有2个模块(应用程序和管理员)在我的ZF2应用程序中,我想要一个类似于ZF1的URL路由。目前,我有以下途径:ZF2 - 试图创建一个像ZF1

'router' => array 
(

    'routes' => array 
    (

     'admin' => array 
     (

      'type' => 'Segment', 
      'options' => array 
      (

       'route' => 'admin/[:controller[/:action]]', 

       'constraints' => array 
       (
        'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
        'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 
       ), 

       'defaults' => array 
       (
        '__NAMESPACE__' => 'Admin\Controller', 
        'controller' => 'Index', 
        'action' => 'index', 
       ), 

      ), 

      'may_terminate' => true, 
      'child_routes' => array 
      (

       'wildcard' => array 
       (

        'type' => 'Wildcard' 

       ) 

      ) 

     ), 

    ), 

), 

所以它将匹配 “/ admin” 的, “/管理/控制”, “/管理/控制/动作”,而不是 “/控制器/动作”。

现在我需要一个路由到应用程序模块。问题是,如果我只是使用类似路由的模块应用程序,这条新路线将匹配“/ admin/controller”作为controller =“admin”和action =“controller”。

我也试过在应用以下的正则表达式路线:

'application' => array 
     (

      'type' => 'Regex', 
      'options' => array 
      (

       'regex' => '/(?<controller>^(?!(admin)$)[a-zA-Z][a-zA-Z0-9_-]*)?' . 
          '(/[a-zA-Z][a-zA-Z0-9_-]*)?', 
       'spec' => '/%controller%/%action%', 

       /*'constraints' => array 
       (
        //The controller cannot be "admin" 
        'controller' => '^(?!(admin)$)[a-zA-Z][a-zA-Z0-9_-]*', 
        'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 
       ),*/ 

       'defaults' => array 
       (
        '__NAMESPACE__' => 'Application\Controller', 
        'controller' => 'Index', 
        'action' => 'index', 
       ), 

      ), 

      'may_terminate' => true, 
      'child_routes' => array 
      (

       'wildcard' => array 
       (

        'type' => 'Wildcard' 

       ) 

      ) 

     ), 

但它没有得到变量“控制器”和“行动”。

有没有人有如何解决这个问题的建议?

回答

0

请注意路径顺序:路由使用LIFO堆栈进行处理,因此匹配请求URL时数组中的最后一个出现在第一位。

这意味着您必须首先定义最通用的路由,以防止它们匹配类似但更具体的路由。

使用下面的命令应该不需要在controller参数的任何约束,因为什么开始/admin将匹配的第一

'route1' => array(
    'type' => 'Segment', 
    'options' => array(
     'route' => '/[:controller[/:action]]', 
     'defaults' => array (
      'controller' => 'controller', 
      'action' => 'index', 
     ), 
    ), 
), 
'route2' => array(
    'type' => 'Segment', 
    'options' => array(
     'route' => '/admin[/:controller[/:action]]', 
     'defaults' => array (
      'controller' => 'admin-controller', 
      'action' => 'index', 
     ), 
    ), 
), 

此外,还可以excplicitly使用priority属性总是指定路由优先级(其不应该在options阵列下定义,但是在路由的最顶端阵列中),因此以下代码与前面的示例等效:

'route2' => array(
    'type' => 'Segment', 
    'options' => array(
     'route' => '/admin[/:controller[/:action]]', 
     'defaults' => array (
      'controller' => 'admin-controller', 
      'action' => 'index', 
     ), 
    ), 
), 
'route1' => array(
    'type' => 'Segment', 
    'options' => array(
     'route' => '/[:controller[/:action]]', 
     'defaults' => array (
      'controller' => 'controller', 
      'action' => 'index', 
     ), 
    ), 
    'priority' => -1, 
),