Php Symfony 3.4在我的包中使用视图
我在使用Symfony 3.4配置新存储库时遇到了一些问题。我使用了symfony命令来创建他与最后的LTS(3.4),我还使用这个命令添加了一个新的包。我的新捆绑包已启动,工作正常,但我无法使用此捆绑包中存储的视图 我将向您展示我的捆绑包的结构: 我想在控制器中使用index.html.twig,如下所示:Php Symfony 3.4在我的包中使用视图,php,symfony,symfony-3.4,Php,Symfony,Symfony 3.4,我在使用Symfony 3.4配置新存储库时遇到了一些问题。我使用了symfony命令来创建他与最后的LTS(3.4),我还使用这个命令添加了一个新的包。我的新捆绑包已启动,工作正常,但我无法使用此捆绑包中存储的视图 我将向您展示我的捆绑包的结构: 我想在控制器中使用index.html.twig,如下所示: <?php namespace Lister\ListerBundle\Controller; use Symfony\Bundle\FrameworkBundle\Contr
<?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');
}
}
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(),
];
...
再一次:这是我的依赖注射
以及文件的内容:
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;
}
}
基本问题似乎是,在S3.4中,不再支持诸如“ListerListerBundle:Default:index.html.twig”之类的细枝模板路径
将控制器中的路径替换为:
'@ListerLister/Default/index.html.twig'
一切都会好起来的。如果您不确定实际的名称空间前缀是什么,请运行:
bin/console debug:twig
列出它们
S3.3仍然可以正常工作,因此这在3.4中有所改变。应该使用名称空间格式,所以这不是什么大问题
我确实在github上提交了一个关于此的问题:
我们将看看维修人员要说些什么
更新:伟大而强大的Fabbot自己回答了我的问题。如果要继续使用“ListerListerBundle:Default:index.html.twig”格式作为模板,请编辑app/config/config.yml文件:
# app/config/config.yml
framework:
templating:
engines: ['twig']
只有当您有仍然使用旧格式的遗留代码时,才应该这样做。将细枝名称空间用于所有新代码。#config/config.yml
#在路由器之后添加like
路由器:
资源:'%kernel.project\u dir%/app/config/routing.yml'
严格要求:~
模板:
引擎:[twig]正确的模板名称应该是ListerBundle:Default:index.html.twig
(不带供应商前缀)或使用twig约定@Lister/Default/index.html.twig
(推荐)。感谢您的回复。在第一种情况下,我有相同的错误,在第二种情况下,我有以下错误:命名空间“Lister”没有注册路径。您是否已将此捆绑包添加到内核getBundles()
?我已在PSS中的原始帖子部分添加了我的Appkernel。是的,我有。symfony知道我的注释路线。Symfony只是不在bundle Resources/views中搜索我的视图出于某种原因,S3.4不再喜欢bundle:Dir:name方法来指定细枝路径,并且generate:bundle命令尚未更新。不确定这是错误还是功能。上面建议的@ListerLister/Default/index.html.twig路径应该可以工作。尝试bin/console debug:twig查看您的twig名称空间路径。我使用的是3.4,它不需要在config.yml中添加引擎:['twig']。对我来说唯一的问题是关于细枝名称空间。可以从bin/控制台debug:twig中找到它
bin/console debug:twig
# app/config/config.yml
framework:
templating:
engines: ['twig']