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

Using namespaces problems

Hi.

I created my first project yesterday with the phalcon create project command. It worked fine but today I would like to use namespace (like App\Controllers for my controllers)

I changed my loader froim registerDirs to registerNamespaces, added the namespace line on all my controllers. Here is the structure I actually have.

IndexController (with my indexAction, it extends PublicController) PublicController (with an initialize function that will add assets for public pages) (It extends ControllerBase) ControllerBase (with an initialize function that add the assests that will be on all pages.) (it extends the Phalcon\Mvc\Controller)

My config file

'application' => [ 'appDir' => APP_PATH . '/', 'controllersDir' => APP_PATH . '/controllers/', 'modelsDir' => APP_PATH . '/models/', 'formsDir' => APP_PATH . '/forms/', 'servicesDir' => APP_PATH . '/services', 'viewsDir' => APP_PATH . '/views/', 'securityDir' => APP_PATH . '/security/', 'cacheDir' => BASE_PATH . '/cache/', // This allows the baseUri to be understand project paths that are not in the root directory // of the webpspace. This will break if the public/index.php entry point is moved or // possibly if the web server rewrite rules are changed. This can also be set to a static path. 'baseUri' => preg_replace('/public([\/\\])index.php$/', '', $_SERVER["PHP_SELF"]), ],

Loader file

$loader->registerNamespaces( [ 'App\Controllers' => $config->application->controllersDir, 'App\Models' => $config->application->modelsDir, 'App\Services' => $config->application->servicesDir, 'App\Forms' => $config->application->formsDir, 'App\Security' => $config->application->securityDir, ] )->register();

ControllerBase

<?php namespace App\Controllers;

use Phalcon\Mvc\Controller;

class ControllerBase extends Controller { public function initialize() {

    //TODO Inject js and css in all pages/layout from here.
    //If a layout need specific js/css add an ACL condition.

    $headerCollection = $this->assets->collection('header');

    // Add some local CSS resources
    $headerCollection->addCss('semantic/dist/semantic.min.css');
    $headerCollection->addCss('css/main.css');

    $footerCollection = $this->assets->collection('footer');
    // And some local JavaScript resources
    $footerCollection->addJs('https://code.jquery.com/jquery-3.1.1.min.js');
    $footerCollection->addJs('semantic/dist/semantic.min.js');
}

}

PublicController

<?php namespace App\Controllers;

class PublicController extends ControllerBase { public function initialize() { parent::initialize(); $this->view->setTemplateAfter('layout/public'); } }

IndexController

<?php namespace App\Controllers;

class IndexController extends PublicController { public function initialize() { parent::initialize(); }

public function indexAction()
{

}

}

And I have this error message

IndexController handler class cannot be loaded

0 [internal function]: Phalcon\Mvc\Dispatcher->_throwDispatchException('IndexController...', 2)

1 [internal function]: Phalcon\Dispatcher->_dispatch()

2 [internal function]: Phalcon\Dispatcher->dispatch()

3 /var/www/html/phalcon-test/public/index.php(42): Phalcon\Mvc\Application->handle()

4 {main}

But nothing I could find on internet helped me ^^"

Thanks in advance.



8.2k
Accepted
answer

My bad I took vokuro as an example but didn't see the dispatcher setter in services config.