slimphp / Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
http://slimframework.com
MIT License
11.94k stars 1.95k forks source link

Enforce ResponseInterface from route actions #1917

Closed geggleto closed 7 years ago

geggleto commented 8 years ago

Currently, Slim does not enforce developers to return a ResponseInterface from a route callable.

Example:

$app->get('/test', function ($req, $res, $args) {
    print "do stuff";
});

Proper:

$app->get('/test', function ($req, $res, $args) {
    return $res->write("do stuff");
});

What should happen

$app->get('/test', function ($req, $res, $args) {
    print "do stuff";
});

An exception should be raised. \Exception("Response Interface should be returned from a Route Callable");

silentworks commented 8 years ago

This is due to BC, where we allow you to echo instead of return ResponseInterface.

geggleto commented 8 years ago

I thought we had stopped that?

silentworks commented 8 years ago

I don't remember it being removed. Maybe add to the 4.0 Roadmap.

geggleto commented 8 years ago

I would then propose a different Strategy for capturing the buffer output from the callable and pass it down the chain.

ob_start();
$callable($req, $res);
$response->write(ob_get_clean());
geggleto commented 8 years ago

Or we can possibly modify the current one to include a ob_* capture if the callable doesn't respond with a response interface?

geggleto commented 8 years ago

I suppose we could make this a 4.0 thing

akrabat commented 8 years ago

I would then propose a different Strategy for capturing the buffer output from the callable and pass it down the chain.

ob_start();
$callable($req, $res);
$response->write(ob_get_clean());

https://github.com/slimphp/Slim/blob/3.x/Slim/Route.php#L324-L326

This would have to be a 4.0 thing anyway.