2014-01-19 138 views
1

假設我有一個相當標準的路線定義,比方說,用戶的東西:多個控制器

'router' => array(
    'routes' => array(
     'user' => array(
      'type' => 'Zend\Mvc\Router\Http\Segment', 
      'options' => array(
       'route' => '/user[/:action]', 
       'constraints' => array('action' => '[a-zA-Z0-9_-]*'), 
       'defaults' => array(
        'controller' => 'usercontroller', 
        'action'  => 'index', 
       ), 
      ), 
     ), 
    ), 
), 

現在,假設我想使用的「用戶」行動的不同羣體不同的控制器。比如,說一兩個動作('特殊'和'超級')應該轉到'特殊控制器'。我該如何配置?我嘗試過使用'child_routes'無濟於事,我嘗試在'routes'數組中有多個'user'條目,但是沒有喜悅。

回答

3

您可以創建一個路由類型文本,與孩子路由類型細分:

'router' => array(
    'routes' => array(
     'user' => array(
      'type' => 'Literal', 
      'options' => array(
       'route' => '/user', 
       'defaults' => array(
        '__NAMESPACE__' => 'MyModule\Controller', 
        'controller' => 'User', 
        'action'  => 'index', 
       ), 
      ), 
      'may_terminate' => true, 
      'child_routes' => array(
       'default' => array(
        'type' => 'Segment', 
        'options' => array(
         'route' => '/[:controller[/:action[/:id]]]', 
         'constraints' => array(
          'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
          'action'  => '[a-zA-Z][a-zA-Z0-9_-]*', 
          'id'   => '[0-9]+', 
         ), 
         'defaults' => array(
         ), 
        ), 
       ), 
      ), 
     ), 
    ), 
), 

,或者如果你願意,直接申報控制器名稱作爲路由參數路由類型區段:

'router' => array(
    'routes' => array(
     'user' => array(
      'type' => 'Segment', 
      'options' => array(
       'route' => '/[:controller[/:action[/:id]]]', 
       'defaults' => array(
        '__NAMESPACE__' => 'MyModule\Controller', 
        'controller' => 'User', 
        'action'  => 'index', 
       ), 
      ), 
     ), 
    ), 
), 

我寧願避免路由的模塊控制器之間衝突的第一種方式

你也可以,如果你有倍數控制器溜溜的特定部分做烏爾應用程序(這裏:用戶)您的應用程序,並倍數部分控制在同一模塊=>組織你的控制器在不同的命名空間像:

namespace MyModule\Controller\Users; 
1

對於那些像我一樣,誰仍在學習ZF2和希望通過NonoHERON一個簡單的版本偉大的答案,下面的代碼:

 'user' => array(
      'type' => 'Zend\Mvc\Router\Http\Literal', 
      'options' => array(
       'route' => '/user', 
       'defaults' => array(
        'controller' => 'usercontroller', 
        'action'  => 'index', 
       ), 
      ), 
      'may_terminate' => true, 
      'child_routes' => array(
       'default' => array(
        'type' => 'Segment', 
        'options' => array(
         'route' => '/[:action]', 
         'constraints' => array('action' => '[a-zA-Z0-9_-]*'), 
         'defaults' => array(
          'controller' => 'usercontroller', 
          'action'  => 'index', 
         ), 
        ), 
       ), 
       'special' => array(
        'type' => 'Literal', 
        'options' => array(
         'route' => '/details', 
         'defaults' => array(
          'controller' => 'specialcontroller', 
          'action'  => 'special', 
         ), 
        ), 
       ), 
      ), 
     ), 

爲了得到「$這個 - > URL」在您的視圖代碼工作,你現在需要來進行設置略有不同。在默認分組的動作,就變成:

$this->url('user/default', array('action'=>'whatever')) 

雖然對於特殊的,嚴格的,它應該是:

$this->url('user/special') 

但是,如果你正在做的是改變其控制器的動作變對,然後

$this->url('user/default', array('action'=>'special')) 

也應努力產生正確的鏈接。

還有一個非常有用的zf2備忘單:http://zf2cheatsheet.com/