Controllers and Actions
Create controllers with artisan and handle requests in action methods.
Controllers and Actions is a free PHP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is a Controller?
A controller groups related request-handling logic. Instead of closures in route files, you delegate to controller methods (actions).
Generating a Controller
Use Artisan to scaffold a controller.
$ php artisan make:controller UserController
$ php artisan make:controller PostController --resourceBasic Controller
A controller extends Controller and has public methods for each route action.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
public function index(): \Illuminate\View\View
{
$users = User::all();
return view("users.index", compact("users"));
}
}Accessing the Request
Type-hint Illuminate\Http\Request in the action method — Laravel injects it automatically.
<?php
public function store(Request $request): \Illuminate\Http\RedirectResponse
{
$name = $request->input("name");
$email = $request->input("email");
User::create(["name" => $name, "email" => $email]);
return redirect()->route("users.index");
}Form Validation
Use $request->validate() to validate input. On failure, Laravel redirects back with errors automatically.
<?php
public function store(Request $request): \Illuminate\Http\RedirectResponse
{
$validated = $request->validate([
"name" => "required|string|max:255",
"email" => "required|email|unique:users",
]);
User::create($validated);
return redirect()->route("users.index");
}Resource Controller Methods
A resource controller has 7 conventional methods: index, create, store, show, edit, update, destroy.
Single Action Controllers
Invokable controllers have a single __invoke() method — ideal for actions that do exactly one thing.
<?php
class SendWelcomeEmail extends Controller
{
public function __invoke(User $user): void
{
Mail::to($user)->send(new WelcomeMail($user));
}
}
// Route:
Route::post("/users/{user}/welcome", SendWelcomeEmail::class);Controller Middleware
Apply middleware to specific actions using the middleware() method in the constructor.
<?php
public function __construct()
{
$this->middleware("auth")->except(["index", "show"]);
}Dependency Injection
Type-hint services in action method parameters — Laravel resolves them from the service container.
<?php
public function store(Request $request, Mailer $mailer): JsonResponse
{
// $mailer is auto-injected
}Returning Responses
Controllers can return: views, redirects, JSON responses, file downloads.
<?php
return view("users.show", compact("user"));
return redirect()->back()->with("status", "Saved!");
return response()->json($user);
return response()->download(storage_path("report.pdf"));Controller Namespacing
All controllers live in App\Http\Controllers by default. Subdirectories create sub-namespaces: App\Http\Controllers\Admin\UserController.
Keeping Controllers Thin
Controllers should delegate business logic to service classes or actions. Aim for controllers that only: validate input, call a service, return a response.
Summary
Generate controllers with Artisan. Type-hint Request and services for auto-injection. Use validate() for input validation. Keep controllers thin — delegate to services.
Quick Check
What Artisan command generates a resource controller?
Frequently asked questions
Is the “Controllers and Actions” lesson free?
Yes — the full text of “Controllers and Actions” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Controllers and Actions”?
Create controllers with artisan and handle requests in action methods. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Controllers and Actions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Laravel Project Structure
- Defining Routes in Laravel
- Controllers and Actions
- Blade Templates and Views