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

Integration PHP-DI with Phalcon

Hey,

I wnat to use PHP-DI instead of Phalcon\Di default container. According to the docs, i had to implement Phalcon\DiInterface. But i have troubles with that. I decide to look how Syfmony2 is integrated with PHP-DI. Here is implementation of PHP-DI for Symfony.

So i came up with something similar: <?php namespace Plugin\Container;

use DI\NotFoundException;
use Interop\Container\ContainerInterface;

class PhpDiContainer extends \Phalcon\Di implements \Phalcon\DiInterface
{
    /**
     * @var ContainerInterface
     */
    private $fallbackContainer;

    /**
     * @return ContainerInterface
     */
    public function getFallbackContainer()
    {
        return $this->fallbackContainer;
    }

    /**
     * @param ContainerInterface $container
     */
    public function setFallbackContainer(ContainerInterface $container)
    {
        $this->fallbackContainer = $container;
    }

    public function get($name, $parameters = null)
    {
        if (parent::has($name)) {
            return parent::get($name, $parameters);
        }

        if (! $this->fallbackContainer) {
            return false;
        }

        try {
            $entry = $this->fallbackContainer->get($name);
            return $entry;
        } catch (NotFoundException $e) {
            throw new \Exception($e->getMessage());
        }

        return null;
    }

    public function has($name)
    {
        if (parent::has($name)) {
            return true;
        }

        if (! $this->fallbackContainer) {
            return false;
        }

        return $this->fallbackContainer->has($name);
    }
}

The problem is that if i create a new instance of this container and pass it to application like this:

$container = new \Plugin\Container\PhpDiContainer();
$application = new \Phalcon\Mvc\Application($container);
$application->handle()->getContent();

I got Trying to call method handle on a non-object. I don't know what to do next, i don't know if my implementation is good. Can you help me implement PHP-DI for Phalcon?

Hi,

as your class extends Phalcon\DI (there is a typo here you've written Di not DI), no need to tell it implements Phalcon\DIInterface.

Bitwise, if you want it to work in PHP-DI you should first make it PHP-DI compatible so tell it implements ContainerInterface.

For the pb, if you've got this error, it means $application is not an object... don't you have any error in the code ?

Hi there, I'm the author of PHP-DI,

I don't know if I can help a lot because I've never used Phalcon, but a few things:

  • You need to configure the fallback container (which is PHP-DI)

    $container = new \Plugin\Container\PhpDiContainer();
    
    // PHP-DI is the fallback container to Phalcon
    $builder = new \DI\ContainerBuilder();
    $builder->wrapContainer($container);
    $container->setFallbackContainer($builder->build());
  • "Trying to call method handle on a non-object" That error message is not enough to understand what's going on, add a more complete trace.

  • Maybe you don't have to do all this? You could maybe simply write an adapter that implements Phalcon\DiInterface and calls PHP-DI? Or is the Phalcon container already pre-configured with entries?

  • If you succeed in integrating them, that would be interesting to release that on GitHub some day

Hi there,

Could you update for this problem? Just code snippet or code source on github(or gist).

Thank you so much.