traffske
traffske

Reputation: 111

Change location Controllers\Auth

I make project in Laravel 7. I change location Http\Controllers\Auth to Http\Controllers**Admin**\Auth

In all files in Auth directory I change namespaces:

from:

namespace App\Http\Controllers\Auth;

to

namespace App\Http\Controllers\Admin\Auth;

Them I make composer dump-autoload.

Login work fine.

Now when I try logout with my code:

<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>

I have error:

Illuminate\Contracts\Container\BindingResolutionException Target class [App\Http\Controllers\Front\Auth\LoginController] does not exist.

My 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'));
    }
}

What is wrong? How can I repair it?

Upvotes: 0

Views: 730

Answers (2)

Shashwat Singh
Shashwat Singh

Reputation: 3

Change The RouteService Provider inside

App\Http\Providers\RouteServiceProvider.php file and update from

protected $namespace = 'App\Http\Controllers'; to protected $namespace = 'App\Http\Controllers\NewPath';

Do php artisan config:cache and it should work fine.

Upvotes: 0

BlackXero
BlackXero

Reputation: 880

What you have changed is all from the front side of your application but when the vendor/directory is calling it's still trying to access the Auth related classes from the same namespace i.e

namespace App\Http\Controllers\Auth;

To successfully alter the namespace of your Auth file you need to tell Laravel where to access these files from the file AuthServiceProvider.php located in

app\Providers\AuthServiceProvider.php

Then there won't be that class not found issue

Upvotes: 3

Related Questions