Ainda é simples
Usar um framework como o Laravel significa que o projeto está automaticamente seguro… Né?
<?php
// Arquivo: app/Http/Requests/TestRequest.php
namespace App\Http\Requests;
use App\Models\Post;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class TestRequest extends FormRequest
{public function rules(): array
{return [
'slug' => [
'required',
'string',
'max:64',
'alpha_dash:ascii',
class),
Rule::unique(Post::,
]'title' => [
'required',
'string',
'max:128',
,
]'content' => [
'required',
'string',
'max:65536',
,
];
]
} }
<?php
// Arquivo: app/Http/Controllers/TestController.php
namespace App\Http\Controllers;
use App\Http\Requests\TestRequest;
use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
class TestController extends Controller
{public function get(Request $request, string $slug): Response
{$post = Post::where('slug', $slug)
$request->user()->id)
->userCanRead(
->select(['title',
'content',
]);
->firstOrFail()
return response()->view('test', [
'post' => $post,
;
])
}
public function post(TestRequest $request): Response
{
Post::create(['slug' => $request->slug,
'title' => $request->title,
'content' => $request->content,
'author_id' => $request->user()->id,
;
])
return response()->noContent();
} }
<!-- Arquivo: resources/views/test.blade.php -->
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<title>{{ $post->title }} | TestSite</title>
<meta charset="utf-8">
</head>
<body>
<main>
<h1>{{ $post->title }}</h1>
<div>{{ $post->content }}</div>
</main>
</body>
</html>