简体   繁体   English

添加参数时Symfony 2路由错误

[英]Symfony 2 route error when adding parameters

I seem to have come across an issue with Symfony 2 that I have not seen before (or more likley im missing something really obvious). 我似乎遇到了一个Symfony 2的问题,这是我以前从未见过的(或者更多likley我缺少真正明显的东西)。

I had a route which took no parameters and worked perfectly, linking up to the controller and displaying the correct view. 我有一条路线,该路线没有参数,并且运行良好,已链接到控制器并显示正确的视图。 I then updated the route to take a single parameter like so: 然后,我将路线更新为采用单个参数,如下所示:

# Note routing
    gibbo_dummy_notes_Add:
        pattern:  /notes/add/{notebook}
        defaults: { _controller: GibboDummyBundle:Note:add, notebook: 0}
        requirements:
                _method:  POST|GET

However if I now try and access this route notes/add/test I get the error The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller? 但是,如果现在尝试访问此路由notes/add/test则会收到错误消息The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller? The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller? . If I remove the parameter it works perfectly notes/add . 如果我删除该参数,它可以完美地工作在notes/add

This setup is exactly the same as the rest of the routes that use parameters in my app. 此设置与在我的应用中使用参数的其余路由完全相同。

The action in the controller definitely returns a response object. 控制器中的动作肯定会返回一个响应对象。 If I place a die('test'); 如果我die('test'); at the top of the action and remove the parameter from the URL I reach the die statement and see 'test', but if I add the parameter to the URL it shows the error, so its clearly not reaching my controller or action when including the parameter but as far as I can tell the route is setup correctly, and im not getting the normal route not defined error I would expect to see if there was an issue with the url. 在操作的顶部并从URL删除参数,我到达了die语句并看到“测试”,但是如果我将参数添加到URL,它将显示错误,因此当包含参数,但据我所知,路由设置正确,并且我没有收到未定义的常规路由错误,我希望查看网址是否存在问题。

Im running it under the dev environment and I have tried other browsers / cleared cache etc. The action inside the controller NoteController looks like 我在开发环境下运行它,我尝试了其他浏览器/清除了缓存等。控制器NoteController内的操作如下所示

public function addAction($notebook)
{

    $pageTitle = 'Note';


    $form = $this->createForm(new NoteType(), null, array(
        'validation_groups' => array('add')
    ));

    $request = $this->getRequest();

    if ($request->isMethod('POST')) {
        $form->bind($request);

        if ($form->isValid()) {

            /** @var $em \Doctrine\ORM\EntityManager */
            $em = $this->getDoctrine()->getManager();

            /** @var $note Note */
            $note = $form->getData();
            $note->setNotebook();

            $em->persist($note);
            $em->flush();

            return $this->redirect($this->generateUrl('gibbo_dummy_note_View'));
        }
    }

    return $this->render('GibboDummyBundle:Note:new.html.twig', array(
        'pageTitle' => $pageTitle,
        'form_add'  => $form->createView(),
        'selected' => 'codebooks'
    ));


}

Can anyone shed some light about what I might be doing wrong? 谁能告诉我我可能做错了什么?

Please add the controller code for gibbo_dummy_note_View route. 请为gibbo_dummy_note_View路线添加控制器代码。

Most likely, your redirection ends up in a bad controller method - the one that does not have return statement 最有可能的是,您的重定向最终以错误的控制器方法结束-一种没有return语句的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM