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

Register Namespaces and Classes ?

EDIT: This is working example. Thank You, Jason Socha!

/pub/index.php

$loader->registerNamespaces([
    'App\Controllers' => '../app/controllers/',
    'App\Libraries' => '../app/libraries/'
])->register();

$di->set('dispatcher', function() {
    $dispatcher = new \Phalcon\Mvc\Dispatcher();
    $dispatcher->setDefaultNamespace('App\Controllers');
    return $dispatcher;
});

/app/routes.php

$router->add('/:controller/:action/:params', [
    'namespace' => 'App\Controllers',
    'controller' => 1,
    'action' => 2,
    'params' => 3,
]);

$router->add('/:controller', [
    'namespace' => 'App\Controllers',
    'controller' => 1
]);

/app/libraries/MyClass.php

namespace App\Libraries;

class MyClass {

  public static function test() {
    return 'Test Output from MyClass';
  }

}

/app/controllers/IndexController.php

namespace App\Controllers;

use App\Libraries\MyClass;

class IndexController extends \Phalcon\Mvc\Controller {

  public function indexAction() {
    echo 'IndexController.php Test';
    echo MyClass::test();
  }

}

What is the namespace of that class? Probably changed perhaps something like App\Library\MyClass?

This is how I have my registerNamespaces set up.

$loader->registerNamespaces( array( 'App\Controllers' => $config->application->controllersDir, 'App\Models' => $config->application->modelsDir, 'App\Library' => $config->application->libraryDir ) );

One thing I've noticed is that in the Models if you have any relationships set up you need to register as a namespace.

edited Mar '15

If you register your namespace(s), you don't have to register your class. The loader will find it. For example:

$loader->registerNamespaces(array(
  'MyNamespace\Models' => $config->application->modelsDir,
  'MyNamespace\Controllers' => $config->application->controllersDir,
  'MyNamespace\Forms' => $config->application->formsDir,
  'MyNamespace' => $config->application->libraryDir
));

$loader->register();

Now, just about anywhere in my app, I can do:

use MyNamespace\FooBar;

FooBar::doSomething();

The loader will automatically look in the specified dir for a file called FooBar.php and expect it to contain a class called FooBar.

Any class should work this way. Using the example above, the class file would look like this:

// path/to/libraryDir/FooBar.php
namespace MyNamespace;

class FooBar
{
  public static function doSomething()
  {

  }
}


3.4k
Accepted
answer

Any class will work this way. Using the example above, the class file would look like this:

// path/to/libraryDir/FooBar.php
namespace MyNamespace;

class FooBar
{
  public static function doSomething()
  {

  }
}

It's difficult to say; your code looks basically right.

I suspect that you can't echo from inside a controller when Phalcon\Mvc is in control; that could be the issue. Try this maybe:

class MyClass {
  public static function test() {
    die('MyClass::test() was called.');
  }
}

No problem



7.7k
edited Mar '15

If you please, combine your posts ^_^ So any other user looking for the solution will find it easier to read. Then delete the other ones. Like I did above ;-)

No problem



7.7k

Thanks for trying, but next time better examples please ^_^ See what Jason Socha did for example!

What is the namespace of that class? Probably changed perhaps something like App\Library\MyClass?

This is how I have my registerNamespaces set up.

$loader->registerNamespaces( array( 'App\Controllers' => $config->application->controllersDir, 'App\Models' => $config->application->modelsDir, 'App\Library' => $config->application->libraryDir ) );

One thing I've noticed is that in the Models if you have any relationships set up you need to register as a namespace.