简体   繁体   中英

Fatal error: Uncaught Error: Class 'App\Config' not found

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")

在此处输入图像描述

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

In my case, the problem was caused by using a Symbolic Link in Windows. I am using WAMP and PHP framework CodeIgniter (version 4.2.1). Some versions of CodeIgniter have issues with this.

Possible cause:

I think the cause is the use of the PHP function file_exists. The function returns a FALSE in some cases when the path contains a symbolic link with certain characters. Or the problem is in sanitizing the filename.

Possible solutions:

  1. Ask the developers of CodeIgniter to fix this problem. I can't wait for that now.
  2. Override the functions of the framework in which the problem occurs. Takes me too much time to figure this out.
  3. Use an older version of CodeIgniter that does not have the issue. This, of course, has other drawbacks.
  4. Do not use a symbolic link. Duh!
  5. Make sure the symbolic link does not contain any 'special' characters.

Solution 5 was the easiest for me. I had to remove the brackets () in my symbolic link name and that solved the problem.

Maybe this helps someone else.

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;
    }
}

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/"
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM