Доброго времени суток. Если наследование уже использовано, возможно ли не дублировать код? 2 день как пересел с процедурного стиля
Дубли:
private function isAjax()
{
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH'])) ? true : false;
}
$vGetMenu = parent::selectMenu();
$uri = $_SERVER['REQUEST_URI'];
protected function selectMenu()
{
return parent::selectDbh("SELECT `id_menu`, `text_menu`, `path_menu` FROM `menu` WHERE `power_menu` = 'on' ORDER BY `position_menu` ASC");
}
<?php
require_once 'config/config.php';
spl_autoload_register(function ($class)
{
if (file_exists(__DIR__ . '/controllers/' . $class . '.php')) require_once __DIR__ . '/controllers/' . $class . '.php';
else if(file_exists(__DIR__ . '/models/' . $class . '.php')) require_once __DIR__ . '/models/' . $class . '.php';
});
$route = new Route();
$route->get('/', function() use ($dsn, $user, $pass, $attribute)
{
new Home($dsn, $user, $pass, $attribute);
});
$route->get('/albums', function() use ($dsn, $user, $pass, $attribute)
{
new Albums($dsn, $user, $pass, $attribute);
});
$route->get('/contact', function() use ($dsn, $user, $pass, $attribute)
{
new Contact($dsn, $user, $pass, $attribute);
});
$route->run();
<?php
class Route
{
protected $routes = [];
protected $uri;
protected $method;
protected $action = null;
protected $params = [];
private function getUri()
{
return '/' . trim($_SERVER['REQUEST_URI'], '/');
}
private function getMethod()
{
return $_SERVER['REQUEST_METHOD'];
}
private function addRoute($method, $uri, $action)
{
$uri = '/' . trim($uri, '/');
$this->routes[$method][] = ['uri' => $uri, 'action' => $action];
}
public function get($uri, $action)
{
$this->addRoute('GET', $uri, $action);
}
public function post($uri, $action)
{
$this->addRoute('POST', $uri, $action);
}
private function handle()
{
if (is_callable($this->action) && !is_null($this->action))
{
call_user_func_array($this->action, $this->params);
}
else
{
header("HTTP/1.0 404 Not Found");
exit;
}
}
public function run()
{
$this->uri = $this->getUri();
$this->method = $this->getMethod();
foreach ($this->routes[$this->method] as $route)
{
if ($route['uri'] === $this->uri)
{
$this->action = $route['action'];
}
else if (preg_match('#^' . $route['uri'] . '$#i', $this->uri, $matches))
{
$matches = array_slice($matches, 1);
$this->action = $route['action'];
$this->params = $matches;
}
}
$this->handle();
}
}
<?php
class Home extends ModelHome
{
public function __construct($dsn, $user, $pass, $attribute)
{
parent::__construct($dsn, $user, $pass, $attribute);
$this->homePage();
}
private function isAjax()
{
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH'])) ? true : false;
}
private function homePage()
{
$vGetAlbumsPreview = parent::selectAlbumsPreview();
$vGetMenu = parent::selectMenu();
$uri = $_SERVER['REQUEST_URI'];
if ($this->isAjax()) $template = ['slider', 'album', 'event', 'video', 'gallery', 'news'];
else $template = ['header', 'slider', 'album', 'event', 'video', 'gallery', 'news', 'footer'];
require_once '../app/views/index.php';
}
}
<?php
class Albums extends ModelAlbums
{
public function __construct($dsn, $user, $pass, $attribute)
{
parent::__construct($dsn, $user, $pass, $attribute);
$this->albumsPage();
}
private function isAjax()
{
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH'])) ? true : false;
}
private function albumsPage()
{
$vGetAlbums = parent::selectAlbums();
$vGetMenu = parent::selectMenu();
$uri = $_SERVER['REQUEST_URI'];
if ($this->isAjax()) $template = ['albums_content'];
else $template = ['header', 'albums_content', 'footer'];
require_once '../app/views/index.php';
}
}
<?php
class ModelHome extends Model
{
public function __construct($dsn, $user, $pass, $attribute)
{
parent::__construct($dsn, $user, $pass, $attribute);
}
protected function selectAlbumsPreview()
{
return parent::selectDbh("SELECT albums.album_id,
albums.album_cover,
albums.album_name,
albums.album_artist FROM albums, bits WHERE albums.album_id = bits.bits_album_id GROUP BY bits.bits_album_id ORDER BY `album_id` DESC LIMIT 3");
}
protected function selectMenu()
{
return parent::selectDbh("SELECT `id_menu`, `text_menu`, `path_menu` FROM `menu` WHERE `power_menu` = 'on' ORDER BY `position_menu` ASC");
}
}
<?php
class ModelAlbums extends Model
{
public function __construct($dsn, $user, $pass, $attribute)
{
parent::__construct($dsn, $user, $pass, $attribute);
}
protected function selectAlbums()
{
return parent::selectDbh("SELECT albums.album_id,
albums.album_cover,
albums.album_name,
albums.album_artist FROM albums, bits WHERE albums.album_id = bits.bits_album_id GROUP BY bits.bits_album_id ORDER BY `album_id` DESC");
}
protected function selectMenu()
{
return parent::selectDbh("SELECT `id_menu`, `text_menu`, `path_menu` FROM `menu` WHERE `power_menu` = 'on' ORDER BY `position_menu` ASC");
}
}
<?php
class Model
{
private $dbh;
public function __construct($dsn, $user, $pass, $attribute)
{
try
{
$this->dbh = new PDO($dsn, $user, $pass, $attribute);
}
catch (PDOException $e)
{
echo "Error!: " . $e->getMessage();
exit;
}
}
protected function selectDbh($sql, $params = [])
{
$stmt = $this->dbh->prepare($sql);
$stmt->execute();
if ($stmt->rowCount() > 0) return $stmt->fetchAll(PDO::FETCH_ASSOC);
else return false;
}
protected function insertDbh($sql, $params = [])
{
}
protected function updateDbh($sql, $params = [])
{
}
protected function deleteDbh($sql, $params = [])
{
}
}