Category Archives: autoloader

Composer Autoload.php not loading in Namespaces and Classes on live hosting

I have a simple PHP web app with the following structure:

/         (composer.json .htaccess ...)  
/Core/    (Router.php, Controller.php ...)    
/App/     (/Controllers, /Models, /Views ...)    
/Public/  (index.php ...)  
/Vendor/  (autoload.php /composer ...)  

The codes runs without issue on my local server.

When I copy it to a live server and make the necessary changes to .htaccess in route I get the following error: Fatal error: Class 'Core\Router' not found in.... I have tried 3 different hosts but no luck.

The issue seems to be to do with the composer autoload function not loading in the namespaces and classes using psr-4. I have this set up in my composer.json file:

{
"autoload": {
    "psr-4": {
         "Core\\": "Core/",
         "App\\": "App/"
    }
}

The code for my project is on GitHub at

https://github.com/imoprojects/upbook

I am new to programming in an MVC structure and also with using composer in this way.

If anyone could assist with what maybe happening, I would really appreciate it.

Cheers, Ian

My class autoload function is not working in my mvc

MVC uploaded on cloud9, but when the auto-load class located in lib/init.php is not working. The .htaccess in the project folder or webroot folder files are not properly configured.

When I visit my project link https://my-mvc-hunteelar.c9users.io/ it gives this exception :

Failed to load class: Config' in /home/ubuntu/workspace/lib/init.php on line 21

I have 2 .htaccess files. The first .htaccess code in the main project folder is:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^$ webroot/ [L]
    RewriteRule (.*) webroot/$1 [L]
</IfModule>

The second .htaccess file located in the webroot folder which is supposed to be the public folder of my MVC:

<IfModule mod_rewrite.c>

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php [PT,L] 

</IfModule>

The init.php code which contains the __autoload function.

require_once (ROOT.DS.'config'.DS.'config.php');
function __autoload($class_name){

    $lib_path = ROOT.DS.'lib'.DS.strtolower($class_name).'class.php';
    $controllers_path = ROOT.DS.'controllers'.DS.str_replace('controller', '', strtolower($class_name)).'class.php';
    $models_path = ROOT.DS.'models'.DS.strtolower($class_name).'class.php';

    if(file_exists($lib_path)){
        require ($lib_path);
    }
    elseif (file_exists($controllers_path)){
        require_once ($controllers_path);
    }
    elseif (file_exists($models_path)){
        require_once ($models_path);
    }
    else{
            throw new Exception('Failed to load class : '.$class_name);
        }
}

Here is my code uploaded on cloud9 https://ide.c9.io/hunteelar/my-mvc