Empty Twig template keeps throwing “Some mandatory parameters are missing” exception

一曲冷凌霜 提交于 2020-01-24 09:57:10

问题


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.

My routing.yml file:

mgfbw_blogslug:
  path: /blog/{slug}
  defaults: { _controller: MGFBWBundle:Blog:blogShow }

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,
    ));
}

My blogshow.html.twig:

{% extends 'MGFTMBundle::layout.html.twig' %}
{% block title %}Title{% endblock %}
{% block heading %}Heading{% endblock %}
{% block content %}

{% endblock %}

My layout.html.twig is too long to paste, but I swear there's no Twig {{ path() }} calls in it. I've double-checked.

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 }})

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?

Thanks for your help.


回答1:


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.

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')}) }}

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.
    }

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
    );
}



回答2:


I think your $blog is an empty array, which goes through your check, so it's unable to set the slug parameter here {{ path('mgfbw_blogslug', { 'slug': blog.slug }) }}

You are not using the right way to retrieve your blog, and the exception throw is not right as well since findBy methods return array. (unlike php array methods).

You should try like this:

public function blogShowAction(Blog $blog)
{
    return $this->render('MGFBWBundle:Blog:blogshow.html.twig', array(
        'blog' => $blog,
    ));
}

Blog $blog will implicitly use the doctrine param converter, which will throw an exception if no blog is found.



来源:https://stackoverflow.com/questions/16790970/empty-twig-template-keeps-throwing-some-mandatory-parameters-are-missing-excep

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!