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

Redirection form Micro application to Phalcon controller Not working

Im trying a application with phalcon and all the operations by web service .So Using Micro application for RestFul services.

See below my code: Usertypecontroller.php

public function createAction() { $ch = curl_init();

curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST); curl_setopt($ch,CURLOPT_URL,'https://localhost/xxx/yyy/api/usertype'); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE); $data = curl_exec(); curl_close($ch); print_r($data); // Here not printing the output!! $this->dispatcher->forward(array( 'controller' => "usertype", 'action' => 'index' )); }

myrestAPI index.php file

$app->post('/api/usertype', function () use ($app) { $usertype = new Usertype(); $usertype->name = $this->request->getPost("name"); $usertype->status = $this->request->getPost("status"); $usertype->createdon = $this->request->getPost("createdon"); $usertype->updateon = $this->request->getPost("updateon"); if (!$usertype->save()) { foreach ($usertype->getMessages() as $message) { $this->flash->error($message); } return; } // Create a response $response = new Response();

// Check if the insertion was successful
if ($usertype->save() == true) {

    // Change the HTTP status
    $response->setStatusCode(201, "Created");

    $usertype->user_type_id = $usertype->user_type_id;

    $response->setJsonContent(
        array(
            'status' => 'OK',
            'data'   => $usertype
        )
    );

} else {

    // Change the HTTP status
    $response->setStatusCode(409, "Conflict");

    // Send errors to the client
    $errors = array();
    foreach ($status->getMessages() as $message) {
        $errors[] = $message->getMessage();
    }

    $response->setJsonContent(
        array(
            'status'   => 'ERROR',
            'messages' => $errors
        )
    );
}    

return $response;

});

Using Curl the the values are posting fine and inserting but its not redirecting to my controller and not showing my index page .it jut stops at my Curl and displaying

{"status":"OK","data":{"user_type_id":"71","name":"zczxc","status":"1"}} I have tried with CURLOPT_RETURNTRANSFER and CURLOPT_FOLLOWLOCATION configs but nothing works.Anybody can help me would be grateful.Thanks

You are forwarding and not redirecting. I think you cant forward from micro application to regular application, try to use redirect instead

// Redirect to the default URI
$response->redirect();

// Redirect to the local base URI
$response->redirect("posts/index");

// Redirect to an external URL
$response->redirect("https://en.wikipedia.org", true);

// Redirect specifying the HTTP status code
$response->redirect("https://www.example.com/new-location", true, 301);

Ok but How to redirect with my response data? it seems very confusing

edited Apr '16

You have to put it to query params

$response->redirect("https://www.example.com/new-location?status=OK&data[usertypeid]=71&...", true, 301);
edited Apr '16

Not working!! Here is my code:

$app->post('/api/usertype', function () use ($app) { $usertype = new Usertype(); $usertype->name = $this->request->getPost("name"); $usertype->status = $this->request->getPost("status"); $usertype->createdon = $this->request->getPost("createdon"); $usertype->updatedon = $this->request->getPost("updatedon");

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

        return;
    }
 // Create a response
$response = new Response();
if ($usertype->save() == 1) {
    response->redirect("../xxxyyzzz/usertype/create?status=OK&data[".$usertype->user_type_id."]", true, 301);

}else{
    $response->redirect("../xxxyyzzz/usertype/create?status=ERROR", true, 301);
}
});

Try ti send that response or return it? Does it return some error or what happens?

$response->send();

// or

return $response;