目标类别不存在.laravel 8中的问题 [英] Target class does not exist. problem in laravel 8
本文介绍了目标类别不存在.laravel 8中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用laravel 8创建新项目时,出现此错误.
When create a new project with laravel 8 and I get this error.
Illuminate \ Contracts \ Container \ BindingResolutionException目标类[SayhelloController]不存在. 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文档中,Routes控制器类必须这样定义
In laravel documents Routes controller class must define like this
// Using PHP callable syntax...
Route::get('/users', [UserController::class, 'index']);
// Using string syntax...
Route::get('/users', 'App\Http\Controllers\UserController@index');
目标类别
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SayhelloController extends Controller
{
public function index($name = null)
{
return 'Hello '.$name;
}
}
所以我做到了.
推荐答案
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');
这篇关于目标类别不存在.laravel 8中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文