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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Symfony 3.4在我的包中使用视图脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
使用Symfony 3.4配置新的存储库时遇到了一些麻烦.我已经使用symfony命令创建了他的最后一个LTS(3.4),我也使用命令添加一个新的Bundle.我的新Bundle已经运行良好,但是我不能使用存储在这个包中的视图.

我告诉你我的Bundle的结构:

我想在我的控制器中使用这个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');
    }
}

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

我明白这说什么,我的文件夹不是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 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(),];
...

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

文件内容

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

解决方案:来自@Cerad

@ ListerLister /认/ index.html.twig

来自@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名称空间.

脚本宝典总结

以上是脚本宝典为你收集整理的php – Symfony 3.4在我的包中使用视图全部内容,希望文章能够帮你解决php – Symfony 3.4在我的包中使用视图所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。