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

Phalcon2 & Loader: Unable to use Register Namespaces

Hi Guys,

I have a problem when using $loader->registerNamespaces() in phalcon2/app/pulbic/index.php. When using it, the page cannot load and when I check in Apache error log, i got the following:

[Wed Apr 22 10:06:05.557768 2015] [mpm_winnt:notice] [pid 10956:tid 468] AH00428: Parent: child process 12160 exited with status 255 -- Restarting.
[Wed Apr 22 10:06:05.827784 2015] [mpm_winnt:notice] [pid 10956:tid 468] AH00455: Apache/2.4.9 (Win32) PHP/5.5.12 configured -- resuming normal operations
[Wed Apr 22 10:06:05.827784 2015] [mpm_winnt:notice] [pid 10956:tid 468] AH00456: Apache Lounge VC11 Server built: Mar 16 2014 12:13:13
[Wed Apr 22 10:06:05.827784 2015] [core:notice] [pid 10956:tid 468] AH00094: Command line: 'c:\\wamp\\bin\\apache\\apache2.4.9\\bin\\httpd.exe -d C:/wamp/bin/apache/apache2.4.9'
[Wed Apr 22 10:06:05.828784 2015] [mpm_winnt:notice] [pid 10956:tid 468] AH00418: Parent: Created child process 224
[Wed Apr 22 10:06:06.440819 2015] [mpm_winnt:notice] [pid 224:tid 396] AH00354: Child: Starting 64 worker threads.
[Wed Apr 22 10:06:28.908104 2015] [mpm_winnt:notice] [pid 10956:tid 468] AH00428: Parent: child process 224 exited with status 255 -- Restarting.
[Wed Apr 22 10:06:29.099115 2015] [mpm_winnt:notice] [pid 10956:tid 468] AH00455: Apache/2.4.9 (Win32) PHP/5.5.12 configured -- resuming normal operations
[Wed Apr 22 10:06:29.099115 2015] [mpm_winnt:notice] [pid 10956:tid 468] AH00456: Apache Lounge VC11 Server built: Mar 16 2014 12:13:13
[Wed Apr 22 10:06:29.099115 2015] [core:notice] [pid 10956:tid 468] AH00094: Command line: 'c:\\wamp\\bin\\apache\\apache2.4.9\\bin\\httpd.exe -d C:/wamp/bin/apache/apache2.4.9'
[Wed Apr 22 10:06:29.102115 2015] [mpm_winnt:notice] [pid 10956:tid 468] AH00418: Parent: Created child process 13248
[Wed Apr 22 10:06:30.085171 2015] [mpm_winnt:notice] [pid 13248:tid 396] AH00354: Child: Starting 64 worker threads.

I have a simple phalcon app structure now with phalcon2/public/index.php as following:

<?php
use Phalcon\Mvc\Router;
try {

    //Register an autoloader
    $loader = new \Phalcon\Loader();

    $loader->registerNamespaces(
        array(
            'App\Controller' => '../app/controllers/',
            'App\Model' => '../app/models/',
        )
    )->register();

    //Create a DI
    $di = new Phalcon\DI\FactoryDefault();

    $di->set('router', function (){

        $router = new Router(false);

        $router->removeExtraSlashes(true);

        $router->add('/', array(
            // 'namespace' => 'App\Controller',
            'controller' => 'Index',
            'action' => 'index'
        ));

        return $router;
    });

    //Setup the database service
    $di->set('db', function(){
        return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
            "host" => "localhost",
            "username" => "root",
            "password" => "",
            "dbname" => "phalcon2"
        ));
    });

    //Setup the view component
    $di->set('view', function(){
        $view = new \Phalcon\Mvc\View();
        $view->setViewsDir('../app/views/');
        return $view;
    });

    //Setup a base URI so that all generated URIs include the "tutorial" folder
    $di->set('url', function(){
        $url = new \Phalcon\Mvc\Url();
        $url->setBaseUri('/phalcon2/');
        return $url;
    });

    //Handle the request
    $application = new \Phalcon\Mvc\Application($di);

    echo $application->handle()->getContent();

} catch(\Phalcon\Exception $e) {
     echo "PhalconException: ", $e->getMessage();
}

And my phalcon2/app/controllers/IndexController.php as following:

<?php
 namespace App\Controller;

class IndexController extends \Phalcon\Mvc\Controller
{

    public function indexAction()
    {

    }

}

If i'm using $loader->registerDirs(), and remove the namespace in IndexController.php, the app working fine.

Any Ideas?

Thanks.

Try Registering Namespaces within the module.php file

       public function registerAutoloaders()
{

    $loader = new Loader();

    $loader->registerNamespaces(
        array(
            'App\Controllers' => __DIR__.'\controllers',
            'App\Models'      => __DIR__.'\models',
        )
    );

    $loader->register();

}

Then in your index file

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

$router = new \Phalcon\Mvc\Router\Annotations(false);

$router->setDefaultModule("app");

$router->add('/', array(
        // 'namespace' => 'App\Controller',
        'controller' => 'Index',
        'action' => 'index'
    ));
    return $router;
});

 $application = new Application($di);

// Register the installed module
$application->registerModules(
    array(
        'app'  => array(
            'className' => 'app\Module',
            'path'      => __DIR__.'/app/Module.php',
        )
    )
);