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

Phalcon Multi Module fatal error in ../apps/frontend/Module.php

below is fatal error : Fatal error: Declaration of forum\frontend\Module::registerAutoloaders() must be compatible with Phalcon\Mvc\ModuleDefinitionInterface::registerAutoloaders(Phalcon\DiInterface $dependencyInjector = NULL) in D:\xampp\htdocs\forum\apps\frontend\Module.php on line 9

pls help support on this and below is code for ../apps/frontend/Module.php,thanks.

namespace forum\backend;

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

class Module implements ModuleDefinitionInterface{ //注册自定义加载器 public function registerAutoloaders($di = NULL){ $loader = new Loader(); $loader->registerNamespaces(array( 'forum\backend\controllers' => '../apps/backend/controllers/', 'forum\backend\models' => '../apps/backend/models/' ) );

    $loader->register();
}

//注册自定义服务
public function registerServices($di){
    $di->set('dispatcher',function(){
        $dispatcher = new Dispatcher();
        $dispatcher->setDefaultNamespace('forum\backend\controllers');
        return $dispatcher;
    });

    $di->set('view',function(){
        $view = new View();
        $view->setViewsDir('../apps/backend/views/');
        return $view;
    });
}

}



85.5k

i cant raed properly your post but ...


public function registerAutoloaders(\Phalcon\DiInterface $di = null){ 
    ....
}

As @Izopi4a wrote, your new function declaration is incompatible with the parent one.

Fatal error: Declaration of forum\frontend\Module::registerAutoloaders() must be compatible with Phalcon\Mvc\ModuleDefinitionInterface::registerAutoloaders(Phalcon\DiInterface $dependencyInjector = NULL)

If you use type hinting in the argument list, the error will go away:

public function registerAutoloaders(\Phalcon\DiInterface $di = null){ 
    // register dependencies
}

Hi Lzo,

There is project for 2 apps: backend and frontend, i have create each controllers & models & views $ Module.php in each Module.

I have add the default parameter as below :

but there is another error show :Phalcon Exception: Service 'Module' wasn't found in the dependency injection container

<?php public function registerAutoloaders(DiInterface $di=null){ $loader = new Loader(); $loader->registerNamespaces(array( 'forum\frontend\controllers' => '../apps/frontend/controllers/', 'forum\frontend\models' => '../apps/frontend/models/' ) );

    $loader->register();
}

?>

in ../public/index.php i have registerModules as below code:

<?php $di = new FactoryDefault();

$di->set('router', function(){ $router = new Router(); $router->setDefaultModule('frontend'); return $router; });

$di->set('db', function() {
    return new DbAdapter(array(
        'host'     =>'localhost',
        'username' =>'root',
        'password' =>'root',
        'dbname'   =>'forum'
    )
    );
});

    $di->set('url',function(){
        $url = new UrlProvider();
        $url->setBaseUri("/");
        return $url;
    });

        try{

            $application = new Application($di);

            $application->registerModules(array(
                'frontend'  => array(
                    'classname'  => 'forum\frontend\Module',
                    'path'       => '../apps/frontend/Module.php'
                ),
                'backend' => array(
                    'classname'  => 'forum\backend\Module',
                    'path'       => '../apps/backend/Module.php'
                )
            ));

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

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

        }

?>

i cant raed properly your post but ...


public function registerAutoloaders(\Phalcon\DiInterface $di = null){ 
  ....
}


85.5k
Accepted
answer
edited Jun '16
$application->registerModules(array(
               'frontend'  => array(
                    'classname'  => 'forum\frontend\Module',
                    'path'       => '../apps/frontend/Module.php'
                ),

classname => className :-)

Yes,bro,i have updated,and it is OK now,thanks for your great support.

$application->registerModules(array(
              'frontend'  => array(
                   'classname'  => 'forum\frontend\Module',
                   'path'       => '../apps/frontend/Module.php'
               ),

classname => className :-)