0

I am refactoring my application using PSR-7 Requests using Slim3 as Router. On some of my entry points I have incoming GET and POST HTTP-Requests from external applications. Both GET and POST use the same parameter names. In the old code, a simple $_REQUEST solved the issue, but I do not want to use the superglobals any longer.

For getting the parameters of the GET-Request, I use the following code:

$parameters = $request->getQueryParams();

For the POST-Request, I use:

$parameters = $request->getParsedBody();

Is there a PSR-7 function for solving the issue, or do I have to use array_merge() each time?

Trendfischer
  • 7,112
  • 5
  • 40
  • 51
  • Please show us what the methods you have written actually look like. – Martin May 10 '16 at 14:21
  • @Martin The question is not specific for a special implementation. Usually you have a callback like `function ($request, $response) { /* do some stuff */ }`, see the Slim3 docs for more information. It is a very common pattern for microframeworks. – Trendfischer May 10 '16 at 14:37
  • PSR-7 does not suggest or even consider using `$_REQUEST`. – Daniel W. May 10 '16 at 15:06
  • @DanFromGermany Sorry, it seems my question is not very clear. I do not want to use `$_REQUEST` either, that is the reason for this question. But having to deal with URLs with no clear definition of using GET or POST in a request is a common problem, at least in my experience and especially on older sites. – Trendfischer May 10 '16 at 15:45

2 Answers2

4

Slim's request object has getParam() and getParams() which does what you want. These are not part of PSR-7 though.

Rob Allen
  • 12,643
  • 1
  • 40
  • 49
2

In PSR-7 itself there is not a method on the request interface to do what you are asking for.

It could be possible that some implementation provides it, but I would find it odd.

If you can, the easiest possibility could be to extend the ServerResponseInterface implementation that you are using with a new method that does what you need.

In Slim you could do this overriding the request configuration key, providing your own request object

marcosh
  • 8,780
  • 5
  • 44
  • 74
  • Thanks, just to be sure: I extend the ServerResponse-Class from Slim with a function and on initializing Slim, I set an empty `request` in the container and slim uses this instance to parse the HTTP request? – Trendfischer May 10 '16 at 15:03
  • not exactly empty, you need to pass to it some arguments when you construct it. Have a look on how Slim itself does it in its `Container.registerDefaultServices` method – marcosh May 10 '16 at 15:07
  • I believe you mean the class `DefaultServicesProvider`. By reading the slim3 source, I found, that slim implements a non psr-7 method called `createFromEnvironment`, so I could inherit `\Slim\Http\Request` and can configure an injection container using my class. But it could lead to naming conflicts on future slim versions. I fear, I will stick to my `array_merge()`, but thanks. – Trendfischer May 10 '16 at 15:59
  • sorry, I have an older version on Slim (3.1), so the method is in another place... anyway, however you'll decide to proceed, try to isolate you behaviour not to violate the DRY principle (another, less elegant, option could be having a static method in a Utils class, for example...) – marcosh May 10 '16 at 16:09
  • Yes, a static method is what I thought to implement, too. But thankfully, @RobAllen wrote in his answer, that there is already a proper `getParams()`-method in the slim request object. But being a core developer of slim has certainly some advantages, I guess ;-) – Trendfischer May 10 '16 at 16:30