Tsuna
Tsuna

Reputation: 2198

how to use route to view static apidoc page with laravel

I am currently using http://apidocjs.com/ as my laravel apidoc because I am just used to it before.

to view the apidoc, each time I have to drag the index.html into the browser which is quite annoying.

Is it possible to make it into a static page so people I am sharing the apidoc with can just generate the doc then go to the route.

I tried something like...putting my apidoc folder under public folder of the application and also tried adding a route such as

Route::get('/apidoc', function(){
    return File::get(public_path() . '/apidoc/index.html');
});

Both of them didn't work the index.html cannot load the css and js because in index.html the source url is something like vendor/polyfill.js which then tried to go localhost:8000/vendor/polyfill.js but actually the url should be something like localhost:8000/apidoc/vendor/polyfill.js

Does anyone know how to easily fix this?

Thanks in advance for any help

Upvotes: 1

Views: 920

Answers (2)

DARCKRAIII
DARCKRAIII

Reputation: 11

Route::get('apidoc', function(){
return File::get(public_path() . '/apidoc/index.html');
});

I hope to help someone.

To solve the 400 files not found issue, the apiDocjs installation is in the public folder, it's just a matter of changing the name of the folder where you make the request for the file search.

In my case I'm using Laravel for an API and the file paths (img, docs) always go to the api/assets/{filename} path.

Now just install apiDoc inside a folder called api and you're done!

the structure of the files is as follows

the structure of the files is as follows

Upvotes: 0

apokryfos
apokryfos

Reputation: 40673

You can "cheat" a little bit by registering the vendor routes as well:

Route::get('vendor/{any}', function ($any) {
    abort_unless(is_readable(public_path("apidoc/vendor/$any")), 404);
    return File::get(public_path("apidoc/vendor/$any")); 
})->where('any', ".*");

Route::get('apidoc', function(){
    return File::get(public_path() . '/apidoc/index.html');
});

Of course the ideal solution is if you actually manage to change the template you use for index.html to use relative and not absolute paths (i.e. change all <link href='/vendor...'> to <link href='vendor...'> so the file can automatically request the correct resource.

Upvotes: 2

Related Questions