phplaravelroutesapi-docstatic-pages

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


Solution

  • 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.