Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php ZF2:控制器中具有不同控件的多个操作_Php_Routes_Zend Framework2 - Fatal编程技术网

Php ZF2:控制器中具有不同控件的多个操作

Php ZF2:控制器中具有不同控件的多个操作,php,routes,zend-framework2,Php,Routes,Zend Framework2,我想制作一个模块Api,它有多个操作,其中URL中的参数是不同的——我想检查它们的约束 如果在module.config.php中,我在下面做了类似的事情,那么这个控制器/模块将控制应用程序中的所有路由 例如,如果我尝试运行它,它将在没有zf2布局的情况下生成错误,因为它尝试处理此控制器,但当我从应用程序配置中删除此模块时,它将处理zf2布局中的错误 这个控制器怎么了 return array( 'controllers' => array( 'invokables' =>

我想制作一个模块Api,它有多个操作,其中URL中的参数是不同的——我想检查它们的约束

如果在module.config.php中,我在下面做了类似的事情,那么这个控制器/模块将控制应用程序中的所有路由

例如,如果我尝试运行它,它将在没有zf2布局的情况下生成错误,因为它尝试处理此控制器,但当我从应用程序配置中删除此模块时,它将处理zf2布局中的错误

这个控制器怎么了

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模块中的my Module.php

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__,
                    ),
            ),
    );
}   
}

ZF2路由是第一匹配原则。将使用匹配的第一条路线。因此,您的第一个路由是/api,它将匹配所有/api*路由。所以你的其他路线永远不会被使用

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',
    ),          
),  
);
编辑: 我会使用更通用的路线。请参见以下示例:

        '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(
                            )
                        )
                    )
                ),
            ),
        ),

这将匹配一个控制器和一个操作,带有通配符的childroute允许我需要的所有参数。

先重新排序特定的,后重新排序通用的。。else/api总是匹配的。你能说得更具体一点吗?我不明白你的意思。请看下面我的答案。谢谢,但没用。当我试图打开而不是它试图呈现api而不是od notapi时,它应该不可用,但它不会给出ZF2错误,但php错误你有/notapi的路由吗?我没有notapi的路由,但即使我可以在没有约束的情况下运行此路由,它也不会生成notapi,而是api路由。也许你可以在中编写模块配置的示例我可以通过URL api请求它-它将运行索引操作,action1和action2,其中param1和param2有不同的控件?thxsee my edit,以不同的方法解决您的问题。好的,我在Api模块中使用了您的路由,但现在在Api Module.php中,我对JSON进行了错误处理,当我运行不同的路由而不是Api时,它会在JSON中给我错误,而不是在ZF2布局中?为什么Api目录中的Module.php会处理此错误?