dpwtr666638 2017-10-22 20:36
浏览 47

是否可以使用Zend Servicemanager管道中间件?

I am using Zend Expressive as an API. I have succeeded adding a middleware, which validates the request headers of API Keys, for every single request.

At the moment I add the middleware using the pipe() function in config/pipeline.php

$app->pipe(new MyAuthMiddleware(....);

This actually works pretty well. However, I would like to add the piping using Zend Servicemanager instead, with a configuration file, like:

return [
'dependencies' => [
    /* ... */
    'invokables' => [
        // Remove this entry:
        App\Action\HelloAction::class => App\Action\HelloAction::class,
    ],
    'factories' => [
        /* ... */
        // Add this:
        App\Action\HelloAction::class => App\Action\HelloActionFactory::class,
    ],
    /* ... */
],];

Question: Is it possible to pipe a middleware using Zend Servicemanager? And how if it is.

  • 写回答

1条回答 默认 最新

  • dongou5100 2017-10-23 08:50
    关注

    Yes it's possible. Up to expressive 1.1 it was config driven as you are asking for. Since 1.1 it's programmatic driven by default if you install via the skeleton. You can still use config driven but I have to mention you can't use both. At least, it's not recommended.

    A configuration might look like this (taken from an expressive 1.0 expressive app). The error handling changed in 1.1+ but I don't have an example for it.

    <?php
    
    return [
        'dependencies' => [
            'factories'  => [
                // ...
            ],
        ],
    
        'middleware_pipeline' => [
            'always' => [
                'middleware' => [
                    Zend\Expressive\Helper\ServerUrlMiddleware::class,
                ],
                'priority'   => 10000,
            ],
    
            'routing' => [
                'middleware' => [
                    Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
                    Zend\Expressive\Helper\UrlHelperMiddleware::class,
                    LocalizationMiddleware::class,
                    AuthenticationMiddleware::class,
                    AuthorizationMiddleware::class,
                    Zend\Expressive\Container\ApplicationFactory::DISPATCH_MIDDLEWARE,
                ],
                'priority'   => 1,
            ],
    
            'error' => [
                'middleware' => [
                    Application\Middleware\Auth\UnauthorizedErrorMiddleware::class,
                    Application\Middleware\Auth\ForbiddenErrorMiddleware::class,
                    Application\Middleware\Logger\ExceptionLoggerMiddleware::class,
                ],
                'error'      => true,
                'priority'   => -10000,
            ],
        ],
    ]; 
    

    Here is some more info I can find right now:

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀