I have been following Alex Gareth's tutorial on building a shopping cart . I am stock because I think that the composer.json isn't autoloading my files correctly. Here is my error :
"Fatal error: Uncaught Error: Class 'Order\\App' not found in C:\\xampp\\htdocs\\order\\bootstrap\\app.php:10 Stack trace: #0 C:\\xampp\\htdocs\\order\\public\\index.php(3): require() #1 {main} thrown in C:\\xampp\\htdocs\\order\\bootstrap\\app.php on line 10"
composer.json file
{
"require": {
"slim/slim": "^3.0",
"slim/twig-view": "^2.4",
"php-di/slim-bridge": "^2.0",
"illuminate/database": "^5.6"
},
"autoload": {
"psr-4": {
"Order\\": "Order"
}
}
}
App class
namespace Order;
use DI\ContainerBuilder;
use DI\Bridge\Slim\App as DIBridge;
class App extends DIBridge{
protected function configureContainer(ContainerBuilder $builder){
$builder->addDefinitions([
'settings.displayErrorDetails' => true
]);
//
}
}
app.php file which is the bootstrap file
<?php
use Order\App;
session_start();
require __DIR__ . '/../vendor/autoload.php';
//require __DIR__ . '/../app/App.php';
$app = new App;
If i un-comment the require line where i pulled in App.php it works fine.
Directory Structure:
OS : Windows 10
Try to change autoload
to following:
"autoload": {
"psr-4": {
"Order\\": ""
}
}
You should change your autoload to target the app/
folder instead, since there's where your App.php
is located:
"autoload": {
"psr-4": {
"Order\\": "app/"
}
}
What this essentially does is telling the autoloader that all classes that has the namespace Order\\
exists in app/
.
So from now on, if you would create sub folders in app/
:
app/
App.php
Foo/
Bar.php
Then Bar.php
should have the namespace Order\\Foo
and you would access that class with: $bar = new Order\\Foo\\Bar()
.
Note: This is where the file/folder casing is important. The namespace casing and the class name must have the same casing as the files and folders. Otherwise, it will still work for you on Windows, but it will fail on other OS's (like linux).
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.