ZF2 - ApiGility installed in my own app - route not working

∥☆過路亽.° 提交于 2019-12-11 09:57:08

问题


I am attempting to install APIGILITY in my app. I have followed this tutorial:

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

When I attempt to access the apigility admin: www.myapp.dev/apigility I get a "The requested URL could not be matched by routing" error.

My config is as follows:

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

I have enabled developer mode.

Typically if a route exists and ZfcRbac is blocking the route, I am re-directed. In this case when the route is not accessible I get the error.

Is there a simple way to test this?


回答1:


To follow up on HappyCoder's own answer, you can match all routes in zf-apigility module with

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');
            }
        }
    );
}

When doing this way - it will set appropriate layout for all apigility views, including /apiligity (welcome screen)




回答2:


I solved this issue by doing the following:

The tutorial makes no mention of copying the ApiGility template to your app. You need to do this. What I did was to add the template to my application/config/module.config.php file.

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

In the Application module I check routing and switch the template accordingly:

 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');
            }
        }
    );
}

To access the apigility pages, I now access via: http://www.myapp.com/apigility/ui#/

Hope this helps someone...



来源:https://stackoverflow.com/questions/25676164/zf2-apigility-installed-in-my-own-app-route-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!