gpt4 book ai didi

php - Laravel 5 找不到自定义命名空间的类

转载 作者:行者123 更新时间:2023-12-04 00:24:00 24 4
gpt4 key购买 nike

在我的 Laravel 应用程序中,我将前端和后端代码拆分到文件夹中。这些是 app/Http/Controllers/BackEndapp/Http/Controllers/FrontEnd .我认为定义两个命名空间 BackEnd 会更容易,而不是在每个文件上键入所有内容。和 FrontEnd .我已经将我的 Composer 文件编辑为:

"autoload": {
"classmap": [
"app/Models",
"database"
],
"psr-4": {
"App\\": "app/",
"BackEnd\\": "app/Http/Controllers/BackEnd",
"FrontEnd\\": "app/Http/Controllers/FrontEnd"
}
},

然后我运行 composer autodump 并像这样设置我的路由文件:

Route::group(['prefix' => 'webman', 'middleware' => 'auth', 'namespace' => 'BackEnd'], function()
{
Route::get('/', ['as' => 'webmanHome', 'uses' => 'HomeController@index']);
});

但是当我浏览到 localhost:8000/webman/我刚收到一个错误,Class App\Http\Controllers\BackEnd\HomeController does not exist . Controller 确实存在,这是文件:

<?php namespace BackEnd;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HomeController extends Controller {

/**
* Display the admin home page, showing the front-end menu and "apps" as links to other sections of the ACP.
*
* @param Reqeust $request
*
* @return View
*/
public function index(Request $request)
{
return view('backend.index');
}

}

我检查过 vendor/composer/autoload_psr4.php确保命名空间被定义,它在返回的数组中 'BackEnd\\' => array($baseDir . '/app/Http/Controllers/BackEnd'), .

奇怪的是,如果我使用 <?php namespace App\Http\Controllers\BackEnd;HomeController.php 的顶部然后一切正常,为什么?我错过了什么?为什么不能自动加载找到文件 BackEnd

最佳答案

Route::group() 中设置 namespace 时,它实际上是将其附加到 App\Http\Controllers。你可以做的是删除它并像这样引用完整路径:

Route::group(['prefix' => 'webman', 'middleware' => 'auth'], function()
{
Route::get('/', ['as' => 'webmanHome', 'uses' => '\BackEnd\HomeController@index']);
});

关于php - Laravel 5 找不到自定义命名空间的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31112491/

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