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

Wierd Loader() behavior

Hello, I have encountered wierd behavior which I can't really describe, perhaps someone will be kind enough to shed some light on it.

Scenario 1 (working, note: the logic is in one file)

bootstrap.php
<?php
use Phalcon\Mvc\Application,
    Phalcon\Loader;

try {
    //Register an autoloader
    $loader = new Loader();

    //Namespaces
    $loader->registerNamespaces(array(
        'Phalcon' => __DIR__ . '/../vendor/incubator/Library/Phalcon',
        'CoreBundle\Models' => __DIR__ . '/../app/bundles/CoreBundle/models/',
    ));

    $loader->register();

     //Twig
    require "../vendor/twig/twig/lib/Twig/Autoloader.php";
    Twig_Autoloader::register();

Scenario 2 (Not working, note: the logic is seperated into two files (Bootstrap.php and loader.php)

bootstrap.php
<?php
use Phalcon\Mvc\Application,
    Phalcon\Loader;

try {
    include __DIR__ . "/../app/config/loader.php";

     //Twig
    require "../vendor/twig/twig/lib/Twig/Autoloader.php";
    Twig_Autoloader::register();
loader.php
<?php
$loader = new \Phalcon\Loader();

$loader->registerNamespaces(array(
    'Phalcon' => __DIR__ . '/../vendor/incubator/Library/Phalcon',
    'CoreBundle\Models' => __DIR__ . '/../app/bundles/CoreBundle/models/',
))->register();

What I get is "Service 'Phalcon\Mvc\View\Engine\Twig' was not found in the dependency injection container".

Scenario 3 (Not working, note: logic is seperate into two files but register happens in bootstrap)

bootstrap.php
<?php
use Phalcon\Mvc\Application,
    Phalcon\Loader;

try {
    $loader = include __DIR__ . "/../app/config/loader.php";

    $loader->register();

     //Twig
    require "../vendor/twig/twig/lib/Twig/Autoloader.php";
    Twig_Autoloader::register();
loader.php
<?php
$loader = new \Phalcon\Loader();

$loader->registerNamespaces(array(
    'Phalcon' => __DIR__ . '/../vendor/incubator/Library/Phalcon',
    'CoreBundle\Models' => __DIR__ . '/../app/bundles/CoreBundle/models/',
));

return $loader;

What I get is "Service 'Phalcon\Mvc\View\Engine\Twig' was not found in the dependency injection container".

Why is that happening, why is Scenario 2 or 3 not working?

edited Apr '14

The magic variable __DIR__ refers to the directory of the current file : https://www.php.net/manual/en/language.constants.predefined.php

I think the probleme is the directory you give in the $loader->registerNamespaces(array(

  • Bootstrap.php is in /public and __DIR__ refers to path/to/app/public
  • loader.php is in /app/config/ and __DIR__ refers to path/to/app/app/config

Then when you load all in bootstrap.php your pathes are ok. But when you load from load.php you should correct your path :

    loader.php
    <?php
    $loader->registerNamespaces(array(
        'Phalcon' => __DIR__  .  '/../../vendor/incubator/Library/Phalcon',
        'CoreBundle\Models' => __DIR__ . '/../../app/bundles/CoreBundle/models/',
    ));

Note the addtional "/..".

Please let me know if it fixes


A possible workarround would be to define a constant in bootstrap.php that refers to the root of your application and using it in loader.php

    bootstrap.php
    <?php
    define("APP_ROOT" , __DIR__ . "/..");
    include(APP_ROOT . "/app/config/loader.php");
    loader.php
    <?php
    $loader->registerNamespaces(array(
        'Phalcon' => APP_ROOT . '/vendor/incubator/Library/Phalcon',
        'CoreBundle\Models' => APP_ROOT . '/app/bundles/CoreBundle/models/',
    ));    


1.1k

Hi, thanks for replay, here is what I got so far.

So my project dir structure is: /var/www/phlacon -app -public -vendor

When i try to add "/.."

loader.php
<?php
$loader->registerNamespaces(array(
    'Phalcon' => __DIR__ . '/../../vendor/phalcon/incubator/Library/Phalcon',
));
bootstrap.php
        //Register an autoloader
        $loader = new Loader();

        include __DIR__ . "/../app/config/loader.php";

        echo '<pre>';
        var_dump($loader);
        exit();

        //Namespaces
        $loader->registerNamespaces(array(
            'Library' => __DIR__ . '/../app/library',
            'Engine' => __DIR__ . '/../app/modules/engine',
        ));

        $loader->register();

                //Twig
        require "../vendor/twig/twig/lib/Twig/Autoloader.php";
        Twig_Autoloader::register();

string(74) "/var/www/phalcon/app/config/../../vendor/phalcon/incubator/Library/Phalcon"

When I try to use bootstrap APP_ROOT that you have proposed here is what I get

When I var_dump($loader) in bootstrap I get this path string(67) "/var/www/phalcon/public/../vendor/phalcon/incubator/Library/Phalcon"

loader.php
<?php
$loader->registerNamespaces(array(
    'Phalcon' => APP_ROOT . '/vendor/phalcon/incubator/Library/Phalcon',
));
bootstrap.php
        //Register an autoloader
        $loader = new Loader();

        include __DIR__ . "/../app/config/loader.php";

        echo '<pre>';
        var_dump($loader);
        exit();

        //Namespaces
        $loader->registerNamespaces(array(
            'Library' => __DIR__ . '/../app/library',
            'Engine' => __DIR__ . '/../app/modules/engine',
        ));

        $loader->register();

        //Twig
        require "../vendor/twig/twig/lib/Twig/Autoloader.php";
        Twig_Autoloader::register();

The problem is the same, and yeah from what I read the path is correct? Unless I can't have "../" in mid of the path

Still when I do this directly in bootstrap.php it works fine no error.

bootstrap.php
        //Register an autoloader
        $loader = new Loader();

        //include __DIR__ . "/../app/config/loader.php";

        //Namespaces
        $loader->registerNamespaces(array(
            'Library' => __DIR__ . '/../app/library',
            'Engine' => __DIR__ . '/../app/modules/engine',
            'Phalcon' => __DIR__ . '/../vendor/phalcon/incubator/Library/Phalcon',
        ));

        $loader->register();

                //Twig
        require "../vendor/twig/twig/lib/Twig/Autoloader.php";
        Twig_Autoloader::register();