gpt4 book ai didi

php - 找不到 Laravel 5.2 类,但类有命名空间

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:02:29 25 4
gpt4 key购买 nike

UPDATE 01/26/16 10:30pm EST:通过大量的 Google 搜索,我发现我误解了如何使用命名空间和自定义类。如果其他人遇到此问题,请阅读本教程:http://www.techigniter.in/tutorials/how-to-add-custom-class-in-laravel-5/它很短而且很容易理解。它帮助解决了这个问题并将我带到了下一个错误...:D

问题:尝试全新安装 Laravel 5 并将我的 Laravel 4 代码转换为 Laravel 5。

请求:请帮助我找到错误并提供有关如何更正错误的详细说明。

错误:additionalPCs.php 第 4 行中的 FatalErrorException:找不到类“App\Library\AdditionalPCs\additionalComputer”

注意:我已将 additionalComputer.php 文件放在其自己的目录 App\Libary\additionalPCs 中,并直接放入 App\Libary 目录中。这两个地方都产生相同的错误。我正在使用命名空间。 (可能不正确)

Composer.json

"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},

IndexController.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

<?php
namespace App\Library;

class additionalPCs extends additionalComputer {
public function display() {
return $this->displayMenu();
}
}

additionalComputer.php(我也尝试过使用 App\Library\additionalComputer;)

<?php
namespace App\Library;

use App\Library\AdditionalPCs\additionalComputer;

class additionalPCs extends additionalComputer {
public function display() {
return $this->displayMenu();
}
}

最佳答案

在您的帖子更新后更新:

你想使用additionalComputer,所以你必须导入他的命名空间,像这样:

<?php
namespace App\Library;

use App\Library\additionalComputer;

class additionalPCs extends additionalComputer {
public function display() {
return $this->displayMenu();
}
}

(为 additionalComputer 添加了命名空间导入)

原帖:

你的库中有这一行:

namespace App\Library\AdditionalPCs;

要使用 AdditionalPCs(例如在您的 Controller 中),请更改:

use App\Library\AdditionalPCs;

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.

重要:它是new AdditionalPCs()(见开头的A而不是a),它必须完全是你的类(class)名称!这是一般规则!

注意您的姓名(区分大小写)。最好使用 PSR-2 中的代码约定:https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md

关于php - 找不到 Laravel 5.2 类,但类有命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35028235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com