2013-12-17 125 views
0

我以如下的路線我module.config.phpZend框架2通配符路線

'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', 
       'module' => 'Admin', 
       'controller' => 'Index', 
       'action' => 'index', 
      ), 
     ), 
     'may_terminate' => true, 
     'child_routes' => array(
      'wildcard' => array(
       'type' => 'Wildcard', 
      ) 
     ), 
     'priority' => 1000 
    ), 
), 

在路線的終點的原因[/]是這樣一個問題:Zend Framework 2 Segment Route matching 'test' but not 'test/'

我想這條路線像ZF1一樣。我想通過$_GET parameters(如/id/1/test/2/)。

它這條路線實際上是匹配/admin/customer/edit//id/20但不匹配/admin/customer/edit/id/20

任何想法的問題?

回答

0

您可以添加路由參數ID:

'route' => '/admin[/:controller[/:action]][/:id][/]', 
    'constraints' => array(
     'controller' => '[a-zA-Z][a-zA-Z0-9_-]+', 
     'action' => '[a-zA-Z][a-zA-Z0-9_-]+', 
     'id' => '[0-9]+', 
), 

這條路配襯admin/customer/edit/20/,這樣你就可以在控制器獲得ID:

$this->params()->fromRoute('id'); 

如果你想擁有admin/customer/edit/id/20/,請嘗試:

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

這是一個方法,但不是我想要的東西。 – rafaame

+0

我不知道我是否在跟蹤這個問題。我想你想添加一個參數到你的路線。你需要什麼路線? –

0

如果我的理解正確,你試圖得到多tiple參數來自URL,對嗎?

例如

傳統GET:www.domain.com/ctrl/action?key1=abc &鍵2 = EFG & ...... & keyN = XYZ

ZF2路線:www.domain.com/ctrl/action/key1 /abc/key2/efg/.../keyN/xyz

如果是這樣,這是做的一種方式:

  'adminPage' => array(
      'type' => 'regex', 
      'options' => array(
       'regex' => '/admin/customer/edit[/](?<keyValuePairs>.*)', 
       'defaults' => array(
        'controller' => 'YourProject\Controller\YourController', 
        'action' => 'yourAction', 
       ), 
       'spec' => '/admin/customer/edit/%keyValuePairs%', 
      ) 

有了這個, '管理/用戶/編輯/' 之後的每一個字符將存儲在'keyValuePairs'參數中。在Controller :: yourAction中,獲取'keyValuePairs'參數,然後將字符串拆分回有意義的鍵值數據結構。

0

您正處在正確的軌道上!使用「Wilcard」作爲管理路線的一種子路線。

有兩種選擇:key_value_delimiterparam_delimiter。 兩者的默認值都是'/',它等於路由參數的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', 
       'module' => 'Admin', 
       'controller' => 'Index', 
       'action' => 'index', 
      ), 
      'may_terminate' => true, 
      'child_routes' => array(
       'wildcard' => array(
        'type' => 'Wildcard', 
        'options' => array(
         'key_value_delimiter' => '/', 
         'param_delimiter' => '/' 
        ) 
       ) 
      ) 
     ) 
    ) 
) 

如果要解決與URL視圖助手的幫助下通配符的路線,你可以用它這樣的:

$this->url('admin/wildcard', array('id' => 1234, 'foo' => 'bar'));