Annotations is trying to access almost any file even when they are empty (REALLY ANNOYING)

Hi guys,

i'm trying to work with annotations and we are couple of developers that is pulling our hair off!

This is my code public/index.php

<?php
namespace Clive;

error_reporting(E_ALL);
define('BASE_DIR', dirname(__DIR__));
define('APP_DIR', BASE_DIR . '/app');
require_once BASE_DIR . '/vendor/autoload.php';

use Tracy\Debugger,
    Illuminate\Database\Capsule\Manager as Capsule,
    \Phalcon\Debug,
    \Phalcon\Loader,
    Phalcon\Mvc\Application as BaseApplication,
    Phalcon\Mvc\Dispatcher,
    Phalcon\DI\FactoryDefault,
    Phalcon\Mvc\View,
    Phalcon\Mvc\View\Engine\Volt,
    Phalcon\Db\Adapter\Pdo\Mysql,
    Phalcon\Http\Response,
    Phalcon\Http\Request,
    Phalcon\Config\Adapter\Php as Config,
    Phalcon\Mvc\Router\Annotations,
    Phalcon\Security,
    \League\Fractal\Manager as FractalManager;

class Application extends BaseApplication {

    private function debugger() {
        Debugger::enable(Debugger::DEVELOPMENT);

        /**
         * Define some useful constants
         */
        $debug = new Debug();
        $debug->listen();
    }

    private function registerAutoLoaders() {
        $loader = new Loader();
        $loader->registerNamespaces([
            'Clive\Controllers' => APP_DIR . '/Controllers/',
            'Clive\Models' => APP_DIR . '/Models/',
            'Jowy\Phrest\Core' => APP_DIR . '/Core/'
        ])->register();
    }

    private function registerServices($di) {
        $di->set('response', function(){
            return new Response();
        });
        //Registering a Http\Request
        $di->set('request', function(){
            return new Request();
        });

        $di->set('dispatcher', function() use ($di) {
            $eventsManager = $di->getShared('eventsManager');
            $dispatcher = new Dispatcher();
            $dispatcher->setEventsManager($eventsManager);
            return $dispatcher;
        });

        $di->setShared(
            "apiResponse",
            function () {
                $response = new \Jowy\Phrest\Core\Response(new FractalManager());
                $response->setPhalconResponse(new Response());
                return $response;
            }
        );
    }

    private function registerView($di) {

        $view = function () {
            $view = new View();
            return  $view->disable();
        };
        $di->set('view', $view);
    }

    private function registerSecurity($di) {
        $di->setShared(
            "security",
            function () {
                $security = new Security();
                $security->setWorkFactor(12);
                return $security;
            }
        );
    }

    private function registerRoutes($di) {
        $di->set('router', function(){
            $router = new Annotations(false);
            $files = array_diff(scandir(APP_DIR . "/Controllers/"), array('..', '.'));
            foreach ($files as $file) {
                $file = array_slice(preg_split('/(?=[A-Z])/', $file), 1);
                $router->addResource("Clive\\Controllers\\" . $file[0]);
            }
            return $router;
        });
    }

    public function main() {
        $this->debugger();
        $config = new Config(BASE_DIR . '/config/config.php');
        $di = new FactoryDefault();
        $capsule = new Capsule();
        $capsule->addConnection(
            $config->database->toArray()
        );
        $capsule->bootEloquent();
        $di->set('config', $config);
        $this->registerAutoLoaders();
        $this->registerServices($di);
        $this->registerRoutes($di);
        $this->registerSecurity($di);
        $this->registerView($di);

        $this->setDI($di);
        echo $this->handle()->getContent();
    }
}

$application = new Application();
$application->main();

my /app/Controllers/IndexController.php

<?php
namespace Clive\Controllers;

Use Phalcon\Mvc\Controller;

/**
 * RoutePrefix("/v1")
 */
class IndexController extends Controller
{
    /**
     * @Get("/")
     */
    public function indexAction()
    {
        echo "hi, api code here...";
    }
}

Random Controller for testing: /app/Controllers/GoogleController.php

<?php
use Phalcon\Mvc\Controller;

/**
 * RoutePrefix("/google")
 */
class GoogleController extends Controller
{
    /**
     * @Get("/google")
     */
    public function indexAction()
    {

    }
}

When i go to / i get ``` ReflectionException: Class Clive\Controllers\GoogleController does not exist ```

And i get that on /v1 too. What am i doing wrong ?

Because this is really annoying.

Well in router you added:

$router->addResource("Clive\Controllers\" . $file[0]);

So its looking for Clive\Controllers\ namespace and then GoogleController, just put GoogleController in right namespace and it should be okay.



13.7k

any good idea to automate it ? I guess it's because we loop in all files and when they don't have a namespace that match, it fucks up ?

But anyway i switched to normal routing.

Well in router you added:

$router->addResource("Clive\Controllers\" . $file[0]);

So its looking for Clive\Controllers\ namespace and then GoogleController, just put GoogleController in right namespace and it should be okay.

What you mean automate ? You cant automating adding namespace to class, you have to add it yourself.