laravel 8.1.0上的路线

zdwk9cvp  于 2022-11-18  发布在  其他
关注(0)|答案(2)|浏览(146)

大家好,我在使用laravel 8.1.0时遇到了一个问题。当我想用下面的代码在web.php文件中创建路由时,我收到了一个错误消息,说控制器不存在
web.php文件

Route::get('/home/','HomeController@index');

我收到错误消息“Illuminate\Contracts\Container\BindingResolutionException目标类[应用程序\Http\控制器\家庭控制器]不存在”。
我在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']);,但我有相同的错误信息
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');

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

问题是相同的所有路线,我试图访问。我已经失去了一整天试图修复路线,但我得到的错误没有找到他们,我想知道如何做到这一点,非常感谢阅读到目前为止:)

8hhllhi2

8hhllhi21#

您的命名空间错误。应用正在App\Http\Controller中搜索此控制器,但您的控制器命名空间是sisVentas\Http\Controllers
根据您的目录结构,如果您的根文件夹是 sisVentas,您应该使用

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

或者,如果根目录是 App,则应将控制器和提供程序名称空间更改为

namespace App\Http\Controllers;
8hhllhi2

8hhllhi22#

在routeservicesprovider.php中不需要这样的函数
应用程序\Http\控制器'; X射线

请试试这个肯定会成功的..

用户可以在应用程序中创建一个新的控制器。
写入完成路径。
不用写信给上级

相关问题