2014-09-22 138 views
0

我有在ZF2默認路由的煩惱:Zend的2缺省路由

'router' => array(
    'routes' => array(
     'hostbill-api' => array(
      'type' => 'Literal', 
      'options' => array(
       'route' => '/api/hostbill', 
       'constraints' => array(
        'service' => '[a-zA-Z][a-zA-Z0-9_-]*', 
        'call' => '[a-zA-Z][a-zA-Z0-9_-]*', 
       ), 
       'defaults' => array(
        '__NAMESPACE__' => 'Hostbill\Controller', 
        'controller' => 'Api', 
        'action'  => 'index', 
       ), 
      ), 
      'may_terminate' => true, 
      'child_routes' => array(
       'hostbill-api-service' => array(
        'type' => 'Segment', 
        'options' => array(
         'route' => '[/:service[/:call]]', 
         'constraints' => array(
          'service' => '[a-zA-Z][a-zA-Z0-9_-]*', 
          'call' => '[a-zA-Z][a-zA-Z0-9_-]*', 
         ), 
        ), 
       ), 
       'hostbill-api-hook' => array(
        'type' => 'Segment', 
        'options' => array(
         'route' => '/hook[/:action]', 
         'constraints' => array(
          'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 
         ), 
         'defaults' => array(
          '__NAMESPACE__' => 'Hostbill\Controller', 
          'controller' => 'Hook', 
          'action'  => 'index', 
         ), 
        ), 
       ), 
      ), 
     ), 
    ), 
), 

我想要做什麼,這是當我打電話URL/API/hostbill /掛機/ non_existent_action URL時,默認它被稱爲動作。實際上,如果我調用一個存在的動作,路由工作,但如果我使用一個不存在的鉤子動作,則不會調用默認值,並激發一個404。

任何幫助apreciated

回答

0

把這個在你的控制器:

/** 
* Create an HTTP view model representing a "not found" page 
* 
* @param HttpResponse $response 
* @return ViewModel 
*/ 
protected function createHttpNotFoundModel(HttpResponse $response) 
{ 
    return $this->indexAction();//or whatever that is your default action is 
} 

或者這一個:

/** 
* Action called if matched action does not exist 
* 
* @return array 
*/ 
public function notFoundAction() 
{ 
    return $this->indexAction();//or whatever that is your default action is 
} 
+0

真好!愛這樣做,而不是使用事件 – kitensei 2014-09-23 11:18:49