Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 3.4 Use view inside my bundle

I've some trouble for the configuration of a new repository using Symfony 3.4. I've used the symfony command for create him with last LTS (3.4) and I add a new Bundle using command too. My new Bundle is up and work well but I can't use view stored inside this bundle.

I show you the structure of my Bundle :

structure

I want to use this index.html.twig in my controller like this :

<?php

namespace Lister\ListerBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class DefaultController extends Controller
{
    /**
     * @Route("/lister")
     */
    public function indexAction()
    {
        return $this->render('ListerListerBundle:Default:index.html.twig');
    }
}

But when I try to render it I've this error.

Unable to find template "ListerListerBundle:Default:index.html.twig" (looked into: /home/emendiel/Data/Code/Perso/WebLister/app/Resources/views, /home/emendiel/Data/Code/Perso/WebLister/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form).

I understand what that say, my folder is not where symfony search my view but I don't found how I can said to Symfony go in "ListerBundle/Ressources/views"

In my oldest project that was work without other configuration.

Info: I use my bundle as reusable bundle.

Regards,

PS: This is my autoload part in composer.json

"autoload": {
    "psr-4": {
        "": "src/"
    },
    "classmap": [
        "app/AppKernel.php",
        "app/AppCache.php"
    ]
},

PSS: My AppKernel :

public function registerBundles()
{
    $bundles = [
        new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
        new Symfony\Bundle\SecurityBundle\SecurityBundle(),
        new Symfony\Bundle\TwigBundle\TwigBundle(),
        new Symfony\Bundle\MonologBundle\MonologBundle(),
        new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
        new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
        new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
        new AppBundle\AppBundle(),
        new Lister\ListerBundle\ListerListerBundle(),
    ];
...

And Again: Here My dependencyInjection

enter image description here

And the content of files :

Configuration.php

<?php

namespace Lister\ListerBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
 * This is the class that validates and merges configuration from your app/config files.
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/configuration.html}
 */
class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritdoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('lister_lister');

        // Here you should define the parameters that are allowed to
        // configure your bundle. See the documentation linked above for
        // more information on that topic.

        return $treeBuilder;
    }
}

ListerListerExtension.php

<?php

namespace Lister\ListerBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
 * This is the class that loads and manages your bundle configuration.
 *
 * @link http://symfony.com/doc/current/cookbook/bundles/extension.html
 */
class ListerListerExtension extends Extension
{
    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }
}

Solution: from @Cerad

@ListerLister/Default/index.html.twig

Original response from @Cerad

For some reason, S3.4 no longer likes the Bundle:Dir:name approach to specifying twig paths and the generate:bundle command has not yet been updated. Not sure if it is a bug or feature. The @ListerLister/Default/index.html.twig path suggested above should work. Try bin/console debug:twig to see your twig namespaces paths. – Cerad

like image 234
David Allios Avatar asked Dec 15 '17 13:12

David Allios


1 Answers

The basic problem appears to be that in S3.4, twig template paths such as 'ListerListerBundle:Default:index.html.twig' are no longer supported.

Replace the path in the controller with:

'@ListerLister/Default/index.html.twig'

And all should be well. If you are ever not sure what the actual namespace prefix is then run:

bin/console debug:twig

to list them.

S3.3 still works fine so this is something that changed in 3.4. Supposed to be using the namespaced format anyways so this is not a big deal.

I did file an issue about this on github: https://github.com/sensiolabs/SensioGeneratorBundle/issues/587

We shall see what the maintainers have to say.

Update: The great and powerful Fabpot himself replied to my issue. If you want to keep using the 'ListerListerBundle:Default:index.html.twig' format for templates then edit your app/config/config.yml file:

# app/config/config.yml
framework:
    templating:
        engines: ['twig']

You should only do this if you have legacy code which still uses the old format. Use twig namespaces for all new code.

like image 150
Cerad Avatar answered Oct 17 '22 20:10

Cerad