Service 'config' was not found in the dependency injection container

This is my index.php

<?php

error_reporting(E_ALL);

try {

    /**
     * Read the configuration
     */
    $config = new Phalcon\Config\Adapter\Ini(__DIR__ . '/../app/config/config.ini');

    $loader = new \Phalcon\Loader();
    /**
     * We're a registering a set of directories taken from the configuration file
     */
    $loader->registerDirs(
        array(
            __DIR__ . $config->application->controllersDir,
            __DIR__ . $config->application->pluginsDir,
            __DIR__ . $config->application->libraryDir,
            __DIR__ . $config->application->modelsDir,
        )
    )->register();

    /**
     * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
     */
    $di = new \Phalcon\DI\FactoryDefault();

    /**
     * We register the events manager
     */
    $di->set('dispatcher', function() use ($di) {

        $eventsManager = $di->getShared('eventsManager');

        $security = new Security($di);

        /**
         * We listen for events in the dispatcher using the Security plugin
         */
        $eventsManager->attach('dispatch', $security);

        $dispatcher = new Phalcon\Mvc\Dispatcher();
        $dispatcher->setEventsManager($eventsManager);

        return $dispatcher;
    });
    //echo $config->application->baseUri;
    /**
     * The URL component is used to generate all kind of urls in the application
     */
    $di->set('url', function() use ($config){
        $url = new \Phalcon\Mvc\Url();
        $url->setBaseUri($config->application->baseUri);
        return $url;
    });

    /**
     * Database connection is created based in the parameters defined in the configuration file
     */
    $di->set('db', function() use ($config) {
        return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
            "host" => $config->database->host,
            "username" => $config->database->username,
            "password" => $config->database->password,
            "dbname" => $config->database->name
        ));
    });

    /**
     * If the configuration specify the use of metadata adapter use it or use memory otherwise
     */
    $di->set('modelsMetadata', function() use ($config) {
        if (isset($config->models->metadata)) {
            $metaDataConfig = $config->models->metadata;
            $metadataAdapter = 'Phalcon\Mvc\Model\Metadata\\'.$metaDataConfig->adapter;
            return new $metadataAdapter();
        }
        return new Phalcon\Mvc\Model\Metadata\Memory();
    });

    /**
     * Start the session the first time some component request the session service
     */
    $di->set('session', function(){
        $session = new Phalcon\Session\Adapter\Files();
        $session->start();
        return $session;
    });

    /**
     * Register the flash service with custom CSS classes
     */
    $di->set('flash', function(){
        return new Phalcon\Flash\Direct(array(
            'error' => 'alert alert-error',
            'success' => 'alert alert-success',
            'notice' => 'alert alert-info',
        ));
    });

    $di->set(
    'flashSession',
    function(){
    $flash = new \Phalcon\Flash\Session(
    array(
    'error' => 'alert alert-danger',
    'success' => 'alert alert-success',
    'notice' => 'alert alert-info',
    'warning' => 'alert alert-warning',
    )
    );
    return $flash;
    }
    );

    $di->set('view', function() use ($config) {

        $view = new \Phalcon\Mvc\View();

        $view->setViewsDir(__DIR__ . $config->application->viewsDir);

        $view->registerEngines(array(
            '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
        ));

        return $view;
    });



    /**
     * Register a user component
     */
    $di->set('elements', function(){
        return new Elements();
    });

    $application = new \Phalcon\Mvc\Application();
    $application->setDI($di);
    echo $application->handle()->getContent();

} catch (Phalcon\Exception $e) {
    echo $e->getMessage();
} catch (PDOException $e){
    echo $e->getMessage();
}


1.3k
edited Sep '14

You should be able to reference the $config without using the dependency injector unless you are trying to access it outside of your index.php. If you are attempting to access it outside of your index.php, you will need to add it to the DI Container. Take a look at the link below on how to register it to the DI Container.

http://docs.phalconphp.com/en/latest/reference/di.html#registering-services-in-the-container

//Using an anonymous function, the instance will be lazy loaded
$di->set("config", function($config) {
    return $config;
});