且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

目标类别不存在.laravel 8中的问题

更新时间:2023-02-19 19:13:00

Laravel 8更新写路由的方式

参考链接 https://laravel.com/docs/8.x/upgrade

use App\Http\Controllers\SayhelloController;
Route::get('/users/{name?}' , [SayhelloController::class,'index']);

Route::get('/users', 'App\Http\Controllers\UserController@index');

如果您想使用旧方法

然后在 RouteServiceProvider.php

添加此行

 /**
     * 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'; // need to add in Laravel 8
    

public function boot()
{
    $this->configureRateLimiting();

    $this->routes(function () {
        Route::prefix('api')
            ->middleware('api')
            ->namespace($this->namespace) // need to add in Laravel 8
            ->group(base_path('routes/api.php'));

        Route::middleware('web')
            ->namespace($this->namespace) // need to add in Laravel 8
            ->group(base_path('routes/web.php'));
    });
}

然后您可以使用

Route::get('/users/{name?}' , [SayhelloController::class,'index']);
Route::resource('/users' , SayhelloController::class);

Route::get('/users', 'UserController@index');