Controller¶
Controllers are responsible for handling requests and returning responses. They act as intermediaries between models and views, and are used to process input, perform validation, and interact with databases and other external services.
Anatomy of a Controller¶
A controller typically consists of several methods, each corresponding to a different action that can be performed on a resource. Here's an example controller named HomeController
:
<?php
namespace App\Controllers;
use App\Core\Controller;
use App\Models\Contact;
class HomeController extends Controller
{
public static function index()
{
view('front/home');
}
public static function contact()
{
Contact::insert($_POST);
back();
}
}
This controller has two methods: index()
and contact()
. The index()
method is responsible for rendering the home page view, while the contact()
method handles form submissions and inserts the contact information into the database.
The Controller
class that HomeController
extends provides some helper methods, such as the view()
and back()
functions used in this example.
Routing to a Controller¶
To route a request to a controller, you'll need to define a route that specifies the controller and method to be invoked. Here's an example route that maps the root URL to the index()
method of the HomeController
class:
Router::get('/', [HomeController::class, 'index']);
This route tells the router to invoke the index()
method of the HomeController
class when a GET request is made to the root URL.
Controller Parameters¶
You can define parameters in a controller method by adding them to the method signature. Here's an example controller method that accepts a parameter named $id
:
public static function show($id)
{
$user = User::find($id);
view('users/show', compact('user'));
}
This method will respond to requests to URLs like /users/1
, /users/2
, etc. The $id
parameter will be set to the value of the wildcard in the route.
Controller Middleware¶
You can apply middleware to a controller method by adding it to the method signature. Here's an example controller method that applies the AuthenticationMiddleware
middleware:
public static function show($id)
{
$user = User::find($id);
view('users/show', compact('user'));
}
This method will respond to requests to URLs like /users/1
, /users/2
, etc. The $id
parameter will be set to the value of the wildcard in the route.
Resource Controllers¶
Resource controllers provide a convenient way to define a controller that handles all of the typical actions for a resource. For example, you can define a UserController
that handles all of the typical CRUD operations for users:
<?php
namespace App\Controllers;
use App\Core\Controller;
use App\Models\User;
class UserController extends Controller
{
public static function index()
{
$users = User::all();
view('users/index', compact('users'));
}
public static function create()
{
view('users/create');
}
public static function store()
{
User::insert($_POST);
back();
}
public static function show($id)
{
$user = User::find($id);
view('users/show', compact('user'));
}
public static function edit($id)
{
$user = User::find($id);
view('users/edit', compact('user'));
}
public static function update($id)
{
User::update($id, $_POST);
back();
}
public static function destroy($id)
{
User::delete($id);
back();
}
}
This controller handles all of the typical CRUD operations for users. It also provides a create()
method that renders a form for creating new users.