Bonjour tout le monde
Mon problème est le suivant:
Notice: Undefined property: PostsController::$Request in C:\wamp\www\site\controller\Postscontroller.php on line 15
Call Stack
1 0.0012 371216 {main}( ) ..\index.php:0
2 0.0075 456112 Dispatcher->__construct( ) ..\index.php:9
3 0.0088 470984 call_user_func_array ( ) ..\Dispatcher.php:52
4 0.0088 471120 PostsController->index( ) ..\Dispatcher.php:52
( ! ) Notice: Trying to get property of non-object in C:\wamp\www\site\controller\Postscontroller.php on line 15
Call Stack
1 0.0012 371216 {main}( ) ..\index.php:0
2 0.0075 456112 Dispatcher->__construct( ) ..\index.php:9
3 0.0088 470984 call_user_func_array ( ) ..\Dispatcher.php:52
4 0.0088 471120 PostsController->index( ) ..\Dispatcher.php:52
mes fichiers sont:
<?php
class Request{
public $url;//url appelle par l'utilisateur
public $page = 1;
function __construct(){
$this->url = $_SERVER'PATH_INFO'];
if(isset($_GET'page'])){
if(is_numeric($_GET'page'])){
$this->page = round($_GET'page']);
}
}
}
}
?>
et
<?php
class PostsController extends controller{
/*
* Permet de lister les artiles
*/
function index(){
$perpage = 1;
$this->loadModel('Post');
$condition = array('onligne' => 1, 'type' => 'post');
$d'posts'] = $this->Post->find(array(
'conditions' => $condition,
'limit' => ($perpage*$this->request->page).','.$perpage
));
// die($this->request->page);
$d'total'] = $this->Post->findCount($condition);
$d'page'] = ceil($d'total'] / $perpage);
$this->set($d);
}
/*
* Permet de voire les articles
*/
function view($id){
$this->loadModel('Post');
$d'posts'] = $this->Post->findfirst(array(
'conditions' => array('onligne' => 1,'id' => $id, 'type' => 'post')
));
if(empty($d'posts'])){
$this->e404('page introuvable');
}
$this->set($d);
}
}
?>
<?php
class Dispatcher{
var $req;
function __construct()
$this->req = new Request();
router::parse($this->req->url,$this->req);
$controlleur = $this->loadcontroller();
if(!in_array($this->req->action, array_diff(get_class_methods($controlleur),
get_class_methods('controller')))){
$this->error('Le controlleur '.$this->req->controller.' n\'a pas de
methode '.$this->req->action
);
}
call_user_func_array(array($controlleur,$this->req->action),
$this->req->params
);
//rendre la vue automatiquement
$controlleur->render($this->req->action);
}
function error($message){
$controller = new Controller($this->req);
$controller->e404($message);
}
function loadcontroller(){
$name = ucfirst($this->req->controller).'Controller';
$file = ROOT.DS.'controller'.DS.$name.'.php';
require $file;
return new $name($this->req);
}
}
?>
cette variable $this->request->page n'est pas connue.
merci infiniment
Désolé pour le retard.
Dans ton fichier Controller.php
if(! is_null( $req ) ) {
$this->req = $req; //on stock la requeste dans l'instance
}
et dans ton PostsController tu remplaces $this->request par $this->req
<?php
class Dispatcher{
var $req;
function __construct()
$this->req = new Request();
router::parse($this->req->url,$this->req);
$controlleur = $this->loadcontroller();
if(!in_array($this->req->action, array_diff(get_class_methods($controlleur),
get_class_methods('controller')))){
$this->error('Le controlleur '.$this->req->controller.' n\'a pas de
methode '.$this->req->action
);
}
call_user_func_array(array($controlleur,$this->req->action),
$this->req->params
);
//rendre la vue automatiquement
$controlleur->render($this->req->action);
}
function error($message){
$controller = new Controller($this->req);
$controller->e404($message);
}
function loadcontroller(){
$name = ucfirst($this->req->controller).'Controller';
$file = ROOT.DS.'controller'.DS.$name.'.php';
require $file;
return new $name($this->req);
}
}
?>
<?php
class Controller{
public $req;
public $vars = array();
public $layout = 'default';
private $rendered = false;
function __construct($req =null) {
if($req) {
$this->re = $req;//on stock la requeste dans l'instance
}
}
public function render($view){
if($this->rendered){return FALSE;}
//extract()permet de rendre les variables au même niveau et
//la vue aura accer
extract($this->vars);
if(strpos($view, '/')===0){
$view = ROOT.DS.'view'.$view.'.php';
}
else{
$view = ROOT.DS.'view'.DS.$this->re->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;
//die($view);
}
public function set($k,$v=NULL){
if(is_array($k)){
$this->vars+=$k;
}else{
$this->vars$k] = $v;
}
}
function loadModel($name){
$file = ROOT.DS.'model'.DS.$name.'.php';
require_once ($file);
$this->$name = new $name();
}
//permet de gerer les erreurs 404
function e404($message){
header("HTTP/1.0 404 not Found");
$this->set('message', $message);
$this->render('/errors/404');
die();
}
/*
* permet d'appeller un controlleur depuis une vue
*/
function request($controller,$action){
$controller .= 'controller';
// die($controller);
require_once ROOT.DS.'controller'.DS.$controller.'.php';
$c = new $controller();
return $c->$action();//pour que la vue recupere des information
}
}
?>
<?php
//classe d'apstraction permettant 'interagir avec la base de donnees
class Model{
static $connections =array();
public $conf = 'default';
public $table = false;
public $db;
public $primarykey = 'id';
public function __construct() {
//J'initialise qques variable
//si j'initialise pas le nom de la table au niveau de mon
//sous model :Post if($this->table === false)
if($this->table === false){
// je veut auto deviner le nom de la table par la
// methode get_class()
$this->table = strtolower(get_class($this)).'s' ;
}
//jme connecte 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'passeword'],
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 charger la base de donnees');
}
}
}
public function find($req){
// die($this->table);: permet d'afficher le nom de la table liée au
// sous model
$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).'';
//construction 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);
//die ($sql);
}
}
if(isset($req'limit'])){
$sql .= 'LIMIT '.$req'limit'];
}
// die($sql);//:comande permettant d'afficher la requete
$pre = $this->db->prepare($sql);
$pre->execute();
return $pre->fetchAll(PDO::FETCH_OBJ);
}
public function findfirst($req){
$this->find($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 ;
}
}
?>