4

Trying to use doctrine with slim framework 4, but there is no documentation available. By following information on the link http://www.slimframework.com/docs/v3/cookbook/database-doctrine.html gives error.

Class 'slim container' not found

<?php

// bootstrap.php

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Cache\FilesystemCache;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Tools\Setup;
use Slim\Container;

require_once __DIR__ . '/vendor/autoload.php';

$container = new Container(require __DIR__ . '/settings.php');

$container[EntityManager::class] = function (Container $container): EntityManager {
    $config = Setup::createAnnotationMetadataConfiguration(
        $container['settings']['doctrine']['metadata_dirs'],
        $container['settings']['doctrine']['dev_mode']
    );

    $config->setMetadataDriverImpl(
        new AnnotationDriver(
            new AnnotationReader,
            $container['settings']['doctrine']['metadata_dirs']
        )
    );

    $config->setMetadataCacheImpl(
        new FilesystemCache(
            $container['settings']['doctrine']['cache_dir']
        )
    );

    return EntityManager::create(
        $container['settings']['doctrine']['connection'],
        $config
    );
};
Lovepreet Singh
  • 4,792
  • 1
  • 18
  • 36

1 Answers1

3

Slim 4 hasn't integrated container. You can use for example php-di/php-di (add it by composer). After that you can use it like this:

// bootstrap.php
use DI\ContainerBuilder;

require __DIR__ . '/vendor/autoload.php';

// Instantiate PHP-DI ContainerBuilder
$containerBuilder = new ContainerBuilder();

// Set up settings
$settings = require __DIR__ . '/app/settings.php';
$settings($containerBuilder);

// Set up dependencies
$dependencies = require __DIR__.'/app/dependencies.php';
$dependencies($containerBuilder);

// Build PHP-DI Container instance
$container = $containerBuilder->build();

// Instantiate the app
AppFactory::setContainer($container);
$app = AppFactory::create();

/* .. other code here .. */
?>

<? 
// settings.php

use DI\ContainerBuilder; 

return function (ContainerBuilder $containerBuilder) {
// Global Settings Object
$containerBuilder->addDefinitions([
'settings' => [
   'doctrine' => [
            'dev_mode' => true,
            'cache_dir' => __DIR__.'/../var/cache/doctrine',
            'metadata_dirs' => [__DIR__.'/../src/Domain/'],
            'connection' => [
                'driver' => 'pdo_mysql',
                'host' => 'webdb',
                'port' => 3306,
                'dbname' => 'db',
                'user' => 'user',
                'password' => 'pass',
            ]
  ]
]);
}

?>

<?
//dependencies.php
use DI\ContainerBuilder;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Cache\FilesystemCache;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;

return function (ContainerBuilder $containerBuilder) {
$containerBuilder->addDefinitions([
         EntityManagerInterface::class => function (ContainerInterface $c): EntityManager {
        $doctrineSettings = $c->get('settings')['doctrine'];

        $config = Setup::createAnnotationMetadataConfiguration(
            $doctrineSettings['metadata_dirs'],
            $doctrineSettings['dev_mode']
        );

        $config->setMetadataDriverImpl(
            new AnnotationDriver(
                new AnnotationReader,
                $doctrineSettings['metadata_dirs']
            )
        );

        $config->setMetadataCacheImpl(
            new FilesystemCache($doctrineSettings['cache_dir'])
        );

        return EntityManager::create($doctrineSettings['connection'], $config);
    }
]);
}       

?>
  • This gives me the following error message Expected type 'Doctrine\Common\Persistence\Mapping\Driver\MappingDriver'. Found 'Doctrine\ORM\Mapping\Driver\AnnotationDriver'. – Marcus Christiansen May 08 '20 at 13:52
  • Too few info for debug. Look to namespaces from classes you have used in ```dependencies.php``` Are you copy - pasted this code? It's only example how to, and maybe something was changed in doctrine. – Максим Дубинин May 08 '20 at 14:05
  • This method is in the Configuration.php file. I thought you might know what has changed in Slim. This is my first time working with the framework. It seems like that function now expects `MappingDriver $driverImpl` – Marcus Christiansen May 08 '20 at 14:16
  • Hm, I can't see now this file in my project, can you show your code? – Максим Дубинин May 08 '20 at 14:29
  • I've created a github repo at https://github.com/marcuschristiansen/slim-boilerplate . The file is slim-boilerplate/app/orm.php - Line 26-31 Hope this helps If I pass `MappingDriver $driverImpl` the error goes away but I'm not sure if that is all. – Marcus Christiansen May 08 '20 at 15:29
  • public function setMetadataDriverImpl(MappingDriver $driverImpl) use interface MappingDriver, we use class AnnotationDriver which extends Doctrine\Common\Persistence\Mapping\Driver\AnnotationDriver as AbstractAnnotationDriver.Also Doctrine\Common\Persistence\Mapping\Driver\AnnotationDriver implements MappingDriver.I think Problem in class autoloading, not in slim.Try to use composer dump-autoload -o. – Максим Дубинин May 09 '20 at 02:55