Search code examples
phpsymfonylocalesymfony5sylius

Trying to install SyliusProductBundle on Symfony 5.4: LocaleNotFoundException


I am trying to install the standalone SyliusProductBundle on Symfony 5.4, but keep getting a LocaleNotFoundException: "Locale could not be found!"

Started with a clean Symfony 5.4 installation (--webapp) then followed the (outdated) documentation here: https://docs.sylius.com/en/1.12/components_and_bundles/bundles/SyliusProductBundle/installation.html

Installed the bundle:

composer require sylius/product-bundle -w

Added the bottom 3 lines to bundles.php manually because they weren't added automatically:

<?php

return [
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
    Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
    Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true],
    Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
    Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
    Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true],
    Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
    Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
    Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
    Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true],
    winzou\Bundle\StateMachineBundle\winzouStateMachineBundle::class => ['all' => true],
    JMS\SerializerBundle\JMSSerializerBundle::class => ['all' => true],
    Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle::class => ['all' => true],
    FOS\RestBundle\FOSRestBundle::class => ['all' => true],
    BabDev\PagerfantaBundle\BabDevPagerfantaBundle::class => ['all' => true],
    Sylius\Bundle\ResourceBundle\SyliusResourceBundle::class => ['all' => true],
    Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle::class => ['all' => true],

    Sylius\Bundle\ProductBundle\SyliusProductBundle::class => ['all' => true],
    Sylius\Bundle\AttributeBundle\SyliusAttributeBundle::class => ['all' => true],
    Sylius\Bundle\LocaleBundle\SyliusLocaleBundle::class => ['all' => true],
];

Created config/config.yml:

stof_doctrine_extensions:
    orm:
        default:
            sluggable: true
            timestampable: true

And updated the database:

php bin/console doctrine:schema:update --force

Now when I try to get a product repository it throws the LocaleNotFoundException:

$repository = $this->container->get('sylius.repository.product');

I've tried adding the locale manually in the sylius_locale table, as described here, but that doesn't seem to work: https://github.com/Sylius/Sylius/issues/8976#issuecomment-468678369

Also tried renaming the sylius_locale table to sylius_channel_locales (as per the last comment in that issue) and adding the locale to that, but that doesn't work either.

How can I set the locale?


Solution

  • Found the answer in the issues here: https://github.com/Sylius/Sylius/issues/8452 and https://github.com/Sylius/Sylius/issues/7853

    The solution is to replace the Kernel.php file in the src directory with the following:

    <?php
    
    namespace App;
    
    use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
    use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    use Symfony\Component\HttpKernel\Kernel as BaseKernel;
    
    class Kernel extends BaseKernel implements CompilerPassInterface
    {
        use MicroKernelTrait;
    
        public function process(ContainerBuilder $container)
        {
            $definition = $container->getDefinition("sylius.context.locale.composite");
            $definition->setDecoratedService(null);
        }
    }
    

    You do not have to add the locale to the database.