如何使用路由查看 laravel 的静态 apidoc 页面

how to use route to view static apidoc page with laravel

我目前正在使用 http://apidocjs.com/ 作为我的 laravel apidoc,因为我之前已经习惯了。

看apidoc,每次都得把index.html拖到浏览器里,很烦人。

是否可以将它变成一个静态页面,以便与我共享 apidoc 的人可以只生成文档然后转到路由。

我试过类似...将我的 apidoc 文件夹放在应用程序的 public 文件夹下,还尝试添加一个路由,例如

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

它们都不起作用 index.html 无法加载 cssjs 因为在 index.html 中源 url 类似于 vendor/polyfill.js 然后试图去 localhost:8000/vendor/polyfill.js 但实际上 url 应该是 localhost:8000/apidoc/vendor/polyfill.js

有谁知道如何轻松解决这个问题?

在此先感谢您的帮助

您也可以 "cheat" 通过注册供应商路线来做一点:

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');
});

当然,理想的解决方案是,如果您真的设法将用于 index.html 的模板更改为使用相对路径而不是绝对路径(即将所有 <link href='/vendor...'> 更改为 <link href='vendor...'>,那么该文件可以自动请求正确的资源。