2

I'm beginner to symfony. I have a twig template with 2 buttons that calls an external .js that executes an ajax call.

Button 1 calls function 'delete', and this is the js code:

    var path = $("#abc").attr("data-path");
 /*grabs it from some div in the twig template.. <div id="abc" data-path="{{path('delete')}}"></div>*/

    function delete(n){
        $.ajax({
          type: "POST",     
          url: path,
          data: {id : n},
          "success":function(data){
          alert('ok');
          }
          });
    }

Button 2 calls function 'edit' which is the same code except that the 'url' goes to another 'action', and the 'data' is not a json, (it is data: formData)

Routing.yml for function delete is:

    delete:
        pattern:  /delete
        defaults: {_controller: TPMainBundle:Default:delete }  

And this is the controller action:

public function deleteAction()
    {
 $id = $_POST['id']; 
 /*other code to work with doctrine making queries to delete from database*/ 
}

(The js code is from a webpage done without symfony and it works fine)

I was told the right way to retrieve the POST in the action, reglardless it was whether a json or formData, was using the same that I used in PHP:

 $id = $_POST['id']; 

Here, I have 2 problems.

First, I don't know if this is correct because it doesn't work.

Second, I don't know how can I know if i'm retrieving the POST OK !!

When I did this without symfony, I checked if I was getting the POST with the command 'fwrite', because the ajax went to a PHP file instead of an Action, and then with the command fwrite I created a .txt file with the output of an echo to see if the $_POST was recovered or not.

But here in symfony I don't know how to check it, so I'm driving myself crazy.. trying to implement the solutions I read without being sure if they work..

and with the extra problem that since I'm newbie for me it's a bit confusing trying to install some external bundles for debug. Please help

3 Answers 3

3

The correct approach for acessing post or get params is using Symfony's Request object. You can pass it as an argument of a controller action, or retrieve it from the controller directly. Here's two examples:

public function deleteAction(Request $request)
{
    if ($request->isMethod('POST')) {
        $id = $request->get('id');
    }
}

Without passing the Request as a parameter:

public function deleteAction()
{
    if ($this->getRequest()->isMethod('POST')) {
        $id = $this->getRequest()->get('id');
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

The code works if I don't pass Request as a parameter but, why doesn't it work with Request as a parameter? I'm not missing the use Symfony\Component\HttpFoundation\Request; Is there a special code if I have to get a formData which as a type File within ?
I updated the code to include the type casting. There's no additional code needed on this part, but if you are sending files through AJAX you may have a bit extra work. For this simple delete example, the code above is enough.
I get it, it was the typecasting the reason with Request didn't worked, thank you again !
0

Extra tip for people who comes from PHP and doesn't know how to check if they are retrieving the POST or not: - send it to a .php

public function deleteAction($request)
{  
    $id = SOME RETRIEVED CODE YOU AREN'T SURE YOU ARE RETRIEVING THE RIGHT WAY;

/*you can send this variable to a view, symfony allows you to use .php files but you have to name their extension as html.php not just .php*/

return $this->render('TPMainBundle:Default:test.html.php', array('id' => $id));

}

And then, in that view:

<?php

echo $id;

$myfile = fopen("somename.txt", "w") or die("Unable to open file!");
$txt = 'Id is: '.$id;
fwrite($myfile, $txt);
fclose($myfile);

This will generate a .txt So, you can open the .txt and check if you have recovered the DATA or not!

The file will be located inside the 'web' folder..

The other tips is to check the 'app/log/dev.log' thanks @Zain Saqer

Comments

0

In Symfony you get your POST data from the current Request object (That's how we do it in Symfony), one way to get the current Request object is by adding $request variable as first parameter in your action function.
Your action function could be like this:

public function deleteAction(Request $request)
{
    //check if our POST var is there
    if($request->request->has('id')){
        //it's there
        $id = $request->request->get('id');
        /*other code to work with doctrine making queries to delete from database*/ 
    }else{
        //it's not there!
    }

}

Regarding installing third party bundle, use Composer to do this task for you, and don't forget to add the class path of the installed bundle to $bundles array in AppKernal class. Follow this link to know how to do this.

9 Comments

Thank you Zain, but it's not working, I added use Symfony\Component\HttpFoundation\Request; and for checking I simplified by removing the 'if' and the 'else' clause, but the firebug says it's a 500 error message in the deleteAction.. thank you for the link !!! (sorry, I don't know why the </br> are not being shown in my answer)
Can you see where the error occurred in your code (class name and line number) from firebug?
Use double space instead of </br> for line break
Nothing, it just says: POST localhost/TP/web/app_dev.php/delete 500 (Internal Server Error)
Check response body in firebug to locate the error, Symfony error page contain these information.
|

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.