Can't find resources.json

2019-09-15 17:42发布

问题:

For some reason when I deploy my API using Restler's API Explorer (a fork of Swagger UI) to production it gives me a general 404 error when I load:

/api/explorer

When I am more explicit and state:

/api/explorer/index.html

It loads the framing for the page but then reports "404 : Not Found ../resources.json" in red text below the header:

I'm fairly certain there's something environmental flaring up as the same files locally work. I also checked the .htaccess file in the /api/explorer directory and it looks right to me:

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>

Any help would be appreciated.

回答1:

It turns out all the problems were down to a mod_rewrite problem. I'm still not 100% on WHY this problem showed up in one environment but not others with precisely the same httpd.conf and .htaccess. Oh well, the solution is to be explicit in the rewrite rule about your base url using the RewriteBase directive.

In the API directory I now have the following .htaccess file:

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>

In the API-Explorer directory I now have the following .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>

In order to troubleshoot this problem one invaluable tip that I came across is turning on Apache's mod_rewrite logging.

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

Put this in any globally scoped area of httpd.conf; I put it around the entries that were already there about logging but you can also just put it at the end if you like that better. In addition, the basics around rewrite troubleshooting includes (apologies if this basic):

  1. make sure that your httpd.conf has AllowOverride All and Options FollowSymLinks set for the directories you serving Restler out of.
  2. You can put all of the configuration into httpd.conf and avoid .htaccess files altogether (and it's a bit faster that way too) but if you do that remember that httpd.conf has absolute url referencing versus .htaccess's relative.


回答2:

You can check the ReadMe file on the Restler Github page https://github.com/Luracast/Restler-API-Explorer#readme



回答3:

Did you add the 'Luracast\Restler\Resources' class to create resources.json at API Root?

In your own index.php, the addAPIClass section should look like this:

$r = new Restler();

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

// ... your own addApiClass

$r->handle();

It is in the documentation under "Use", but I had trouble figuring this out as well...



回答4:

Make sure you have cache directory with write permission in your api root