0

出于某种原因,当我使用 Restler 的 API Explorer(Swagger UI 的一个分支)将我的 API 部署到生产环境时,它在我加载时给了我一个一般的 404 错误:

/api/explorer

当我更明确地说:

/api/explorer/index.html

它加载页面的框架,然后在标题下方以红色文本报告“404:未找到../resources.json”:

在此处输入图像描述

我相当肯定,当相同的文件在本地工作时,环境会出现一些问题。我还检查了目录中的 .htaccess 文件,/api/explorer它看起来对我来说是正确的:

DirectoryIndex index.php
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^$ index.html [QSA,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.html [QSA,L]
</IfModule>

任何帮助,将不胜感激。

4

4 回答 4

2

事实证明,所有问题都归结为 mod_rewrite 问题。我仍然不是 100% 知道为什么这个问题会出现在一个环境中,但在其他环境中却httpd.conf没有.htaccessRewriteBase哦,好吧,解决方案是使用指令在关于您的基本 url 的重写规则中明确。

在 API 目录中,我现在有以下.htaccess文件:

DirectoryIndex index.php
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /api
    RewriteRule ^$ index.php [QSA,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

在 API-Explorer 目录中,我现在有以下内容.htaccess

DirectoryIndex index.php
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /api/explorer
    RewriteRule ^$ index.html [QSA,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.html [QSA,L]
</IfModule>

为了解决这个问题,我遇到的一个非常宝贵的技巧是打开 Apache 的 mod_rewrite 日志记录。

# Adding logging for Rewrites
RewriteLog logs/rewrite.log
RewriteLogLevel 2

把它放在任何全局范围的区域中httpd.conf;我把它放在已经存在的关于日志记录的条目周围,但如果你更喜欢它,你也可以把它放在最后。此外,有关重写故障排除的基础知识包括(抱歉,如果这是基本的):

  1. 确保您的 httpd.conf 已为您服务 Restler 的目录设置AllowOverride All并设置。Options FollowSymLinks
  2. 您可以将所有配置放入httpd.conf并完全避免.htaccess文件(这样也更快一些),但如果您这样做,请记住httpd.conf绝对 url 引用与.htaccess' 的相对引用。
于 2013-02-11T17:37:15.647 回答
0

您可以在 Restler Github 页面 https://github.com/Luracast/Restler-API-Explorer#readme查看自述文件

于 2013-02-05T21:20:32.370 回答
0

您是否添加了“Luracast\Restler\Resources”类以在 API Root 中创建 resources.json?

在您自己的 index.php 中,addAPIClass 部分应如下所示:

$r = new Restler();

$r->addAPIClass('Luracast\\Restler\\Resources'); //this creates resources.json at API Root

// ... your own addApiClass

$r->handle();

它在“使用”下的文档中,但我也很难弄清楚这一点......

于 2013-02-07T06:12:14.023 回答
0

确保您的 api 根目录中有具有写入权限的缓存目录

于 2015-10-02T10:50:58.170 回答