未找到 Laravel 5 类,使用命名空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30386880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:36:09  来源:igfitidea点击:

Laravel 5 Class not Found, using namespace

phplaravellaravel-5

提问by jason

I have a custom class I added to Classes, invoice.phpwithin the Classes folder (\App\Classes).

invoice.php在 Classes 文件夹 ( \App\Classes) 中有一个添加到 Classes 的自定义类。

When using a controller function, I can't execute a static function, says Class not found. I have the namespace and use set up like any other classes, similar to Helpers.php.

使用控制器函数时,我无法执行静态函数,说找不到类。我像任何其他类一样设置命名空间和使用,类似于Helpers.php.

Class:

班级:

<?php namespace App\Classes;

class invoice {
//
}
?>

Contoller:

控制器:

<?php namespace App\Http\Controllers;

use App\Classes\invoice;

class CustomerController extends Controller {
//
}
?>

Error:

错误:

FatalErrorException in CustomerController.php line 284:
Class 'App\Http\Controllers\invoice' not found

I've been stuck here for two hours and I don't understand what I'm doing wrong. Composer is using psr-4 and the Helpers.php file works fine, but my custom class file doesn't.

我被困在这里两个小时了,我不明白我做错了什么。Composer 正在使用 psr-4 并且 Helpers.php 文件工作正常,但我的自定义类文件没有。

Thanks

谢谢

采纳答案by prograhammer

To follow PSR-4 you need to have your class names initial-caps. The "i" should be capitalized here:

要遵循 PSR-4,您需要让您的班级名称首字母大写。这里的“i”应该大写:

<?php namespace App\Classes;

class Invoice {
   //
}

Also fix your useas well. Then when Invoiceis used in your controller, it will pick up the correct namespace to use for resolving that class. What's happening currently is it is not finding a matching reference in your usesection, so it assumes it is in the same namespace of the controller class that calls it.

也修复你的use。然后当Invoice在您的控制器中使用时,它将选择正确的命名空间用于解析该类。当前发生的情况是它没有在您的use部分中找到匹配的引用,因此它假定它位于调用它的控制器类的同一命名空间中。