Symfony 3.4在我的捆绑包内使用视图 [英] Symfony 3.4 Use view inside my bundle
问题描述
使用Symfony 3.4配置新存储库时遇到一些麻烦.我已经使用symfony命令使用最后的LTS(3.4)创建了他,并且我也使用command添加了一个新的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 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');
}
}
但是当我尝试渲染它时,我遇到了这个错误.
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 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(),
];
...
再次:这里是我的dependencyInjection
And Again: Here My dependencyInjection
文件内容:
Configuration.php
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
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
@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"之类的树枝模板路径.
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屋!