模块配置ViewJsonStrategy设置在child_route链接下不起作用

时间:2014-08-27 19:51:05

标签: php json module zend-framework2

我不知道如何解释我的问题。我正试图从我的一个扩展abstractrestfulcontroler的模块中获取JSON响应。我有以下模块配置

文件: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/或tfw.com.bd/user-defined-templates-direct-link /

但是以下链接将无法正常工作(它表示未找到404控制器) - tfw.com.bd/communication/message/json-request-by-child-route

这里tfw.com.bd表示localhost。 请注意,我预计输出为JSON格式。 还要注意的是,我在这里使用的是同一个控制器。在某些链接/路由中它不起作用。我无法弄清楚原因。

这里的任何ZF2专家都可以解释行为背后的真正原因。

由于

1 个答案:

答案 0 :(得分:1)

最后我自己得到了这个答案。由于默认“action”定义为“index”,上述路由配置不起作用。 Zend寻找“动作”参数,如果找到则加载该动作。如果没有找到,则返回“notFoundAction”。因此,在上面路由器的子路由中定义了“action”参数。因此,当它默认为“indexAction”或“notFoundAction”时,Zend不会再进一步​​了。

脚注,您需要避免使用action参数来使“JsonStrategy”按预期工作。如果您无法避免路由器中的action参数,可以通过重载“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);
}