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

Are Annotations usable with Mvc\Micro?


I'm trying to use annotations on an Mvc\Micro app, but getClassAnnotations() always return me False. Are the annotation working also with Mvc\Micro or do I have to use Mvc\Application?

My current testing code is:

// Loading Configs
$config = require__DIR__.'/../app/config/config.php';

// Autoloading classes
require __DIR__.'/../app/config/loader.php';

// Initializing DI container
/** @var \Phalcon\DI\FactoryDefault $di */
$di = require __DIR__.'/../app/config/di.php';

// Initializing application
$app = new \Phalcon\Mvc\Micro();

// Setting DI container

// Setting up routing
require __DIR__.'/../app/config/routes.php';

// TEST Annotations
$reader = new MemoryAdapter();
$reflector = $reader->get(\App\Controllers\UsersController::class);
$annotations = $reflector->getClassAnnotations();
echo gettype($annotations);

// Traverse the annotations
foreach ($annotations as $annotation) {
  // Print the annotation name
  echo $annotation->getName(), PHP_EOL;

  // Print the number of arguments
  echo $annotation->numberArguments(), PHP_EOL;

  // Print the arguments

// Processing request

Excerpt of \App\Controllers\UsersController::class

     * Returns user list
     * @return array
     * @Authentication(true)
    public function getUserListAction()
        try {
            $userList = $this->usersService->getUserList();
        } catch (ServiceException $e) {
            throw new Http500Exception(_('Internal Server Error'), $e->getCode(), $e);

        return $userList;

Which results as:

  <br />
  <b>Warning</b>:  Invalid argument supplied for foreach() in
  <b>/var/www/public/index.php</b> on line
  <br />

I expect to see a key for Authentication.

Thanks Kumy

PS: I'm using Phalcon 3.2.1

edited Jul '17

Ho !!!

Just get the meaning of $reflector->getClassAnnotations(). I had to use $reflector->getMethodsAnnotations() instead.

Documentation always refer to getClassAnnotations(). Some words about getMethodsAnnotations() would be great. ok, now I need to figure how to filter annotation for current route handler in Micro...


Edit: FTR $app->getActiveHandler() is the key.