In discussions with @macnibblet there is a need to be able to pass arbitrary parameters to the Resource object, and the events it triggers. As examples:
For child resources (e.g., /someresource/:id/children/:id), you may want to (a) validate that the parent exists, and (b) pass the parent resource to the child.
You may want to pass arbitrary query string parameters to the resource (e.g., ?from=2010-01-01&to=2011-01-01).
To facilitate this, I propose adding the ability to add/set "event parameters" on the Resource object. These would then be passed as parameters to any events triggered by the Resource object.
This will also require adding a getResource() method on the ResourceController so that developers can access the Resource object.
The above would inject selected query string parameters as event parameters in the resource.
$events->attach('dispatch', function ($e) use ($resourceManager) {
$routeMatch = $e->getRouteMatch();
$parentId = $routeMatch->getParam('parentId', false);
if (!$parentId) {
// raise an error or return an ApiProblem
}
if (!$resourceManager->has($parentId)) {
// raise an error or return an ApiProblem
}
$parent = $resourceManager->get($parentId);
$e->getTarget()->getResource()->addEventParam('parent', $parent);
}, 10);
The above would test for existence of a parent ID and valid parent ID, and then inject the resource with the parent object.
In discussions with @macnibblet there is a need to be able to pass arbitrary parameters to the
Resource
object, and the events it triggers. As examples:/someresource/:id/children/:id
), you may want to (a) validate that the parent exists, and (b) pass the parent resource to the child.?from=2010-01-01&to=2011-01-01
).To facilitate this, I propose adding the ability to add/set "event parameters" on the
Resource
object. These would then be passed as parameters to any events triggered by theResource
object.This will also require adding a
getResource()
method on theResourceController
so that developers can access theResource
object.As examples of usage:
The above would inject selected query string parameters as event parameters in the resource.
The above would test for existence of a parent ID and valid parent ID, and then inject the resource with the parent object.