0

I am having a bit of difficulty understanding how I can implement an API in a separate directory using an Application type object instead of an Micro.

Currently when I try to implement:

$application->get('/api/robots', function () {

});

within my index.php inside public, where I create:

$application = new Application($di);    

echo $application->handle()->getContent();

It always treats them as view controllers and ask for a controller. I tried following the docs for creating a simple RESTful API and I ended up creating a separate folder called api where the micro index.php (follows the exact layout of the api micro application in the tutorial) lives, but at the end of the day I keep getting the ApiController not found.

I am a bit stumped and any clarification/simplification would be very helpful!

Here is the tutorial: https://docs.phalconphp.com/en/latest/reference/tutorial-rest.html

David Biga
  • 2,763
  • 8
  • 38
  • 61

1 Answers1

0

If you prefer to usePhalcon\Mvc\Application over Phalcon\Mvc\Micro, you can handle your routing the same way as you would normally.

$router = new Phalcon\Mvc\Router();

// route for a GET request (controller: ApiController, action: requestAction)
$router->addGet('/api/request/{id}', [
    'controller' => 1,
    'action'     => 2,
]);

// route for a DELETE request (controller: ApiController, action: removeAction)
$router->addDelete('/api/remove/{id}', [
    'controller' => 1,
    'action'     => 2,
]);

// this works the same for 
// $router->addPut(...) and $router->addPost(...)

// other normal route
$router->add('/([a-zA-Z\-]+)/([a-zA-Z\-]+)/:params', [
    'controller' => 1,
    'action'     => 2,
    'params'     => 3
]);
Timothy
  • 2,004
  • 3
  • 23
  • 29