php - laravel 5.5 资源 restful API

标签 php laravel rest laravel-5 laravel-5.5

我正在尝试为我的应用程序创建一个 API,以便我可以共享端点并将一个应用程序作为具有业务逻辑的核心应用程序,而另一个可以连接到公开的端点以将功能作为服务使用。

当我尝试到达终点时出现错误。

下面是我的route/api.php

<?php

use App\PostModell;
use App\Http\Resources\PostModellResource;
use Illuminate\Http\Request;

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});
Route::get( '/cars',function(){
    return new PostModellResource(PostModell::all());
}); 

我的资源类看起来像

class PostModellResource extends Resource
{
         public function toArray($request)
    {
        return
        [
            'id'=>$this->id,
            'title'=>$this->title,
            'body'=>$this->body,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,

        ];          
    }

错误是

抱歉,找不到您要查找的页面。

最佳答案

使用api前缀-

127.0.0.1:8000/api/cars 

要转换资源集合,需要使用collection()方法-

return PostModellResource::collection(PostModell::all());

关于php - laravel 5.5 资源 restful API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48417116/

相关文章:

php - 网站上传后出现错误500(没有.htaccess,没有WP)

php - 列出用于选择和处理表单 php 的数据库值

php - php 变量的算术运算

javascript - vuejs 中的 bundle 和 components 单独的 js 文件

php - 从表中获取事件和非事件记录

java - 使用 RestTemplate 进行 RESTful 服务测试

php - ArrayAccess 如何工作?

php - Laravel 4 中搜索结果的 anchor 标签

java - 在java中使用SSO身份验证使用服务?

c# - 如何在 RestSharp 中使用 OAuth2