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

[SOLVED] Trying to use auth class - not found

I am trying to replicate the Auth class more or less from Vokuro. Added these lines to my project's app/config/services.php:

at the top:

use test\Auth\Auth;

at the bottom:

$di->set('auth', function () {
    return new Auth();
});

inside app/plugins I have a file called Auth.php:

namespace test\Auth;

use Phalcon\Mvc\User\Component;

use test\Models\Users
use test\Models\RememberTokens;
use test\Models\SuccessLogins;
use test\Models\FailedLogins;

class Auth extends Component{
...

And I am getting :

PHP Fatal error: Class 'test\Auth\Auth' not found in /var/www/html/test/app/config/services.php on line 96

Any ideas? Do I need to register the "plugins" directory somewhere? What am I missing? TIA!



43.9k

If you do not register the plugin directory (in the place where you register models, controllers ... directories), phalcon will never find your Auth class



40.7k

contents of app/config/config.php:

<?php

return new \Phalcon\Config(array(
    'database' => array(
        'adapter'     => 'Mysql',
        'host'        => 'localhost',
        'username'    => 'root',
        'password'    => '123456',
        'dbname'      => 'medonc',
    ),
    'application' => array(
        'controllersDir' => __DIR__ . '/../../app/controllers/',
        'modelsDir'      => __DIR__ . '/../../app/models/',
        'viewsDir'       => __DIR__ . '/../../app/views/',
        'pluginsDir'     => __DIR__ . '/../../app/plugins/',
        'libraryDir'     => __DIR__ . '/../../app/library/',
        'cacheDir'       => __DIR__ . '/../../app/cache/',
        'baseUri'        => '/test/',
    )
));

Is there something else that I need to do to register the plugins directory?

In your bootstrap file, you need to loop through the application array and register each directory as a place the autoloader should look.



43.9k
$loader->registerDirs(
    array(
        $config->application->controllersDir,
        $config->application->modelsDir,
        $config->application->pluginsDir,
        $config->application->libraryDir
    )
)->register();


40.7k

inside test/app/config/loader.php I changed

$loader->registerNamespaces(
                            array(
                                  'test\Controllers'=>__DIR__.'/../controllers/',
                                  'test\Models'=>__DIR__.'/../models/',
                                  'test\Forms'=>__DIR__.'/../forms/'
                                  ),true);

to

$loader->registerNamespaces(
                            array(
                                  'test\Controllers'=>__DIR__.'/../controllers/',
                                  'test\Models'=>__DIR__.'/../models/',
                                  'test\Forms'=>__DIR__.'/../forms/',
                                  'test\Auth'=>__DIR__.'/../plugins/'
                                  ),true);

and that did it. Thanks!