1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Api\V1\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Models\User; |
6
|
|
|
use League\Fractal\Manager; |
7
|
|
|
use League\Fractal\Resource\Item; |
8
|
|
|
use App\Http\Controllers\Controller; |
9
|
|
|
use Illuminate\Support\Facades\Input; |
10
|
|
|
use League\Fractal\Resource\Collection; |
11
|
|
|
use App\Api\V1\Transformers\UserTransformer; |
12
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter; |
13
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
14
|
|
|
|
15
|
|
|
class UserController extends Controller |
16
|
|
|
{ |
17
|
|
|
public function index() |
18
|
|
|
{ |
19
|
|
|
$fractal = new Manager(); |
20
|
|
|
|
21
|
|
|
$fractal->parseIncludes(Input::get('include', '')); |
22
|
|
|
|
23
|
|
|
$limit = Input::get('limit', 10); |
24
|
|
|
|
25
|
|
|
$queryParams = array_diff_key($_GET, array_flip(['page'])); |
26
|
|
|
$usersPaginator = User::paginate($limit) |
27
|
|
|
->appends($queryParams) |
28
|
|
|
->appends([ |
29
|
|
|
'limit' => $limit, |
30
|
|
|
]); |
31
|
|
|
|
32
|
|
|
$users = new Collection($usersPaginator->items(), new UserTransformer()); |
33
|
|
|
$users->setPaginator(new IlluminatePaginatorAdapter($usersPaginator)); |
34
|
|
|
|
35
|
|
|
return $fractal->createData($users)->toArray(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function show($id) |
39
|
|
|
{ |
40
|
|
|
$fractal = new Manager(); |
41
|
|
|
|
42
|
|
|
$fractal->parseIncludes(Input::get('include', '')); |
43
|
|
|
|
44
|
|
|
if (! $user = User::find($id)) { |
45
|
|
|
throw new NotFoundHttpException(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$user = new Item($user, new UserTransformer()); |
49
|
|
|
|
50
|
|
|
return $fractal->createData($user)->toArray(); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|