tealou's avatar

PSR7 - how does it work with Lumen?

How do I use PSR7 with Lumen and access the HTTP request data?

My code:

$app->bind('Psr\Http\Message\ServerRequestInterface', function ($app) {
    return (new Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory)->createRequest($app->make('request'));
});

$app->get('hello/{name}', function (Psr\Http\Message\ServerRequestInterface $request) {
    $name = $request->getAttribute('name');
    var_dump($name);
});

And then in my URL:

http://example.com/public/hello/world

So with $name = $request->getAttribute('name'); I should get world but I get NULL instead.

Any ideas?

0 likes
9 replies
bobbybouwmann's avatar

You get the variable as a second parameter in your function

$app->get('hello/{name}', function (Psr\Http\Message\ServerRequestInterface $request, $name) {
    var_dump($name);
});
pmall's avatar

As mentioned above it is not a request parameter but a route parameter so you get it as a parameter of your callback function.

tealou's avatar

It totally works differently from Slim 3 and Zend Expressive!

I don't need a second parameter (which does not look neat) but only $name = $request->getAttribute('name');

So which one does it correctly then??

pmall's avatar

It is a route parameter so you get it as an argument of your action callback. If for some reason you want to retrieve it like this pass it as an url query parameter.

tealou's avatar

ok. probably i should stick with Expressive or Slim then.

pmall's avatar

What is the problem about this ?

tealou's avatar

I thought it should it works to comply with PSR 7.

pmall's avatar

I thought it should it works to comply with PSR 7.

I dont think psr7 specify how route parameter should be handled. Does it ?

Please or to participate in this conversation.