首页 > 解决方案 > laravel 8.1.0 上的路由

问题描述

大家好,我对 laravel 8.1.0 有疑问。在想要使用以下代码在 web.php 文件中创建路由时,我收到一条错误消息,指出控制器不存在

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] 不存在。”

我尝试添加use App\Http\Controllers;web.php 文件,但我尝试使用相同的错误消息Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');Route::get('/home', [HomeController::class, 'index']);但我有相同的消息错误

web.php 完整文件

<?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', [HomeController::class, 'index']);
Route::get('/persona/','PersonaController@index')->name('per','persona');
Auth::routes();

//Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/home/','HomeController@index');

routeservicesprovider.php 完整文件

<?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');
    }
}

我尝试访问的所有路由都存在相同的问题。我花了一整天的时间试图修复路线,但我得到了找不到它们的错误,我想知道该怎么做,非常感谢您阅读本文:)

标签: phplaravelrouteslaravel-artisanlaravel-8

解决方案


您的命名空间错误。应用程序正在App\Http\Controller中搜索此控制器,但您的控制器命名空间是sisVentas\Http\Controllers

如果您的根文件夹是sisVentas ,则取决于您的目录结构, 您应该使用

    protected $namespace = 'sisVentas\Http\Controllers';

或者,如果您的根是App,您应该将控制器和提供程序命名空间更改为

    namespace App\Http\Controllers;

推荐阅读