【问题标题】:Symfony5 throws 404 in browser, but route is found in cliSymfony5 在浏览器中抛出 404,但在 cli 中找到路由
【发布时间】:2021-02-18 22:58:00
【问题描述】:

我正在使用 react+symfony 和 webpack。一切都适用于简单的网址,例如。 url 中有一个斜杠 ("/aboutus","/moodle") 但是当我尝试使用多个斜杠访问路由时 ("/admin/users" em>) 我得到 NotFoundResource 错误。但是router:match我会找到它。

服务器日志:

|WARN | SERVER GET  (404) /admin/users 
|ERROR| REQUES Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /admin/users"" at /home/rtkpf/Programming/Niners/vendor/symfony/http-kernel/EventListener/RouterListener.php line 136 

路由器:匹配 /admin/users

(master) [1]> bin/console router:match /admin/users


                                                                                                                        
 [OK] Route "index" matches                                                                                             
                                                                                                                        

+--------------+---------------------------------------------------------+
| Property     | Value                                                   |
+--------------+---------------------------------------------------------+
| Route Name   | index                                                   |
| Path         | /{reactRouting}                                         |
| Path Regex   | {^/(?P<reactRouting>.+)?$}sDu                           |
| Host         | ANY                                                     |
| Host Regex   |                                                         |
| Scheme       | ANY                                                     |
| Method       | GET                                                     |
| Requirements | reactRouting: .+                                        |
| Class        | Symfony\Component\Routing\Route                         |
| Defaults     | _controller: App\Controller\UserController::index()     |
|              | reactRouting: NULL                                      |
| Options      | compiler_class: Symfony\Component\Routing\RouteCompiler |
|              | utf8: true                                              |
+--------------+---------------------------------------------------------+

组件:

class DefaultController extends AbstractController
{
    /**
     * @Route("/{reactRouting}", name="index", defaults={"reactRouting": null}, methods="GET", requirements={"reactRouting":".+"})
     */
    public function index()
    {
        return $this->render('default/index.html.twig');
    }
}

以前有人经历过吗?哪里可能出错? P.S.:我使用的是 symfony 服务器

【问题讨论】:

  • 你在 Symfony 服务器的控制台中看到了什么吗?您的请求是否被服务器捕获或根本没有?
  • 我有一个类似的问题,我以this 的方式解决了。
  • 虽然我使用的是 Symfony:server,但我尝试添加 .htacces。一切都一样

标签: php reactjs symfony symfony5 symfony-routing


【解决方案1】:

它在 router:match 中显示 App\Controller\UserController::index(),而您的控制器显示 DefaultController,这可能是问题吗?

也可能存在优先级问题,从 5.1 开始,您可以在路由中使用优先级 (https://symfony.com/blog/new-in-symfony-5-1-route-annotations-priority),或者尝试在路由器注释中添加 priority=-10, 以降低优先级(默认为 0)或 priority=10, 以防万一你想在其他地方提高优先级。

【讨论】:

  • 由于它使用了不带前缀的占位符,是的,优先级似乎是问题,它将匹配任何请求。但反过来,index 路由的优先级应该更低
  • 也可以使用负整数。
  • 是的,但这是答案的一个非常重要的部分,我发表评论是为了让您有机会编辑它,否则您的建议将无助于解决问题。
  • 非常有趣。我不知道路线优先级。我处理了 UserController,错误仍然在这里。然后我按照建议将 DefaultController:index 路径设置为优先级 -10。错误仍然存​​在
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-05
  • 2019-05-13
  • 2017-06-12
  • 2021-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多