Skip to content

Commit 7a28798

Browse files
author
Gareth Redfern
committed
refactor auth user and user methods to controllers
1 parent 24ce3b9 commit 7a28798

File tree

3 files changed

+84
-14
lines changed

3 files changed

+84
-14
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Http\Resources\UserResource;
6+
use Illuminate\Support\Facades\Auth;
7+
8+
class AuthController extends Controller
9+
{
10+
public function __invoke()
11+
{
12+
return new UserResource(Auth::user());
13+
}
14+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Models\User;
6+
use Illuminate\Http\Request;
7+
use App\Http\Resources\UserResource;
8+
9+
class UserController extends Controller
10+
{
11+
/**
12+
* Display a listing of the resource.
13+
*
14+
* @return \Illuminate\Http\Response
15+
*/
16+
public function index()
17+
{
18+
return UserResource::collection(User::paginate());
19+
}
20+
21+
/**
22+
* Store a newly created resource in storage.
23+
*
24+
* @param \Illuminate\Http\Request $request
25+
* @return \Illuminate\Http\Response
26+
*/
27+
public function store(Request $request)
28+
{
29+
//
30+
}
31+
32+
/**
33+
* Display the specified resource.
34+
*
35+
* @param int $id
36+
* @return \Illuminate\Http\Response
37+
*/
38+
public function show(User $user)
39+
{
40+
return new UserResource($user);
41+
}
42+
43+
/**
44+
* Update the specified resource in storage.
45+
*
46+
* @param \Illuminate\Http\Request $request
47+
* @param int $id
48+
* @return \Illuminate\Http\Response
49+
*/
50+
public function update(Request $request, $id)
51+
{
52+
//
53+
}
54+
55+
/**
56+
* Remove the specified resource from storage.
57+
*
58+
* @param int $id
59+
* @return \Illuminate\Http\Response
60+
*/
61+
public function destroy($id)
62+
{
63+
//
64+
}
65+
}

routes/api.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<?php
22

3-
use Illuminate\Support\Facades\Auth;
43
use Illuminate\Support\Facades\Route;
4+
use App\Http\Controllers\AuthController;
5+
use App\Http\Controllers\UserController;
56
use App\Http\Controllers\TokenController;
67
use App\Http\Controllers\AvatarController;
7-
use App\Http\Resources\UserResource;
8-
use App\Models\User;
98

109
/*
1110
|--------------------------------------------------------------------------
@@ -21,17 +20,9 @@
2120
Route::post('/sanctum/token', TokenController::class);
2221

2322
Route::middleware(['auth:sanctum'])->group(function () {
24-
Route::get('/users/auth', function () {
25-
return new UserResource(Auth::user());
26-
});
27-
28-
Route::get('/users/{id}', function ($id) {
29-
return new UserResource(User::findOrFail($id));
30-
});
31-
32-
Route::get('/users', function () {
33-
return UserResource::collection(User::paginate());
34-
});
23+
Route::get('/users/auth', AuthController::class);
24+
Route::get('/users/{user}', [UserController::class, 'show']);
25+
Route::get('/users', [UserController::class, 'index']);
3526

3627
Route::post('/users/auth/avatar', [AvatarController::class, 'store']);
3728
});

0 commit comments

Comments
 (0)