更新 01/26/16 10:30pm EST:通过大量的谷歌搜索,我发现我误解了如何使用命名空间和自定义类。 如果其他人遇到此问题,请阅读本教程:http://www.techigniter.in/tutorials/how-to-add-custom-class-in-laravel-5/它很短,很容易理解。 它帮助解决了这个问题,并将我转移到下一个错误...... :D
问题:尝试全新安装 Laravel 5 并将我的 Laravel 4 代码转换为 Laravel 5。
请求:请帮助我找到错误并提供有关如何更正它的详细说明。
错误:其他 PC 中的 FatalErrorException .php第 4 行:找不到类"应用程序''库''附加电脑''附加计算机"
注意:我已经将 extraalComputer.php 文件放在它自己的目录 App''Libary''additionalPC 中,并直接放入 App''Libary 目录中。 这两个地方会产生相同的错误。 我正在使用命名空间。(可能不正确)
作曲家.json
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App''": "app/"
}
},
索引控制器.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());
}
}
其他电脑.php
<?php
namespace App'Library;
class additionalPCs extends additionalComputer {
public function display() {
return $this->displayMenu();
}
}
extraalComputer.php (我也尝试使用App''Library''additionalComputer;)
<?php
namespace App'Library;
use App'Library'AdditionalPCs'additionalComputer;
class additionalPCs extends additionalComputer {
public function display() {
return $this->displayMenu();
}
}
更新后更新:
你想使用extraalComputer,所以你必须导入他的命名空间,如下所示:
<?php
namespace App'Library;
use App'Library'additionalComputer;
class additionalPCs extends additionalComputer {
public function display() {
return $this->displayMenu();
}
}
(为其他计算机添加了命名空间导入)
原文:
您的库中有以下行:
namespace App'Library'AdditionalPCs;
要使用其他电脑(例如控制器),请更改:
use App'Library'AdditionalPCs;
自
use App'Library'AdditionalPCs'AdditionalPCs;
第一个
AdditionalPCs
来自您的命名空间,第二个是您的类名。您的类 ExtraalPC 位于子命名空间 AdditionalPC 中。
重要提示:它是new AdditionalPCs()
(请参阅开头A
而不是a
),它一定是你的类名!这是一般规则!
注意你的名字(区分大小写)。最好使用 PSR-2 中的代码约定: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md
每个类的命名空间是该类的容器目录,而不是类文件本身。
在命名空间的附加 PC.php 文件中,删除 ''AdditionalPC,它应该只是:
namespace App'Library;