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:
composer install
(I also tried: composer install --no-dev
)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:
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.