0

私は zend 表現力豊かなネストされたアプリケーションを使用しようとしているので、このブログ投稿に従っています: https://framework.zend.com/blog/2017-03-15-nested-middleware-in-expressive.html

問題はミドルウェア ファクトリにあるようです。

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

このブログ投稿から正しく得られないものはありますか?

4

2 に答える 2

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;
    }
}
于 2017-09-21T06:34:43.440 に答える
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;
    }
}
于 2017-09-21T07:31:19.810 に答える