【问题标题】:ZF2 - ApiGility installed in my own app - route not workingZF2 - ApiGility 安装在我自己的应用程序中 - 路线不起作用
【发布时间】:2014-09-04 22:49:55
【问题描述】:

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

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

当我尝试访问 apigility 管理员:www.myapp.dev/apigility 时,我收到 “请求的 URL 无法通过路由匹配” 错误。

我的配置如下:

'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 阻塞了该路由,我会被重定向。在这种情况下,当路由不可访问时,我会收到错误消息。

有没有简单的测试方法?

【问题讨论】:

    标签: zend-framework2 laminas-api-tools


    【解决方案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');
                }
            }
        );
    }
    

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

    【讨论】:

      【解决方案2】:

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

      本教程没有提到将 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#/

      希望这对某人有所帮助...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-01
        • 2012-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多