【发布时间】:2018-05-29 16:49:51
【问题描述】:
我在使用 Symfony 3.4 配置新存储库时遇到了一些麻烦。我已经使用 symfony 命令使用最后一个 LTS(3.4)创建他,并且我也使用命令添加了一个新的 Bundle。我的新 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');
}
}
但是当我尝试渲染它时,我遇到了这个错误。
找不到模板“ListerListerBundle:Default:index.html.twig”(查看:/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 搜索我的视图的地方,但我没有找到我可以在“ListerBundle/Ressources/views”中对 Symfony 说什么
在我最古老的项目中,无需其他配置即可工作。
信息:我将我的捆绑包用作可重复使用的捆绑包。
问候,
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(),
];
...
再说一遍:这里是我的依赖注入
还有文件内容:
配置.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/Default/index.html.twig
@Cerad 的原始回复
出于某种原因,S3.4 不再喜欢 Bundle:Dir:name 方法来指定树枝路径,并且 generate:bundle 命令尚未更新。不确定这是错误还是功能。上面建议的 @ListerLister/Default/index.html.twig 路径应该可以工作。尝试 bin/console debug:twig 查看您的 twig 命名空间路径。 – 赛拉德
【问题讨论】:
-
正确的模板名称应该是
ListerBundle:Default:index.html.twig(不带供应商前缀)或使用 Twig 约定@Lister/Default/index.html.twig(推荐)。 -
感谢您的回复。在第一种情况下我有同样的错误,在第二种情况下我有这个:命名空间“Lister”没有注册路径
-
你有没有把这个bundle添加到内核
getBundles()? -
我已将我的 Appkernel 添加到 PSS 部分的原始帖子中。所以是的,我有它。 symfony 知道我的注释路线。 Symfony 只是不在 bundle Resources/views 中搜索我的视图
-
出于某种原因,S3.4 不再喜欢使用 Bundle:Dir:name 方法来指定树枝路径,并且 generate:bundle 命令尚未更新。不确定这是错误还是功能。上面建议的 @ListerLister/Default/index.html.twig 路径应该可以工作。试试 bin/console debug:twig 来查看你的 twig 命名空间路径。
标签: php symfony symfony-3.4