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.

Message upon startup: "A dependency injection object is required to access internal services"

Hi,

I am porting my Phalcon-based application to a Raspberry Pi B+ running Arch Linux + Nginx.

Since there wasn't a v1.3.x image for Phalcon for Arch Linux, I successfully compiled Phalcon on the Raspberry Pi, installed the extension in the appropriate directory, and enabled it in php.ini. A call to phpinfo() shows the phalcon extension.

After pointing to the application's "public" directory as webroot in nginx.conf, then starting Nginx and php-fpm, I only see the following line on the browser when I try to access the application:

"A dependency injection object is required to access internal services"

Any suggestions about how to resolve this?

Btw, this app is working on a different machine using Ubuntu+Nginx. Would Raspbian instead of Arch Linux on the Raspberry Pi be a better choice?

Thanks!



5.7k

could you please post your bootstrap code here?



2.1k
edited Nov '14

Here is the bootstrap code:

<?php

error_reporting(E_ALL);

try {

    /**
     * Read the configuration
     */
    $config = include __DIR__ . "/../app/config/config.php";

    /**
     * Read auto-loader
     */
    include __DIR__ . "/../app/config/loader.php";

    /**
     * Read services
     */
    include __DIR__ . "/../app/config/services.php";

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

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

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

AND Here is Services.php:

<?php

use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Db\Adapter\Pdo\Sqlite as DbAdapter;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;

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

/**
 * The URL component is used to generate all kind of urls in the application
 */
$di->set('url', function () use ($config) {
    $url = new UrlResolver();
    $url->setBaseUri($config->application->baseUri);

    return $url;
}, true);

/**
 * Setting up the view component
 */
$di->set('view', function () use ($config) {

    $view = new View();

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

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

    return $view;
}, true);


/**
 * Database connection for Sqlite based on config.php
 */
$di->set('db', function () use ($config) {
    return new DbAdapter(array(
        'dbname' => $config->database->dbname
    ));
});


/**
 * If the configuration specify the use of metadata adapter use it or use memory otherwise
 */
$di->set('modelsMetadata', function () {
    return new MetaDataAdapter();
});

/**
 * Start the session the first time some component request the session service
 */
$di->set('session', function () {
    $session = new SessionAdapter();
    $session->start();

    return $session;
});

/**
 * Create a models cache for frequently used static data
 */
$di->set('modelsCache',function() {

    //Create a Data frontend and set a default lifetime to 1 day
    $frontend = new Phalcon\Cache\Frontend\Data(array(
        'lifetime' => 86400
    ));

    // Set up APC Cache
    $cache = new Phalcon\Cache\Backend\Apc($frontend, array(
        'prefix' => 'cache'
    ));

    return $cache;
});

/**
 * Create a regular cache for frequently used static data
 */
$di->set('appCache',function() {

    //Create a Data frontend and set a default lifetime to 1 day
    $frontend = new Phalcon\Cache\Frontend\Data(array(
        'lifetime' => 86400
    ));

    // Set up APC Cache
    $cache = new Phalcon\Cache\Backend\Apc($frontend, array(
        'prefix' => 'appcache'
    ));

    return $cache;
});


/**
 * Take control of dispatcher in order to override EventManager methods
 */
$di->set('dispatcher', function() use ($di) {

    //Obtain the standard eventsManager from the DI
    $eventsManager = $di->getShared('eventsManager');

    //Instantiate the Security plugin
    $security = new Security($di);

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

    $dispatcher = new Phalcon\Mvc\Dispatcher();

    //Bind the EventsManager to the Dispatcher
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
});


5.7k

There is no problem about it, it should works



2.1k

There is no problem about it, it should works

Right, the same files work on Ubuntu+Nginx and I ported them unchanged. Could it be some environment variables or something else related to Arch Linux? Since this is an exception thrown by Phalcon, it means the extension is being loaded.

This isn't a problem down at the OS level - this is simply a PHP code error. I've received this error before - it was caused by me not passing the dependency injector variable properly.

Is the error giving you a specific file and line number?



2.1k
Accepted
answer

FOUND THE PROBLEM AND FIXED IT

There was no specific file and line number error, but it was complaining about "open_basedir restrictions". I had missed that the first time around, looked everywhere but nginx error log.

The open_basedir parameter in my php.ini file did not include the top-level application directory, so the include statements in the bootstrap were not working. I had only included the /usr/share/nginx/html/ path in open_basedir but that is actually a softlink to my Phalcon app's public directory (i.e. /usr/share/nginx/html -> /<my phalcon app location>/myphalconapp/public).

Now, I have cleaned up my installation by moving my Phalcon app under the /usr/share/webapps directory. This directory was already included in the open_basedir parameter. Of course, the softlink above had to be reset to: /usr/share/nginx/html -> /usr/share/webapps/myphalconapp/public.

Thanks for spurring me on. Everything is working great now. To have this running on a Raspberry Pi is really cool.