how to make api with micro phalcon ???

I tried to make a fire with micro Phalcon but always error 500 can please help me to give a tutorial ???

Did you read docs? I will suggest you to spend 1-2 hours on reading the whole docummentation. This will give you brief knowledge of all functionalities.

Rest api tutorial is here: https://docs.phalconphp.com/en/latest/reference/tutorial-rest.html



8.1k

try to check apache error log and you will know how to fix it



67.7k

what is the reason people put more than 1 question / exclamation mark everywhere they can ?

To create urgency and to tell us that we need to answer immediately :)

what is the reason people put more than 1 question / exclamation mark everywhere they can ?

Did you read docs? I will suggest you to spend 1-2 hours on reading the whole docummentation. This will give you brief knowledge of all functionalities.

Rest api tutorial is here: https://docs.phalconphp.com/en/latest/reference/tutorial-rest.html

I've read it and already I follow like in the doc, but when I want a connection to the database is always error 500, can you help me?

Did you check your server error logs, like @xeleniumz suggested? We can't help you debug your code without showing us error and problematic code.

edited Oct '16

this is my script

<?php

use Phalcon\Loader;
use Phalcon\Mvc\Micro;
use Phalcon\Di\FactoryDefault;
use Phalcon\Db\Adapter\Pdo\Mysql as PdoMysql;

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

$loader->registerNamespaces(
    [
        "Store\\Toys" => __DIR__ . "/models/",
    ]
);

$loader->register();

$di = new FactoryDefault();

// Set up the database service
$di->set(
    "db",
    function () {
        return new PdoMysql(
            [
                'adapter'     => 'Mysql',
                'host'        => 'localhost',
                'username'    => 'root',
                'password'    => '',
                'dbname'      => 'api',
                'charset'     => 'utf8',
            ]
        );
    }
);

// Create and bind the DI to the application
$app = new Micro($di);

$app->get(
    "/api/robots",
    function () use ($app) {
        $phql = "SELECT * FROM Store\\Toys\\Robots ORDER BY name";

        $robots = $app->modelsManager->executeQuery($phql);

        $data = [];

        foreach ($robots as $robot) {
            $data[] = [
                "id"   => $robot->id,
                "name" => $robot->name,
            ];
        }

        echo json_encode($data);
    }
);

$app->handle();

and my error when I turn display error php

Fatal error: Uncaught Phalcon\Mvc\Micro\Exception: Not-Found handler is not callable or is not defined in /var/www/html/api/index.php:61 Stack trace: #0 /var/www/html/api/index.php(61): Phalcon\Mvc\Micro->handle() #1 {main} thrown in /var/www/html/api/index.php on line 61

please help me

What do you think about this Fatal error of yours? I think it is very clear what is the problem with your app.



67.7k

sounds a bit like wrong route ( i am guessing )

First, don't show us your password to database. Second you access /api/robots url ? If yes then the problem is about your server configuration like nginx/apache. Show your conf file associated with this vhost/location.

ITs Clear, from docs:

Not-Found Handler¶ When a user tries to access a route that is not defined, the micro application will try to execute the “Not-Found” handler.

this is my script

<?php

use Phalcon\Loader;
use Phalcon\Mvc\Micro;
use Phalcon\Di\FactoryDefault;
use Phalcon\Db\Adapter\Pdo\Mysql as PdoMysql;

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

$loader->registerNamespaces(
   [
       "Store\\Toys" => __DIR__ . "/models/",
   ]
);

$loader->register();

$di = new FactoryDefault();

// Set up the database service
$di->set(
   "db",
   function () {
       return new PdoMysql(
           [
               'adapter'     => 'Mysql',
               'host'        => 'localhost',
               'username'    => 'root',
               'password'    => '',
               'dbname'      => 'api',
               'charset'     => 'utf8',
           ]
       );
   }
);

// Create and bind the DI to the application
$app = new Micro($di);

$app->get(
   "/api/robots",
   function () use ($app) {
       $phql = "SELECT * FROM Store\\Toys\\Robots ORDER BY name";

       $robots = $app->modelsManager->executeQuery($phql);

       $data = [];

       foreach ($robots as $robot) {
           $data[] = [
               "id"   => $robot->id,
               "name" => $robot->name,
           ];
       }

       echo json_encode($data);
   }
);

$app->handle();

and my error when I turn display error php

Fatal error: Uncaught Phalcon\Mvc\Micro\Exception: Not-Found handler is not callable or is not defined in /var/www/html/api/index.php:61 Stack trace: #0 /var/www/html/api/index.php(61): Phalcon\Mvc\Micro->handle() #1 {main} thrown in /var/www/html/api/index.php on line 61