[英]Fatal error due to composer.json in slim tree framework
I have been following Alex Gareth's tutorial on building a shopping cart . 我一直在关注Alex Gareth的构建购物车的教程 。 I am stock because I think that the composer.json isn't autoloading my files correctly.
我有库存,因为我认为composer.json无法正确自动加载文件。 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"
“致命错误:未捕获的错误:在C:\\ xampp \\ htdocs \\ order \\ bootstrap \\ app.php:10中找不到类'Order \\ App':堆栈跟踪:#0 C:\\ xampp \\ htdocs \\ order \\ public \\ index。 php(3):require()#1 {main}在第10行的C:\\ xampp \\ htdocs \\ order \\ bootstrap \\ app.php中抛出”
composer.json file composer.json文件
{
"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 app.php文件,是引导文件
<?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. 如果我取消注释我在App.php中拉出的require行,则可以正常工作。
Directory Structure: 目录结构:
OS : Windows 10
作业系统:Windows 10
Try to change autoload
to following: 尝试将
autoload
更改为以下内容:
"autoload": {
"psr-4": {
"Order\\": ""
}
}
You should change your autoload to target the app/
folder instead, since there's where your App.php
is located: 由于
App.php
其中,因此您应该将自动加载更改为针对app/
文件夹:
"autoload": {
"psr-4": {
"Order\\": "app/"
}
}
What this essentially does is telling the autoloader that all classes that has the namespace Order\\
exists in app/
. 这实际上是告诉自动加载器,所有具有命名空间
Order\\
类都存在于app/
。
So from now on, if you would create sub folders in app/
: 因此,从现在开始,如果您要在
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()
. 然后
Bar.php
应该具有命名空间Order\\Foo
,您将使用以下Order\\Foo
访问该类: $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).
否则,它仍然可以在Windows上为您工作,但在其他操作系统(例如linux)上将失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.