2012-11-06 100 views
2

我有配置我的死記硬背?如何查詢路由zf2?

'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(
        'controller' => 'index', 
        'action' => 'index', 
       ), 
      ), 
      'may_terminate' => true, 
      'child_routes' => array(
       'query' => array(
        'type' => 'Query', 
       ), 
      ), 
     ) 

ADN爲什麼我試圖去管理/控制/ some_action ID = 234234234,我有一個錯誤:

A 404 error occurred 
Page not found. 
The requested URL could not be matched by routing. 

有什麼不對我的配置?

回答

2

如果你定義的路由配置這些參數:

//... 
'defaults' => array(
        'controller' => 'index', 
        'action' => 'Index', 
        ), 
//... 

你需要確保你有你的控制器配置水平關聯到這個鍵控制器類:

'controllers' => array(
     'invokables' => array(
      'index' => 'Your\Controller\Namespace\YourController', 
      //... 
     ), 
    ), 

但是,它推薦定義更多的「結構化」鍵(想象你在不同模塊級別有不同的索引控制器......) 這可以通過添加與控制器名稱空間相對應的鍵來輕鬆實現:

'defaults' => array(
        'controller' => 'Index', 
        'action' => 'Index', 
        '__NAMESPACE__'=>'Your\Controller\Namespace' 
        ), 

//... 

//Controllers configuration 

'controllers' => array(
     'invokables' => array(
      'Your\Controller\Namespace\Index' => 'Your\Controller\Namespace\YourController', 
      //... 
     ), 
    ), 

[編輯]

嘗試用這種(結構化)配置:

'admin' => array(
      'type' => 'Segment', 
      'options' => array(
       'route' => '/admin', 
       'defaults' => array(
        'controller' => 'Index', 
        'action'  => 'index', 
       ), 
      ), 
      'may_terminate' => true, 
      'child_routes' => array(
       'default' => array(
        'type' => 'Segment', 
        'options' => array(
         'route' => '/[:controller[/:action]]', 
         'constraints' => array(
          'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
          'action'  => '[a-zA-Z][a-zA-Z0-9_-]*', 
         ), 
         'defaults' => array(
         ), 
        ), 
        'child_routes' => array(
         'query'=>array(
          'type'=>'Query', 
         ) 
        ) 
       ), 
      ), 
     ), 
+0

我有這樣的配置,如果我有管理員\控制器其工作,但管理員\控制器\行動dosn't! – Cawa

+0

您在哪個級別擁有'admin \ controller \ action'? – yechabbi

+0

你是指什麼水平? – Cawa

0

您已經定義也許是你的正則表達式:

'controller' => '[a-zA-Z][a-zA-Z0-9_-]*/?', 
'action' => '[a-zA-Z][a-zA-Z0-9_-]*/?', 

這迫使你匹配尾部斜線,試試這個:

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