加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Symfony 3.4在我的包中使用视图

发布时间:2020-12-13 13:15:49 所属栏目:PHP教程 来源:网络整理
导读:使用Symfony 3.4配置新的存储库时遇到了一些麻烦.我已经使用symfony命令创建了他的最后一个LTS(3.4),我也使用命令添加了一个新的Bundle.我的新Bundle已经运行良好,但是我不能使用存储在这个包中的视图. 我告诉你我的Bundle的结构: 我想在我的控制器中使用这
使用Symfony 3.4配置新的存储库时遇到了一些麻烦.我已经使用symfony命令创建了他的最后一个LTS(3.4),我也使用命令添加了一个新的Bundle.我的新Bundle已经运行良好,但是我不能使用存储在这个包中的视图.

我告诉你我的Bundle的结构:

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

<?php

namespace ListerListerBundleController;

use SymfonyBundleFrameworkBundleControllerController;
use SensioBundleFrameworkExtraBundleConfigurationRoute;

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

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

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).

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

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

信息:我使用我的捆绑包作为可重复使用的捆绑包.

问候,

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

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

PSS:我的AppKernel:

public function registerBundles()
{
    $bundles = [
        new SymfonyBundleFrameworkBundleFrameworkBundle(),new SymfonyBundleSecurityBundleSecurityBundle(),new SymfonyBundleTwigBundleTwigBundle(),new SymfonyBundleMonologBundleMonologBundle(),new SymfonyBundleSwiftmailerBundleSwiftmailerBundle(),new DoctrineBundleDoctrineBundleDoctrineBundle(),new SensioBundleFrameworkExtraBundleSensioFrameworkExtraBundle(),new AppBundleAppBundle(),new ListerListerBundleListerListerBundle(),];
...

再说一遍:这里我的依赖注入

和文件的内容:

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

解决方案:来自@Cerad

@ ListerLister /默认/ index.html.twig

来自@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

基本问题似乎是在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名称空间.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读