Skip to content

Commit 8ef3c91

Browse files
committed
Added Users Api With Proper Transformer
1 parent 5980664 commit 8ef3c91

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

api/V1/Users/Controllers/UsersController.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313

1414
public function __construct()
1515
{
16-
$this->scopes(['read_user_data']);
16+
// You Should Enable api.auth middleware
17+
// This is Useful for Put Patch Delete
18+
// For a user
19+
// $this->scopes(['read_user_data']);
1720
}
1821

1922
public function index()
2023
{
2124
$users = User::all();
25+
// Use Collection Property for Collections
2226
return $this->collection($users, new UserTransformer);
2327
}
2428
/**
@@ -32,9 +36,13 @@ public function index()
3236
public function show($id)
3337
{
3438
try {
35-
return User::findOrFail($id);
39+
$user = User::findOrFail($id);
40+
// Use Item Property for Single Item to be Returned
41+
return $this->item($user, new UserTransformer);
3642
}catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e){
43+
3744
$api= app(\Dingo\Api\Http\Response\Factory::class);
45+
// Override the Custom Errors
3846
return $api->errorNotFound("Requested resource with id={$id} where not found.");
3947
}
4048

api/V1/Users/Transformers/UserTransformers.php renamed to api/V1/Users/Transformers/UserTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function transform(User $user)
2929
'on_trial' => (bool) $user->on_trial,
3030
'subscribed' => (bool) $user->subscribed,
3131
'resent' => (int) $user->resent,
32-
'settings' => $user->settings
32+
'settings' => (array) $user->settings
3333
];
3434
}
3535

config/auth_scaffold.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
return [
44

55
'sign_up' => [
6-
'release_token' => env('SIGN_UP_RELEASE_TOKEN'),
6+
// If You want User a New User Given a New Token
7+
// This Will Log in the User to the App
8+
'release_token' => env('SIGN_UP_RELEASE_TOKEN', false),
79
'validation_rules' => [
810
'first_name' => 'required',
911
'last_name' => 'required',
@@ -27,6 +29,8 @@
2729
],
2830

2931
'reset_password' => [
32+
// If You Want the User Who Request Reset be Login
33+
// He Will be Given a Token
3034
'release_token' => env('PASSWORD_RESET_RELEASE_TOKEN', false),
3135
'validation_rules' => [
3236
'token' => 'required',

routes/api.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
|--------------------------------------------------------------------------
2121
|
2222
*/
23+
24+
// Default Sample Middleware
2325
$api->version('v1', function ($api) {
2426
$api->group(['middleware' => ['auth:api']], function ($api) {
2527
$api->group(['middleware' => ['roles'],
@@ -65,4 +67,16 @@
6567
// we get token from the url segment
6668
// required: email, password , and password_confirmation , token
6769
});
70+
});
71+
72+
/*
73+
|--------------------------------------------------------------------------
74+
| User Api
75+
| With Proper Transformer Set Up
76+
|--------------------------------------------------------------------------
77+
|
78+
*/
79+
$api->version('v1', function ($api) {
80+
$api->get('/users', ['as' => 'api.index', 'uses' => 'Api\V1\Users\Controllers\UsersController@index']);
81+
$api->get('/user/{id}', ['as' => 'api.index', 'uses' => 'Api\V1\Users\Controllers\UsersController@show']);
6882
});

0 commit comments

Comments
 (0)