问题描述
大家好,我对laravel 8.1.0有疑问。 现在想在web.PHP文件中创建路由 使用以下代码,我得到一个错误,指出该控制器不存在
Route::get('/home/','HomeController@index');
我收到错误“ Illuminate \ Contracts \ Container \ BindingResolutionException 目标类[App \ Http \ Controllers \ HomeController]不存在。“
我在laravel文档中看到,版本8对控制器的调用与以前的版本不同,我已经应用了文档建议的更改,但是一切都保持不变
我已经在app \ providers中的routeserviceprovider.PHP文件中添加了以下代码
protected $namespace = 'App\Http\Controllers';
->namespace($this->namespace) //inside $this-> routes (function ()
->namespace($this->namespace) //inside Route::prefix('api')
根据laravel文档,这应该可以工作,但是我仍然遇到相同的错误。 “ Illuminate \ Contracts \ Container \ BindingResolutionException 目标类[App \ Http \ Controllers \ HomeController]不存在。“
我尝试在web.PHP文件中添加use App\Http\Controllers;
,但是出现相同的错误消息
我尝试使用Route::get('/home',[App\Http\Controllers\HomeController::class,'index'])->name('home');
和Route::get('/home',[HomeController::class,'index']);
,但是出现相同的消息错误
<?PHP
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/',function () {
return view('welcome');
});
Auth::routes();
//Route::get('/home','index']);
Route::get('/persona/','PersonaController@index')->name('per','persona');
Auth::routes();
//Route::get('/home','index'])->name('home');
Route::get('/home/','HomeController@index');
<?PHP
namespace sisventas\Providers;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* The path to the "home" route for your application.
*
* This is used by Laravel authentication to redirect users after login.
*
* @var string
*/
public const HOME = '/home';
/**
* If specified,this namespace is automatically applied to your controller routes.
*
* In addition,it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers'; //agregado
/**
* Define your route model bindings,pattern filters,etc.
*
* @return void
*/
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::middleware('web')
->namespace($this->namespace) //agregado
->group(base_path('routes/web.PHP'));
Route::prefix('api')
->middleware('api')
->namespace($this->namespace) //agregado
->group(base_path('routes/api.PHP'));
});
}
/**
* Configure the rate limiters for the application.
*
* @return void
*/
protected function configureRateLimiting()
{
RateLimiter::for('api',function (Request $request) {
return Limit::perMinute(60);
});
}
}
我的家庭控制器文件
<?PHP
namespace sisventas\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('home');
}
}
问题与我尝试访问的所有路由相同。 我整天都在尝试修复这些路线,但是却遇到了找不到它们的错误,我想知道如何做,非常感谢您阅读本文:)
解决方法
您的命名空间不正确。应用程序正在 App \ Http \ Controller 中搜索此控制器,但您的控制器命名空间为 sisVentas \ Http \ Controllers 。
根据根目录结构(如果您的根文件夹是 sisVentas ) 您应该使用
protected $namespace = 'sisVentas\Http\Controllers';
或者,如果您的根是 App ,则应将控制器和提供程序的命名空间更改为
namespace App\Http\Controllers;