Symfony 4 - 如何修复路由存在的 web api REST 的“404 Not Found”?

Symfony 4 - How to fix '404 Not Found' for a web api REST where routes does exists?

我正在尝试使用 symfony 4 实现 REST API。我的问题是我无法到达方法 GET 和 POST 的路由。每次尝试时,我都会收到 404 错误。

但是当我尝试在 symfony 上获取我的路线时,我有:

Name                            Method   Scheme   Host   Path

app_statistics_poststatistics   POST     ANY      ANY    /api/stat
app_statistics_getstatistics    GET      ANY      ANY    /api/stat/{statId}

所以我的路线确实存在,但我无法到达它们。

我正在 Windows 环境中使用 WAMP 在本地工作。

我已经在 2 个 windows 环境和一个 Linux 环境 LAMP 上尝试了我的 api。总是收到 404 Not Found。

教程的作者告诉我,我的 api 正在他的计算机上工作,但我没有关于他的环境的更多信息。所以,我预计问题出在我的虚拟主机配置上。

这是我的虚拟主机:

<VirtualHost *:80>
    ServerName statroche.fr
    ServerAlias www.statroche.fr
    DocumentRoot "D:/other/symfony/testRoche/public"
    <Directory "D:/other/symfony/testRoche/public">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require local
    </Directory>
</VirtualHost>

重要说明:我的 public 文件夹中没有任何内容,因为我不需要任何页面,只有 API,我是否还需要放入 'public'在 DocumentRootDirectory ?.

我想到达我的 API 然后对于 GET 方法,例如 return a json。实际上,我可以到达我项目的/,但无法到达其他路线。

即使您不想使用 Symfony 构建 "old-style website",您也必须使用 index.php 到 bootstrap Symfony 并使 API 可访问

而不是

<Directory "D:/other/symfony/testRoche/public">

像这样将 apache 目录路径指向你的 Symfony 项目的主文件夹

<Directory "D:/other/symfony/testRoche">

然后,使用.htaccess文件(放在D:/other/symfony/testRoche/.htaccess中使public/index.php 作为主要端点,如果你去 http://localhost:80

完整的 apache 配置

<VirtualHost *:80>
    ServerName statroche.fr
    ServerAlias www.statroche.fr
    DocumentRoot "D:/other/symfony/testRoche/public"
    <Directory "D:/other/symfony/testRoche/public">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require local
    </Directory>
</VirtualHost>

.htaccess 文件

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule !\.(js|gif|jpg|png|css|txt)$ public/index.php [L]
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ public/ [L]
</IfModule>