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

Creating an API controller

I am trying to create an api controller for my application. The general idea is based on the post data I have a parent class I look up inthe database and create a new child and save it. This works well initally but if there is a second post the response is extremely slow.


class ApiController extends ControllerBase
{
    public function initialize()
    {
        $this->tag->setTitle('API');
        parent::initialize();
    }

    public function startAction(){
        $this->view->disable();
        $this->setJsonResponse();
        if($this->request->isPost()){
            $data = $this->request;
            if($data != null){
                $json = file_get_contents('php://input');
                $obj = json_decode($json);
                $id = $obj->parent_id;
                $message = "message: " . $obj->parent_id;
                $conditions = "serialid = :serialid:";
                $parameters = array(
                    "serialid" => $id
                );
                $parent = Parent::findFirst(array(
                    $conditions,
                    "bind" => $parameters));
                if($parent != null){
                    $child = new Child();
                    $child->parent_id = $parent->id;
                    $child->uuid = $obj->event_id;
                    $child->timestamp = date("Y-m-d H:m:s", $obj->simestamp);
                    if ($child->save() == false) {
                        return json_encode(array("result" => "ERROR: failed Attempt"));
                    } else {
                        return json_encode(array("result" => "SUCCESS"));
                    }
                }else{
                    return json_encode(array("result" => "ERROR: parent not found" , "Message" => $message));
                }

            }else{
                return json_encode(array("result" => "ERROR: No data received"));
            }
        }else{
            return json_encode(array("result" => "ERROR: Needs to be Post"));
        }
    }

}


8.1k
class ApiController extends ControllerBase
{
    protected $content;

    public function initialize()
    {
        $this->tag->setTitle('API'); //you don't use view -> delete
        parent::initialize();
        $this->response->setContentType('application/json', 'utf-8');
    }

    public function afterExecuteRoute()
    {
        $this->response->setJsonContent($this->content);
        $this->response->send();
    }

    public function startAction(){
        $this->view->disable();
        $this->setJsonResponse(); // may be $this->response->setContentType('application/json', 'utf-8'); see up
        if($this->request->isPost()){
            $data = $this->request; // this variable always is not null 
            if($data != null){ // delete this block -> always true
                $json = file_get_contents('php://input'); // check $json to null (if ($json))
                /**
                * The better use $data = $this->request->getJsonRawBody() instead next 2 strings and prev 1
                **/
                $obj = json_decode($json);
                $id = $obj->parent_id;
                $message = "message: " . $obj->parent_id;
                $conditions = "serialid = :serialid:";
                $parameters = array(
                    "serialid" => $id
                );
                $parent = Parent::findFirst(array(
                    $conditions,
                    "bind" => $parameters));
                if($parent != null){ // just if ($parent)  
                    $child = new Child();
                    $child->parent_id = $parent->id;
                    $child->uuid = $obj->event_id;
                    $child->timestamp = date("Y-m-d H:m:s", $obj->simestamp);
                    if ($child->save() == false) {
                        return json_encode(array("result" => "ERROR: failed Attempt")); // change to set content
                        /**
                        * $this->content = ...
                        **/
                    } else {
                        return json_encode(array("result" => "SUCCESS"));
                    }
                }else{
                    return json_encode(array("result" => "ERROR: parent not found" , "Message" => $message));
                }

            }else{
                return json_encode(array("result" => "ERROR: No data received"));
            }
        }else{
            return json_encode(array("result" => "ERROR: Needs to be Post"));
        }
    }

}