Simulating a form request in Symfony for special forms and API submissions

One scenario that may come up is that you have an API on your website used to handle different form submissions from other sites. Having those forms outside of your site may be a reason your team is apprehensive to update your website to Symfony. How are you going to migrate those forms relying on your API to handle the incoming data from these old forms?

Well, unless your data requirements have changed, you have the option of leaving those external forms alone. You might want to leave those legacy forms where they are and in Symfony build a means to receive the same data these forms were always sending but translate the data and refactor those form fields to match the requirements of your Symfony form controller. It’s just matching form fields to their new form names and transforming the data to conform, then sending the new request object off to the same controller that handles the Symfony form!

// In your API controller, translate the legacy form data to conform to your Symfony form controller:
$requestTranslated = Request::create(
    '/the-route-coming-from',
    'POST',
    array('formField1' => 'value1','formField2' => 'value2')
);

// Now send new request object to the same form controller your Symfony form uses:
return $this->controllerAction($requestTranslated);

Done!

About Author:

Senior Cloud Software Engineer and 25+ years experienced video production, video editing and 3D animation services for a variety of global clients including local video production here in Jacksonville, Florida.

Leave a Comment

Your email address will not be published. Required fields are marked *