Controllers — Laravel 10 Discovery Training

In this new chapter where we will discover together the principle of controllers in Laravel. They are simply classes whose objective is to group the functions that will contain the logic of our application. At the Laravel level, I can create a controller using the command php artisan make:controller.

This command will create a new file in the folder Http/controllers with inside a class that extends from the class Controller of our app. It is inside this class that we will define our methods

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\View\View;

class BlogController extends Controller
    public function index()
        return Post::paginate(25);

Then we can use this method in our routing.

Route::get("", [BlogController::class, 'index']);

And there is nothing more to know for the moment about the controllers. The methods work like the anonymous methods that we saw at the Routing level (you can inject an object Request or URL parameters).