MVC Framework PHP
Fayllar tuzilmasini to’g’irlab olamiz. Asosiy papka ichiga App papkasini yaratamiz va buning ichidan config, controllers, core, lib, models, views papkalarini yaratamiz. Asosiy papka ichiga public papkasini ham yaratib olamiz. Index.php va .htaccess faylarini yaratamiz.
.htaccess fayliga quyidagilarni yozamiz:
AddDefaultCharset utf-8
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php
App->lib->Dev.php
ini_set('display_errors', 1);
error_reporting(E_ALL);
bu yerda ini_set(‘display_errors’, 1) ekrandagi xatoliklarni chiqarishni aktivlashtiradi. Error_reporting funksiyasi esa xatolar hisobotini chiqarish uchun kerak.
Xatolarni ekranda ko’rsatib beruvchi funksiya yaratamiz:
function debug($str)
{
echo "
";
var_dump($str);
echo "
";
exit;
}
Bu faylni index.php ga bog’laymiz va debug funksiyamizni ishlatib ko’ramiz.
require 'app/lib/Dev.php';
debug($rest);
?>
Brauzer oynasida xatolik chiqadi agar dev.php faylimizdan xatoliklarni ko’rsatuvchi funksiyalarni o’chirsak xatolar chiqmaydi.
App->core->Router.php faylini yaratamiz.
namespace app\core;
class Router
{
function __construct()
{
echo "Router classi";
}
}
?>
Router classining yangi obyektini yaratamiz index.php faylida.
$router = new Router();
Bu albatta bizda fatal error beradi. Sinflarni autoyuklovchi funksiya yaratamiz.
require 'app/lib/Dev.php';
use app\core\Router;
spl_autoload_register(function($class){
echo $class;
});
$router = new Router();
?>
Autoyuklovchi funksiyamizga classlarni avtoyuklash uchun kod yozamiz:
spl_autoload_register(function($class){
$path = str_replace('\\', '/', $class.'.php');
if(file_exists($path)){
require $path;
}
});
App->lib->Db.php faylini yaratamiz.
namespace app\lib;
class Db
{
function __construct()
{
echo "DataBase classi";
}
}
?>
Buni ham index fayliga avtoyuklangan xolda ishlashini tekshiramiz.
require 'app/lib/Dev.php';
use app\lib\Db;
use app\core\Router;
spl_autoload_register(function($class){
$path = str_replace('\\', '/', $class.'.php');
if(file_exists($path)){
require $path;
}
});
$router = new Router();
$db = new Db();
App->lib->Db.php faylini o’chiramiz. Index.php fayliga session_start() funksiyasini yozib sessiyani yoqib qo’yamiz.
require 'app/lib/Dev.php';
use app\core\Router;
spl_autoload_register(function($class){
$path = str_replace('\\', '/', $class.'.php');
if(file_exists($path)){
require $path;
}
});
session_start();
$router = new Router();
?>
Router.php faylimizga ikkita protected xossa va 3-ta metod qo’shamiz.
namespace app\core;
class Router
{
protected $routes = [];
protected $params = [];
public function __construct()
{
//
}
public function add()
{
}
public function match()
{
}
public function run()
{
echo "Ok";
}
}
?>
Add metodi marshurt qo’shish uchun, match metodi marshurtlarni tekshirish va run Router classimizni ishlatish uchun. Index.php faylimizda run metodini ishga tushiramiz:
$router->run();
App->config->routes.php
return [
'account/login' => [
'controller' => 'account',
'action' => 'login',
],
'news/show' => [
'controller' => 'news',
'action' => 'show',
],
];
Bu fayl bizga oddiy massiv qaytaradi. Biz kiritgan marshurtlar bo’yicha controller va actionni bor yoki yo’qligini aniqlab beradi. Bu faylni Router.php faylini constructor metodiga bog’laymiz va debug funksiyasi yordamida uni ekranga chiqaramiz.
Router.php
public function __construct()
{
$arr = require 'app/config/routes.php';
debug($arr);
}
Router.php
public function __construct()
{
$arr = require 'app/config/routes.php';
foreach($arr as $key => $val){
$this->add($key, $val);
}
}
public function add($route, $params)
{
echo $route . "
";
}
Router.php
public function add($route, $params)
{
$route = '#^'.$route.'$#';
$this->routes[$route] = $params;
}
Sahifaning joriy url manzilini tekshiramiz.
public function match()
{
$url = $_SERVER['REQUEST_URI'];
debug($url);
}
public function run()
{
$this->match();
}
public function match()
{
$url = trim($_SERVER['REQUEST_URI'], '/');
foreach($this->routes as $route => $params){
if(preg_match($route, $url, $matches)){
var_dump($matches);
}
}
}
public function match()
{
$url = trim($_SERVER['REQUEST_URI'], '/');
foreach($this->routes as $route => $params){
if(preg_match($route, $url, $matches)){
var_dump($params);
}
}
}
Bu match metodimiz url mavjud yoki mavjudmasligini tekshiradi. Routes.php faylidagi massivlar ichida biz kiritgan url bavjud bo’lsa natija qaytadi mavjud bo’lmasa qaytmaydi. Endi url marshurt mavjud yoki mavjudmasligini tekshiramiz.
public function match()
{
$url = trim($_SERVER['REQUEST_URI'], '/');
foreach($this->routes as $route => $params){
if(preg_match($route, $url, $matches)){
$this->params = $params;
return true;
}
}
return false;
}
public function run()
{
if($this->match())
{
echo "Marshurt topildi";
}
else {
echo "Marshurt topilmadi";
}
}
Routes.php ga yangi url manzil kirtamiz va tekshirib ko’ramiz
'account/register' => [
'controller' => 'account',
'action' => 'register',
],
Router.php
public function run()
{
if($this->match())
{
echo '
Controller:
Do'stlaringiz bilan baham: |