I want to access multiple query parameters in the URL, please refer to the screenshot.
In the corresponding resource file it gets mapped on the fetch($id) method. I can see only one parameter as expected, how do I access the other parameters as given in the screenshot GET method. If someone wants to know why do I need multiple parameters ? The answer is - these multiple parameters collectively identifies an item in the db table.
hope you 're doing well. It’s nice to see that you’re making progress with the API tools. Since resources are event driven and follow a specific pattern, it 's not a good practice to handle more than one identifier in a fetch method, which is designed to return a specific dataset. From the point of view of the consideration, in your case you rather execute a fetchAll with filter parameters.
You can set filter parameters with the doctrine query builder, that do exactly what you want. So you don’t have to reprogram this filter for each resource. It works directly for all resources because of the event driven system of the query builder.
Nevertheless you can archive this functionality on your own. Let us have a look at the \Laminas\ApiTools\Rest\AbstractResourceListener.php file in line 182 and following.
As you can see the fetchAll method retrieves all query params so that you can use it in your implementation. This leads us to your query. Assuming it is formed like
In the given query we got two ids 22 and 33. In your resource you can access them as in the following example.
public function fetchAll($params = [])
{
$ids = $params['id']; // array with the given id 's in the query
...
}
Of course, you can also use the query parameters in all other methods of your resource.
public function fetch($id)
{
$queryParams = $this->event->getQueryParams() ?: [];
$ids = [];
if (isset($queryParams['id'])) {
$ids = $queryParams['id']; // array with the given ids from the query
}
}