خانه درس‌ها
درس ۰۵ مبتدی ۹ دقیقه

کنترلرها

منطق را از مسیر جدا کن.

ساخت کنترلر

Terminal
php artisan make:controller PostController
php artisan make:controller PostController --resource

مثال

app/Http/Controllers/PostController.php
namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function index()
    {
        $posts = Post::latest()->get();
        return view('posts.index', compact('posts'));
    }

    public function show(Post $post)
    {
        return view('posts.show', compact('post'));
    }
}
routes/web.php
Route::get('/posts', [PostController::class, 'index']);
Route::get('/posts/{post}', [PostController::class, 'show']);