Sto provando a implementare da solo classi che gestiscano il pattern MVC, a me è venuto un solo file e una sola classe X_x ve la mostro, classe e come si usa xD
Codice PHP:
<?php
class controllerFront
{
protected $_controller, $_action, $_params;
private static $_instance;
private $_path;
public function getPath()
{
return $this->_path;
}
public static function getInstance()
{
if( !(self::$_instance instanceof self) )
{
self::$_instance = new self();
}
return self::$_instance;
}
private function __construct()
{
$request = $this->getURI();
$argomenti = explode('/' , trim($request, '/'));
$nArgomenti = count($argomenti);
if($nArgomenti > 2)
{
$this->_controller = $argomenti[0];
$this->_action = $argomenti[1];
$i = 2;
$this->_params = array();
while($i < $nArgomenti)
{
if($i % 2 == 0)
{
//se non e [url]www.prova.tld/controller/action/parametro/null/parametro2/valore2[/url]
//setta a null senno settagli il valore..
// xD
$this->_params[$argomenti[$i]] = strtolower($argomenti[$i+1]) != 'null' ? $argomenti[$i+1] : null;
}
else {}
$i++;
}
}
else if($nArgomenti == 2)
{
$this->_controller = $argomenti[0];
$this->_action = $argomenti[1];
}
else if($nArgomenti == 1)
{
$this->_controller = is_null($argomenti[0]) || empty($argomenti[0]) ? 'index' : $argomenti[0];
$this->_action = 'index';
}
}
function getController()
{
return $this->_controller;
}
function getAction()
{
return $this->_action;
}
function getURI()
{
return $_SERVER['REQUEST_URI'];
}
function getParam($param)
{
if(array_key_exists($param, $this->_params)) { return $this->_params[$param]; }
else { return null; }
}
function dispatch($path = "app/")
{
$this->_path = $path;
include $this->_path . "controllers/" . $this->_controller . ".php";
$controller = new $this->_controller();
ob_start();
call_user_func(array($controller, $this->_action));
include $this->_path . "views/" . $this->_controller . ".html.php";
return ob_get_clean();
}
}
?>
Questa è la classe che gestisce il Controller e la View...
Codice PHP:
<?php
class index
{
public function index()
{
$this->ciao = "CIAOOOOOOO";
}
}
?>
La classe index..
Codice PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?php print $controller->ciao; ?>
</body>
</html>
e la vista XD
ecco, mostra CIAOOOOOOOOO... e sembra leggero xD cioè a me serviva qualcosa che non fosse pacchiano, che mi permettesse di separare codice da design.. e ce l'ho fatta.. :-\
avete consigli su qualcosa che gli manca? apparte il routing che implementerò presto xD
Ciau XD