Bonjour,
j'éssaye de redirriger vers une erreur 404 quand on tape n'importe quoi à la suite de l'adresse du site exemple :

/localhost/monsite/hbhqgzsutgzqvi

mais au lieu de ca il m'affiche ca :

( ! ) Warning: require(C:\wamp\www\emmy\controller\55+5Controller.php): failed to open stream: No such file or directory in C:\wamp\www\emmy\core\Dispatcher.php on line 30
Call Stack
#   Time    Memory  Function    Location
1   0.0009  247280  {main}( )   ..\index.php:0
2   0.0151  405992  Dispatcher->__construct( )  ..\index.php:9
3   0.0152  407808  Dispatcher->loadController( )   ..\Dispatcher.php:9

( ! ) Fatal error: require(): Failed opening required 'C:\wamp\www\emmy\controller\hbhqgzsutgzqviController.php' (include_path='.;C:\php\pear') in C:\wamp\www\emmy\core\Dispatcher.php on line 30
Call Stack
#   Time    Memory  Function    Location
1   0.0009  247280  {main}( )   ..\index.php:0
2   0.0151  405992  Dispatcher->__construct( )  ..\index.php:9
3   0.0152  407808  Dispatcher->loadController( )   ..\Dispatcher.php:9

Merci d'avance !

4 réponses


cyyynthia
Réponse acceptée

Dans ton Controller, fonction loadController, à la place de require $file

if(file_exists($file)) {
 require $file;
} else {
 $this->error('Controller not found.');
}

Il te suffit de faire une condition :

if(file_exists($controller)) {
 require $controller;
} else {
 e404();
}

Merci mais je me le e comment dans mon dispacher.php

<?php
class Dispatcher{

    var $request;

    function __construct(){
        $this->request = new Request();
        Router::parse($this->request->url,$this->request);
        $controller = $this->loadController();
        $action = $this->request->action;
        if($this->request->prefix){
            $action = $this->request->prefix.'_'.$action;
        }
        if(!in_array($action , array_diff(get_class_methods($controller),get_class_methods('Controller')))){
            $this->error('Le controller '.$this->request->controller.' n\'a pas de méthode '.$action);
        }
        call_user_func_array(array($controller,$action),$this->request->params);
        $controller->render($action);
    }

    function error($message){
        $controller = new Controller($this->request);
        $controller ->Session = new Session();
        $controller->e404($message);
    }

    function loadController(){
        $name = ucfirst($this->request->controller).'Controller';
        $file = ROOT.DS.'controller'.DS.$name.'.php';
        require $file;
        $controller = new $name($this->request);
        $controller ->Session = new Session();
        $controller ->Form = new Form($controller);
        return $controller;
    }
}
?>

Merci beaucoup !!!!