How controllers work
A controller in MVC is a PHP class or file that receives user input, processes it (often using models), and determines which view should be displayed to the user.
When a user sends a request to a PHP MVC application, it is routed to a controller, which decides how to handle it. The controller reads the user input from the URL or form data, interacts with the model to fetch or update data, and then chooses a view to render the response.
This structure keeps input handling, business logic, and presentation separate, which makes the application easier to maintain, test, and extend.
HomeController.php
views/home.php
1234567891011121314151617<?php class HomeController { public function index() { // Load some data (in a real app, this might come from a model) $message = "Welcome to the homepage!"; // Load the view and pass data to it include 'views/home.php'; } } // Simulate a request to the controller $controller = new HomeController(); $controller->index();
In this example, the HomeController receives the request and runs its index method. It prepares the data (in a real app, usually from a model), then loads the view and passes the data to it. The view renders the data inside an HTML template.
This pattern keeps request handling and data preparation inside the controller, while the view focuses on presentation. The controller is important because it coordinates user actions, data handling, and what the user finally sees on the page.
ContactController.php
views/contact_result.php
1234567891011121314151617181920212223242526<?php class ContactController { public function submit() { $name = isset($_POST['name']) ? trim($_POST['name']) : ''; $email = isset($_POST['email']) ? trim($_POST['email']) : ''; $error = ''; // Basic input validation if ($name === '' || $email === '') { $error = "Name and email are required."; } // In a real app, you might save the data or send an email here include 'views/contact_result.php'; } } // Simulate a form submission $_POST['name'] = 'Alice'; $_POST['email'] = 'alice@example.com'; $controller = new ContactController(); $controller->submit();
This controller example demonstrates basic input validation. The ContactController checks if the required fields are present and not empty before proceeding. If there is an error, it sets an error message that the view can display. By handling input validation and business logic in the controller, you keep your code organized and avoid mixing processing logic with output code. This separation also makes it easier to reuse and test your logic, since controllers can be updated or extended without affecting the view or model layers.
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Can you explain more about how routing works in a PHP MVC application?
What are some best practices for organizing controllers in MVC?
How does the controller interact with the model and view in more complex scenarios?
Awesome!
Completion rate improved to 6.67
How controllers work
Stryg for at vise menuen
A controller in MVC is a PHP class or file that receives user input, processes it (often using models), and determines which view should be displayed to the user.
When a user sends a request to a PHP MVC application, it is routed to a controller, which decides how to handle it. The controller reads the user input from the URL or form data, interacts with the model to fetch or update data, and then chooses a view to render the response.
This structure keeps input handling, business logic, and presentation separate, which makes the application easier to maintain, test, and extend.
HomeController.php
views/home.php
1234567891011121314151617<?php class HomeController { public function index() { // Load some data (in a real app, this might come from a model) $message = "Welcome to the homepage!"; // Load the view and pass data to it include 'views/home.php'; } } // Simulate a request to the controller $controller = new HomeController(); $controller->index();
In this example, the HomeController receives the request and runs its index method. It prepares the data (in a real app, usually from a model), then loads the view and passes the data to it. The view renders the data inside an HTML template.
This pattern keeps request handling and data preparation inside the controller, while the view focuses on presentation. The controller is important because it coordinates user actions, data handling, and what the user finally sees on the page.
ContactController.php
views/contact_result.php
1234567891011121314151617181920212223242526<?php class ContactController { public function submit() { $name = isset($_POST['name']) ? trim($_POST['name']) : ''; $email = isset($_POST['email']) ? trim($_POST['email']) : ''; $error = ''; // Basic input validation if ($name === '' || $email === '') { $error = "Name and email are required."; } // In a real app, you might save the data or send an email here include 'views/contact_result.php'; } } // Simulate a form submission $_POST['name'] = 'Alice'; $_POST['email'] = 'alice@example.com'; $controller = new ContactController(); $controller->submit();
This controller example demonstrates basic input validation. The ContactController checks if the required fields are present and not empty before proceeding. If there is an error, it sets an error message that the view can display. By handling input validation and business logic in the controller, you keep your code organized and avoid mixing processing logic with output code. This separation also makes it easier to reuse and test your logic, since controllers can be updated or extended without affecting the view or model layers.
Tak for dine kommentarer!