Home > database >  laravel8 namespace not working: Target class [DashboardController] does not exist
laravel8 namespace not working: Target class [DashboardController] does not exist

Time:06-13

This happened in route file. I have change the structure, this should not be the problem.

app\Domains\Admin\routes\admin.php

Route::group([
    'namespace' => 'App\Domains\Admin\Http\Controllers',
    'prefix' => 'admin',
    'as' => 'admin.',
], function () use($backend)
{
    //This ok, so I have admin.login, admin.logout...
    Auth::routes();

    //This not ok. I do have admin.dashboard, it didn't say route admin.dashboard not found, 
    //But says Target class [DashboardController] does not exist. 
    //Route::get('', [DashboardController::class, 'index'])->name('dashboard');

    //This ok.
    Route::get('', [App\Domains\Admin\Http\Controllers\DashboardController::class, 'index'])->name('dashboard');
});

app\Providers\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(app_path('Domains/Frontend/routes/web.php'));


        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(app_path('Domains/Admin/routes/admin.php'));
    });
}

Why this doesn't work, when namespace is provided?

Route::get('', [DashboardController::class, 'index'])->name('dashboard');

But admin auth routes all working fine?

I also added namespace in RouteServiceProvider

        Route::middleware('web')
            ->namespace('App\Domains\Admin\Http\Controllers')
            ->group(app_path('Domains/Admin/routes/admin.php'));

No use.

CodePudding user response:

Please add this line at the head of your routes file

use App\Domains\Admin\Http\Controllers\DashboardController;

Or else

use string expression, instead of class expression

Route::get('', 'DashboardController@index')->name('dashboard');
  • Related