Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте How controllers work | Controllers
Quizzes & Challenges
Quizzes
Challenges
/
PHP MVC Development

bookHow controllers work

Note
Definition

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

HomeController.php

views/home.php

views/home.php

copy
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

ContactController.php

views/contact_result.php

views/contact_result.php

copy
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.

question mark

What is the main responsibility of a controller in MVC?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 1

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

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?

bookHow controllers work

Свайпніть щоб показати меню

Note
Definition

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

HomeController.php

views/home.php

views/home.php

copy
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

ContactController.php

views/contact_result.php

views/contact_result.php

copy
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.

question mark

What is the main responsibility of a controller in MVC?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 1
some-alt