0

I'm currently working on a Symfony2 project where a user can comment on parts of a webpage via contenteditable spans. I now want to save these comments, that are assembled in an array of JSON objects, into the database, with a reference to the page and the user id. This is where I'm stuck.

What steps are needed to put the data from the JavaScript code into the specific DB table?

I already created a Comment class in the Entity folder. And based on this answer I added the following code in my saveComment() in JavaScript:

$.post('saveComments.php', {
    page_id : getPageId(),
    user_id : getUserId(),
    comments : getJSONcomments(),
});

What next...?

Ok, let's create a saveComments.php ... but where in the bundle? Or could this be done with a Controller? If so, how and what would I need to replace the url ("saveComments.php") in the $.post(...) call with?

1 Answer 1

0

Don't post to a PHP page as this breaks the Symfony 2 model. Instead post to a route, like save/comment or along these lines.

Configure the route to point to an action in a controller.

Implement the controller and the action so that it does not take any parameters. Inside the action unpack the posted data the usual PHP way using (because you won't have a form to bind your data to):

$_POST

Just to get it working, echo a var_dump() to the console and see what you get. This is an example on how to write to the console.

Decode the JSON data with the serializer.

The decoded data will be a simple associative PHP array. Interpret the data you received and act accordingly (don't forget to handle security and all that stuff, too -- you don't want to open up security holes through AJAX).

Best is, you check that the route you chose falls into the security tier you need and probably already have configured in the Symfony 2 application. This way you don't need to handle security manually in the action.

Once this is done return an HTTP response with code 200 OK.

return new Response('', Response::HTTP_OK); // no response text at all

If there's any error reply with a 500 class HTTP server error:

return new Response('', Response::HTTP_INTERNAL_SERVER_ERROR); // 500

Also, don't forget to handle client-side errors with the .error() method.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for the fast answer! I need more help with getting the route right as there's no output at all from the echo and var_dump() from the action in the controller. In the controller I created the function saveComment() with @Route("/saveComment", name="saveComment"). In JavaScript $.post(...) the url is now "/saveComment". Interesting enough there is no error. Any suggestions on what I should do differently?
I've edited the answer pointing to another one here on SO where it is explained how to write to the console. You cannot var_dump() in a JSON response as there's no HTML output to render. You forfeit any output when you do return new Response(...). But this is exactly how you want the response to work.
Also, if you use Chrome or Firefox press F12 and open the Network tab. Then press F5 and click whatever you need to see the AJAX response fired. In the inspector, you'll see the reply which might be a Symfony 2 exception dump.
The route was correct - just discovered the output in the Network tab. That helps a lot, thx!
There's a new database entry - yay! Thank you so much for your detailed and clear answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.