Salut tout le monde,
J'ai rencontré un nouveau problème avec mon code. Je vais essayer de l'expliquer au mieux. Dans un premier temps, j'ai remarqué que mon message "j'ai chargé la base et je suis connecté" (celui du tuto jour 3) n'apparaissait qu'une seul fois lorsque que je chargait la ligne new model() (aua lieu de deux). En poursuivant le tuto malgré ce détails, je me suis retrouvé confronté à 2 erreur.
Notice: Undefined property: PagesController::$Post in C:\wamp\www\FFF\controller\PagesController.php on line 9
Call Stack
1 0.0006 143288 {main}( ) ..\index.php:0
2 0.0040 174656 Dispatcher->__construct( ) ..\index.php:12
3 0.0042 180144 call_user_func_array ( ) ..\Dispatcher.php:13
4 0.0043 180240 PagesController->view( ) ..\Dispatcher.php:13
Fatal error: Call to a member function find() on a non-object in C:\wamp\www\FFF\controller\PagesController.php on line 9
Call Stack
1 0.0006 143288 {main}( ) ..\index.php:0
2 0.0040 174656 Dispatcher->__construct( ) ..\index.php:12
3 0.0042 180144 call_user_func_array ( ) ..\Dispatcher.php:13
4 0.0043 180240 PagesController->view( ) ..\Dispatcher.php:13
Merci à tout ceux qui pourront m'aider
Guillaume
CONTROLLER
<?php
Class Controller{
public $request;
private $vars = array();
public $layout = 'default';
private $rendered = false;
function __construct($request){
$this->request = $request;
}
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.'default.php';
$this->rendered = true;
}
public function set($key,$value=null){
if(is_array($key)){
$this->vars = $key;
}else{
$this->vars$key] = $value;
}
}
function loadModel($name){
$file = ROOT.DS.'model'.DS.$name.'.php';
require_once($file);
if(isset($this->$name)){
$this->name = new $name();
}
}
}
?>
MODEL
<?php
Class Model{
static $connections = array();
public $db = 'default';
public $table = false;
public function __construct(){
//*connection à la base
$conf = Conf::$databases$this->db];
if(isset(Model::$connections$this->db])){
return true;
}
try{
$pdo = new PDO('mysql:host='.$conf'host'].';dbname='.$conf'database'].';',$conf'login'],$conf'password']);
Model::$connections$this->db] = $pdo;
}catch(PDOException $e){
if(Conf::$debug >= 0){
die($e->getMessage());
}else{
die('impossible de se connecter à la base de donnée');
}
}
//* initialisation d'une variable
if($this->table === false){
$this->table = get_class($this);
}
}
public function find($req){
die($this->table);
}
}
?>
PAGESCONTROLLER
<?php
Class PagesController extends Controller{
function view(){
$this->loadModel('Post');
$posts = $this->Post->find(array(
'conditions' => 'id = 1'
));
print_r($posts);
}
}
CONF
<?php
Class Conf{
static $databases = array(
'default' => array(
'host' => 'localhost',
'database' => 'databasey',
'login' => 'root',
'password' => ''
)
);
}
?>
DISPATCHER
<?php
Class Dispatcher{
var $request;
function __construct(){
$this->request = new Request;
Router::parse($this->request->url,$this->request);
$controller = $this->loadController();
if(!in_array($this->request->action,get_class_methods($controller))){
$this->error('le controller '.$this->request->controller.' n\'a pas de méthode '.$this->request->action);
}
call_user_func_array(array($controller,$this->request->action),$this->request->params);
$controller->render($this->request->action);
}
function error($message){
header("HTTP:1.0 404 Not Found");
$controller = new Controller($this->request);
$controller->set('message',$message);
$controller->render('/errors/404');
die();
}
function loadController(){
$name = ucfirst($this->request->controller).'Controller';
$file = ROOT.DS.'controller'.DS.$name.'.php';
require $file;
return new $name($this->request);
}
}