Início / Optitravel / Controllers
Creating Your Controller
Introduction
Controllers are responsible for handling user input, interacting with models, and returning the appropriate response. They define how the application behaves in response to user actions, such as clicking buttons or submitting forms.
You should create a controller whenever you need to handle specific user interactions or route logic — like displaying a list of items, handling form submissions, or managing user authentication.
Where to Create?
Controllers should always be created in the framework/Controllers directory and placed under the Framework\Controllers namespace. Each file must be named after the class it contains and must use the .php extension. For example, if you create a UserController, the file must be named UserController.php.
How to Create?
Continuing with the previous example — after creating the file (UserController.php) — the next step is to define your controller class. This process is quite simple.
First, you need to declare the class namespace (Framework\Controllers) so it can be loaded automatically via autoloading. Then, create the controller class itself. This base class handles common behavior like returning views, redirecting users, and more.
Next, you can define methods within the controller that handle various actions, such as index, store, show, update, and destroy.
Here’s an example of what a UserController might look like:
Namespace Framework\Controllers;
use Framework\Models\User;
/**
* Controller description
*
* @author Author <author email>
* @since year/month(ex:2025/05)
* @version year/month(ex:2025/05)
*/
Class UserController{
private User $user;
public function __construct() {
$this->user = new User();
}
/**
* Function that receives user id by param, search it in the database and returns the user as an array if it finds it
*
* @author Author <author email>
* @since year/month(ex:2025/05)
* @version year/month(ex:2025/05)
*
* @return int $id
*/
public function getUser($id) {
$user = $this->user->find($id);
echo json_encode($user->toArray());
}
/**
* Function that receives a set of params by POST, inserts user in the database and returns the user id
*
* @author Author <author email>
* @since year/month(ex:2025/05)
* @version year/month(ex:2025/05)
*
* @return int $id
*/
public function insert() {
$data = $_POST; // Assume data is sent via POST for example
$id = $this->user->insert($data);
return $id;
}
//...
}
Good Practices
Here are some tips to keep your controllers clean, consistent, and easy to maintain:
-
Keep it simple: Controllers should focus on user interaction and delegate business logic to models. Avoid placing complex logic directly inside the controller.
-
Organize by resource: Name controllers according to the resource they manage (e.g., UserController, ProductController, OrderController).
-
Use meaningful methods: Name controller methods clearly to reflect what they do (e.g., create(), edit(), delete(), etc.).
-
Reuse code: Use helper methods or services for common logic to keep controllers lean.
-
Document your controllers and methods: Add a PHPDoc block at the top of each controller and method with a brief description of its purpose, function params and type of return if applies.
-
Follow conventions: Stick to naming conventions like camelCase for methods and properties, and PascalCase for classes.
↑ (Última atualização: 06/05/2025)