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

Phalcon simple REST API tutorial issue

I'm trying to make this simple tutorial work on my server but I'm facing a problem which I don't know how to solve.

Suggested directory api is created and only index.php and .htaccess files.

Content of .htaccess file is the same as in tutorial but index.php is altered so it looks like this:

<?php
    $app = new Phalcon\Mvc\Micro();

    $app->get('/api/robots', function() use ($app){
        $response->setJsonContent(array('status' => 'OK', 'messages' => 'This works'));    
        return $response;
    });    

    $app->handle();

?>

But when I try to send GET request to address:

         https://SERVER_IP_ADDR/test-api/api/robots  

using hurl.it and I get the following response:

404 Not Found       297 bytes       175 ms

HEADERS

Connection: close
Content-Length: 297
Content-Type: text/html; charset=iso-8859-1
Date: Fri, 27 Mar 2015 17:44:46 GMT
Server: Apache/2.2.15 (CentOS)
BODY view raw

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
  <head>
    <title>404 Not Found</title>
  </head>
  <body>
    <h1>Not Found</h1>
    <p>The requested URL /test-api/api/robots was not found on this server.</p>
    <hr>
      <address>Apache/2.2.15 (CentOS) Server at SERVER_IP_ADDRESS Port 80</address>
    </body>
</html>

I must note that my apache configuration file is a default one because I assume that the catch lies somwhere here.

Is mod rewrite enabled on your machine? I assume your index.php file is in test-api folder.

edited Mar '15

It is enabled now, but still it's not working. I get the following error:

500 Internal Server Error       0 bytes       173 ms

Connection: close
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Date: Sat, 28 Mar 2015 09:26:45 GMT
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.3

According to the last line of response, I guess that php is now handling the request.

And yes, index.php is in test-api folder.



3.5k

Seems to be a problem within your Code, PHP- or Phalcon-Installation. Turn on you PHPs error_reporting and look at your php-logs

Try adding some output and exit in the front of your index.php. i.e.

<?php
echo "Success";
exit;
//...

If the request of https://SERVER_IP_ADDR/test-api/api/robots now returns "Success", PHP is up and running.

then try

<?php

$app = new Phalcon\Mvc\Micro();

echo "Success";
exit;
//...

If the request of https://SERVER_IP_ADDR/test-api/api/robots now returns "Success", Phalcon is up and running, too.

edited Mar '15

500 Internal Server Error, is mistake of your code, try enable your display_errors in your php.ini file, also put up try catch in yout bootstrap file, or (new \Phalcon\Debug)->listen();

edited Mar '15

I had a problem with Phalcon installation. When I've fixed it, both echos work. But, it seems that nothing is beeing send within function

$app->get('/api/robots', function(){
    ...
}); 

you try to create project with phalcon dev-tools, do you have installed phalcon dev tools?, with this tool you can create the project without problems to htaccess.