首页 > 解决方案 > 更改位置 Controllers\Auth

问题描述

我在 Laravel 7 中制作项目。我将位置 Http\Controllers\Auth 更改为 Http\Controllers**Admin**\Auth

在 Auth 目录中的所有文件中,我更改了命名空间:

从:

namespace App\Http\Controllers\Auth;

namespace App\Http\Controllers\Admin\Auth;

他们我让作曲家转储自动加载。

登录工作正常。

现在,当我尝试使用我的代码注销时:

<a href="{{ route('logout') }}"
                       onclick="event.preventDefault(); document.getElementById('logout-form').submit();" class="btn btn-sm btn-light-primary font-weight-bolder py-2 px-5">Wyloguj</a>
                    <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                        @csrf
                    </form>

我有错误:

Illuminate\Contracts\Container\BindingResolutionException 目标类 [App\Http\Controllers\Front\Auth\LoginController] 不存在。

我的 RouteServiceProvider:

class RouteServiceProvider extends ServiceProvider
{
    /**
     * 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';

    /**
     * The path to the "home" route for your application.
     *
     * @var string
     */
    public const HOME = '/';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        //

        parent::boot();
    }

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();

        if(config('app.admin_only'))
        {
            $this->mapAdminOnlyRoutes();
        }
        else
        {
            $this->mapWebRoutes();
            $this->mapAdminRoutes();
        }
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
            ->namespace($this->namespace. '\Front')
            ->group(base_path('routes/web.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('routes/api.php'));
    }

    protected function mapAdminRoutes()
    {
        Route::prefix(config('app.admin_prefix'))
            ->middleware('web')
            ->namespace($this->namespace.'\Admin')
            ->group(base_path('routes/admin.php'));
    }

    protected function mapAdminOnlyRoutes()
    {
        Route::middleware('web')
            ->namespace($this->namespace. '\Admin')
            ->group(base_path('routes/admin.php'));
    }
}

怎么了?我该如何修复它?

标签: phplaravellaravel-5laravel-7

解决方案


您所做的更改全部来自应用程序的前端,但是当供应商/目录调用它时,它仍在尝试从同一命名空间访问与 Auth 相关的类,即

namespace App\Http\Controllers\Auth;

要成功更改 Auth 文件的命名空间,您需要告诉 Laravel 从 AuthServiceProvider.php 文件中访问这些文件的位置

app\Providers\AuthServiceProvider.php

那么就不会有那个class not found的问题


推荐阅读