RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php [L]
<?php class App { public static $router; public static $db; public static $kernel; public static function init() { spl_autoload_register(['static','loadClass']); static::bootstrap(); set_exception_handler(['App','handleException']); } public static function bootstrap() { static::$router = new App\Router(); static::$kernel = new App\Kernel(); static::$db = new App\Db(); } public static function loadClass ($className) { $className = str_replace('\\', DIRECTORY_SEPARATOR, $className); require_once ROOTPATH.DIRECTORY_SEPARATOR.$className.'.php'; } public function handleException (Throwable $e) { if($e instanceof \App\Exceptions\InvalidRouteException) { echo static::$kernel->launchAction('Error', 'error404', [$e]); }else{ echo static::$kernel->launchAction('Error', 'error500', [$e]); } } }
<?php namespace App; class Router { public function resolve () { if(($pos = strpos($_SERVER['REQUEST_URI'], '?')) !== false){ $route = substr($_SERVER['REQUEST_URI'], 0, $pos); } $route = is_null($route) ? $_SERVER['REQUEST_URI'] : $route; $route = explode('/', $route); array_shift($route); $result[0] = array_shift($route); $result[1] = array_shift($route); $result[2] = $route; return $result; } }
<?php namespace App; use App; class Db { public $pdo; public function __construct() { $settings = $this->getPDOSettings(); $this->pdo = new \PDO($settings['dsn'], $settings['user'], $settings['pass'], null); } protected function getPDOSettings() { $config = include ROOTPATH.DIRECTORY_SEPARATOR.'Config'.DIRECTORY_SEPARATOR.'Db.php'; $result['dsn'] = "{$config['type']}:host={$config['host']};dbname={$config['dbname']};charset={$config['charset']}"; $result['user'] = $config['user']; $result['pass'] = $config['pass']; return $result; } public function execute($query, array $params=null) { if(is_null($params)){ $stmt = $this->pdo->query($query); return $stmt->fetchAll(); } $stmt = $this->pdo->prepare($query); $stmt->execute($params); return $stmt->fetchAll(); } }
<?php return [ 'type' => 'mysql', 'host' => 'localhost', 'dbname' => 'gotlib', 'charset' => 'utf8', 'user' => 'root', 'pass' => '' ];
<?php namespace App; use App; class Kernel { public $defaultControllerName = 'Home'; public $defaultActionName = "index"; public function launch() { list($controllerName, $actionName, $params) = App::$router->resolve(); echo $this->launchAction($controllerName, $actionName, $params); } public function launchAction($controllerName, $actionName, $params) { $controllerName = empty($controllerName) ? $this->defaultControllerName : ucfirst($controllerName); if(!file_exists(ROOTPATH.DIRECTORY_SEPARATOR.'Controllers'.DIRECTORY_SEPARATOR.$controllerName.'.php')){ throw new \App\Exceptions\InvalidRouteException(); } require_once ROOTPATH.DIRECTORY_SEPARATOR.'Controllers'.DIRECTORY_SEPARATOR.$controllerName.'.php'; if(!class_exists("\\Controllers\\".ucfirst($controllerName))){ throw new \App\Exceptions\InvalidRouteException(); } $controllerName = "\\Controllers\\".ucfirst($controllerName); $controller = new $controllerName; $actionName = empty($actionName) ? $this->defaultActionName : $actionName; if (!method_exists($controller, $actionName)){ throw new \App\Exceptions\InvalidRouteException(); } return $controller->$actionName($params); } }
<?php namespace App; use App; class Controller { public $layoutFile = 'Views/Layout.php'; public function renderLayout ($body) { ob_start(); require ROOTPATH.DIRECTORY_SEPARATOR.'Views'.DIRECTORY_SEPARATOR.'Layout'.DIRECTORY_SEPARATOR."Layout.php"; return ob_get_clean(); } public function render ($viewName, array $params = []) { $viewFile = ROOTPATH.DIRECTORY_SEPARATOR.'Views'.DIRECTORY_SEPARATOR.$viewName.'.php'; extract($params); ob_start(); require $viewFile; $body = ob_get_clean(); ob_end_clean(); if (defined(NO_LAYOUT)){ return $body; } return $this->renderLayout($body); } }
<?php define('ROOTPATH', __DIR__); require __DIR__.'/App/App.php'; App::init(); App::$kernel->launch();
<?php namespace Controllers; class Home extends \App\Controller { public function index () { return $this->render('Home'); } }
<img src="Img/my_photo.jpeg" alt="my_photo" id="my_photo"> <h1></h1> <p> - -.</p> :<br> 8-912-641-3462<br> goootlib@gmail.com
<!DOCTYPE html> <html lang="ru"> <head> <meta charset="utf-8"> <title> </title> <meta name="viewport" content="width=device-width,initial-scale=1"> <link href="/Css/style_layout.css" rel="stylesheet" type="text/css"> <link href="https://fonts.googleapis.com/css?family=Roboto+Condensed" rel="stylesheet"> </head> <body> <header> <nav> <a id="about_button" href="/home"> </a> <a id="portfolio_button" href="/portfolio"></a> <a id="blog_button" href="/blog"></a> </nav> </header> <div class="main"> <?= $body ?> </div> </body> </html>
Source: https://habr.com/ru/post/320480/
All Articles