【问题标题】:Symfony 4 bugged routeSymfony 4窃听路线
【发布时间】:2019-02-02 01:18:15
【问题描述】:

我试图创建一个路由类型“any”以允许getpost,但它不起作用,所以我尝试像这样在我的路由注释中添加两条路由

 /**
 * @Route("/news", name="newsSinglePOST", methods={"post"})
 * @Route("/news", name="newsSingleGET", methods={"get"})
 * @return \Symfony\Component\HttpFoundation\Response
 */

这也不起作用,但后来我尝试删除一个,但路由现在无法使用,控制器确实说了一些关于缺少返回语句的内容,并且在我的路由器中我有以下行:

newsGet ANY ANY ANY /news

为了让我的控制器再次工作,我不得不将 /news 更改为 news2 所以这条路线现在不知何故无法使用

我尝试清除开发缓存但没有成功

所以我的问题是如何恢复我的路线/news

【问题讨论】:

  • 方法名必须大写
  • 不要使用两个注解条目。正确的方式是* @Route("/news", name="newsSingleGET", methods="GET|POST")
  • @MarcosRegis 在我看来,您的建议比下面建议的更简单、更好,发布答案,我会接受
  • 我这里做了一个测试,我错了。你在 Symfony 4.1 上使用 Symfony\Component\Routing\Annotation\Route; ` * @Route("/news", name="newsSinglePOST", methods={"post"}) 的方式` ` * @Route("/news", name="newsSingleGET", methods={"get"})` 当我运行php bin/console debug:router 时,两条路线都会显示。哪个版本的 Symfony?
  • 检查thisthis

标签: php symfony methods action symfony-routing


【解决方案1】:

你试过这样使用method注解吗?

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

/**
 * @Route("/news", name="news")
 * @Method("GET|POST")
 * @return \Symfony\Component\HttpFoundation\Response
 */

【讨论】:

  • 感谢您的建议,我会在恢复路线后尝试一下
【解决方案2】:

您可以使用以下解决方案:

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

/**
 * @Route("/news", name="newsSinglePOST")
 * @Route("/news", name="newsSingleGET")
 * @Method({"GET", "POST"})
 */

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 2011-04-06
    • 2021-12-27
    • 2015-01-13
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多