Hello.
I am building a API, and want to use controllers for my routes.
So a route will look like this:
$this->get('/account', 'MyController:foobar');
The MyController is extending a class called BaseController. In that file I have the following constructor:
class BaseController
{
private $request;
private $response;
public function __construct($app)
{
$this->request = $app->request;
$this->response = $app->response;
}
Now, I can use $this->response to return, and I can get user data with $this->request->getParsedBody().
But I cannot figure out how to access the variables in the URL: /foo/bar/{user_id}. Using functions we got the $args variable, but I don’t want to have to use ($request, $response, $args) in every function in my controllers, if I can get access to it on the BaseController.
From reading the manual I thought I could use the getAttribute-function on the request-object, but no…
$route = $app->request->getAttribute('route');
This returns NULL. getAttributes returns an empty array.
What am I doing wrong here? Am I going about this the wrong way?
Thanks!