Kommunikasiyalarini rivojlantirish vazirligi muhammad al-xorazmiy nomidagi toshkent axborot texnologiyalari universiteti


Download 0.91 Mb.
bet11/12
Sana21.01.2023
Hajmi0.91 Mb.
#1107597
1   ...   4   5   6   7   8   9   10   11   12
Bog'liq
dori darmon

V. Ilova


// FRONT CONTROLLER
// 1. Obshiy nastroyka
//ini_set('display_errors', 1); //error_reporting(E_ALL); session_start();
// 2. Podklyucheniya faylov sistem
define('ROOT', dirname(__FILE__)); // Proyektning polniy putini ROOT constanataga ozlashtirish
require_once(ROOT.'/components/Autoload.php'); // Autoload da parcha klasslar avtomatik yoqiladi
//require_once(ROOT.'/components/Router.php'); // Router.php da biz sorovlarni boshqaramiz
//require_once(ROOT.'/components/Db.php');
// 3. Ustanovka soyideneniya s BD
// 4. Vizov Router
$router = new Router;
$router -> run();
// Front controller bilan ishlash tugadi buyogiga router bilan ishlanadi
?> abstract class AdminBase {
* @return boolean public static function checkAdmin()
{
$userId = User::checkLogged(); $user = User::getUserById($userId); if ($user['role'] == 'admin') { return true;
} die('Access denied');
}
}
function __autoload($class_name)
{
$array_paths = array(
'/models/',
'/components/',
'/controllers/',
);
foreach ($array_paths as $path) {
$path = ROOT . $path . $class_name . '.php';
if (is_file($path)) {
include_once $path;
}
}
}
class Db
{
public static function getConnection()
{
$paramsPath = ROOT . '/config/db_params.php';
$params = include($paramsPath);
$dsn = "mysql:host={$params['host']};dbname={$params['dbname']}";
$db = new PDO($dsn, $params['user'], $params['password']);
$db->exec("set names utf8");
return $db;
}
}
class Router { private $routes; //massiv marshrutlar uchun yani: news, article, ...
public function __construct()
{
$routesPath = ROOT . '/config/routes.php';
$this->routes = include($routesPath); // yani routes.php dan massivni qabul qilish
}
/**

  • Vozvrashayet CHPU yani saytndan keyingi /..../.. zaproslar masalan news/index

  • @return string

*/
private function getURI()
{
if(!empty($_SERVER['REQUEST_URI']))
{
return trim($_SERVER['REQUEST_URI'], '/');
}
}
/**

*/
public function run()
{
/******** Keyingilarni bajarishdan oldin 1-dars 25minutdan korish kere ********/
// Poluchit stroku zaprosa
$uri = $this->getURI();
// Proverit nalichiye takova zaprosa v routes.php foreach ($this->routes as $uriPattern => $path)
{
// Sravnivayem $uriPattern i $uri
if(preg_match("~$uriPattern~", $uri))
{ .
$internalRoute = preg_replace("~$uriPattern~", $path, $uri);
// Opredelit kontroller, action, parametr
$segments = explode('/', $internalRoute);
$controllerName = array_shift($segments) . 'Controller';
$controllerName = ucfirst($controllerName);
$actionName = 'action' . ucfirst(array_shift($segments));
$parametrs = $segments;
// Podklyucheniya fayl klassa-kontollera
$contollerFile = ROOT . '/controllers/' . $controllerName . '.php'; if(file_exists($contollerFile))
{
include_once ($contollerFile);
}
// Sozdat obyekt i vizvat metod(yani action)
$controllerObject = new $controllerName;
$result = call_user_func_array(array($controllerObject, $actionName),
$parametrs); if($result != NULL)
{
break; //agar obyekt hosil qilinib funksiyaga murojat qiganimizada
natija ose osha klass bilan ishlash uchun foreach siklidan chiqib ketamiza
}
}
}
}
}
?>
return array (
'host' => 'localhost',
'dbname' => 'apteka',
'user' => 'root',
'password' => '',
);







Download 0.91 Mb.

Do'stlaringiz bilan baham:
1   ...   4   5   6   7   8   9   10   11   12




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling