目标 class 不存在。 laravel 8 中的问题
Target class does not exist. problem in laravel 8
使用 laravel 8 创建新项目时出现此错误。
Illuminate\Contracts\Container\BindingResolutionException Target class
[SayhelloController] does not exist. http://127.0.0.1:8000/users/john
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('/users/{name?}' , [SayhelloController::class,'index']);
在laravel文档中路由控制器class必须这样定义
// Using PHP callable syntax...
Route::get('/users', [UserController::class, 'index']);
// Using string syntax...
Route::get('/users', 'App\Http\Controllers\UserController@index');
目标class
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SayhelloController extends Controller
{
public function index($name = null)
{
return 'Hello '.$name;
}
}
所以我做到了。
Laravel 8更新路由的写法
ref link https://laravel.com/docs/8.x/upgrade
在laravel 8你需要使用like
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'));
});
}
那你可以用like
Route::get('/users/{name?}' , [SayhelloController::class,'index']);
Route::resource('/users' , SayhelloController::class);
或
Route::get('/users', 'UserController@index');
使用 laravel 8 创建新项目时出现此错误。
Illuminate\Contracts\Container\BindingResolutionException Target class [SayhelloController] does not exist. http://127.0.0.1:8000/users/john
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('/users/{name?}' , [SayhelloController::class,'index']);
在laravel文档中路由控制器class必须这样定义
// Using PHP callable syntax...
Route::get('/users', [UserController::class, 'index']);
// Using string syntax...
Route::get('/users', 'App\Http\Controllers\UserController@index');
目标class
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SayhelloController extends Controller
{
public function index($name = null)
{
return 'Hello '.$name;
}
}
所以我做到了。
Laravel 8更新路由的写法
ref link https://laravel.com/docs/8.x/upgrade
在laravel 8你需要使用like
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'));
});
}
那你可以用like
Route::get('/users/{name?}' , [SayhelloController::class,'index']);
Route::resource('/users' , SayhelloController::class);
或
Route::get('/users', 'UserController@index');