Pozdrav svima :)
Nastavak sledi :)
Sazetak nove funkcionalnosti (za sad i dalje grube)
- view klasa koja ce nam od sad procesirati svaki zahtev za stranicom i inicijalizirati sve sto je potrebno za rad (template engine, request handler (urlHandler), bazu, sesiju)
- rekonstruisali smo klase za galeriju, kontakt i home stranu.
Dobre strane nove funkcionalnosti:
- nemamo vise potrebu da diramo index.php koji sad ima samo svrhu prikazivanja sadrzaja
- obradjujemo sve potrebne inicijalizacije na jednom mestu i nije ih potrebno dirati
- jedino na sta trebamo obratiti paznju su klase koje odgovaraju stranicama koje zelimo prikazati
Lose strane:
- nije moguce aktivno menjati core klase kao sto su urlHandler, sessionHandler, template engine
- nije moguce imati vise opcija za jednu stvar (da recimo parsiramo nase template sa necim drugim osim smartijem)
Ima jos dobrih i losih stvari koje sam verovatno preskocio, no njima cemo se baviti u nastavku :)
Za sledecu zastavicu bi mogli da izvucemo template engine iz klase view napravimo jednu apstraktnu klasu preko koje bi mogli da postavljamo template engine (nesto kao plugin (dependency injection))
Ovo ce nam omoguciti da biramo template engine koji zelimo da koristimo. Elaboriracu u nastavku :)
Sta smo postigli sa ovim klasama?
Dovoljno je kreirati novu klasu koja nasledjuje view i u jednoj metodi obraditi prikaz te stranice.
Svu funkcionalnost novo napravljene stranice imamo na jednom mestu izdvojenu od ostalih stranica. Mozemo posvetiti svu svoju paznju samo toj stranici, bez da gledamo kod za ostale stranice.
Lakse nam je obraditi greske koje ce se javljati i dodavati nove funkcionalnosti.
Unificirali smo pristup urlHandleru (getRequest), tako da sad mozemo nesmetano menjati tu klasu a nas kod na stranicama ce i dalje funkcionirati.
Funkcionalnosti koje su nam omogucile da ovako nesto uradimo je facotry metoda.
- Factory metoda (
factory template)
p.s. Nisam koristio interface kao sto sam naglasio u ranijem postu, jer mi se ovo cinilo kao bolji sledeci korak. Interface bi mogli da implementiramo kasnije.
Slede promene.
file: index.php
Code:
<?php
include_once 'View.php';
View::dispatch();
?>
file: class.View.php
Code:
<?php
// Including all the classes we need to display a page. Instead of including this classes one by one each time
// we could also use _autoload method which php provides
include_once "./base/class.urlhandler.php";
include_once "./base/class.db.php";
include_once "./base/class.session.php";
include_once "./smarty/Smarty.class.php";
class View
{
private $_scripts = array();
private $_meta = array();
private $_body = array();
private $_css = array();
private $_session = null;
private $_request = null;
private $_templateEngine = null;
private function __construct()
{
// Creating instance of db (connection to data base)
if(!$db = new DB("localhost", "root", "", "elitesecurity")){ echo "Could not connect to database!";exit(1);}
// Creating instance of session and starting up the session (note: session must start before any display happens.
$this->_session = new Session();
$this->_session->startSession();
$this->_templateEngine = new Smarty();
}
/**
* Display content
*/
static function dispatch()
{
// Build url handler
$request = new urlHandler();
$view = View::factory($request);
$view->_templateEngine->display("header.tpl"); // Executing header
$view->_templateEngine->display("menu.tpl"); // Executing menu
$view->dispatchIndex(); // Executing desired page
$view->_templateEngine->display("footer.tpl"); // Executing footer.
}
private function buildMenu()
{
// Creating menu. Later we will retrieve menu options from data base. There fore this is useful to
// be done here.
$menu = array(
"0" => array("href" => "index.php", "name" => "Home"),
"1" => array("href" => "index.php?page=gallery", "name" => "Gallery"),
"2" => array("href" => "index.php?page=contact", "name" => "Contact"),
);
$this->_templateEngine->assign("menu", $menu); // Menu array
}
/**
* Set request handler
*
* @param urlHandler $request
*/
private function setRequest(urlHandler $request)
{
$this->_request = $request;
}
/**
* @return urlHandler
*/
private function getRequest()
{
return $this->_request;
}
/**
* Initiate page that user requested.
* This method will create instance of class that is in direct releationship with page name.
*
* @param urlHandler $request
* @return View
*/
static private function factory(urlHandler $request)
{
// Creating instance of urlHandler which we will use later.
$page = $request->getValue("page"); // Getting the page we want to display
if(!$page) $page = "home"; // Setting up startup page
// Here we should add a checkup if page does not exist.
// If we dont have the page we should return 404. For now we will assume that we always have the page :/
require_once('class.'.ucfirst($page).'.php');
$view = new $page();
$view->setRequest($request);
return $view;
}
}
?>
file: class.Gallery.php
Code:
<?php
class Gallery extends View
{
public function dispatchIndex()
{
// Assigning variables that will be used in certain pages. This should be called dynamically so
// that it is not executed each time index.php loads.
$haspictures = false;
// Executing query
$pictures = $db->getrows("SELECT * FROM pictures");
// did we got something back?
if($pictures) $haspictures = true;
// We did. Ooo looky looky we have some photos :) Lets display them.
if($haspictures){
foreach($pictures as $key => $value){
$pictures[$key]['src'] = "./uploads/pictures/{$pictures[$key]['picture_name']}";
$pictures[$key]['alt'] = $pictures[$key]['picture_name'];
$pictures[$key]['title'] = $pictures[$key]['picture_name'];
}
}
$this->_templateEngine->assign("pictures", $pictures);
$this->_templateEngine->assign("haspictures", $haspictures);
$this->_templateEngine->display("gallery.tpl");
}
}
?>
file: class.Contact.php
Code:
<?php
class Contact extends View
{
public function dispatchIndex()
{
// Getting email and msg values which are posted and storing those values in corresponding variables.
$email = $this->getRequest()->getValue("email");
$msg = $this->getRequest()->getValue("msg");
// Checking if values aer empty
if($msg != "" && $email != ""){
// Do something. For example send email
/*
if(mail("
[email protected]", "Contact", "Email:$email<br/>\n".$msg)){
echo "Message sent.";
}else{
// Handle error.
}
*/
$msg = "Message sent.";
}
$this->_templateEngine->assign("message", $msg);
$this->_templateEngine->display("contact.tpl");
}
}
?>
file: class.Home.php
Code:
<?php
class Home extends View
{
public function dispatchIndex()
{
$this->_templateEngine->display("index.tpl");
}
}
?>
U ovom momentu struktura sajta treba da izgleda ovako
+base
+cache
+cms
+css
+smarty
+uploads
index.php
class.Gallery.php
class.View.php
class.Contact.php
class.Home.php
---------------------------------------------------------------------
Just do it.