我告诉你我的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');
}
}
但是当我尝试渲染它时,我发现了这个错误.
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搜索我的视图的地方但是我没有找到我怎么说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的原始回复
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
用以下内容替换控制器中的路径:
'@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名称空间.