【问题标题】:Laravel 5 - Using php file in public folder as view and pass jsonLaravel 5 - 使用公共文件夹中的 php 文件作为视图并传递 json
【发布时间】:2017-11-23 03:47:34
【问题描述】:

我正在使用 Laravel 5.3 制作后端 API

其中一个 API 是 OnaController 中的这个,它将返回 Json 格式:

public function index()
{
    $limit = Input::get('limit') ?: 10;

    if ($limit < 1 or $limit > 100) {
        $limit = 10;
    }

    $ona_datas = Onaio::where('status', '<>', 4)->paginate($limit);

    return $this->respondWithPagination(
                $ona_datas,
                $this->onaTransformer->transformCollection($ona_datas->all())
            );
}

名为ona_list.php的前端文件放在公共文件夹(/public)

我的问题是如何将文件作为视图然后将 json 传递给它? 我想用相同的 URL 打开文件 (my_website.com/ona)

这是我迄今为止尝试过的:

  1. return view(asset('ona_list.php') 但它抛出了InvalidArgumentException in FileViewFinder.php line 137: View [http:..localhost:8000.ona_list.php] not found.

  2. return redirect(asset('ona_list.php')) ->with(json_decode($this->respondWithPagination($ona_datas, $this->onaTransformer->transformCollection( $ona_datas->all())))); 但网址更改为 my_website.com\ona_list.php 且数据未显示

【问题讨论】:

  • 你的 routes/api.php 是什么样的?

标签: php json laravel api laravel-5


【解决方案1】:

为什么要将 ona_list.php 放在公共文件夹中?所有请求默认重定向到 index.php

您可以做的是,将新路由定义为以下Route::get('ona', 'OnaController@index'); 并通过 your_website.com/ona 访问它

OnaController@index 中确保您返回json,如下所示

class OnaController extends Controller { ... return response()->json($ona_dates); }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-30
    • 2011-10-01
    • 2017-02-03
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 2018-05-03
    相关资源
    最近更新 更多