Controllers in MVC in PHP
The first thing we'll figure out is controllers. Controllers process user requests, understand what the user wanted to ask of the site, request the appropriate data from the model, and send it to the view.
Controllers are OOP classes.
One file is one class and, accordingly,
one controller. In our framework, controllers
will be stored in the folder project/controllers.
Let's practice creating controllers.
For a warm-up, let's make a class PageController,
which will manage the text pages
on our site.
Let's immediately create a file for our controller.
According to the rules of our framework, each class
must be stored in a file with the same name
(including case). That is, our class PageController
will be stored in the file PageController.php.
Create this file in the folder project/controllers.
Let's make our class in this file:
<?php
namespace Project\Controllers;
use \Core\Controller;
class PageController extends Controller
{
}
?>
As you can see, our class belongs to the namespace
Project\Controllers, following the convention
for file autoloading (i.e., the folder path
must match the namespace).
In addition, our class inherits from the class
Core\Controller, located in the core
of the framework. There is no need to look for a deep
meaning in this, but simply accept it as a rule
of the framework. Here is the rule: all controllers
you create must inherit from
the class Core\Controller, so that everything
works as it should.