Is it possible to access controller plugins from a form/controller factory (any factory implementing FactoryInterface)?
I have a form factory that i want to set the form action depending on the request parameter, but need to access the url from the route defined in config.
So whereas in a controller i would use the url controller plugin:
$form->setAttribute('action', $this->url()->fromRoute('appointment.add', array('clientId' => $clientId)));
...how can i access this in a factory? eg something like:
class MyFormFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator){
$serviceManager = $serviceLocator->getServiceLocator();
//...snip...
$form = new AddAppointmentForm($client);
$serviceManager->get('ControllerPluginManager');
$url = $controllerPluginManager->get('Url');
die($url->fromRoute('appointment.add', ['clientId' => $clientId]));
return $form;
}
It would not be good practice to use controller plugins in a non controller context.
To assemble an URL with the ZF2 router you could just use the router which is also available in the ServiceManager
.
$router = $serviceManager->get('HttpRouter');
$url = $router->assemble(['clientId' => $clientId], ['name' => 'appointment.add']);