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

Api is not working for me

Hello, i tried the Api tutorial, i copied it 1:1 but i get this error message:

https://docs.phalcon.io/4.0/de-de/tutorial-rest

Fatal error: Uncaught Phalcon\Mvc\Micro\Exception: Not-Found handler is not callable or is not defined in

I see this line:

     'MyApp\Models' => __DIR__ . '/models/',

What is MyApp? The dir at my localhost?

Also it doesn't jumps into get /api/robots, when i call:

     https://localhost/phalconapi/api/robots/

Index.php

     <?php

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

     $loader = new Loader();
     $loader->registerNamespaces(
    [
        'MyApp\Models' => __DIR__ . '/models/',
    ]
    );
    $loader->register();

    $container = new FactoryDefault();
    $container->set(
    'db',
    function () {
        return new PdoMysql(
            [
                'host'     => 'localhost',
                'username' => 'root',
                'password' => '',
                'dbname'   => 'robotics',
            ]
        );
    }
    );

    $app = new Micro($container);

    $app->get(
    '/api/robots',
    function () use ($app) {
        $phql = 'SELECT id, name '
            . 'FROM MyApp\Models\Robots '
            . 'ORDER BY name'
        ;

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

        $data = [];

        var_dump($robots);
        die();

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

        echo json_encode($data);
    }
    );

    $app->handle(
    $_SERVER['REQUEST_URI']
    );

Robots.php

     <?php

     namespace MyApp\Models;

     use Phalcon\Mvc\Model;
     use Phalcon\Messages\Message;
     use Phalcon\Validation;
     use Phalcon\Validation\Validator\Uniqueness;
     use Phalcon\Validation\Validator\InclusionIn;

     class Robots extends Model
    {
    public function validation()
    {
        $validator = new Validation();

        $validator->add(
            "type",
            new InclusionIn(
                [
                    'message' => 'Type must be "droid", "mechanical", or "virtual"',
                    'domain' => [
                        'droid',
                        'mechanical',
                        'virtual',
                    ],
                ]
            )
        );

        $validator->add(
            'name',
            new Uniqueness(
                [
                    'field'   => 'name',
                    'message' => 'The robot name must be unique',
                ]
            )
        );

        if ($this->year < 0) {
            $this->appendMessage(
                new Message('The year cannot be less than zero')
            );
        }

        if ($this->validationHasFailed() === true) {
            return false;
        }
    }
     }

Thx for help :-)

edited Mar '20

MyApp is part of the namespace. You're setting up the autoloader like:

When I reference a class that has the namespace MyApp\Model , look in the __DIR__."/models" directory for a file that contains the class defininition.

But I don't think that is your problem. Evidently you need to define a notFound() route: https://docs.phalcon.io/4.0/en/application-micro#routing-handlers-not-found

yup thats working perfactly fine