未找到 Laravel 5.2 类,但类存在命名空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35028235/
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
Laravel 5.2 Class not found but class is there with namespace
提问by scrfix
UPDATE 01/26/16 10:30pm EST:Through a lot of Google searching I discovered that I was misunderstanding how to utilize namespaces and custom classes. If anyone else is having this issue read this tutorial: http://www.techigniter.in/tutorials/how-to-add-custom-class-in-laravel-5/It's short and very easy to understand. It helped resolve this issue and move me along to my next error... :D
更新 01/26/16 东部时间晚上 10:30:通过大量的谷歌搜索,我发现我误解了如何利用命名空间和自定义类。如果其他人遇到此问题,请阅读本教程:http: //www.techigniter.in/tutorials/how-to-add-custom-class-in-laravel-5/它很短而且很容易理解。它帮助解决了这个问题并使我继续我的下一个错误......:D
ISSUE:Attempting to freshly install Laravel 5 and convert my Laravel 4 code to Laravel 5.
问题:尝试全新安装 Laravel 5 并将我的 Laravel 4 代码转换为 Laravel 5。
REQUEST:Please help me find the error and provide detailed instructions on how to correct it.
请求:请帮助我找到错误并提供有关如何更正的详细说明。
ERROR:FatalErrorException in additionalPCs.php line 4: Class 'App\Library\AdditionalPCs\additionalComputer' not found
错误:additionalPCs.php 第 4 行中的 FatalErrorException:找不到类“App\Library\AdditionalPCs\additionalComputer”
Notes:I have put the additionalComputer.php file in both its own directory App\Libary\additionalPCs and directly into the App\Libary directory. Both places produce the same error. I am using namespaces. (possibly incorrectly)
注意:我已将 additionalComputer.php 文件放在其自己的目录 App\Libary\additionalPCs 中,并直接放入 App\Libary 目录中。两个地方都会产生相同的错误。我正在使用命名空间。(可能不正确)
Composer.json
Composer.json
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\": "app/"
}
},
IndexController.php
索引控制器.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Library\additionalPCs;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class IndexController extends Controller
{
Protected $layout = 'master';
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
/** Wayne - 03-02-2014 - Moved for loop to a method within its own class. */
$numberofpcs = new additionalPCs();
$addtpcs=$numberofpcs->display();
$this->layout->content = View::make('index')->with('addtpcs', $addtpcs)->with('businesstypelist', businesstype::dropdown())->with('contracttermlist',ContractTerm::dropdown());
}
}
additionalPCs.php
附加PCs.php
<?php
namespace App\Library;
class additionalPCs extends additionalComputer {
public function display() {
return $this->displayMenu();
}
}
additionalComputer.php(I have also attempted use App\Library\additionalComputer;)
additionalComputer.php(我也尝试过使用 App\Library\additionalComputer;)
<?php
namespace App\Library;
use App\Library\AdditionalPCs\additionalComputer;
class additionalPCs extends additionalComputer {
public function display() {
return $this->displayMenu();
}
}
采纳答案by schellingerht
Update after your post update:
在您的帖子更新后更新:
You want to use additionalComputer, so you have to import his namespace, like so:
你想使用 additionalComputer,所以你必须导入他的命名空间,像这样:
<?php
namespace App\Library;
use App\Library\additionalComputer;
class additionalPCs extends additionalComputer {
public function display() {
return $this->displayMenu();
}
}
(Added namespace import for additionalComputer)
(为 additionalComputer 添加命名空间导入)
Original post:
原帖:
You have this line in your library:
您的库中有此行:
namespace App\Library\AdditionalPCs;
To use AdditionalPCs (in for example your controller), change:
要使用 AdditionalPC(例如在您的控制器中),请更改:
use App\Library\AdditionalPCs;
To
到
use App\Library\AdditionalPCs\AdditionalPCs;
The first
AdditionalPCs
is from your namespace, the second is your class name. Your class AdditionalPCs is in the sub namespace AdditionalPCs.
第一个
AdditionalPCs
来自您的命名空间,第二个是您的类名。您的类 AdditionalPCs 位于子命名空间 AdditionalPCs 中。
Important: it's new AdditionalPCs()
(see the beginning A
instead of a
), it must be your class name exactly! That's a general rule!
重要提示:这是new AdditionalPCs()
(见开头A
,而不是a
),那一定是你的类名完全相同!这是普遍规律!
Be care with your names (case sensitivity). It's better to use the code conventions from PSR-2: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md
请注意您的姓名(区分大小写)。最好使用 PSR-2 的代码约定:https: //github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md
回答by Makan
Namespace of each class is the container directory of the that class not the class file itself.
每个类的命名空间是该类的容器目录,而不是类文件本身。
In your additionalPCs.php file for the namespace, get rid of \AdditionalPCs, it should just be:
在命名空间的 additionalPCs.php 文件中,去掉 \AdditionalPCs,它应该是:
namespace App\Library;