We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Sending JSON to controller

I am trying to send some JSON to my controller using angular's http post method. The following json gets sent to the orderAction in my controller:

{"food":"pizza","drink":"beer"}

in my controller i tried receiving the request like this:

public function orderAction($data)
{
    $this->view->disable();
    if ($this->request->isPost()) {
        if (isset($data)) {
            echo $data;
        } else {
            echo "No data received";
        }
    }
}

The $data object is always null and i keep getting my 'No data received response'. Which brings me to the following question: how do i access the sent json data in the controller?



16.3k
edited Jun '14

Maybe you should catch the POST method with request->getPost() or request->get()

public function orderAction()
{
    $this->view->disable();
    if ($this->request->isPost()) {
        $data = $this->request->getPost();
    }
}

in Phalcon MVC, parameters to the action (method) are part of the uri string by default. eg:

localhost/controller/order/shoe/black

will pass two parameters to orderAction. assummed the action is orderAction($par1, $par2) so it will pass shoe as par1 and black as par2



12.1k
edited Jun '14

I already tried using request->getPost(), it returns me an empty array. E.g.:

public function orderAction()
{
    $this->view->disable();
    if ($this->request->isPost()) {
        $data = $this->request->getPost();
        echo print_r($data);
    }
}

Response:

Array
(
)
1

Also note that i am using angular's $http. I'm just filling up the data object:

$http.post("order", {"food": $scope.food, "drink": $scope.drink})
    .success(function (data, status, headers, config) {
        console.log("Your order was placed successfully.");
    }).error(function (data, status, headers, config) {
        console.log("Failed to place your order.");
    });


12.1k
Accepted
answer

I found the problem, it's php related. I was able to retrieve the data using:

  public function orderAction()
  {
        $data = file_get_contents("php://input");
        $data = json_decode($data, TRUE);
    }


1.1k
edited Jan '16

i am using angular in my project. i am having this code but the data is not inserted. /**

/**
 * Creates a new student
 */
public function createAction()
{
    $this->view->disable();
     if (!$this->request->isPost()) {
         return $this->dispatcher->forward(array(
             "controller" => "student",
             "action" => "index"
         ));
     }

    $student = new Student();

    $student->name = $this->request->getPost("name");
    $student->age = $this->request->getPost("age");
    $student->email = $this->request->getPost("email");
    $student->address = $this->request->getPost("address");

    if (!$student->save()) {
        foreach ($student->getMessages() as $message) {
            $this->flash->error($message);
        }

        return $this->dispatcher->forward(array(
            "controller" => "student",
            "action" => "new"
        ));
    }

    $this->flash->success("student was created successfully");

    // return $this->dispatcher->forward(array(
    //     "controller" => "student",
    //     "action" => "index"
    // ));
}

    and the post  in firebug in this form
    {"student":{"name":"jay","age":"22","email":"[email protected]","address":"delhi"}}

    how to solve
edited Feb '16

See the OP's accepted answer.

You need to read from php://input

Phalcon does support this through two options.

/** $request->getRawBody();

or, even better

/** $request->getPut('fieldname')

i am using angular in my project. i am having this code but the data is not inserted. /**

/**

  • Creates a new student */ public function createAction() { $this->view->disable(); if (!$this->request->isPost()) { return $this->dispatcher->forward(array( "controller" => "student", "action" => "index" )); }

    $student = new Student();

    $student->name = $this->request->getPost("name"); $student->age = $this->request->getPost("age"); $student->email = $this->request->getPost("email"); $student->address = $this->request->getPost("address");

    if (!$student->save()) { foreach ($student->getMessages() as $message) { $this->flash->error($message); }

       return $this->dispatcher->forward(array(
           "controller" => "student",
           "action" => "new"
       ));

    }

    $this->flash->success("student was created successfully");

    // return $this->dispatcher->forward(array( // "controller" => "student", // "action" => "index" // )); }

    and the post in firebug in this form {"student":{"name":"jay","age":"22","email":"[email protected]","address":"delhi"}}

    how to solve