首页 文章

Symfony 3.4在我的包中使用视图

提问于
浏览
7

使用Symfony 3.4配置新的存储库时遇到了一些麻烦 . 我已经使用symfony命令创建了他的最后一个LTS(3.4),我也使用命令添加了一个新的Bundle . 我的新Bundle已经运行良好,但是我不能使用存储在这个包中的视图 .

我告诉你我的Bundle的结构:

structure

我想在我的控制器中使用这个index.html.twig,如下所示:

<?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');
    }
}

但是当我尝试渲染它时,我发现了这个错误 .

无法找到模板“ListerListerBundle:默认:index.html.twig”(查看:/ home / emendiel / Data / Code / Perso / WebLister / app / Resources / views,/ home / emendiel / Data / Code / Perso / WebLister /供应商/ symfony中/ symfony中/ src目录/ Symfony的/桥梁/嫩枝/资源/视图/表) .

我明白这说什么,我的文件夹不是symfony搜索我的视图的地方但是我没有找到我怎么说Symfony进入“ListerBundle / Ressources / views”

在我最老的项目中没有其他配置工作 .

Info: 我将我的捆绑包用作可重复使用的捆绑包 .

问候,

PS: 这是我在composer.json中的自动加载部分

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

PSS: 我的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: 这里我的dependencyInjection

enter image description here

和文件的内容:

的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: 来自@Cerad

@ ListerLister /默认/ index.html.twig

来自@Cerad的原始回复

出于某种原因,S3.4不再喜欢Bundle:Dir:name方法来指定twig路径,而generate:bundle命令尚未更新 . 不确定它是否是错误或功能 . 上面建议的@ListerLister / Default / index.html.twig路径应该有效 . 尝试bin / console debug:twig以查看twig命名空间路径 . - 塞拉德

2 回答

  • 29

    基本问题似乎是在S3.4中,不再支持twig模板路径,例如'ListerListerBundle:Default:index.html.twig' .

    用以下内容替换控制器中的路径:

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

    一切都应该好 . 如果您不确定实际的名称空间前缀是什么,那么运行:

    bin/console debug:twig
    

    列出他们 .

    S3.3仍然可以正常工作,所以这在3.4中有所改变 . 假设无论如何都要使用命名空间格式,所以这不是什么大问题 .

    我确实在github上提出了这个问题:https://github.com/sensiolabs/SensioGeneratorBundle/issues/587

    我们将看到维护者必须说些什么 .

    更新:伟大而强大的Fabpot自己回答了我的问题 . 如果您想继续使用模板的'ListerListerBundle:Default:index.html.twig'格式,请编辑您的app / config / config.yml文件:

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

    如果您的遗留代码仍使用旧格式,则应该只执行此操作 . 对所有新代码使用twig名称空间 .

  • 0
    "psr-4": {
           "ListerBundle\\": "src/ListerBundle",
        }
    

    打开项目所在文件夹中的终端 .

    $composer dump-autoload
    

相关问题