2017-09-20 48 views
0

我試圖使用Zend的表現嵌套的應用程序,所以我下面的這篇博客文章: https://framework.zend.com/blog/2017-03-15-nested-middleware-in-expressive.htmlZend的表現嵌套應用

這個問題似乎是在中間件廠家:

class CreateBookMiddlewareFactory 
{ 
    public function __invoke(ContainerInterface $container) 
    { 
     $nested = new Application(
      $container->get(RouterInterface::class), 
      $container 
     ); 

     $nested->pipe(AuthenticationMiddleware::class); 
     $nested->pipe(ContentValidationMiddleware::class); 
     $nested->pipe(BodyParamsMiddleware::class); 
     $nested->pipe(BookValidationMiddleware::class); 
     $nested->pipe(CreateBookMiddleware::class); 

     return $nested; 
    } 
} 

我不知道CreateBookMiddleware可以如何加入到管道中,因爲我們在工廠。因此,管道就會調用工廠,創建一個新的嵌套應用程序,它會調用工廠,這將創造另一個嵌套應用...

(!) Fatal error: Maximum function nesting level of '256' reached, aborting! in /var/www/project/vendor/zendframework/zend-stratigility/src/Next.php on line 
    158 

有什麼事我不從這個博客帖子越來越好嗎?

回答

2

您的工廠名稱爲CreateBookMiddlewareFactory。然後在__invoke裏面有$nested->pipe(CreateBookMiddleware::class);。這取決於你的配置,但通常CreateBookMiddlewareFactory將是CreateBookMiddleware的工廠。所以它被困在一個循環中,因爲它不斷創建它自己。

由於您擁有與博文中完全相同的代碼,我猜這是該博客文章中的錯誤。我認爲它應該是在最後一個代表工廠的例子中:沒有最後的$nested->pipe(CreateBookMiddleware::class);

我已通知該博客文章的作者。

編輯:博客文章更新與此修復程序:

namespace Acme\Api; 

use Acme\AuthenticationMiddleware; 
use Acme\ContentNegotiationMiddleware; 
use Psr\Container\ContainerInterface; 
use Zend\Expressive\Application; 
use Zend\Expressive\Helper\BodyParams\BodyParamsMiddleware; 
use Zend\Expressive\Router\RouterInterface; 

class CreateBookMiddlewareFactory 
{ 
    public function __invoke(ContainerInterface $container) 
    { 
     $nested = new Application(
      $container->get(RouterInterface::class), 
      $container 
     ); 

     $nested->pipe(AuthenticationMiddleware::class); 
     $nested->pipe(ContentValidationMiddleware::class); 
     $nested->pipe(BodyParamsMiddleware::class); 
     $nested->pipe(BookValidationMiddleware::class); 

     // If dependencies are needed, pull them from the container and pass 
     // them to the constructor: 
     $nested->pipe(new CreateBookMiddleware()); 

     return $nested; 
    } 
} 
0

我接受了澄清@xtreamwayz答案。但這裏是我如何做它的工作:

class CreateBookMiddlewareFactory 
{ 
    public function __invoke(ContainerInterface $container) 
    { 
     $nested = new Application(
      $container->get(RouterInterface::class), 
      $container 
     ); 

     $nested->pipe($container->get(AuthenticationMiddleware::class)); 
     $nested->pipe($container->get(ContentValidationMiddleware::class)); 
     $nested->pipe($container->get(BodyParamsMiddleware::class)); 
     $nested->pipe($container->get(BookValidationMiddleware::class)); 
     // instanciate the new class, so it will not call the factory again 
     $nested->pipe(new CreateBookMiddleware()); 

     return $nested; 
    } 
} 
+0

這正是它是如何固定在後剛纔:https://github.com/zendframework/zf3-web/commit/2ae14d151e512b13506c4f854ad6a811d9a74af0 – xtreamwayz