如何在 oc-api-plugin 中使用分页?
How to Using Pagination in oc-api-plugin?
octobercms 插件:
oc-api-plugin
不知道少了什么
我已经尝试了一段时间,但没有成功使用 API 使用分页
下面是我构建的程序
routes.php
<?php
Route::group([
'prefix' => 'api/v1/sample',
'namespace' => 'Sh\Sample\Controllers',
], function () {
Route::get('posts', 'postsAPI@index');
Route::get('posts/{id}', 'postsAPI@show');
});
PostTransformer.php
<?php
namespace Sh\Sample\Transformers;
use Octobro\API\Classes\Transformer;
use Sh\Sample\Models\Post;
class PostTransformer extends Transformer
{
// Related transformer that can be included
public $availableIncludes = [
//'entities'
];
// Related transformer that will be included by default
public $defaultIncludes = [
//'entities',
];
public function data(Post $item)
{
return [
'id' => (int)$item->id,
'title' => $item->title,
'content' => $item->content
];
}
}
PostsAPI.php
<?php namespace Sh\Sample\Controllers;
use Sh\Sample\Models\Post;
use Octobro\API\Classes\ApiController;
use Sh\Sample\Transformers\PostTransformer;
class PostsAPI extends ApiController
{
public function index()
{
$item = Post::get();
return $this->respondwithCollection($item, new PostTransformer);
}
public function show(int $id)
{
$item = Post::find($id);
return $this->respondwithItem($item, new PostTransformer);
}
}
测试:
获取:http://127.0.0.1/api/v1/sample/posts
{
"data": [
{
"id": 1,
"title": "test11",
"content": "teste222"
},
{
"id": 2,
"title": "test222",
"content": "test222222"
},
{
"id": 3,
"title": "test333",
"content": "content333"
},
{
"id": 4,
"title": "test333",
"content": "test333"
}
]
}
无法使用分页器
您需要使用respondWithPaginator
方法来使用分页。
参考:Pagination in oc-api-plugin
class PostsAPI extends ApiController
{
public function index()
{
$postPaginator = Post::paginate();
return $this->respondWithPaginator($postPaginator, new PostTransformer);
}
如有疑问请评论。
octobercms 插件: oc-api-plugin
不知道少了什么
我已经尝试了一段时间,但没有成功使用 API 使用分页
下面是我构建的程序
routes.php
<?php
Route::group([
'prefix' => 'api/v1/sample',
'namespace' => 'Sh\Sample\Controllers',
], function () {
Route::get('posts', 'postsAPI@index');
Route::get('posts/{id}', 'postsAPI@show');
});
PostTransformer.php
<?php
namespace Sh\Sample\Transformers;
use Octobro\API\Classes\Transformer;
use Sh\Sample\Models\Post;
class PostTransformer extends Transformer
{
// Related transformer that can be included
public $availableIncludes = [
//'entities'
];
// Related transformer that will be included by default
public $defaultIncludes = [
//'entities',
];
public function data(Post $item)
{
return [
'id' => (int)$item->id,
'title' => $item->title,
'content' => $item->content
];
}
}
PostsAPI.php
<?php namespace Sh\Sample\Controllers;
use Sh\Sample\Models\Post;
use Octobro\API\Classes\ApiController;
use Sh\Sample\Transformers\PostTransformer;
class PostsAPI extends ApiController
{
public function index()
{
$item = Post::get();
return $this->respondwithCollection($item, new PostTransformer);
}
public function show(int $id)
{
$item = Post::find($id);
return $this->respondwithItem($item, new PostTransformer);
}
}
测试:
获取:http://127.0.0.1/api/v1/sample/posts
{
"data": [
{
"id": 1,
"title": "test11",
"content": "teste222"
},
{
"id": 2,
"title": "test222",
"content": "test222222"
},
{
"id": 3,
"title": "test333",
"content": "content333"
},
{
"id": 4,
"title": "test333",
"content": "test333"
}
]
}
无法使用分页器
您需要使用respondWithPaginator
方法来使用分页。
参考:Pagination in oc-api-plugin
class PostsAPI extends ApiController
{
public function index()
{
$postPaginator = Post::paginate();
return $this->respondWithPaginator($postPaginator, new PostTransformer);
}
如有疑问请评论。