class MyAction {
protected $ci;
//Constructor
public function __construct(ContainerInterface $ci) {
$this->ci = $ci;
}public function __invoke($request, $response, $args) {
// Return Empty Array. $attr = $this->ci->get('request')->getAttributes(); var_dump($attr); // Return Array of Attributes. $attr = $request->getAttributes(); var_dump($attr); }
}
I don’t understand why the following are not working. ( I used the doc code for exemplification, the real code is not here but the problem is the same )
$attr = $this->ci->get(‘request’)->getAttributes()
The getAttributes method was supposed to return array of attributes about the route but using the ContainerInterface it return an empty array.
Using var_dump($attr) on it this what it shows:
array(0) {
}
Now using var_dump($request->getAttributes()) it shows all the attributes that should show. ( Not going to post here because is a lot of things ).
The point is that I wanted to access a specific attribute with getAttribute() ( $this->ci->get(‘request’)->getAttribute(‘routeInfo’) ) using the ContainerInterface and not the function($request,$response,$args).
What is wrong with my approuch ?
Note:
-
The method getParams() , getBody() works perfectly fine with ContainerInterface.
-
$this->ci->get(‘request’)->getParams() - Works
-
$this->ci->get(‘request’)->getBody() - Works
-
$this->ci->get(‘request’)->getAttributes() - Don’t work