空的 Twig 模板不断抛出“缺少某些必需参数";例外 [英] Empty Twig template keeps throwing "Some mandatory parameters are missing" exception
问题描述
我尝试的每次搜索都会让我遇到占位问题.那不是我的情况.让我们看看你们是否可以帮助我.提前致谢.
Every search I try with this, gets me to host placeholding problems. That's not my case. Let's see if you guys can help me. Thanks in advance.
我的routing.yml文件:
My routing.yml file:
mgfbw_blogslug:
path: /blog/{slug}
defaults: { _controller: MGFBWBundle:Blog:blogShow }
我的博客ShowAction:
My blogShowAction:
public function blogShowAction()
{
$request = $this->getRequest();
$slug = $request->get('slug');
$em = $this->getDoctrine()->getEntityManager();
$blog = $em->getRepository('MGFBWBundle:Blog')->findBySlug($slug);
if (!$blog) {
throw $this->createNotFoundException('No posts.');
}
return $this->render('MGFBWBundle:Blog:blogshow.html.twig', array(
'blog' => $blog,
));
}
我的 blogshow.html.twig:
My blogshow.html.twig:
{% extends 'MGFTMBundle::layout.html.twig' %}
{% block title %}Title{% endblock %}
{% block heading %}Heading{% endblock %}
{% block content %}
{% endblock %}
我的 layout.html.twig 太长无法粘贴,但我发誓其中没有 Twig {{ path() }} 调用.我已经仔细检查过了.
My layout.html.twig is too long to paste, but I swear there's no Twig {{ path() }} calls in it. I've double-checked.
所以从我的博客索引页面我生成了一个 mgfbw_blogslug 路径:
So from my blog index page I generate a mgfbw_blogslug path:
<a href="{{ path('mgfbw_blogslug', { 'slug': blog.slug }) }}">Comments</a> ({{ blog.comments.count }})
我的问题来了.在尝试了一切之后,我的 blogshow.html.twig 故意为空,但我不断收到此异常:
And here comes my problem. My blogshow.html.twig is empty on purpose, after trying everything and more, but I keep getting this exception:
An exception has been thrown during the rendering of a template ("Some mandatory parameters are missing ("slug") to generate a URL for route "mgfbw_blogslug".") in "MGFBWBundle:Blog:blogshow.html.twig".
有人遇到同样的问题吗?我没有在模板中生成任何路径!我怎么会得到这个异常?
Anyone having the same problem? I'm not generating any path in the template! How come I get this exception?
感谢您的帮助.
推荐答案
您博客实体上的 getSlug 方法返回 null.您没有设置 slug,但您的路线需要一个 slug.path方法不知道如何生成路由,因为缺少必选参数slug.
your getSlug method on your blog entity returns null. You don't have the slug set but your route requires a slug. The path method does not know how to generate the route because the mandatory parameter slug is missing.
在您的路由中为 slug 参数添加一个默认值(并从您的控制器中捕获该默认 slug 渲染类似 - 这篇博文不存在).
add a default value for the slug parameter in your routing ( and catch that default slug from your controller rendering something like - this blogpost does not exist ).
mgfbw_blogslug:
path: /blog/{slug}
defaults: { _controller: MGFBWBundle:Blog:blogShow, slug: default_slug }
和/或像这样在树枝中向路径方法添加默认值:
and/or add a default-value to your path method in twig like this:
{{ path('route', {'slug': blog.slug|default('default_slug')}) }}
检查控制器中的默认 slug 并为用户返回一些有用的信息.例子
check for default slug in your controller and return some useful information for the user. example
public function blogShowAction($slug)
{
if (!$slug) {
// ... return some template.
}
最好的方法是使用 @ParamConverter 让你的实体自动从实体管理器中获取并传递给您的 blogShowAction 方法.也许使用@Template 来清理你的控制器.如果没有博客实体,ParamConverter 会抛出 NotFoundException,因此请确保正确捕获它.
Best way would be using @ParamConverter to have your entity fetched from the entity manager automatically and passed to your blogShowAction method. Maybe use @Template to clean up your controller. ParamConverter throws NotFoundException if there is no blog entity, so make sure you catch it properly.
/**
* @ParamConverter("blog", options={"mapping": {"slug": "slug"}})
* @Template('MGFBWBundle:Blog:blogshow.html.twig')
*/
public function blogShowAction(Blog $blog)
{
return array(
'blog' => $blog
);
}
这篇关于空的 Twig 模板不断抛出“缺少某些必需参数";例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!