ZF2 - ApiGility安装在我自己的应用程序中 - 路由无法正常工作

时间:2014-09-04 22:49:55

标签: zend-framework2 apigility

我正在尝试在我的应用中安装APIGILITY。我已经按照本教程进行了操作:

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

当我尝试访问apigility admin:www.myapp.dev/apigility时,我收到 “请求的网址无法通过路由匹配” 错误。< / p>

我的配置如下:

'modules' => array(
    'DoctrineModule',
    'DoctrineORMModule',
    'ZfcRbac',              //Keep this at the top
    'Application',          //The applications main functions run from this module

    //APIGILITY
    'ZF\Apigility',
    'ZF\Apigility\Provider',
    'AssetManager',
    'ZF\ApiProblem',
    'ZF\MvcAuth',
    'ZF\OAuth2',
    'ZF\Hal',
    'ZF\ContentNegotiation',
    'ZF\ContentValidation',
    'ZF\Rest',
    'ZF\Rpc',
    'ZF\Versioning',
    'ZF\DevelopmentMode',
    'ZF\Apigility\Admin',
    'ZF\Configuration',

我启用了开发者模式。

通常情况下,如果存在路由并且ZfcRbac阻止该路由,我将被重定向。在这种情况下,当路由无法访问时,我收到错误。

有没有一种简单的方法来测试它?

2 个答案:

答案 0 :(得分:1)

要跟进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视图设置适当的布局,包括/ apiligity(欢迎屏幕)

答案 1 :(得分:0)

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

本教程未提及将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#/

访问

希望这有助于某人...