我正在尝试使用Laravel运行测试页面 .
当我使用Controller时,每次收到消息时:
Laravel:ReflectionException - 类App \ Http \ Controllers \ XXXX不存在
有人知道问题在哪里吗?
这是我的routes / 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”,但没有任何作用 .
先感谢您 .
2 回答
运行以下命令(假设您的应用名称为 app ):
然后在控制器中也使用以下命名空间:
问:为什么XXXX如果是HiController?关于这个问题,如果它返回hi,从技术上讲,它必须工作,如果它是香草 . 由于它在Windows和wamp64上,我不认为文件权限是一个东西,所以让我们跳过它 .
确保文件名确实正确
确保文件确实存在于正确的目录中
确保namespce为
namespace App\Http\Controllers;
添加此用法,以防万一:使用
App\Http\Controllers\Controller;
为了确保,请确保
composer.json
你've got this (you should have since it'默认):php artisan make:controller SomeController
并尝试使用此控制器 .