【问题标题】:Can't find resources.json找不到 resources.json
【发布时间】:2013-01-22 13:50:58
【问题描述】:

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

/api/explorer

当我更明确地说:

/api/explorer/index.html

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

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

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>

任何帮助将不胜感激。

【问题讨论】:

    标签: php restler swagger


    【解决方案1】:

    事实证明,所有问题都归结为 mod_rewrite 问题。我仍然不是 100% 知道为什么这个问题会出现在一个环境中,但在其他环境中却没有出现完全相同的httpd.conf.htaccess。哦,好吧,解决方案是使用 RewriteBase 指令在关于您的基本 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 AllOptions FollowSymLinks
    2. 您可以将所有配置放入httpd.conf 并完全避免.htaccess 文件(这样也快一点),但如果您这样做,请记住httpd.conf 具有绝对url 引用而不是.htaccess'是相对的。

    【讨论】:

    • 注意:我已将 RewriteLogLevel 设置为 2,因为这样可以提供有用的信息而不会太啰嗦。
    【解决方案2】:

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

    【讨论】:

    • 具体是什么?我已经多次查看自述文件,但正如我妻子会告诉你的那样,我有时看不到我面前的内容。
    【解决方案3】:

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

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

    $r = new Restler();

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

    // ... your own addApiClass

    $r-&gt;handle();

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

    【讨论】:

    • 是的,我已经这样做了。实际上,相同的文件(在 /restler 和 /api 目录中)在我的桌面开发环境中对我来说可以正常工作,但在生产环境和笔记本电脑环境(其中矛盾的是,使用 Dropbox 从工作开发环境中镜像)。
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-23
      • 2021-06-19
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      • 2012-02-10
      • 2021-07-13
      相关资源
      最近更新 更多