Last active
March 16, 2017 18:37
-
-
Save nikolaykrylov/13d374578860b5509f87082fea12e500 to your computer and use it in GitHub Desktop.
lesson_1 MVC_1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AddDefaultCharset utf-8 | |
RewriteEngine on | |
RewriteBase / | |
RewriteRUle ^(.*)$ index.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Router | |
{ | |
private $routes; | |
public function __construct() | |
{ | |
$routesPath = ROOT.'/config/routes.php'; | |
$this->routes = include($routesPath); | |
} | |
/** | |
* Returns request string | |
* @return string | |
*/ | |
private function getURI() | |
{ | |
if (!empty($_SERVER['REQUEST_URI'])) { | |
return trim($_SERVER['REQUEST_URI'], '/'); | |
} | |
} | |
public function run() | |
{ | |
// Получить строку запроса | |
$uri = $this->getURI(); | |
// Проверить наличие такого запроса в routes.php | |
foreach ($this->routes as $uriPattern => $path) { | |
// Сравниваем $uriPattern и $uri | |
if (preg_match("~$uriPattern~", $uri)) { | |
// Определить какой контроллер и action обрабатывает запрос | |
$segments = explode('/', $path); | |
$controllerName = array_shift($segments).'Controller'; | |
$controllerName = ucfirst($controllerName); | |
$actionName = 'action'.ucfirst(array_shift($segments)); | |
// Подключаем файл класса-контроллера | |
$controllerFile = ROOT . '/controllers/' . | |
$controllerName . '.php'; | |
if (file_exists($controllerFile)) { | |
include_once($controllerFile); | |
} | |
// Создать объект, вызвать метод (т.е. action) | |
$controllerObject = new $controllerName; | |
$result = $controllerObject->$actionName(); | |
if ($result != null) { | |
break; | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return array( | |
'news' => 'news/index', // actionIndex в NewsController | |
'products' => 'product/list', //actionList в ProductController | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class NewsController | |
{ | |
public function actionIndex() | |
{ | |
echo 'NewsController actionIndex'; | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ProductController | |
{ | |
public function actionList() | |
{ | |
echo 'ProductController actionList'; | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// FRONT CONTROLLER | |
// 1. Общие настройки | |
ini_set('display_errors', 1); | |
error_reporting(E_ALL); | |
// 2. Подключение файлов системы | |
define('ROOT', dirname(__FILE__)); | |
require_once(ROOT.'/components/Router.php'); | |
// 3. Установка соединения с БД | |
// 4. Вызов Router | |
$router = new Router(); | |
$router->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment