Permalink
Browse files

урок 5

  • Loading branch information...
vaajnur committed Mar 5, 2017
1 parent 0b8f0d6 commit 8b4a16b14f582fdee148497e237a34cc9cf18dcb
@@ -9,5 +9,6 @@
require_once 'core/view.php';
require_once 'core/controller.php';
require_once 'core/route.php';
require_once 'core/autoload.php';
$router = new Route();
$router->start(); // запускаем маршрутизатор
@@ -1,6 +1,6 @@
<?php
use Lib\Lib_Registry;
use Lib\Registry;
define('PATH_SITE', $_SERVER['DOCUMENT_ROOT']);
define('HOST', 'localhost');
@@ -9,7 +9,7 @@
define('NAME_BD', 'articles');
define ('DS', DIRECTORY_SEPARATOR);
$mysqli = new mysqli(HOST, USER, PASSWORD,NAME_BD)or die("Невозможно установить соединение c базой данных".$mysqli->connect_errno());
Lib_Registry::set('mysqli',$mysqli);
Registry::set('mysqli',$mysqli);
$mysqli->query('SET names "utf8"'); //база устанавливаем кодировку данных в базе
@@ -0,0 +1,38 @@
<?php
namespace Controllers\Admin;
use Models\Admin\Articles as Articles1;
use Core\Controller;
class Articles extends Controller{
/**
*
*/
public function index(){
$articles = new Articles1();
$articles = $articles->get_articles();
$data = array(
'articles' => $articles
);
$this->view->generate('', $data);
}
/**
* @param array $args
*/
function article(array $args = null){
$id = (isset($args[0])? $args[0]: "");
$model = new Articles1();
$article = $model->getById($id);
$data = array(
'article' => $article,
);
$this->view->generate('', $data);
}
}
?>
@@ -0,0 +1,35 @@
<?php
namespace Controllers\Admin;
use Core\Controller;
use Models\Admin\Categories as Categories1;
class Categories extends Controller
{
/**
*
*/
public function index(){
$categories = new categories1();
$categories = $categories->get_categories();
$data = array(
'categories' => $categories
);
$this->view->generate('categories', $data);
}
/**
*
*/
public function category(array $args = null){
$id = (isset($args[0])? $args[0]: "");
$categories = new categories1();
$category = $categories->getById($id);
$data = array(
'category' => $category
);
$this->view->generate('', $data);
}
}
@@ -1,17 +1,18 @@
<?php
namespace Client;
namespace Controllers\Client;
use Core\Controller;
use Models\Client\Articles as Articles1;
class Controller_Articles extends Controller
class Articles extends Controller
{
/**
*
*/
function action_index()
function index()
{
$model = new Model_Articles();
$model = new Articles1();
$articles = $model->get_articles();
$data = array(
'breadcrumb' => 'Главная',
@@ -24,7 +25,7 @@ function action_index()
/**
* @param array $args
*/
function action_article(array $args = null){
function article(array $args = null){
$id = (isset($args[0])? $args[0]: "");
$model = new Model_Articles();
$article = $model->getById($id);
@@ -0,0 +1,35 @@
<?php
namespace Controllers\Client;
use Core\Controller;
use Models\Client\Categories as Categories1;
class Categories extends Controller
{
/**
*
*/
public function index(){
$categories = new categories1();
$categories = $categories->get_categories();
$data = array(
'categories' => $categories
);
$this->view->generate('categories', $data);
}
/**
*
*/
public function category(array $args = null){
$id = (isset($args[0])? $args[0]: "");
$categories = new categories1();
$categories = $categories->get_category($id);
$data = array(
'categories' => $categories
);
$this->view->generate('', $data);
}
}
@@ -0,0 +1,17 @@
<?
function __autoload($class_name)
{
define('APP_PATH', 'application');
echo $class_name;
$path = strtolower(str_replace("\\", DS, $class_name));
if (file_exists(PATH_SITE.DS.APP_PATH.DS.$path . ".php")) {
include_once(PATH_SITE.DS.APP_PATH.DS.$path . ".php");
}else {
header("HTTP/1.0 404 Not Found");
echo "К сожалению такой страницы не существует. " .PATH_SITE.DS.APP_PATH.DS.$path . ".php ";
exit;
}
}
?>
@@ -2,9 +2,9 @@
namespace Core;
use Lib\Lib_DateBase;
use Lib\DateBase;
class Model extends Lib_DateBase
class Model extends DateBase
{
/**
*
@@ -44,7 +44,7 @@ private function getDirections(&$file, &$controller, &$action, &$args) {
if (empty($route)) {
/* ******************* Default directions ******** */
$controller = 'articles';
$action = 'action_index';
$action = 'index';
$controller_path = $this->controller_path_folder = "application/controllers/$this->namespace/";
$file = $controller_path.$controller.".php";
}
@@ -78,9 +78,9 @@ private function getDirections(&$file, &$controller, &$action, &$args) {
$file = $fullpath."/$part.php";
$action = array_shift($parts);
if(!$action)
$action = 'action_index';
$action = 'index';
else
$action = "action_$action";
$action = "$action";
$args = $parts;
}
}
@@ -105,7 +105,7 @@ public function dispatch(){
}
/* ****** получаем класс ** */
$controller = ucfirst($controller);
$class = ucfirst($this->namespace).'\Controller_' . $controller;
$class = 'Controllers\\'.ucfirst($this->namespace).'\\' . $controller;
// создаем экземпляр
$controller = new $class($this->controller_path_folder);
if (is_callable(array($controller, $action)) == false) {
@@ -21,7 +21,6 @@ public function __construct($controller_path_folder){
*/
function generate($content_view, $data = null)
{
if(is_array($data)) {
extract($data);
}
@@ -32,7 +31,7 @@ function generate($content_view, $data = null)
/* ************ caller для дефолтной вьюхи если не задана */
$trace = debug_backtrace();
$caller = $trace[1]['function'];
$template = str_replace("action_", "", $caller);
$template = $caller;
$content_view = strtolower($template).".php";
}else{
// вьюха для контента
@@ -2,7 +2,7 @@
namespace Lib;
class Lib_DateBase {
class DateBase {
/**
* @param $query
@@ -11,7 +11,7 @@ class Lib_DateBase {
function query($query)
{
// получаем объект mysqli
$mysqli = Lib_Registry::get('mysqli');
$mysqli = Registry::get('mysqli');
// если запрос с параметрами
if(($num_args = func_num_args()) > 1){
$arg = func_get_args();
@@ -24,7 +24,7 @@ function query($query)
$query = vsprintf($query,$arg);
}
// выполняем запрос
$sql = Lib_Registry::get('mysqli')->query($query);
$sql = Registry::get('mysqli')->query($query);
if(preg_match('`^(INSERT|UPDATE|DELETE|REPLACE)`i',$query,$null)){
if($sql = $mysqli->affected_rows){
return $sql;
@@ -49,7 +49,7 @@ function build_query($query,$array,$where = '', $_devide = ',')
{
if(is_array($array)){
$part_query = '';
$mysqli = Lib_Registry::get('mysqli');
$mysqli = Registry::get('mysqli');
foreach($array as $index=>$value){
$part_query .= sprintf(" %s = '%s'".$_devide,$index,$mysqli->real_escape_string($value));
}
@@ -92,6 +92,6 @@ function affected_rows($object)
*/
function insert_id()
{
return Lib_Registry::get('mysqli')->insert_id;
return Registry::get('mysqli')->insert_id;
}
}
@@ -2,7 +2,7 @@
namespace Lib;
class Lib_Registry {
class Registry {
static private $data = array();
static public function set($key, $value) {
self::$data[$key] = $value;
@@ -0,0 +1,28 @@
<?php
namespace Models\Admin;
use Core\Model;
use Lib\Registry;
use Lib\DateBase;
class Articles extends Model{
/**
*
*/
public function get_articles(){
$sql = 'SELECT * FROM article';
$result = DateBase::query($sql);
$row2 = array();
while ($row = $result->fetch_assoc()) {
array_push($row2 , $row);
}
if(empty($row2))return;
return $row2;
}
}
?>
@@ -0,0 +1,39 @@
<?php
namespace Models\Admin;
use Core\Model;
use Lib\Registry;
use Lib\DateBase;
class Categories extends Model{
/**
* @param $id
* @return mixed
*/
public function getById($id){
$sql = 'SELECT * FROM category WHERE id = %s';
$result = DateBase::query($sql, $id);
$row = $result->fetch_assoc();
return $row;
}
/**
*
*/
public function get_categories(){
$sql = 'SELECT * FROM category';
$result = DateBase::query($sql);
$row2 = array();
while ($row = $result->fetch_assoc()) {
array_push($row2 , $row);
}
if(empty($row2))return;
return $row2;
}
}
?>
Oops, something went wrong.

0 comments on commit 8b4a16b

Please sign in to comment.