24

When create a new project with laravel 8 and I get this error.

Illuminate\Contracts\Container\BindingResolutionException Target class [SayhelloController] does not exist. http://127.0.0.1:8000/users/john

<?php use Illuminate\Support\Facades\Route; Route::get('/', function () { return view('welcome'); }); Route::get('/users/{name?}' , [SayhelloController::class,'index']); 

In laravel documents Routes controller class must define like this

 // Using PHP callable syntax... Route::get('/users', [UserController::class, 'index']); // Using string syntax... Route::get('/users', 'App\Http\Controllers\UserController@index'); 

Target class

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class SayhelloController extends Controller { public function index($name = null) { return 'Hello '.$name; } } 

So I did exactly.

7
  • Can you share more details, like the definition of that target class? Commented Sep 14, 2020 at 9:56
  • target class is added Commented Sep 14, 2020 at 9:58
  • You answered your own question within two minutes, why ask if you already know answer? Commented Sep 14, 2020 at 9:59
  • You forgot to add Namespace for controller class. Commented Sep 14, 2020 at 10:06
  • 2
    @Orhan, thats good thinking but this is not how SO works, you should first look if the problem/solution you are addressing is already solved or not, also if you wish to answer then you should add some more details too like kamlesh-paul has done, cheers :) Commented Sep 14, 2020 at 10:10

1 Answer 1

64

Laravel 8 Update the way to write routes

ref link https://laravel.com/docs/8.x/upgrade

in laravel 8 you need to use like

use App\Http\Controllers\SayhelloController; Route::get('/users/{name?}' , [SayhelloController::class,'index']); 

or

Route::get('/users', 'App\Http\Controllers\UserController@index'); 

If you want to use old way

then in RouteServiceProvider.php

add this line

 /** * 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'; // need to add in Laravel 8 public function boot() { $this->configureRateLimiting(); $this->routes(function () { Route::prefix('api') ->middleware('api') ->namespace($this->namespace) // need to add in Laravel 8 ->group(base_path('routes/api.php')); Route::middleware('web') ->namespace($this->namespace) // need to add in Laravel 8 ->group(base_path('routes/web.php')); }); } 

Then you can use like

Route::get('/users/{name?}' , [SayhelloController::class,'index']); Route::resource('/users' , SayhelloController::class); 

or

Route::get('/users', 'UserController@index'); 
Sign up to request clarification or add additional context in comments.

5 Comments

Hello, i cannot open a new threat, I find this and is exactly that I need, but I use a resource controller, how can I do?
@MauEspaña i added resource example as well
Today, I am facing the same problem months later when compared to this thread. In my case, the RouteServiceProvider.php reference was not necessary/not my case, because it was already there! So, I´ve changed my class StudentController from App\Http\Controller dir to/subdir: App\Http\Controller\Student\StudentController. And ran again artisan route:list. The same issue happened again, but the path for reference called my attention: App\Http\Controller\App\Http\Controller\Student\StudentController. So, it means some kind of cached route. I put back the Class and worked!
For the uninitiated, you may want to mention at the top (of the answer) that one needs to include the Controller on the top of the routing file
the best way, i think with old way open RouteServiceProvider.php and add protected $namespace = 'App\\Http\\Controllers'; above public function boot() { for no many need change