2014-09-04 34 views
1

我試圖在我的應用程序中安裝APIGILITY。我按照這個教程:ZF2 - ApiGility安裝在我自己的應用程序中 - 路由不工作

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

當我試圖訪問apigility管理員:www.myapp.dev/apigility我得到一個「的請求的網址無法通過路由匹配」錯誤。

我的配置如下:

'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阻塞了路由,我將被重定向。在這種情況下,當路由不可訪問時,我得到錯誤。

有沒有簡單的方法來測試這個?

本教程隻字不提複製ApiGility模板,您的應用程序:

回答

0

我通過以下操作解決了這個問題。你需要這樣做。我所做的是將模板添加到我的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#/

希望這可以幫助別人...

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(歡迎屏幕)

+0

有道理,謝謝你添加的信息! – HappyCoder 2014-11-18 18:42:51

相關問題