zendframework / zend-stratigility

Middleware for PHP built on top of PSR-7 and PSR-15
BSD 3-Clause "New" or "Revised" License
235 stars 57 forks source link

Specific group of route for some middleware #60

Closed ooghry closed 8 years ago

ooghry commented 8 years ago

Hi all Currently we must define path as string but it is good to have ability of define as Array : middleware-pipeline.global.php: 'm2' => [ 'middleware' => [ App\Middleware\My::class, ], 'path' => ['/ping','/another/one'], 'priority' => 10000, ],

Please see this commit: https://github.com/ooghry/zend-stratigility/commit/dde05123261cd6bdd2176eab8e12b47f50932360

adamlundrigan commented 8 years ago

@weierophinney what are your thoughts on this?

Personally, I think it blurs the line between routing and piping and adds extra complexity to the MiddlewarePipe implementation without enough benefit to justify.

This can already be accomplished by attaching middleware from the route side instead, eg:

return [
    'routes' => [
        [
            'name' => 'ping',
            'path' => '/ping',
            'middleware' => [
                'App\Middleware\My::class',
            ],
        ],
        [
            'name' => 'another-one',
            'path' => '/another/one',
            'middleware' => [
                'App\Middleware\My::class',
            ],
        ],
    ],
];

That's an Expressive example, but in the case of using Stratigility directly it's also straightforward to do already:

$middleware = new \App\Middleware\My();
$app->pipe('/ping', $middleware);
$app->pipe('/another/one', $middleware);

Unless I've missed the point?

weierophinney commented 8 years ago

This issue is confusing because it's posted against Stratigility, but referencing configuration from Expressive, which makes evaluation difficult.

The ability to define an array of middleware in order to define a pipeline is specific to configuration-based marshaling of an Expressive application, and not a feature of Stratigility.

What you're requesting, if I understand correctly, is to be able to do similarly with the path configuration, which would be analogous to passing an array of paths to Zend\Stratigility\MiddlewarePipe::pipe().

The chief problem with adding such a feature, whether here or in Expressive, is that doing so adds a lot of complexity; determining if an array means an array of paths or indicates a PHP callable to use as middleware can become messy. Basically, any additional overloading of arguments makes maintenance much more difficult.

On top of that, while we'll be keeping the current config-based pipeline creation, our plan for Expressive 1.1 is to recommend instead programmatic creation of both the pipeline and the routing. Our experience so far is that doing so makes learning Expressive (and Stratigility) easier for newcomers, and provides a much more clear indication of the actual pipeline and routes than is presented in the configuration.

If you really want such a feature, I think a better approach would be to have a specialized MiddlewarePipe implementation that provides this ability through a new method, or via its constructor, or to create such pipelines separately, capture them in variables, and reference those within your configuration.