Voici mes fichier
Controller.php
<?php
class Controller {
public $request; // Objet Request
private $vars = array(); // Variables à passer à la vue
public $layout = 'default'; // Layout à utiliser pour rendre la vue
private $rendered = false; // Si le rendu a été fait ou pas ?
/**
* Constructeur
* @param $requet Objet requet de notre application
**/
function __construct($request = null) {
$this->Session = new Session();
$this->Form = new Form($this);
if($request){
$this->request = $request; // on stock la request dans l'instatced
require ROOT.DS.'config'.DS.'hook.php';
}
}
/**
* Permet de rendre une vue
* @param $view Fichier à rendre (chemin depuis view ou nom de la vue)
**/
public function render($view) {
if($this->rendered) { return false; }
extract($this->vars);
if(strpos($view,'/')===0){
$view = ROOT.DS.'view'.$view.'.php';
}else{
$view = ROOT.DS.'view'.DS.$this->request->controller.DS.$view.'.php';
}
ob_start();
require($view);
$content_for_layout = ob_get_clean();
require ROOT.DS.'view'.DS.'layout'.DS.$this->layout.'.php';
$this->rendered = true;
}
/**
* Permet de passer une ou pluisieur variable à la vue
* @param $key nom de la variable ou tableau de variables
* @param $value Valeur de la variable
**/
public function set($key,$value=null) {
if(is_array($key)) {
$this->vars += $key;
}else{
$this->vars$key] = $value;
}
}
/**
* Permet de charger un model
**/
function loadModel($name){
if(!isset($this->$name)){
$file = ROOT.DS.'model'.DS.$name.'.php';
require_once($file);
$this->$name = new $name();
if(isset($this->Form))
$this->$name->Form = $this->Form;
}
}
/**
* Permet de gérer les erreurs 404
**/
function e404($message){
header("HTTP/1.0 404 Not found");
$this->set('message',$message);
$this->render('/errors/404');
die();
}
/**
* permet d'appeler un controller depuis une vue
**/
function request ($controller,$action){
$controller .= 'Controller';
require_once ROOT.DS.'controller'.DS.$controller.'.php';
$c = new $controller();
return $c->$action();
}
/**
* redirect
**/
function redirect ($url,$code = null ){
if($code == 301){
header("HTTP/1.1 301 Moved Permanently");
}
header("location: ".Router::url($url));
}
}
et voila le fichier
Model.php
<?php
class Model{
static $connections = array();
public $conf = 'default';
public $table = false;
public $db;
public $primaryKey = 'id';
public $id;
public $errors = array();
public $form;
public function __construct(){
//j'initialise quelque variable
if($this->table=== false){
$this->table = strtolower(get_class($this)).'s';
}
// Jme connect a la base
$conf = Conf::$databases$this->conf];
if(isset(Model::$connections$this->conf])){
$this->db = Model::$connections$this->conf];
return true;
}
try{
$pdo = new PDO(
'mysql:host='.$conf'host'].';dbname='.$conf'database'].';',
$conf'login'],
$conf'password'],
array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')
);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
Model::$connections$this->conf] = $pdo;
$this->db = $pdo;
}catch(PDOException $e){
if(Conf::$debug >= 1){
die($e->getMessage());
}else{
die('Impossible de se connecter à la base de donnée');
}
}
}
public function find($req = array()){
$sql = 'SELECT ';
if(isset($req'fields'])){
if(is_array($req'fields'])){
$sql .= implode(', ',$req'fields']);
}else{
$sql .= $req'fields'];
}
}else{
$sql.='*';
}
$sql .= ' FROM '.$this->table.' as '.get_class($this).' ';
//contruction de la condition
if(isset($req'conditions'])){
$sql .= 'WHERE ';
if(!is_array($req'conditions'])){
$sql .= $req'conditions'];
}else{
$cond = array();
foreach($req'conditions'] as $k=>$v){
if(!is_numeric($v)){
$v = '"'.mysql_escape_string($v).'"';
}
$cond] = "$k=$v";
}
$sql .= implode(' AND ',$cond);
}
}
if(isset($req'limit'])){
$sql .= 'LIMIT '.$req'limit'];
}
$pre = $this->db->prepare($sql);
$pre->execute();
return $pre->fetchAll(PDO::FETCH_OBJ);
}
public function findFirst($req){
return current($this->find($req));
}
public function findCount($conditions){
$res = $this->findFirst(array(
'fields' => 'COUNT('.$this->primaryKey.') as count',
'conditions' => $conditions
));
return $res->count;
}
public function delete($id){
$sql = "DELETE FROM {$this->table} WHERE {$this->primaryKey} = $id";
$this->db->query($sql);
}
public function save($data){
$key = $this->primaryKey;
$fields = array();
$d = array();
foreach($data as $k=>$v){
if($k!=$this->primaryKey){
$fields] = "$k=:$k";
$d":$k"] = $v;
}elseif(!empty($v)){
$d":$k"] = $v;
}
}
if(isset($data->$key) && !empty($data->$key)){
$sql = 'UPDATE '.$this->table.' SET '.implode(',',$fields).' WHERE '.$key.'=:'.$key;
$this->id = $data->$key;
$action = 'update';
} else{
$sql = 'INSERT INTO '.$this->table.' SET '.implode(',',$fields);
$action = 'insert';
}
$pre = $this->db->prepare($sql);
$pre->execute($d);
if($action == 'insert'){
$this->id = $this->db->lastInsertId();
}
}
}