2

I'm working with Laravel 8 to develop my project. And I made a controller called BackendController and add this index() method to call a blade:

public function index()
{
    return view('website.backend.dashboard.index');
}

And on web.php I add this route:

Route::get('/dashboard', 'BackendController@index');

But when I goto /dashboard, it says:

lluminate\Contracts\Container\BindingResolutionException Target class [BackendController] does not exist.

I don't know why it prints this, because the Controller already exists! So if you know how to solve it, please help me...

Thanks in advance.

4 Answers4

1

Laravel 8 has updated its routing.
It was documented, look for the section Routing Namespace Updates

In new Laravel 8.x applications, controller route definitions should be defined using standard PHP callable syntax;

use App\Http\Controllers\UserController;

Route::get('/users', [UserController::class, 'index']);

New documentation is here; https://laravel.com/docs/8.x/routing#basic-routing

Rooneyl
  • 7,802
  • 6
  • 52
  • 81
1

In conclusion, you should define your route like this; Route::get('/dashboard', [\App\Http\Controllers\BackendController::class, 'index']).

To be more specific; In previous releases of Laravel, the RouteServiceProvider contained a $namespace property. This property's value would automatically be prefixed onto controller route definitions and calls to the action helper / URL::action method. In Laravel 8.x, this property is null by default. This means that no automatic namespace prefixing will be done by Laravel. Therefore, in new Laravel 8.x applications, controller route definitions should be defined using standard PHP callable syntax:

use App\Http\Controllers\UserController;

Route::get('/users', [UserController::class, 'index']);

[Copied from Laravel Docs. See "Routing Namespace Updates" section from https://laravel.com/docs/8.x/releases]

cednore
  • 874
  • 4
  • 20
1

In previous releases of Laravel, the RouteServiceProvider contained a $namespace property. This property's value would automatically be prefixed onto controller route definitions and calls to the action helper / URL::action method. In Laravel 8.x, this property is null by default. This means that no automatic namespace prefixing will be done by Laravel. releases#laravel-8

So, try this one:

use App\Http\Controllers\BackendController;
Route::get('/dashboard',  [BackendController::class, 'show'])->name('backend.index');
ORHAN ERDAY
  • 1,020
  • 8
  • 31
0

Use the following syntax: Route::get('/dashboard', [BackendController::class, 'index'])->name('backend.index');

see laravel docs

Dikkepanda
  • 21
  • 1
  • 8