Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Migration doc] Response object changed at the "default" package #2940

Closed
igorsantos07 opened this issue Apr 15, 2020 · 11 comments
Closed

[Migration doc] Response object changed at the "default" package #2940

igorsantos07 opened this issue Apr 15, 2020 · 11 comments

Comments

@igorsantos07
Copy link

@igorsantos07 igorsantos07 commented Apr 15, 2020

Up to v3 there were a couple of easy ways to write out responses to a path:

  • simply return a string from the function: $app->get('/', fn() => 'hello world');
  • write to the response: $app->get('/', fn($req, $res) => $res->write('hello world'));
  • write JSON with $res->withJson() (this one is completely gone and we've got to default to json_encode() calls)

The first method causes a TypeError, and the second one complains there's no write() method in the Response object anymore.

I'm using the "default" PSR7 package, slim/psr7. I call it "default" because, when migrating, I expect the package coming from the framework itself to either mention API changes from the previous version, or to support everything that used to exist.

As this is a more complicated matter, I suggest someone else write the migration docs about it.

This issue also makes this last example in the docs wrong or, at least, incomplete.

@l0gicgate
Copy link
Member

@l0gicgate l0gicgate commented Apr 15, 2020

That example is in fact wrong.

The write method does not return the response object.

We will need to fix that.

@igorsantos07
Copy link
Author

@igorsantos07 igorsantos07 commented Apr 15, 2020

I should mention these changes are quite breaking for me - or I'm doing something very, very wrong.

The entire application I'm migrating (from v2 lol) relies on echo json_encode() or similar stuff. I'll have to write a new Strategy class to handle the fact 99% of the API endpoints do not return anything. This was working fine on v3.

@l0gicgate
Copy link
Member

@l0gicgate l0gicgate commented Apr 15, 2020

I understand that typing the return on the strategies has been a paint point during migration for a lot of people. I do think it forces user into a better pattern though. You can also easily implement your own strategy to handle your use case as you mentioned.

@igorsantos07
Copy link
Author

@igorsantos07 igorsantos07 commented Apr 15, 2020

Yep, I totally understand the reason behind this. However, if this was supported in v3, it should be clearly stated in the migration doc this is no longer acceptable, and ways to overcome it.

Personally I would also include some sort of LegacyStrategy class - it could issue an E_USER_DEPRECATED error and then, drop that class in v5. But I'm completely new to Slim and have no idea on how things go around here, so this is just a random opinion about easier migration steps for users :)

Not to mention this is quite a deep change. I've spent days and days changing a lot of stuff through search-replace and double-check, but in this case it's not feasible to migrate the actual code - there's just way too many echo's around (and god knows what else) to reliable search-replace. Just saying "this is not going to work anymore, rewrite all your functions" is definitely not friendly.

@igorsantos07 igorsantos07 changed the title [Migration doc] $response->write() is not available at the "default" package [Migration doc] Response object changed at the "default" package Apr 15, 2020
@igorsantos07
Copy link
Author

@igorsantos07 igorsantos07 commented Apr 15, 2020

I noticed only now the docs reside in another package. Nonetheless, as there's code suggestions here, I'll leave this open for now. If you want, I can split it into code suggestions and doc suggestions.

@nixoncode
Copy link

@nixoncode nixoncode commented May 1, 2020

Lots of helper functions are actually, gone. I did a migration from Slim 3 to Slim 4 but managed to get everything working. took a couple of weeks but finally managed it. From reading issues raised in the past, I came to understand that this is because the framework wants to abide by psr7 standards.

Here are a few tips.

// writing out
return $response->write('Hello World'); // v3

$response->getBody()->write('Hello World'); // v4
return $response;

// returning json
return $response->withJson(['message' => 'success']); //v3
$response->getBody()->write(json_encode(['message' => 'success'])); // v4
return $response;

// redirects
return $response->withRedirect($this->router->pathFor('users.index')); // v3
return $response->withHeader('Location', $this->router->urlFor('users.index')); //v4
@l0gicgate
Copy link
Member

@l0gicgate l0gicgate commented May 1, 2020

@nixoncode all those helper functions were migrated to Slim-Http which can decorate any PSR-7 implementation.

@nixoncode
Copy link

@nixoncode nixoncode commented May 1, 2020

@l0gicgate Thanks for the heads up.

This is so sad, I did all that and what I just needed to do was

$ composer require slim/http

Switch Type hinting

// from
use Psr\Http\Message\ResponseInterface as Response;
//to
use Slim\Http\Response;

And everything works as just as it was

Thank you! Migration Upgrade needs to mention this somewhere

@nixoncode
Copy link

@nixoncode nixoncode commented May 1, 2020

I should mention am using slim/psr7 as the prs7 implementation and php-di/slim-bridge

@igorsantos07
Copy link
Author

@igorsantos07 igorsantos07 commented May 4, 2020

While upgrading to 4 wasn't that easy for us because the Upgrade Guide is missing info, I should praise the past versions: I'm actually migrating an ancient application from Slim 1.6 all up to 4 (so we can get PHP 7, look at that), and 1>2 and 2>3 were pretty smooth :)

@JustSteveKing
Copy link

@JustSteveKing JustSteveKing commented May 21, 2020

I think one of the problems is that Slim 4 was a paradigm shift, moving to a move flexible but PSR compliant application which is a fantastic move for the framework however, it does not leave problems. Capturing every BC in this scenario is obviously going to be quite difficult, and a few mising the net is not a bad thing - we are all human!

Also from Slim 3 to Slim 4 the project lead has swapped hands, so patience while new hands get a grip on their direction is needed.

I wouldn't call this an issue, move of a "should have made a note of that". The functionality is still available in the Slim-Http package, so I would vote to close this issue

@l0gicgate l0gicgate closed this Nov 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants