Question

My MVC structure is following:

- web
  -- Classes
    --- Loader.php
  -- Core
    --- Controller.php
    --- Model.php
    --- View.php
    --- Bootstrap.php
    --- DB.php
  -- Project
    --- Controllers (folder)
    --- Models (folder)
    --- Views (folder)

Now i have namespaces specified to each. For example i have

    namespace Classes; //for Loader.php
    namespace Core; //For Controller.php, Model.php etc...
    namespace Project\Controllers; //For Controllers inside Controllers folder etc...

My autoloader looks like this:

public static function Autoloader2($className) {
    $className = explode('\\', $className);
    $class = array_pop($className);
    $namespace = implode(DIRECTORY_SEPARATOR, $className);
    $file = $namespace . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';

   require $file;
}

and My main index.php uses

    spl_autoload_register( array('Loader', 'Autoloader2') );
    $app = new Core\Bootstrap();

when i open localhost/web/ i get the following error:

  Warning: require(Core/Project/Controllers/Index.php): failed to open stream: N

It seems it prepends Core/ to the requested file. Index.php is inside Project/Controllers and not Core/Project/Controllers. If i try to remove

  //namespace Core; from Bootstrap.php i get the following error
  Fatal error: Class 'Core\Bootstrap' not found 

Please help

Was it helpful?

Solution

You should use $app = \Core\Bootstrap();.

The slash before the path is important, without it your namespace paths will be relative to each other.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top