Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
94 views
in Technique[技术] by (71.8m points)

php - 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 ListerListerBundleController;

use SymfonyBundleFrameworkBundleControllerController;
use SensioBundleFrameworkExtraBundleConfigurationRoute;

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 SymfonyBundleFrameworkBundleFrameworkBundle(),
        new SymfonyBundleSecurityBundleSecurityBundle(),
        new SymfonyBundleTwigBundleTwigBundle(),
        new SymfonyBundleMonologBundleMonologBundle(),
        new SymfonyBundleSwiftmailerBundleSwiftmailerBundle(),
        new DoctrineBundleDoctrineBundleDoctrineBundle(),
        new SensioBundleFrameworkExtraBundleSensioFrameworkExtraBundle(),
        new AppBundleAppBundle(),
        new ListerListerBundleListerListerBundle(),
    ];
...

And Again: Here My dependencyInjection

enter image description here

And the content of files :

Configuration.php

<?php

namespace ListerListerBundleDependencyInjection;

use SymfonyComponentConfigDefinitionBuilderTreeBuilder;
use SymfonyComponentConfigDefinitionConfigurationInterface;

/**
 * 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 ListerListerBundleDependencyInjection;

use SymfonyComponentDependencyInjectionContainerBuilder;
use SymfonyComponentConfigFileLocator;
use SymfonyComponentHttpKernelDependencyInjectionExtension;
use SymfonyComponentDependencyInjectionLoader;

/**
 * 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 LoaderYamlFileLoader($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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...