2013-06-05 75 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, 
),