View class, refactoring
This commit is contained in:
parent
8d2af9181a
commit
63e7c95415
@ -3,12 +3,8 @@
|
||||
class ExampleController extends Controller{
|
||||
|
||||
public function index(){
|
||||
echo 'example';
|
||||
|
||||
// TODO:
|
||||
// - set view
|
||||
// - process POST variables
|
||||
// ...
|
||||
$this->view->title = 'Example title';
|
||||
$this->view->render('example.phtml');
|
||||
}
|
||||
|
||||
public function subpage(){
|
||||
|
20
app/views/layouts/default.phtml
Normal file
20
app/views/layouts/default.phtml
Normal file
@ -0,0 +1,20 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title><?php echo $this->title ?></title>
|
||||
<link rel="stylesheet" href="css/main.css">
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
{HEADER}
|
||||
</header>
|
||||
|
||||
<?php echo $this->content(); ?>
|
||||
|
||||
<footer>
|
||||
{FOOTER}
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
1
app/views/templates/example.phtml
Normal file
1
app/views/templates/example.phtml
Normal file
@ -0,0 +1 @@
|
||||
Example template
|
@ -1,20 +0,0 @@
|
||||
.loader {
|
||||
border: 16px solid #f3f3f3;
|
||||
border-radius: 50%;
|
||||
border-top: 16px solid #3498db;
|
||||
width: 120px;
|
||||
height: 120px;
|
||||
-webkit-animation: spin 2s linear infinite; /* Safari */
|
||||
animation: spin 2s linear infinite;
|
||||
}
|
||||
|
||||
/* Safari */
|
||||
@-webkit-keyframes spin {
|
||||
0% { -webkit-transform: rotate(0deg); }
|
||||
100% { -webkit-transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
0% { transform: rotate(0deg); }
|
||||
100% { transform: rotate(360deg); }
|
||||
}
|
@ -1,12 +1,11 @@
|
||||
<?php
|
||||
class ApiController {
|
||||
protected $input;
|
||||
protected $authenticated;
|
||||
protected $authenticated = false;
|
||||
|
||||
function __construct() {
|
||||
$this->authenticated = false;
|
||||
|
||||
$input = file_get_contents('php://input');
|
||||
|
||||
if(empty($input)){
|
||||
$this->input = NULL;
|
||||
}else{
|
||||
|
@ -1,5 +1,9 @@
|
||||
<?php
|
||||
|
||||
class Controller{
|
||||
public $view = null;
|
||||
|
||||
public function __construct(){
|
||||
$this->view = new View();
|
||||
}
|
||||
}
|
||||
|
63
library/View.php
Normal file
63
library/View.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
class View{
|
||||
protected $_content = "";
|
||||
protected $_layout = 'default';
|
||||
|
||||
protected $_viewEnabled = true;
|
||||
protected $_layoutEnabled = true;
|
||||
|
||||
protected $_data = array();
|
||||
|
||||
public function disableLayout(){
|
||||
$this->_layoutEnabled = false;
|
||||
}
|
||||
|
||||
public function enableLayout(){
|
||||
$this->_layoutEnabled = false;
|
||||
}
|
||||
|
||||
public function setLayout($layout){
|
||||
$this->_layout = $layout;
|
||||
}
|
||||
|
||||
public function disableView(){
|
||||
$this->_viewEnabled = false;
|
||||
}
|
||||
|
||||
public function __set($key, $value){
|
||||
$this->_data[$key] = $value;
|
||||
}
|
||||
|
||||
public function __get($key){
|
||||
if(array_key_exists($key, $this->_data)){
|
||||
return $this->_data[$key];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function content(){
|
||||
return $this->_content;
|
||||
}
|
||||
|
||||
public function render($template){
|
||||
if($template && $this->_viewEnabled){
|
||||
$this->_fetchContent($template);
|
||||
}
|
||||
|
||||
if($this->_layoutEnabled){
|
||||
include('../app/views/layouts/' . $this->_layout . '.phtml');
|
||||
} else {
|
||||
echo $this->_content;
|
||||
}
|
||||
}
|
||||
|
||||
protected function _fetchContent($template){
|
||||
ob_start();
|
||||
|
||||
include('../app/views/templates/' . $template);
|
||||
|
||||
$this->_content = ob_get_clean();
|
||||
}
|
||||
}
|
@ -1,13 +1,14 @@
|
||||
.loader {
|
||||
border: 16px solid #f3f3f3;
|
||||
border-radius: 50%;
|
||||
border-top: 16px solid rgb(101, 30, 122);;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
border-top: 16px solid #3498db;
|
||||
width: 120px;
|
||||
height: 120px;
|
||||
-webkit-animation: spin 2s linear infinite; /* Safari */
|
||||
animation: spin 2s linear infinite;
|
||||
}
|
||||
|
||||
/* Safari */
|
||||
@-webkit-keyframes spin {
|
||||
0% { -webkit-transform: rotate(0deg); }
|
||||
100% { -webkit-transform: rotate(360deg); }
|
||||
|
Loading…
Reference in New Issue
Block a user