【发布时间】:2019-01-01 11:58:06
【问题描述】:
我正在尝试使用 Laravel 运行测试页面。
当我使用Controller时,每次收到消息:
Laravel:ReflectionException - 类 App\Http\Controllers\XXXX 不存在
有人知道问题出在哪里吗?
这是我的路线/web.php:
Route::get('/hi', 'HiController@index');
HiController.php(在正确的文件夹结构中:app/Http/Controllers/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HiController extends Controller
{
public function index(){
return "test";
}
}
RouteServiceProvider.php
namespace App\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
//
parent::boot();
}
/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
//
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @return void
*/
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
}
此外,当我在 routes/web.php 中使用此代码时,它可以工作:
Route::get('/hi', function (){
return "hi";
});
这是干净的 Laravel 5.6 安装,在 Windows 上,wamp64。
我也尝试了“composer dump auto-load”和“php artisan config:clear”,但没有任何效果。
提前谢谢你。
【问题讨论】:
-
收到同样的错误信息,找不到原因。至少它是我用于 Route 的自定义 Request 类的缺失命名空间,它的名称与控制器方法相同。