【问题标题】:how to use route to view static apidoc page with laravel如何使用路由通过 laravel 查看静态 apidoc 页面
【发布时间】:2018-11-12 06:46:54
【问题描述】:

我目前使用 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 源网址类似于 vendor/polyfill.js 然后试图去 localhost:8000/vendor/polyfill.js 但实际上是网址应该类似于localhost:8000/apidoc/vendor/polyfill.js

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

提前感谢您的帮助

【问题讨论】:

    标签: php laravel routes api-doc static-pages


    【解决方案1】:

    您也可以通过注册供应商路由来“作弊”:

    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...'>,以便文件可以自动请求正确的资源.

    【讨论】:

    • 我明白你的意思,但在获取文件时仍然出现错误:(
    • 这是一个好技巧,我会尝试找出其余的 404 错误。非常感谢 ^_^,问题是index.html 模板是由其他服务生成的
    猜你喜欢
    • 1970-01-01
    • 2014-10-13
    • 2020-12-08
    • 2019-06-22
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    相关资源
    最近更新 更多