2014-09-22 109 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