Hi world I'm trying to get the tutorial 7 code work. but when i implement curl -i -X GET xxx, i get the error like below
PHP Fatal error: Uncaught Phalcon\Mvc\Model\Exception: Model 'Store\Toys\Robots' could not be loaded in /var/www/html/my-rest-api/index.php:48\nStack trace:\n#0 [internal function]: Phalcon\Mvc\Model\Manager->load('Store\\Toys\\Robo...', true)\n#1 [internal function]: Phalcon\Mvc\Model\Query->_prepareSelect()\n#2 [internal function]: Phalcon\Mvc\Model\Query->parse()\n#3 [internal function]: Phalcon\Mvc\Model\Query->execute()\n#4 /var/www/html/my-rest-api/index.php(48): Phalcon\Mvc\Model\Manager->executeQuery('SELECT * FROM S...')\n#5 [internal function]: Closure->{closure}()\n#6 /var/www/html/my-rest-api/index.php(263): Phalcon\Mvc\Micro->handle()\n#7 {main}\n thrown in /var/www/html/my-rest-api/index.php on line 48
my index.php is below:
<?php use Phalcon\Loader; use Phalcon\Mvc\Micro; use Phalcon\Di\FactoryDefault; use Phalcon\Db\Adapter\Pdo\Mysql as PdoMysql; use Phalcon\Http\Response;
$loader = new Loader();
$loader->registerNamespaces( [ "Store\Toys" => DIR . "/models/", ] );
$loader->register();
$di = new FactoryDefault();
//DB setup $di->set( "db", function(){ return new PdoMysql( [ "host" => "localhost", "username" => "", "password" => "", "dbname" => "robotics", ] ); } );
$app = new Micro($di);
$app->get('/', function(){ echo "Restful API Phalcon"; });
//Retrives all robots $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);
} );
//Searches for robots with $name in their name $app->get( "/api/robots/search/{name}", function ($name) use ($app){ $phql = "SELECT * FROM Store\Toys\Robots WHERE name LIKE :name: ORDER BY name";
$robots = $app->modelsManager->executeQuery($phql, [ "name" => "%" . $name . "%" ]
);
$data = [];
foreach ($robots as $robot) { $data[] = [ "id" => $robot->id, "name" => $robot->name ]; } echo json_encode($data); } );
//Retrieves robots based on primary key $app->get( "/api/robots/{id:[0-9]+}", function($id) use ($app){ $phql = "SELECT * FROM Store\Toys\Robots WHERE id = :id:";
$robots = $app->modelsManager->executeQuery($phql,
[ "id" => $id, ] )->getFirst;
$response = new Response();
if ($robot === false) { $response->setJsonContent( [ "status" =>"NOT-FOUND" ] ); } else { $response->setJsonContent( [ "status"=>"FOUND", "data"=> [ "id" => $robot->id, "name" => $robot->name ] ] ); } return $response; } );
//Adds a new robot $app->post( "/api/robots", function() use ($app){ $robot = $app->request->getJsonRawBody();
$phql = "Insert into Store\\Toys\\Robots (name, type, year) VALUES(:name:, :type:, :year:)"; $status = $app->modelsManager->executeQuesry( $phql, [ "name" => $robot->name, "type" => $robot->type, "year" => $robot->year, ] ); $response = new Response(); if ($status->success() === true) { $response->setStatusCode(201,"created"); $robot->id = $status->getModel()->id; $response->setJsonContent( [ "status" => "OK", "data" => $robot, ] ); } else{ //change the http status $response->setStatusCode(409, "Conflict"); $errors = []; foreach ($status->getMessages() as $message) { $error[] = $message->getMessage(); } $response->setJsonContent( [ "status" => "ERROR", "messages"=>$errors, ] ); } return $response;
} );
$app->put( "/api/robots/{id:[0-9]+}", function($id) use ($app) { $robot = $app->request->getJsonRawBody();
$phql = "UPDATE Store\\Toys\\Robots SET name = :name:, type = :type:, year = :year: WHERE id = :id:"; $status = $app->modelsManager->executeQuery( $phql, [ "id" => $id, "name" => $robot->name, "type" => $robot->type, "year" => $robot->year, ] ); // Create a response $response = new Response(); // Check if the insertion was successful if ($status->success() === true) { $response->setJsonContent( [ "status" => "OK" ] ); } else { // Change the HTTP status $response->setStatusCode(409, "Conflict"); $errors = []; foreach ($status->getMessages() as $message) { $errors[] = $message->getMessage(); } $response->setJsonContent( [ "status" => "ERROR", "messages" => $errors, ] ); } return $response;
} );
$app->delete( "/api/robots/{id:0-9+}", function($id) use ($app){ $phql = "DELETE FROM Store\Toys\Robots WHERE id = :id:";
$status = $app->modelsManager->executeQuery( $phql, [ "id" => $id, ] ); // Create a response $response = new Response(); if ($status->success() === true) { $response->setJsonContent( [ "status" => "OK" ] ); } else { // Change the HTTP status $response->setStatusCode(409, "Conflict"); $errors = []; foreach ($status->getMessages() as $message) { $errors[] = $message->getMessage(); } $response->setJsonContent( [ "status" => "ERROR", "messages" => $errors, ] ); } return $response;
} );
$app->handle();
?>