Hi there !
I’m currently working on a legacy ZF2 app and I would really much like to simplify controller code. One way to do that would be, for example on a post request, to hydrate an object with the content of user data to be passed to the function.
By adding some custom key-values pairs in my route configuration and override the AbstractActionController::onDispatch
listener function so that
$actionResponse = $this->$method();
becomes
$actionResponse = call_user_func_array([$this, $method], $args]);
where $args would contain the result of the aforementioned hydration.
The controller function would then read
public function myAction(Purchase $purchase)
{
// Do something with data
}
instead of
public function myAction()
{
$amount = (double) $this->getRequest()->getPost('amount');
}
Casting could be done through the hydration process and controller code would benefit from an object with a proper name and data ready to be processed.
I’m surprised nothing in Zend exists to achieve this. There may be something I’ve missed !
The inspiration is clearly the ParamConverter feature in Symfony Framework.
Thanks in advance