目标类别[Admin \ UsersController]不存在 [英] Target class [Admin\UsersController] does not exist

查看:57
本文介绍了目标类别[Admin \ UsersController]不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将路由从 web.php 分为 admin.php home.php .

然后在 RouteServiceProvider 处添加了名称空间 \ Admin :

Then at RouteServiceProvider I have added namespace \Admin:

    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/home.php'));

            Route::middleware(['web','auth','auth.admin'])
                ->namespace($this->namespace . '\Admin')
                ->prefix('admin')
                ->group(base_path('routes/web/admin.php'));
        });
    }

admin.php 像这样:

Route::get('/', function () {
    return view('admin.index');
});

Route::resource('/users', UsersController::class);

但是以某种方式我会收到此错误:

But somehow I get this error:

Illuminate \ Contracts \ Container \ BindingResolutionException目标类 [Admin \ UsersController]不存在.

但是 UsersController 已经存在:

那那里出了什么问题?

推荐答案

在Laravel 8中,默认情况下, $ this->命名空间 null .这样就可以使用PHP可调用语法.因此,您无需将'\ Admin'追加到名称空间.

In Laravel 8, by default, $this->namespace is null. That allows to use PHP callable syntax. So you don't need to append '\Admin' to the namespace.

->命名空间($ this->命名空间.'\ Admin')更改为->命名空间($ this->命名空间)并添加使用App \ Http \ Controllers \ Admin \ UsersController; admin.php .

这篇关于目标类别[Admin \ UsersController]不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆