1
votes

I have build a custom MVC Framework. Local its working fine, when I try to get it working on a live shared hosting server I will get this error:

Error Message in Browser:

Fatal error: Uncaught Error: Class 'App\Config' not found in /www/htdocs/user/project/public/index.php:19 Stack trace: #0 {main} thrown in /www/htdocs/user/project/public/index.php on line 19

Composer.json file:

{
  "require": {
    "filp/whoops": "^2.3",
    "phpmailer/phpmailer": "^6.0"
  },
  "autoload": {
    "psr-4": {
      "Core\\": "core/",
      "App\\": "app/"
    }
  }
}

My Folder & File structure: (The whole project is inside the folder: "project")

enter image description here

Index.php File

    <?php

/**
 * Front controller
 */

use App\Config;

/**
 * Composer autoloading
 */

require dirname(__DIR__) . '/vendor/autoload.php';

/**
 * Whoops Error and Exception handling
 */

if (Config::SHOW_ERRORS == true){
    $whoops = new \Whoops\Run;
    $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
    $whoops->register();
}

/**
 * Sessions
 */

session_start();

/**
 * Routing
 */

$router = new Core\Router();

// Add the routes
$router->add('', ['controller' => 'Home', 'action' => 'index']);

Config.php file:

<?php

/**
 * Application configuration
 */

namespace App;

class Config {

    /**
     * Database host
     * @var string
     */

    const DB_HOST = 'localhost';

    /**
     * Mail SMTP Port
     * @var int
     */

    const SMTP_PORT = '2525';
}

My method to deploy to the server:

  1. ZIP the local files & Export mysql database
  2. upload zip to server -> unzip it
  3. upload db to phpmyadmin
  4. Change database credentials in Config File
  5. run composer install (I also tried: composer install --no-dev)
  6. Done

I have repeated this procedure for several times now but its still not working

2
try composer dump-autoload - Saad Suri
Are both systems running the same OS? (app/ vs App/) - brombeer
in index.php are you loading the composer autoload file? - medskill
composer dump-autoload brings no change I have added the index.php to the post, please check it out - user3576592
You should use autoload before use App/Config. - medskill

2 Answers

0
votes

I have just tried to do the same thing and it seems work, check this:

/index.php

<?php

require dirname(__DIR__) . '/vendor/autoload.php'; // It must be called first

use App\Config;

echo Config::get('test');
// Result: test

/App/Config.php

namespace App;

class Config 
{
    public function get($str)
    {   
        return $str;
    }
}
0
votes

This is case sensitivity problem - your autoloading rules uses app as directory name, but this is actually App. This may work on case insensitive filesystems (Windows) but it will not work on case sensitive filesystems (Linux). You should fix your autoloading rules to:

{
  "require": {
    "filp/whoops": "^2.3",
    "phpmailer/phpmailer": "^6.0"
  },
  "autoload": {
    "psr-4": {
      "Core\\": "Core/",
      "App\\": "App/"
    }
  }
}