laravel-api-simple-blog/app/Http/Controllers/UserController.php

53 lines
1.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
class UserController extends Controller
{
// GET /user
public function index()
{
return response(User::all());
}
// GET /user/{id}
public function show(string $id)
{
// FIXME: is there more shorter
// validation solution
$v = Validator::make(["id" => $id], [
"id" => 'integer'
]);
if ($v->fails())
throw new BadRequestException();
$user = User::query()->findOrFail($id);
return response($user);
}
// POST /user
public function store(Request $request)
{
return response("Create user");
}
// PUT /user/{id}
public function update(Request $request, string $id)
{
return response("Update user " . $id);
}
// DELETE /user/{id}
public function destroy(string $id)
{
return response("Destroy user " . $id);
}
}