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?

Hi,

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
$app->setDI($di);

// 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);
print_r($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
  print_r($annotation->getArguments());
}

// Processing request
$app->handle();
[...]

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:

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

I expect to see a key for Authentication.

Thanks Kumy

PS: I'm using Phalcon 3.2.1



1.9k
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...

Bests

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