<?php
if ( !defined( 'FILE_COMPILE' ) )
exit;
// Developed by Vadim Kondakov,
// Copyright 2016, SibWeb Group,
// License GPL v 2.
if ( !class_exists ( 'Controller' ) ) {
class Controller extends UDAPI {
public $data = array ( );
protected $uri;
protected $pattern = array();
protected $params = array();
protected $request = array();
public $controller;
public $action;
public $default = array (
'controller' => 'home',
'action' => 'index'
);
public function __construct ( ) {
// Получаем и сразу фильтрируем данные URI
$this->uri = filter_input ( INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL );
$this->uri = trim ( $this->uri, '/\\' );
$this->uri = urldecode ( $this->uri );
$this->uri = parse_url ( $this->uri, PHP_URL_PATH );
if (!$this->uri) { return; }
// Разбиваем в массив...
$this->pattern = array_filter ( explode ( '/', $this->uri ), 'mb_strlen' );
if ( $this->uri == '' )
$this->redirect ( $this->config->root_uri . $this->default[ 'controller' ] . '/' . $this->default[ 'action' ], true );
}
public function ready ( ) {
if ( !is_bool ( $this->uri ) ) {
try {
if ( sizeof ( $this->pattern ) % 2 )
throw new Exception ( );
$this->setController ( array_shift ( $this->pattern ) );
$this->setAction ( array_shift ( $this->pattern ) );
if ( is_array ( $this->pattern ) ) {
for ( $i = 0; $i < sizeof ( $this->pattern ); $i++ )
$this->params[ $this->pattern[ $i ] ] = $this->pattern[ ++$i ];
}
// Запускаем...
$this->getModel ( $this->controller );
$controller = $this->getController ( );
if ( $this->getAction ( $controller ) ) {
if ( is_array ( $this->params ) ) {
foreach ( $this->params as $key => $value )
$this->request[ $key ] = $value;
}
call_user_func_array ( array ( $controller, $this->action ), $this->request );
} else {
echo '404 error';
}
} catch ( Exception $e ) {
echo 404;
}
}
}
/**
* Проверка существование контроллера
* @param string $controller
* @return boolean
*/
public function isController ( $controller ) {
if ( is_dir ( SOURCE_DIR . '/controllers/' . $controller . '/' ) ) {
$query = $this->model->db->prepare ('SELECT * FROM `controllers` WHERE `name` = :name');
$query->execute ( array ( 'name' => $controller ) );
// получаем и сохраняем данные о контроллере...
$this->data = $query->fetch ( PDO::FETCH_ASSOC );
// включен ли контроллер в базе?
if ( $this->data[ 'is_enable' ] == 1 )
return true;
}
return false;
}
/**
* Устанавливает текущий контроллер
* @param string $controller
* @return string
*/
public function setController ( $controller ) {
if ( trim ( $controller ) )
$this->controller = $controller;
else
$this->controller = $this->default[ 'controller' ];
}
/**
* Подключает текущий контроллер
* @return object
*/
public function getController ( ) {
if ( $this->isController ( $this->controller ) ) {
require_once ( SOURCE_DIR . '/controllers/' . $this->controller . '/controller.php' );
$class = $this->controller . 'Controller';
if ( class_exists ( $class ) )
return new $class( );
}
return false;
}
/**
* Проверка существование модели
* @param string $model
* @return boolean
*/
public function isModel ( $model ) {
return is_file ( SOURCE_DIR . '/controllers/' . $model . '/model.php' );
}
/**
* Подключение существующей модели
* @param string $controller
* @return object
*/
public function getModel ( $controller ) {
if ( $this->isModel ( $controller ) ) {
require_once ( SOURCE_DIR . '/controllers/' . $controller . '/model.php' );
$model = $controller . 'Model';
if ( class_exists ( $model ) )
return new $model( );
}
return false;
}
/**
* Проверка существование экшена
* @param string $controller
* @param string $action
* @return boolean
*/
public function isAction ( $controller, $action ) {
if ( method_exists ( $controller, $action ) )
return true;
return false;
}
/**
* Устанавливает текущий экшен
* @param string $action
* @return string
*/
public function setAction ( $action ) {
if ( !is_null ( $action ) )
$this->action = $action;
else
$this->action = '';
}
/**
* Загружает текущий экшен
*/
public function getAction ( $controller ) {
// проверяем чтобы контроллер был включен...
if ( $this->isAction ( $controller, $this->action ) )
return true;
return false;
}
public function redirect ( $url = '/', $moved301 = false ) {
if ( $moved301 !== false ) {
header ( "HTTP/1.1 301 Moved Permanently" );
header ( "Location: {$url}" );
} else {
header ( "Location: {$url}" );
}
}
}
}
Пишу свою модель MVC
и мне кажется что я делаю что-то не так и хочется его добить
if ( !defined( 'FILE_COMPILE' ) )
if ( !class_exists ( 'Controller' ) ) {
class Controller extends UDAPI {
protected $uri;
protected $params = array();
protected $request = array();
$this->uri = filter_input ( INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL );
$this->uri = trim ( $this->uri, '/\\' );
$this->uri = urldecode ( $this->uri );
$this->uri = parse_url ( $this->uri, PHP_URL_PATH );
if ( $this->uri == '' )
$this->redirect ( $this->config->root_uri . $this->default[ 'controller' ] . '/' . $this->default[ 'action' ], true );
public function ready ( ) {
$this->setController ( array_shift ( $this->pattern ) );
$this->setAction ( array_shift ( $this->pattern ) );
call_user_func_array ( array ( $controller, $this->action ), $this->request );
} else {
echo '404 error';
}
public function isController ( $controller ) {
if ( is_dir ( SOURCE_DIR . '/controllers/' . $controller . '/' ) ) {
public function getModel ( $controller ) {