Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

loader registerDirs fails to load class

I am learning phalcon, and I would like to use a custom class Test.php but i can't achie this class from my controller. My directory structure

/multiple
    /apps
        /user
            /controllers
                IndexController.php
    /config
    /plugins
        Test.php
/public
    .htaccess
    index.php

my index.php contain:

$loader = new \Phalcon\Loader();
$loader->registerDirs(
    array(
        "multiple/plugins"
    )
);
$loader->register();

my IndexController loginAction looks like:

  public function loginAction()
  {
    var_dump('ooo');                 //output ooo
    $test = new Test();
    var_dump($test->test('gr'));     //output nothing
  }

and my Test.php

class Test{
  public function test($string){
    var_dump($string);
    return $string;
  }
}

Please help me how can I get may Test class to work. Thanks a lot!



32.0k

What namespace are you using ?

The plugins/Test.php file can be defined as :

namespace Plugins;

class Test{
  public function test($string){
    var_dump($string);
    return $string;
  }
}

And in IndexController.php :

namespace Apps\User\Controllers;

use \Plugins\Test;

class UserController extends \Phalcon\Mvc\Controller
{
  public function loginAction()
  {
    var_dump('ooo');                 //output ooo
    $test = new Test();
    var_dump($test->test('gr'));     //output nothing
  }
}


858
Accepted
answer
edited Dec '14

Unfortunately it still not working.

I started to create a multi-module app.

The content of index.php in the root:

<?php

use Phalcon\Mvc\Router, Phalcon\Mvc\Application, Phalcon\DI\FactoryDefault;

$config = new Phalcon\Config\Adapter\Ini('../multiple/config/config.ini'); $di = new FactoryDefault();

$loader = new \Phalcon\Loader();

$loader->registerDirs( array( "../multiple/plugins/", "../multiple/apps/models/" ) ); $loader->register();

$di->set('session', function() { $session = new Phalcon\Session\Adapter\Files(); $session->start(); return $session; });

//Specify routes for modules $di->set('router', function () {

$router = new Router();

$router->setDefaultModule("user");

$router->add("/:module/:controller/:action", array( 'module' => 1, 'controller' => 2, 'action' => 3 ));

$router->add("/login", array( 'module' => 'user', 'controller' => 'index', 'action' => 'login', ));

$router->add("/admin/products/:action", array( 'module' => 'backend', 'controller' => 'products', 'action' => 1, ));

$router->add("/products/:action", array( 'controller' => 'products', 'action' => 1, ));

return $router; });

$di->set('db', function() use ($config) { return new \Phalcon\Db\Adapter\Pdo\Mysql(array( "host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->name )); });

try {

//Create an application $application = new Application($di);

// Register the installed modules $application->registerModules( array( 'frontend' => array( 'className' => 'Multiple\Frontend\Module', 'path' => '../multiple/apps/frontend/Module.php', ), 'user' => array( 'className' => 'Multiple\User\Module', 'path' => '../multiple/apps/user/Module.php', ) ) );

//Handle the request echo $application->handle()->getContent();

} catch(\Exception $e){ echo $e->getMessage(); }

Module.php in User Module

<?php /** * Created by PhpStorm. * User: Csaba * Date: 2014.12.09. * Time: 13:33 */ namespace Multiple\User;

use Phalcon\Loader, Phalcon\Mvc\Dispatcher, Phalcon\Mvc\View, Phalcon\Mvc\ModuleDefinitionInterface;

class Module implements ModuleDefinitionInterface {

/** * Register a specific autoloader for the module */ public function registerAutoloaders() {

$loader = new Loader();

$loader->registerNamespaces(
  array(
      'Multiple\User\Controllers' => '../multiple/apps/user/controllers/',
      'Multiple\User\Models'      => '../multiple/apps/user/models/'
  )
);

$loader->register();

}

/** * Register specific services for the module */ public function registerServices($di) {

//Registering a dispatcher
$di->set('dispatcher', function() {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace("Multiple\User\Controllers");
return $dispatcher;
});

//Registering the view component
$di->set('view', function() {
$view = new View();
$view->setViewsDir('../multiple/apps/user/views/');
return $view;
});

}

}

And the new IndexController in User Module: <?php

namespace Multiple\User\Controllers; use \Plugins\Test; class IndexController extends \Phalcon\Mvc\Controller {

public function loginAction() { vardump('ooo'); //output ooo $test = new Test(); vardump($test->test('gr')); //output nothing }

public function registerAction() { echo "<h1>Login!</h1>"; } }

Forthemore \multiple\apps\user\models\Users.php is not work too. But, I think, itt will be good, if a found the answer to the original question. I am going to home from work, I'll check the answer later.

What namespace are you using ?

The plugins/Test.php file can be defined as :

```php namespace Plugins;

class Test{ public function test($string){ var_dump($string); return $string; } } ```

And in IndexController.php :

```php namespace Apps\User\Controllers;

use \Plugins\Test;

class UserController extends \Phalcon\Mvc\Controller { public function loginAction() { vardump('ooo'); //output ooo $test = new Test(); vardump($test->test('gr')); //output nothing } } ```

I found the problem. I had to register the custom class to the loader namespaces, like this:

$loader->registerNamespaces(
  array(
      'Plugins'                   => '../multiple/plugins'
  )
);

and of, course from Calin Rada answer I had to add a namespace to the Plugin. If somebody falls to this situation designing a multi-module system, I hope it will help.