Corporate website
Corporate portal for the company!
Internet-shop
Modern and fast website!
Social network
Unique solutions!
Personal website
For your business!
WEB
Full stack Web development...
OUR SOLUTIONS PHP
MVC (Model, View, Controller) architectural pattern.
Application of the MVC architectural template (model, view, controller)... Sample code
The MVC design pattern separates application data, user interface, and control logic into three separate components Model, View, and Controller, so that each component can be modified independently... This allows you to simplify the code of a large application, make it understandable and structured, facilitate maintenance, testing, and reuse of elements!
index.php (Front controller)
// FRONT CONTROLLER
// Error settings
ini_set('display_errors',1);
//ini_set('display_errors','off');
error_reporting(E_ALL);
//error_reporting(E_NOTICE);
session_start();
// Connecting system files
define('ROOT', dirname(__FILE__));
require_once(ROOT.'/components/Autoload.php');
// Calling Router
$router = new Router();
$router->run();
.htaccess
AddDefaultCharset utf-8
#ErrorDocument 404 /
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php
Autoload.php (Automatic class connection function)
function my_autoloader($class_name)
{
// An array of folders in which the necessary classes can be located
$array_paths = array(
'/models/',
'/components/',
'/controllers/',
);
// We are looking through an array of folders
foreach ($array_paths as $path) {
// Formation of the name and path to the file with the class
$path = ROOT . $path . $class_name . '.php';
// If such a file exists, we connect it
if (is_file($path)) {
include_once $path;
}
}
}
spl_autoload_register('my_autoloader');
Router.php (Class for working with routes)
class Router
{
/** array of routes @var array*/
private $routes;
/** The constructor reads and remembers routes*/
public function __construct()
{
// Path to the file with routes
$routesPath = ROOT . '/config/routes.php';
// Getting routes from a file
$this->routes = include($routesPath);
}
/** Returns the query string*/
private function getURI()
{
if (!empty($_SERVER['REQUEST_URI'])) {
return trim($_SERVER['REQUEST_URI'], '/');
}
}
/** Method for processing the request*/
public function run()
{
// Getting the query string
$uri = $this->getURI();
// We check the presence of such a request in the array of routes (routes.php)
foreach ($this->routes as $uriPattern => $path) {
// Comparing $uriPattern and $uri
if (preg_match("~$uriPattern~", $uri)) {
// We get the inner path from the outer one according to the rule (regular expression)
$internalRoute = preg_replace("~$uriPattern~", $path, $uri);
// We define the controller, action, and request parameters
$segments = explode('/', $internalRoute);
// We get the controller name
$controllerName = array_shift($segments) . 'Controller';
$controllerName = ucfirst($controllerName);
// We define the action
$actionName = 'action' . ucfirst(array_shift($segments));
$parameters = $segments;
// We are connecting the controller class file
$controllerFile = ROOT . '/controllers/' .
$controllerName . '.php';
if (file_exists($controllerFile)) {
include_once($controllerFile);
}
// We are creating a controller object
$controllerObject = new $controllerName;
/* We call the necessary method ($actionName) for a certain class ($controllerObject)
* with the specified parameters ($parameters)
*/
$result = call_user_func_array(array($controllerObject, $actionName), $parameters);
// If the controller method (action) is successfully called, shut down the router
if ($result != null) {
break;}
}
}
}
}
routes.php (example of an array with routes)
return array(
/** Here we write routes (!!! using regular expressions)...
* Examples:
* 'confirm/([0-9a-z]+)' => 'user/confirm_email/$1',
* 'news/update/([0-9]+)/([0-9]+)' => 'news/update_news/$1/$2',
*/
// Route for the main page of the site
'index.php' => 'site/index', // actionIndex в SiteController
'' => 'site/index', // actionIndex в SiteController
);
SiteController.php (example of a controller)
class SiteController
{
/**
* Action for the main page of the site
*/
public function actionIndex()
{
// Getting data from the model
$data = Site::getData();
// Connecting the view
require_once(ROOT . '/views/site/index.php');
return true;
}
}
The model interacts with the database. Receives data and performs the necessary actions with it.
/**
* The Site class is a model for SiteController
*/
class Site
{
public static function getData()
{
// connecting to the database
// executing a database query
// getting and returning results
}
}
Here is an example of the implementation of an architectural MVC template in the PHP programming language...
Be sure to use a software development methodology that prevents the introduction of vulnerabilities, provides resistance to malware and unauthorized access!