We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Problem with rest api

  <?php

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

  $loader->registerDirs(array(
      __DIR__ . '/models/'
  ))->register();

  $di = new \Phalcon\DI\FactoryDefault();

  //Set up the database service
  $di->set('db', function(){
      return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
          "host" => "localhost",
          "username" => "root",
          "password" => "xxxxxxx",
          "dbname" => "robotics"
      ));
  });

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

  //Retrieves all robots
  $app->get('/api/robots', function() use ($app) {

      $phql = "SELECT * FROM Robots ORDER BY name";
      $robots = $app->modelsManager->executeQuery($phql);

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

      echo json_encode($data);
  });
  $app->handle();

Hello Guys why i take 500 internal error? I have a Db Robots with table robotics and values id,name,type,year The problem is the $app->modelsManager->executeQuery($phql);

    Phalcon\\DI\\Injectable::__get(): Access to undefined property Phalcon\\Mvc\\Micro::Robots in       /var/www/html/restphalcon/index.php on line 30

Why doesn't work? Can someone tell me?

Thanks



6.2k
Accepted
answer

Solved Problem was the credentials of robots.php and also the db about the names