2014-08-27 37 views
1

我不知道我該如何解釋我的問題。我試圖從擴展abstractrestfulcontroler的模塊中獲取JSON響應。我具有以下模塊配置模塊配置ViewJsonStrategy設置沒有在child_route鏈接下工作

文件:module.config.php

return array(
    'controllers' => array(
      'invokables' => array(
        'TfwCommunication\\Controller\\TfwCommunication' => 'TfwCommunication\\Controller\\TfwCommunicationController', 
        'TfwCommunication\\Controller\\TfwChat' => 'TfwCommunication\\Controller\\TfwChatController', 
        'TfwCommunication\\Controller\\TfwContacts' => 'TfwCommunication\\Controller\\TfwContactsController', 
        'TfwCommunication\\Controller\\TfwMessage' => 'TfwCommunication\\Controller\\TfwMessageController', 
        'TfwCommunication\\Controller\\TfwUserMessageTemplates' => 'TfwCommunication\\Controller\\TfwUserMessageTemplatesController', 
        'TfwCommunicationControllerTfwUserMessageTemplates' => 'TfwCommunication\\Controller\\TfwUserMessageTemplatesController', 
      ), 
    ), 
    'router' => array(
      'routes' => array(
        'communication' => array(
          'type' => 'Segment', 
          'options' => array(
            'route' => '/communication', 
            'constraints' => array(
              #'id' => '[0-9]+', # '[a-zA-Z][a-zA-Z0-9_-]*', 
             #'action'=>'[a-z][a-z0-9]*', 
            ), 
            'defaults' => array(
              'controller' => 'TfwCommunication\\Controller\\TfwCommunication', 
              'action' => 'index', 
              #'id'=>'update', 
              #'action'  => 'index', 
            ), 
          ), 
          'may_terminate' => true, 
          'child_routes' => array(
            'actions'=>array(
              'type' => 'Segment', 
              'options' => array(
                'route' => '/[:action[/]]', 
                'constraints' => array(
                  'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 
                ), 
                'defaults' => array(
                  'controller' => 'TfwCommunication\\Controller\\TfwCommunication', 
                  'action'  => 'index', 
                ), 
              ), 
            ), 
            'message' => array(
              'type' => 'Segment', 
              'options' => array(
                'route' => '/message', 
                'defaults' => array(
                  'controller' => 'TfwCommunication\\Controller\\TfwMessage', 
                  'action'  => 'index', 
                 ), 
               ), 
              'may_terminate' => true, 
              'child_routes' => array(
                'read'=>array(
                  'type' => 'Segment', 
                  'options' => array(
                    'route' => '/[:id]', 
                    'constraints' => array(
                      'id' => '[0-9]*', 
                     ), 
                    'defaults' => array(
                      'controller' => 'TfwCommunication\\Controller\\TfwMessage', 
                      'action'  => 'read', 
                     ), 
                   ), 
                 ), 
                'actions'=>array(
                  'type' => 'Segment', 
                  'options' => array(
                    'route' => '/[:action[/[:id]]]', 
                    'constraints' => array(
                      'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 
                      'id' => '.*', 
                     ), 
                    'defaults' => array(
                      'controller' => 'TfwCommunication\\Controller\\TfwMessage', 
                      'action'  => 'index', 
                     ), 
                   ), 
                 ), 
                'json-request-by-child-route'=>array(
                  'type' => 'Segment', 
                  'options' => array(
                    'route' => '/json-request-by-child-route[/:id]', 
                    'constraints' => array(
                      'id' => '.*', 
                     ), 
                    'defaults' => array(
                      'controller' => 'TfwCommunicationControllerTfwUserMessageTemplates', 
                     ), 
                   ), 
                  ), 
              ), 
            ), 
          ), 
        ), 
        'user-defined-templates'=>array(
          'type' => 'Segment', 
          'options' => array(
            'route' => '/communication/message/user-defined-templates[/:id]', 
            'constraints' => array(
              'id' => '.*', 
            ), 
            'defaults' => array(
              'controller' => 'TfwCommunication\\Controller\\TfwUserMessageTemplates', 
            ), 
          ), 
        ), 
        'user-defined-templates-direct-link'=>array(
          'type' => 'Segment', 
          'options' => array(
            'route' => '/user-defined-templates-direct-link[/:id]', 
            'constraints' => array(
              'id' => '.*', 
            ), 
            'defaults' => array(
              'controller' => 'TfwCommunication\\Controller\\TfwUserMessageTemplates', 
            ), 
          ), 
        ), 
      ), 
    ), 
    'view_manager' => array(
      'template_path_stack' => array(
        'tfwcommunication' => __DIR__ . DS.'..'.DS.'view', 
      ), 
      'strategies' => array(
        'ViewJsonStrategy', 
      ), 
    ), 

);

現在下面的鏈接工作 - tfw.com.bd/communication/message/user-defined-templates/ OR tfw.com.bd/user-defined-templates-direct-link/

但以下鏈接將無法按預期工作(它表示404控制器未找到) - tfw.com.bd/communication/message/json-request-by-child-route

此處tfw.com.bd指示本地主機。 請注意,我預計輸出爲JSON格式。 還指出,我在這裏使用的所有控制器都是相同的。在某些鏈接/路線中,它不起作用。我無法弄清楚原因。

任何ZF2專家都可以解釋行爲背後的真正原因。

謝謝

+0

你說的*以下爲預期的鏈接將無法正常工作*是什麼意思?它是否觸及控制器方法? – 2014-08-27 19:57:12

+0

它說404控制器沒有找到 – 2014-08-28 03:20:09

+0

你錯過了該行反斜槓 – 2014-08-28 12:16:07

回答

1

最後,我自己得到了這個答案。由於默認的「動作」定義爲「索引」,上述路由配置不起作用。 Zend查找「action」參數,如果找到,則加載該操作。如果未找到,則返回「notFoundAction」。因此,在上述路由器的子路由中定義了「動作」參數。所以,當Zend沒有進入默認的「indexAction」或者進入「notFoundAction」時,它不會進一步發展。

腳註,您需要避免動作參數才能讓「JsonStrategy」按預期工作。如果在路由器中無法避免動作參數,則可以通過重載「AbstractRestfulController」控制器的「onDispatch」方法來修復它。

這裏是我的代碼爲我的作品 -

public function onDispatch(MvcEvent $e){ 
    $routeMatch = $e->getRouteMatch(); 
    if (! $routeMatch) { 
     /** 
     * @todo Determine requirements for when route match is missing. 
     *  Potentially allow pulling directly from request metadata? 
     */ 
     throw new Exception\DomainException(
       'Missing route matches; unsure how to retrieve action'); 
    } 

    $request = $e->getRequest(); 

    // Was an "action" requested? 
    #die('get_class($routeMatch): '.get_class($routeMatch).' @'.__LINE__.': '.__FILE__); 
    $action = $routeMatch->getParam('action', false); 


    if ($action) { 
     // Handle arbitrary methods, ending in Action 
     $method = static::getMethodFromAction($action); 
     if (! method_exists($this, $method)) { 
      if($method=='indexAction'){ 
       $routeMatch->setParam('action', false); 
       return parent::onDispatch($e); 
      } 
      $method = 'notFoundAction'; 
     } 
     $return = $this->$method(); 
     $e->setResult($return); 
     return $return; 
    } 
    return parent::onDispatch($e); 
}