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

Need help with Tutorial 3 - Creating a simple REST API

I am an uber-noob but I've managed to get Phalcon and accompnaying PHP services setup and running.

Based on the demo, I am confused of what info goes where in the file structure. It seems the Index.php sets up the basic structure for a call:

<?php
$app = new \Phalcon\Mvc\Micro();
//define the routes here
//Retrieves all sites
$app->get('/api/sites', function() {
});
$app->handle();
?>

With this code I can get a correct response (HTTP/1.1 200 OK) if I run "curl -i -X GET https://localhost/my-rest-api/api/sites" but I get no actual results.

In a model I setup all the database connections needed to actually search my DB for sites (instead of robots) but how do I get the index.php to call my model?

Do I place all the DB info and actual code in the index.php? Or is that used to setup the framework? Is there an example index.php from the tutorial I can look at?

I am sorry I am so clueless but I will take any suggestions or answer any questions you need. Thank you for your time!



43.9k
edited May '14

Hi,

$app->get('/api/sites', function() { echo "hello"; });

have a look at this github phalcon's micro app: https://github.com/phalcon/dasshy

in public folder, index.php start the micro app and register all the services that have been set in config/services.php (dasshy use mongoDb access, but also views, flash ...), then the routes are loaded using handlers.php (where most of the business logic is).



1.3k

Thank you for the reply! I did that and I get this as reponse:

HTTP/1.1 200 OK Date: Tue, 27 May 2014 22:28:35 GMT Server: Apache/2.2.15 (CentOS) X-Powered-By: PHP/5.3.3 Content-Length: 0 Content-Type: text/html; charset=UTF-8

It doesn't seem to be triggering the ECHO command. I checked my 'HTTPD\errorlog' but I see no obvious Phalcon errors. Any thoughts on why the ECHO command isn't triggering? Here is my index.php:

<?php

use Phalcon\Mvc\Model,
    Phalcon\Mvc\Model\Message,
    Phalcon\Mvc\Model\Validator\InclusionIn,
    Phalcon\Mvc\Model\Validator\Uniqueness;

// Use Loader() to autoload our model
$loader = new \Phalcon\Loader();

$loader->registerDirs(array(
    __DIR__ . '/var/www/html/my-rest-api/models/'
))->register();

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

$app->get('/api/sites', function() {
echo "hello";
});

?>


1.3k

Ok I am getting the following error now:

[Tue May 27 15:47:14 2014] [error] [client ::1] PHP Fatal error:  Cannot instantiate abstract class Phalcon\\Mvc\\Model in /var/www/html/my-rest-api/index.php on line 12

I will investigate this some more.

i made a simple rest base on phalcon rest.. you can refer to this... https://github.com/hartjo/phalcon-restapi-rain