即使我返回响应,Symfony也会抛出“未返回响应”错误

时间:2018-11-10 22:03:41

标签: php symfony

1)我做了这个控制器功能:

 /**
 * @Route("/add", name="add" )
 */
public function add_question()
{
   return $this->render("add.html.twig",[]);
}

2)当我转到http://127.0.0.1:8000/add时,Symfony返回此错误:

The controller must return a response (null given). Did you forget to add a 
    return statement somewhere in your controller?

3)我有一堆其他的控制器,它们具有相同的结构并且可以正常工作。

4)每当我要添加新的Controller时,都会引发此错误。

5)我试图返回Response对象-也不起作用。

编辑: 我的 config / routes / annotations.yaml 文件:

controllers:
resource: ../../src/Controller/
type: annotation

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方法。

我的项目中有几个控制器。 其中之一几乎位于文件的顶部,并具有以下路由:

/**
* @Route("/{question_id}", name="questions" )  <--QUESTION ROUTE
*/

控制器的逻辑是这样的:

function GenerateQuestionPage($question_id)

1find question in my database whose $id is equal to question_id.
2a. If you find such question then render proper twig template.
2b. If You don't find such question then you echo "No such question found";

每当我想转到其路由定义在以下 GenerateQuestionPage Controller定义以下的页面时,Symfony都认为我试图将Question Route与 非数字的question_id。然后它正在数据库中搜索这样的问题,但显然找不到,因此它没有返回Response对象。

我的解决方案是将GenerateQuestionPage控制器重新定位到文件底部。

相关问题