Laravel 12 Controllers - Beginner Tutorial

Controllers in Laravel are like managers that handle requests from the user and send responses back. They sit between routes and models keeping your code clean and organized.

In this tutorial, you will learn everything about controllers in Laravel 12, including how to create them, different types of controllers, how to call models and APIs from controllers, handle data, use resource controllers and much more.

Let’s begin.


What is a Controller in Laravel 12?

A controller is a class stored in app/Http/Controllers folder.
It helps you group related request handling logic into a single place.

For example, if you are building a blog, you can create a PostController to handle all requests related to posts — creating, updating, deleting, and displaying posts.

Example:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller {

    public function index() {
        return view('posts.index');
    }

}

This method can then be linked to a route like this:

Route::get('/posts', [PostController::class, 'index']);

This is the basic idea of how routes call controller methods.


How to Create a Controller in Laravel 12

Laravel gives you an Artisan command to quickly generate controllers.

Basic command:

php artisan make:controller PostController

This will create PostController.php inside app/Http/Controllers.

Create controller in a subfolder:

php artisan make:controller Admin/PostController

Create controller with model and resource:

php artisan make:controller PostController --resource --model=Post

This command saves a lot of time because it also prepares all standard CRUD methods automatically.


Types of Controllers in Laravel

Laravel has different types of controllers based on their purpose. Let’s quickly explore them.

(i)- Normal Controller

This is a standard controller that can have multiple methods like index, create, store, edit, update, and destroy.

Use this when you want to manage many actions inside one controller.


 (ii)- Invokable Controller

An invokable controller has only one method called __invoke().

It’s useful when you just need one action in that controller.

Create command:

php artisan make:controller ContactController --invokable

Example:

public function __invoke() {
return "This is a single action controller";
}

Route:

Route::get('/contact', ContactController::class);

(iii)- Resource Controller

A resource controller is built for CRUD operations.

It automatically includes 7 methods:

  • index
  • create
  • store
  • show
  • edit
  • update
  • destroy

Create command:

php artisan make:controller PostController --resource

Resource route:

Route::resource('posts', PostController::class);

 Laravel 12 Resource Controller Route Table

When you create a resource controller using

php artisan make:controller MyController --resource 

and define it like this:

Route::resource('my', MyController::class)

Laravel automatically generates 7 standard routes.

Here’s the full table:

Verb Path Action Route Name
GET /my index my.index
GET /my/create create my.create
POST /my store my.store
GET /my/{my} show my.show
GET /my/{my}/edit edit my.edit
PUT/PATCH /my/{my} update my.update
DELETE /my/{my} destroy my.destroy

 

Example URLs and What They Do

Once you run php artisan serve and open your app in the browser, here is how these routes work:

URL Description Output (Controller Method)
http://localhost:8000/my Executes the index() method of MyController index
http://localhost:8000/my/create Executes the create() method of MyController create
http://localhost:8000/my/1 Executes the show() method of MyController show
http://localhost:8000/my/1/edit Executes the edit() method of MyController edit


Tip: The {my} in the route is a dynamic parameter — it can be any record ID.

This table will visually explain how Laravel resource controller routes are mapped to controller methods, which is super helpful for beginners.


Laravel Resource Controller Example (With Model)

Step 1 — Create Model with Migration and Resource Controller

php artisan make:model Post -mcr

This creates:

  • Post model

  • create_posts_table migration

  • PostController with resource methods

Step 2 — Define Routes

Route::resource('posts', PostController::class);

Step 3 — Example Store Method

public function store(Request $request)
{

  $data = $request->validate([ 'title' => 'required', 'body' => 'required', ]);

  Post::create($data);
  return redirect()->route('posts.index');
}

This is a clean way to build CRUD in Laravel 12.


Handling Data in Laravel 12 Controllers

Controllers are the main place where you handle input data from forms or APIs and then send data back to the views.

Handle Request Data $title = $request->input('title');
Handle Date Fields use Carbon\Carbon; $date = Carbon::now();
Get URL Parameters

public function show($id)

{ return "Post ID is " . $id; }

Get ENV Variable $apiKey = env('API_KEY');
Get Validation Errors $request->validate([ 'title' => 'required' ]);

 

This shows how flexible Laravel controllers are for handling different kinds of data.


Calling Models and APIs from Controllers

You can directly call your models inside controllers to interact with the database.

Example:

$posts = Post::all();

You can also call APIs from controllers using Laravel’s Http client:

use Illuminate\Support\Facades\Http;

$response = Http::get('https://api.example.com/data');

$data = $response->json();

Route to Controller in Laravel 12

You always need to connect your controller methods with routes.

Example Route:

Route::get('/posts', [PostController::class, 'index']);

This route will call the index method from the PostController.


Access Control in Laravel Controllers

Controllers can use middleware to restrict access. This is part of Laravel access control.

Example:

public function __construct()
{

$this->middleware('auth');
}

You can also use authorization policies inside controllers:

$this->authorize('update', $post);

Controller Best Practices in Laravel 12

Here are a few good habits:

  • Use resource controllers for CRUD

  • Keep logic small — don’t write long functions

  • Use Request classes for validation

  • Organize controllers in subfolders if your app grows

  • Use route model binding instead of manually finding records

These small steps make your controller code clean and maintainable.


 Useful Artisan Commands for Controllers

Task Command
Create controller php artisan make:controller NameController
Create controller in subfolder php artisan make:controller Admin/NameController
Create invokable controller php artisan make:controller NameController --invokable
Create resource controller php artisan make:controller NameController --resource
Create controller with model php artisan make:controller NameController --model=Model
Create model + migration + controller php artisan make:model Post -mcr
Delete controller delete file manually from app/Http/Controllers

 

FAQs about Laravel 12 Controllers

Q: What is a controller in Laravel 12?
A controller is a PHP class that handles user requests and sends responses.

Q: How to make a controller in Laravel 12?
Use the command php artisan make:controller YourController.

Q: What is a resource controller in Laravel 12?
It is a controller that comes with 7 built-in CRUD methods.

Q: How to create a controller in a specific folder?
Use php artisan make:controller Admin/YourController.

Q: What is an invokable controller?
It is a controller with only one method __invoke().

Q: How to call model functions in controller?
Just use ModelName::all() or other Eloquent methods.

Q: How to get URL parameters in controller?
Use method arguments like public function show($id).

Q: How to get ENV variable in controller?
Use env('KEY_NAME').

Q: How to handle dates in controller?
Use the Carbon class to work with dates.

Q: How to create controller with model and migration?
Use php artisan make:model Post -mcr.


Final Thoughts

Controllers are one of the most important building blocks in Laravel 12.
They keep your code organized, reusable and easier to test.

Once you understand how to:

  • create controllers

  • handle data

  • use resource controllers

  • and connect routes with controllers

you can build clean and scalable Laravel projects easily.