Search code examples
symfonytemplatescontrollertwigtwig-extension

Symfony2.7 : Twig render controller() ignore my parameters


I have a trouble with a symfony exeption and some parameter i try to get. I have absolutely no idea why i get this exception after thousands checks of my code and on the internet.

The exception is :

An exception has been thrown during the rendering of a template ("Some mandatory parameters are missing ("slug") to generate a URL for route "blogcomment_create".") in @Blog\Default\Blog\singlePost.html.twig at line 29.

The route configuration :

blogcomment_create:
    path:   /{slug}/comment/create/
    defaults: { _controller: "BlogBundle:Comment:create" }
    requirements: { methods: post }

My code in my twig template to import my controller:

<footer>
    {{ render(controller('BlogBundle:Comment:create', {
        'slug': Post.slugname
    })) }}
</footer>

My Action declaration :

 public function createAction(Request $request, $slug = null)
{
    //...
}

I starting to get mad with this exception, i have no clues why i get her and i realy need a good pair of eyes.

i try tons of exemples :

How to insert a Controller in Twig with "render" in Symfony 2.2?

symfony2 - twig - how to render a twig template from inside a twig template

Change template via parameter when render a controller action?

...

Here a dump of the fonction :

object(Symfony\Component\HttpKernel\Controller\ControllerReference)[992]
    public 'controller' => 
        string 'BlogBundle:Comment:create' (length=31)
    public 'attributes' => 
        array (size=1)
            'slug' => string 'article-de-test-yolowowo' (length=24)
    public 'query' => 
        array (size=0)
            empty

Solution

  • Ok i finaly get it !

    The problem was not at all in my twig template but in my controller. I have a fonction to generate my form in this controller. I call her in my action :

    private function createCreateForm(Comment $entity, $slug)
    {
        $form = $this->createForm(new CommentType(), $entity, array(
            'action' => $this->generateUrl('blogcomment_create', array('slug' => $slug)),
            'method' => 'POST',
        ));
    
        $form->add('submit', 'submit', array('label' => 'Create'));
    
        return $form;
    }
    

    In this function i forgot to pass parameters in this fonction :

    $this->generateUrl()
    

    I really need to sleep sooner when i get that kind of problems >.>

    I hope my problem will help other peoples in the future.