ZF2-ApiGility安装在我自己的应用程序中-路由不起作用



我正试图在我的应用程序中安装APIGILITY。我遵循了这个教程:

https://apigility.org/documentation/recipes/apigility-in-an-existing-zf2-application

当我试图访问apigility管理员www.myapp.dev/apigibility时,我收到一个"路由无法匹配请求的URL"错误。

我的配置如下:

'modules' => array(
    'DoctrineModule',
    'DoctrineORMModule',
    'ZfcRbac',              //Keep this at the top
    'Application',          //The applications main functions run from this module
    //APIGILITY
    'ZFApigility',
    'ZFApigilityProvider',
    'AssetManager',
    'ZFApiProblem',
    'ZFMvcAuth',
    'ZFOAuth2',
    'ZFHal',
    'ZFContentNegotiation',
    'ZFContentValidation',
    'ZFRest',
    'ZFRpc',
    'ZFVersioning',
    'ZFDevelopmentMode',
    'ZFApigilityAdmin',
    'ZFConfiguration',

我已启用开发人员模式。

通常,如果存在一条路由,而ZfcRbac正在阻塞该路由,我会被重定向。在这种情况下,当路由不可访问时,我会得到错误。

有没有一个简单的方法来测试这个?

要跟进HappyCoder自己的答案,您可以将zf apigility模块中的所有路由与进行匹配

public function onBootstrap(MvcEvent $e)
{
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
    $e->getApplication()->getEventManager()->attach(
        MvcEvent::EVENT_ROUTE, function(MvcEvent $e) {
            // Route matched
            $route_name = $e->getRouteMatch()->getMatchedRouteName();
            // If apigility - set correct layout
            if(preg_match('/^zf-apigility/', $route_name)) {
                $e->getViewModel()->setTemplate('layout/api-layout');
            }
        }
    );
}

当这样做时,它将为所有apigility视图设置适当的布局,包括/apility(欢迎屏幕)

我通过以下操作解决了这个问题:

本教程没有提及将ApiGility模板复制到您的应用程序中。你需要这样做。我所做的是将模板添加到我的application/config/module.config.php文件中。

 return [
    'view_manager' => [
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/exception',
        'template_map' => [
            'customer/layout'         => __DIR__ . '/../view/layout/customer-layout.phtml',
            'api/layout'              => __DIR__ . '/../view/layout/api-layout.phtml',
            'layout/layout'           => __DIR__ . '/../view/layout/admin-layout.phtml',

在应用程序模块中,我检查路由并相应地切换模板:

 public function onBootstrap(MvcEvent $e)
    {
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
    $e->getApplication()->getEventManager()->attach(
        MvcEvent::EVENT_ROUTE, function(MvcEvent $e) {
            //Set the customer layout
            $needle = $e->getRouteMatch()->getParam('controller');
            $haystack = [
               /* Customer template routes */
            ];
            if (in_array( $needle , $haystack )) {
                $e->getViewModel()->setTemplate('customer/layout');
            }
            //Apigility route
            $haystack = [
                'zf-apigility/ui'
            ];
            if (in_array( $needle , $haystack )) {
                $e->getViewModel()->setTemplate('api/layout');
            }
        }
    );
}

要访问apigility页面,我现在通过访问:http://www.myapp.com/apigility/ui#/

希望这能帮助到别人。。。

最新更新