We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

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.

Service not registered in the application container

Good afternoon, I try to build my first multi-module app using phalcon, but when I run my app only this message appear "0 - Service 'IntranetRH\Front\Module' was not found in the dependency injection container". My bootstrap file looks like this.

<?php
use Phalcon\DI\FactoryDefault;

defined('APPLICATION_PATH')
 || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../apps'));

error_reporting(E_ALL);

try {
 $di = new FactoryDefault();
    include __DIR__ . "/../apps/config/router.php";
    include __DIR__ . "/../apps/config/session.php";
    include __DIR__ . "/../apps/config/url.php";

$application = new \Phalcon\Mvc\Application($di);

    $application->registerModules(
        array(

            'front'  => array(
                'className' => 'IntranetRH\Front\Module',
                'path'      => '../apps/front/Module.php'
            )
        )
    );

    $application->setDI($di);

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

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

Module file :

 <?php
use Phalcon\Loader,
Phalcon\Mvc\Dispatcher,
Phalcon\Mvc\View,
Phalcon\Mvc\ModuleDefinitionInterface,        
Phalcon\Events\Manager as EventsManager;

class Module implements ModuleDefinitionInterface
{
 public function registerAutoloaders()
{
    $loader = new Loader();
    $loader->registerNamespaces(
        array(
            'IntranetRH\Front\Controllers' => '../apps/front/controllers/',
            'IntranetRH\Front\Models'      => '../apps/front/models/'
        )
    );

    $loader->register();
}
public function registerServices($di)
{
    //Registering a dispatcher
    $di->set('dispatcher', function() {
        $dispatcher = new Dispatcher();
        $dispatcher->setDefaultNamespace("IntranetRH\Front\Controllers"); 
         $eventsManager = new EventsManager();

        //Attach a listener
        $eventsManager->attach("dispatch:beforeDispatchLoop", function($event, $dispatcher) {

            $keyParams = array();
            $params = $dispatcher->getParams();

            //Use odd parameters as keys and even as values
            foreach ($params as $number => $value) {
                if ($number & 1) {
                    $keyParams[$params[$number - 1]] = $value;
                }
            }

            //Override parameters
            $dispatcher->setParams($keyParams);
        });

        return $dispatcher;
    });

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

        $view->registerEngines(array(
        ".twig" => function($view, $di) {
        /**
        * Setting up volt
        */
        $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);

        $volt->setOptions(array(
            "compiledPath" => "../volt/index/",
            "compiledExtension" => ".php",
            'compiledSeparator' => '%%',
            'stat'              =>  1,
            'compileAlways' => true
        ));

        $compiler = $volt->getCompiler();
            //This binds the function name 'noformat' in Volt to the PHP function 'round'
            $compiler->addFunction('number_format', 'number_format');
            $compiler->addFunction('empty', 'empty');
            $compiler->addFunction('isset', 'isset');                
            $compiler->addFunction('utf8_encode', 'utf8_encode');
            $compiler->addFunction('utf8_decode', 'utf8_decode');

        return $volt;
        }
        ));

        return $view;
    });
}
}
?>


10.8k
Accepted
answer

Not entirely sure but it looks like your module file needs

    <?php
    namespace IntranetRH\Front;
    # your code

at the beginning.



5.8k
edited May '15

Thanx Andy, I added \Module to my namespace this is why it doesn't work :)