Skip to content

Instantly share code, notes, and snippets.

@ipalaus
Last active August 17, 2018 23:06
Show Gist options
  • Save ipalaus/8588411 to your computer and use it in GitHub Desktop.
Save ipalaus/8588411 to your computer and use it in GitHub Desktop.
Using cursor instead of pagination with Fractal. IE: GET /users?cursor=5
<?php
use League\Fractal\Manager;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Cursor\CursorInterface;
class ApiController extends Controller
{
public function __construct()
{
$this->fractal = new Manager;
$this->fractal->setRequestedScopes(explode(',', Input::get('include')));
}
protected function respondWithCursor($collection, $callback, CursorInterface $cursor)
{
$resource = new Collection($collection, $callback);
$resource->setCursor($cursor);
$rootScope = $this->fractal->createData($resource);
return $this->respondWithArray($rootScope->toArray());
}
}
<?php
namespace League\Fractal\Cursor;
class Cursor implements CursorInterface
{
protected $current;
protected $next;
protected $count;
public function __construct($current = null, $next = null, $count = null)
{
$this->current = $current;
$this->next = $next;
$this->count = $count;
}
public function getCurrent()
{
return $this->current;
}
public function setCurrent($current)
{
$this->current = $current;
return $this;
}
public function getNext()
{
return $this->next;
}
public function setNext($next)
{
$this->next = $next;
return $this;
}
public function getCount()
{
return $this->count;
}
public function setCount($count)
{
$this->count = $count;
return $this;
}
}
{
"data": [
{
"id": 6,
"name": "Lennie Raynor PhD"
},
{
"id": 7,
"name": "Duncan Raynor"
},
{
"id": 8,
"name": "clockman"
},
{
"id": 9,
"name": "Gerard Marks DDS"
},
{
"id": 10,
"name": "josefina32"
}
],
"cursor": {
"current": 5,
"next": 10,
"count": 5
}
}
<?php
namespace League\Fractal\Cursor;
interface CursorInterface
{
public function getCurrent();
public function getNext();
public function getCount();
}
{
"data": [
{
"id": 1,
"name": "Isern Palaus"
},
{
"id": 2,
"name": "Lorem Ipsum"
},
{
"id": 3,
"name": "Javi Martinez"
},
{
"id": 4,
"name": "lowe.yasmin"
},
{
"id": 5,
"name": "abshire.durward"
}
],
"cursor": {
"current": 0,
"next": 5,
"count": 5
}
}
<?php
namespace League\Fractal;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\ResourceInterface;
use League\Fractal\Pagination\PaginatorInterface;
use League\Fractal\Cursor\CursorInterface;
class Scope
{
public function toArray()
{
$output = array(
'data' => $this->runAppropriateTransformer()
);
if ($this->availableEmbeds) {
$output['embeds'] = $this->availableEmbeds;
}
if ($this->resource instanceof Collection) {
$paginator = $this->resource->getPaginator();
if ($paginator !== null and $paginator instanceof PaginatorInterface) {
$output['pagination'] = $this->outputPaginator($paginator);
}
/**
* NEW CODE HERE
*/
$cursor = $this->resource->getCursor();
if ($cursor !== null and $cursor instanceof CursorInterface) {
$output['cursor'] = $this->outputCursor($cursor);
}
}
return $output;
}
}
<?php
use League\Fractal\Cursor\Cursor;
class UsersController extends ApiController
{
public function index()
{
$current = Input::get('cursor', false);
$users = new User;
if ($current and is_numeric($current)) {
$users = $users->where('id', '>', $current);
}
$users = $users->take(20)->get();
$cursor = new Cursor($current, $users->last()->id, $users->count());
return $this->respondWithCursor($users, new UserTransformer, $cursor);
}
}
@developcreativo
Copy link

developcreativo commented Jul 18, 2017

?? eturn $this->respondWithArray($rootScope->toArray()); ?? I can not find this line of code APiController

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment