Symfony 3.4 在我的包中使用视图 [英] Symfony 3.4 Use view inside my bundle
问题描述
我在使用 Symfony 3.4 配置新存储库时遇到了一些麻烦.我已经使用 symfony 命令在上一个 LTS (3.4) 中创建了他,并且我也使用命令添加了一个新的 Bundle.我的新捆绑包已启动并且运行良好,但我无法使用存储在此捆绑包中的视图.
I've some trouble for the configuration of a new repository using Symfony 3.4. I've used the symfony command for create him with last LTS (3.4) and I add a new Bundle using command too. My new Bundle is up and work well but I can't use view stored inside this bundle.
我向你展示了我的 Bundle 的结构:
I show you the structure of my Bundle :
我想像这样在我的控制器中使用这个 index.html.twig :
I want to use this index.html.twig in my controller like this :
<?php
namespace ListerListerBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
class DefaultController extends Controller
{
/**
* @Route("/lister")
*/
public function indexAction()
{
return $this->render('ListerListerBundle:Default:index.html.twig');
}
}
但是当我尝试渲染它时,我遇到了这个错误.
But when I try to render it I've this error.
无法找到模板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).
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 搜索我的视图的地方,但我没有找到如何在ListerBundle/Ressources/views"中对 Symfony 说
I understand what that say, my folder is not where symfony search my view but I don't found how I can said to Symfony go in "ListerBundle/Ressources/views"
在我最古老的项目中,无需其他配置即可工作.
In my oldest project that was work without other configuration.
信息:我将我的捆绑包用作可重复使用的捆绑包.
Info: I use my bundle as reusable bundle.
问候,
PS:这是我在 composer.json 中的自动加载部分
PS: This is my autoload part in composer.json
"autoload": {
"psr-4": {
"": "src/"
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
PSS:我的 AppKernel:
PSS: My AppKernel :
public function registerBundles()
{
$bundles = [
new SymfonyBundleFrameworkBundleFrameworkBundle(),
new SymfonyBundleSecurityBundleSecurityBundle(),
new SymfonyBundleTwigBundleTwigBundle(),
new SymfonyBundleMonologBundleMonologBundle(),
new SymfonyBundleSwiftmailerBundleSwiftmailerBundle(),
new DoctrineBundleDoctrineBundleDoctrineBundle(),
new SensioBundleFrameworkExtraBundleSensioFrameworkExtraBundle(),
new AppBundleAppBundle(),
new ListerListerBundleListerListerBundle(),
];
...
再次:这里是我的dependencyInjection
And Again: Here My dependencyInjection
以及文件的内容:
配置文件
<?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
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/Default/index.html.twig
@ListerLister/Default/index.html.twig
来自@Cerad 的原始回复
Original response from @Cerad
出于某种原因,S3.4 不再喜欢 Bundle:Dir:name 方法来指定树枝路径,并且 generate:bundle 命令尚未更新.不确定这是错误还是功能.上面建议的@ListerLister/Default/index.html.twig 路径应该可以工作.尝试 bin/console debug:twig 查看您的 twig 命名空间路径.– 塞拉德
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 中,不再支持诸如 'ListerListerBundle:Default:index.html.twig' 之类的 twig 模板路径.
The basic problem appears to be that in S3.4, twig template paths such as 'ListerListerBundle:Default:index.html.twig' are no longer supported.
将控制器中的路径替换为:
Replace the path in the controller with:
'@ListerLister/Default/index.html.twig'
一切都应该很好.如果您不确定实际的命名空间前缀是什么,请运行:
And all should be well. If you are ever not sure what the actual namespace prefix is then run:
bin/console debug:twig
列出它们.
S3.3 仍然可以正常工作,所以这在 3.4 中有所改变.无论如何都应该使用命名空间格式,所以这没什么大不了的.
S3.3 still works fine so this is something that changed in 3.4. Supposed to be using the namespaced format anyways so this is not a big deal.
我确实在 github 上提交了一个关于这个的问题:https://github.com/sensiolabs/SensioGeneratorBundle/issues/587
I did file an issue about this on github: https://github.com/sensiolabs/SensioGeneratorBundle/issues/587
我们将看看维护者怎么说.
We shall see what the maintainers have to say.
更新:伟大而强大的 Fabpot 本人回复了我的问题.如果您想继续为模板使用ListerListerBundle:Default:index.html.twig"格式,请编辑您的 app/config/config.yml 文件:
Update: The great and powerful Fabpot himself replied to my issue. If you want to keep using the 'ListerListerBundle:Default:index.html.twig' format for templates then edit your app/config/config.yml file:
# app/config/config.yml
framework:
templating:
engines: ['twig']
仅当您的遗留代码仍使用旧格式时才应执行此操作.为所有新代码使用树枝命名空间.
You should only do this if you have legacy code which still uses the old format. Use twig namespaces for all new code.
这篇关于Symfony 3.4 在我的包中使用视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!