【问题标题】:Symfony 4 - How to fix '404 Not Found' for a web api REST where routes does exists?Symfony 4 - 如何为存在路由的 Web api REST 修复“404 Not Found”?
【发布时间】:2019-06-06 09:59:49
【问题描述】:

我正在尝试使用 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}

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

我正在使用 WAMP 开发 Windows 环境以在本地工作。

我已经在 2 个 Windows 环境和一个带有 LAMP 的 Linux 环境上尝试了我的 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>

重要提示:我的公共文件夹中没有任何内容,因为我不想要任何页面,只需要 API,我还需要在 DocumentRootDirectory 中输入“公共”吗?。

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

【问题讨论】:

  • 那么,如果您在public 文件夹中没有index.php,您就没有Symfony 开始运行的入口点——您在访问/ 时看到通常的欢迎页面吗?
  • 不,我看不到经典的欢迎页面,但我可以在我的虚拟主机中使用+Indexes 访问/

标签: symfony wamp virtualhost symfony4


【解决方案1】:

代替

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

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

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

然后,使用 .htaccess 文件(如果您转到http://localhost:80,则将其放在D:/other/symfony/testRoche/.htaccess 以使public/index.php 作为主要端点 p>

完整的 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/$1 [L]
</IfModule>

【讨论】:

    【解决方案2】:

    即使你不想用 Symfony 构建一个“老式网站”,你也必须使用 index.php 来引导 Symfony 并使 API 可访问

    【讨论】:

    • 我创建了一个新项目来尝试在我的公共文件夹中使用 index.php。现在有一个我无法理解的错误。这是错误NotAcceptableHttpException - No matching accepted Response format could be determined,它引用了我的文件 fos_rest.yaml
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    相关资源
    最近更新 更多