having a bit of trouble working out the best way to do things. I have a normal form, this form already has an action and a data-url
<form action="{{ path('NickAlertBundle_create') }}" data-url="{{ path('NickAlertBundle_view') }}" method="post" enctype="multipart/form-data">
When the form is submitted, it goes to a javascript file and ajax takes care of it
$.ajax({
type: "POST",
url: $("#addAlertForm").attr("action"),
data: data,
success: function(data) {
if(data){
var splitdata = data.split(":");
if(splitdata[0]=="Success"){
var id = splitdata[1];
alert("Your alert has been added");
document.location.href= $("#addAlertForm").attr('data-url');
}else{
alert(data);
}
}else{
alert("Unknown Error!");
}
},
error:function(){
alert("Error! Please try again");
}
});
So it essentially post some data to my forms action route. If it comes back with the message Success, it redirects the user to my data-url route.
So this all works fine. In my createAction I insert the data to the database and if this is successful I return Success. The user is then redirect to the view-alerts (viewAction) page to see the data.
public function createAction(Request $request)
{
try {
//Get data
$em = $this->getDoctrine()->getManager();
$alert = new AvailabilityAlert();
$alert->setSearchCommand($na_command);
$alert->setLastUpdated();
$em->persist($alert);
$em->flush();
return new JsonResponse('Success');
}catch (Exception $e) {
}
}
public function viewAction()
{
$repository = $this
->getDoctrine()
->getManager()
->getRepository('NickAlertBundle:AvailabilityAlert');
$alerts = $repository->getAllActiveAlerts();
return $this->render('NickAlertBundle:Page:view.html.twig', array(
'alerts' => $alerts,
));
}
So this all works great, but now a problem. If the createAction is successful, I now need to do a load of other stuff which involves passing the alert to a new class so I can do some web services stuff with it (nothing is displayed to the user, everything is done in the background).
To do the above, I essentially new to add something like the following to the end of my create action
include 'cron_single.php';
addFlightsAction($alert);
Doing this seems wrong though. So I was thinking about creating a separate action for this, I think this would be the correct way to do this. So in the ajax above, if the response is Success (where I do the redirect), I could potentially call another route. To do this though, I would have to give my form something like another data-url so I can get the route, and having so many of these in my form seems incorrect.
Another thing I am thinking of is doing something directly at the end of the createAction, such as call a route. Don't know if this is possible?
What would be the best way to handle something like this?
Thanks