首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ZF2:不同对比控制器中的多个动作

ZF2:不同对比控制器中的多个动作
EN

Stack Overflow用户
提问于 2014-07-12 09:27:55
回答 1查看 722关注 0票数 0

我想让Module有多个动作,其中URL中的参数是不同的-我想检查它们的约束。

如果在module.config.php中,我制作了如下所示的控制器/模块,控制应用程序中的所有路由。

例如,如果我尝试运行http://example.com/notapi,它将生成没有zf2布局的错误,因为它试图处理这个控制器,但是当我从应用程序配置中删除这个模块时,它将处理ZF2布局中的错误。

这个控制器怎么了?

代码语言:javascript
复制
return array(
'controllers' => array(
    'invokables' => array(
        'Api\Controller\Api' => 'Api\Controller\ApiController',     
    ),
),
'router' => array(
    'routes' => array(
        'api' => array(
            'type'    => 'literal',
            'options' => array(
                'route'    => '/api',
                'defaults' => array(
                    'controller' => 'Api\Controller\Api',
                    'action'     => 'index',
                ),
            ),
        ),
        'action1' => array(
                'type'    => 'segment',
                'options' => array(
                        'route'    => '/api/action1/:param',
                        'constraints' => array(
                                'param'     => '[0-9]+',
                        ),
                        'defaults' => array(
                                'controller' => 'Api\Controller\Api',
                                'action'     => 'action1',
                        ),
                ),
        ),
        'action2' => array(
                'type'    => 'segment',
                'options' => array(
                        'route'    => '/api/action2/:type/:lang',
                        'constraints' => array(
                                'type'   => '[012]',
                                'lang'   => 'pl|by|ru|ua',
                        ),
                        'defaults' => array(
                                'controller' => 'Api\Controller\Api',
                                'action'     => 'action2',
                        ),
                ),
        ),                                                                          
    ),
),
'view_manager' => array(
    'template_path_stack' => array(
        'api' => __DIR__ . '/../view'
    ),
    'strategies' => array(
            'ViewJsonStrategy',
    ),          
),  

);

这是我在Api模块中的Module.php

代码语言:javascript
复制
namespace Api;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\View\Model\JsonModel;

class Module 
{
public function onBootstrap(MvcEvent $e)
{
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);

    $eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'onDispatchError'), 0);
    $eventManager->attach(MvcEvent::EVENT_RENDER_ERROR, array($this, 'onRenderError'), 0);
}

public function onDispatchError($e)
{
    return $this->getJsonModelError($e);
}

public function onRenderError($e)
{
    return $this->getJsonModelError($e);
}

public function getJsonModelError($e)
{
    $error = $e->getError();
    if (!$error) {
        return;
    }

    $response = $e->getResponse();
    $exception = $e->getParam('exception');
    $exceptionJson = array();
    if ($exception) {
        $exceptionJson = array(
                'class' => get_class($exception),
                'file' => $exception->getFile(),
                'line' => $exception->getLine(),
                'message' => $exception->getMessage(),
                'stacktrace' => $exception->getTraceAsString()
        );
    }

    $errorJson = array(
            'message'   => 'An error occurred during execution; please try again later.',
            'error'     => $error,
            'exception' => $exceptionJson,
    );
    if ($error == 'error-router-no-match') {
        $errorJson['message'] = 'Resource not found.';
    }

    $model = new JsonModel(array('errors' => array($errorJson)));

    $e->setResult($model);

    return $model;
}

public function getConfig()
{
    return include __DIR__ . '/config/module.config.php';
}

public function getAutoloaderConfig()
{
    return array(
            'Zend\Loader\StandardAutoloader' => array(
                    'namespaces' => array(
                            __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                    ),
            ),
    );
}   
}
EN

回答 1

Stack Overflow用户

发布于 2014-07-12 09:42:21

ZF2路由是“第一匹配”原则。将使用匹配的第一条路线。所以您的第一条路由是/api,这将与您的所有/api*路由匹配。所以你的其他路线从未被使用过。

代码语言:javascript
复制
return array(
'controllers' => array(
    'invokables' => array(
        'Api\Controller\Api' => 'Api\Controller\ApiController',     
    ),
),
'router' => array(
    'routes' => array(
        'action1' => array(
                'type'    => 'segment',
                'options' => array(
                        'route'    => '/api/action1/:param',
                        'constraints' => array(
                                'param'     => '[0-9]+',
                        ),
                        'defaults' => array(
                                'controller' => 'Api\Controller\Api',
                                'action'     => 'action1',
                        ),
                ),
        ),
        'action2' => array(
                'type'    => 'segment',
                'options' => array(
                        'route'    => '/api/action2/:type/:lang',
                        'constraints' => array(
                                'type'   => '[012]',
                                'lang'   => 'pl|by|ru|ua',
                        ),
                        'defaults' => array(
                                'controller' => 'Api\Controller\Api',
                                'action'     => 'action2',
                        ),
                ),
        ),  
        'api' => array(
            'type'    => 'literal',
            'options' => array(
                'route'    => '/api',
                'defaults' => array(
                    'controller' => 'Api\Controller\Api',
                    'action'     => 'index',
                ),
            ),
        ),                                                                        
    ),
),
'view_manager' => array(
    'template_path_stack' => array(
        'api' => __DIR__ . '/../view'
    ),
    'strategies' => array(
            'ViewJsonStrategy',
    ),          
),  
);

编辑:

我会用更普通的路线。下面是一个例子:

代码语言:javascript
复制
        'myroutename' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/myprefix',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Gantt',
                    '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(
                        ),
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        'default' => array(
                            'type' => 'Wildcard',
                            'options' => array(
                            )
                        )
                    )
                ),
            ),
        ),

这与控制器和操作匹配,使用通配符的子路由允许我所需要的所有参数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24711562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档