require - In PHP, when autoloading files, where PHP will look for these files? -


i have mvc-based application basic url-rewriting rule, makes url this: website/controller/action/id. id optional.

if user enters invalid action, should error handled in class errorcontroller.

all of classes files required in autoloader file, should not require them every time want create object. use spl_autoload_register() autoloading.

the problem occurs when try entering url invalid action. example, url website/main/inde (instead of index) - instance of errorcontroller should created.

instead, 2 php errors:

warning: require(!core/errorcontroller.php): failed open stream: no such file or directory in d:\programs\wamp\www\fanfics\v0.0.2!core\autoloader.php on line 5

and

fatal error: require(): failed opening required '!core/errorcontroller.php' (include_path='.;c:\php\pear') in d:\programs\wamp\www\fanfics\v0.0.2!core\autoloader.php on line 5

here visual of files (the exclamation mark before core folder keeping on top):

visual of files

index.php:

<?php  require "!core/autoloader.php";  $loader = new loader(); 

!core/autoloader.php

<?php  function autoload_core_classes($class) {     require "!core/" . strtolower($class) . ".php"; }  function autoload_controllers($class) {     require "controllers/" . str_replace("controller", "", strtolower($class)) . ".php"; }  function autoload_models($class) {     require "models/" . str_replace("model", "", strtolower($class)) . ".php"; }  spl_autoload_register("autoload_core_classes"); spl_autoload_register("autoload_controllers"); spl_autoload_register("autoload_models"); 

!core/basecontroller.php

<?php  abstract class basecontroller {      protected $model;     protected $view;     private $action;      public function __construct($action)     {         $this->action = $action;         $this->view = new view(get_class($this), $action);     }      public function executeaction()     {         if (method_exists($this->model, $this->action))         {             $this->view->output($this->model->{$this->action}());         }         else         {             // here create errorcontroller object when action invalid             $error = new errorcontroller("badmodel");             $error->executeaction();         }     } } 

if try require controllers/error.php - works fine:

. . . else {     require "controllers/error.php"; // line works fine     $error = new errorcontroller("badmodel");     $error->executeaction(); } 

after online long search, understand there maybe problem include_path, not quite understand it. how can solve problem?

it's idea each autoloader function check if file exists before blindly trying include/require it. autoloaders not expected throw errors , should fail silently can allow next autoloader in queue attempt autoload necessary files.

<?php  function autoload_core_classes($class) {     if (is_readable("!core/" . strtolower($class) . ".php"))         include "!core/" . strtolower($class) . ".php"; }  function autoload_controllers($class) {     if (is_readable("controllers/" . str_replace("controller", "", strtolower($class)) . ".php"))         include "controllers/" . str_replace("controller", "", strtolower($class)) . ".php"; }  function autoload_models($class) {      if (is_readable( "models/" . str_replace("model", "", strtolower($class)))         include "models/" . str_replace("model", "", strtolower($class)) . ".php"; }  spl_autoload_register("autoload_core_classes"); spl_autoload_register("autoload_controllers"); spl_autoload_register("autoload_models"); 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -