我知道这个问题已经被问了几次了,但没有一个答案对我有效,这就是为什么我再次问这个问题。
我正在尝试用Laravel创建一个小网站,我创建了一条路由和一个控制器,但当我试图在url中访问它时,我得到了这个错误:
Illuminate\Contracts\Container\BindingResolutionException
Target class [Admin\PlanController] does not exist.
这是我的web.php
use Illuminate\Support\Facades\Route;
Route::get('admin/plans', 'Admin\PlanController@index')->name('plans.index');
Route::get('/', function () {
return view('welcome');
});
这是我的PlanController.php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class PlanController extends Controller
{
public function index(){
return view('admin.pages.plans.index');
}
}
如果有任何帮助,这里是我的RouteServiceProvider.php
protected $namespace = 'App\\Http\\Controllers';
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
});
}
发布于 2021-08-24 07:32:00
你的代码看起来一切正常。确认一个目录下的控制器必须是
app\Http\Controllers\Admin
或者做一些改变
$this->routes(function () {
Route::middleware('web')
->namespace('App\Http\Controllers')
->group(base_path('routes/web.php'));
而不是在RouteServiceProvider.php
中使用获取名称空间值作为变量。
发布于 2021-08-24 06:27:46
如果您使用的是Laravel 8,最好使用新的路由语法,如下所示:
use App\Http\Controllers\Admin\PlanController;
Route::get('/admin/plans', [PlanController::class, 'index'])->name('plans.index');
确保控制器位于正确的目录app/Http/Controllers/Admin
中
发布于 2021-08-24 13:25:08
您可以在您的路由中描述完整的命名空间;
Route::get('admin/plans','App\Http\Controllers\Admin\PlanController@index')->name('plans.index');
https://stackoverflow.com/questions/68908937
复制相似问题