Slim: Consolidate `App::run()` and `App::__invoke()` methods

Created on 19 Nov 2016  路  8Comments  路  Source: slimphp/Slim

@akrabat and @geggleto Did either of you have ideas for this one?

Slim 4

Most helpful comment

Going off of my earlier comments, in my testing the __invoke method is only usuable as a middleware inside of Slim. I had to make a wrapper class that calls the current process method instead. I would like to Slim as an actual middleware itself.

All 8 comments

My thoughts are that we need to decouple the App and Rendering Content to the browser.

  • The run method takes a Request object and returns a Response object, and in 4.x we mark __invoke as deprecated and fully remove in 5.x. __invoke then becomes a proxy of run() in 4.x
  • Another Slim class is responsible for Rendering the Response Object to the browser.

Reasoning:

  • We can then more fully unit test Core as currently it's very difficult to do.
  • It also opens up the ability to better run Slim in persistent environments IE ReactPHP.

Isn't __invoke there so you can run the App itself as a middleware?
Are you planning on removing that functionality entirely or will you replace it with process(ServerRequestInterface $request, DelegateInterface $delegate)?

@geekish has a good point

@geekish @codeguy We can still support it as middleware if our users need it. I have never seen Slim used in such a manner unless it was to facilitate unit/integration tests. Which should be fixed by more decoupling of the internals.

The use case I have in mind is best explained here:
https://zendframework.github.io/zend-expressive/cookbook/common-prefix-for-routes/

Might be time for me to test the differences between using route groups and apps as middleware.

I use the __invoke method inslide the CLI front controller

// ...
echo $app($request, $response)->getBody();

Going off of my earlier comments, in my testing the __invoke method is only usuable as a middleware inside of Slim. I had to make a wrapper class that calls the current process method instead. I would like to Slim as an actual middleware itself.

run() and __invoke() have totally different purposes:

  • run() runs the app from the top
  • __invoke() is the last element in the middleware stack

We can't consolidate as they perform different functions within the lifecycle.

We will need to look at __invoke when we go to PSR_15 though.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

codeguy picture codeguy  路  3Comments

JamesTheHacker picture JamesTheHacker  路  3Comments

adambro picture adambro  路  3Comments

xymz picture xymz  路  5Comments

basuke picture basuke  路  3Comments