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

$request->getAttribute('id') is returning null #76

Closed
reiniermybeats opened this issue Nov 18, 2022 · 2 comments
Closed

$request->getAttribute('id') is returning null #76

reiniermybeats opened this issue Nov 18, 2022 · 2 comments

Comments

@reiniermybeats
Copy link

reiniermybeats commented Nov 18, 2022

In the readme example Controllers as a services:

class UserController
{
    private $userRepository;
    
    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function delete($request, $response)
    {
        $this->userRepository->remove($request->getAttribute('id'));
        
        $response->getBody()->write('User deleted');
        return $response;
    }
}

$app->delete('/user/{id}', ['UserController', 'delete']);

it show to get the route attribute id using $request->getAttribute('id') but if i use this method i get null returned.

my route is created like this:

$app->post('/my/{name}', MyController::class)->setName('my-name');

and the controller looks like this:

public function __invoke(ResponseInterface $response, ServerRequestInterface $request): ResponseInterface
{
    $name = $request->getAttribute('name');

The only way to get the name argument in my case is like this:

$attributes = $request->getAttributes();
$arguments = ($attributes["__route__"])->getArguments();
$name = $arguments['name'];

I understand I can also get it like this, so it's injected from the __invoke method parameters:

public function __invoke(ResponseInterface $response, ServerRequestInterface $request, $name): ResponseInterface
{
    $test = 'My name is ' . $name; // 

But the readme makes me believe the other way is also possible, somehow.

Is the readme wrong or is there some way to achieve this?

@reiniermybeats reiniermybeats changed the title $request->getAttribute('id') (as seen in the readme) is returning null $request->getAttribute('id') is returning null Nov 18, 2022
@juherr
Copy link
Contributor

juherr commented Jun 29, 2023

Hi,

I have the same issue.
Any update about it?

juherr added a commit to juherr/Slim-Bridge that referenced this issue Jun 29, 2023
@KevinMarques
Copy link

KevinMarques commented Dec 5, 2023

Is this really correct @mnapoli?

As stated in the Slim official documentation:

With PSR-7 it is possible to inject objects/values into the request object for further processing. In your applications middleware often need to pass along information to your route closure and the way to do it is to add it to the request object via an attribute.

So you can for example have a middleware which sets in the request an attribute called "user" and have also a route with a "user" named argument, causing PHP-DI Slim bridge to override the original attribute value.

The Slim documented way to access route parameters is with $route->getArgument('id') function, so maybe the original issue here was the PHP-DI slim bridge documentation indicating a missuse of getAttribute function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants