Reputation: 687
I am using Laravel 6.0 and I try to list all my routes with artisan route:list
, but it fails and returns:
Illuminate\Contracts\Container\BindingResolutionException : Target class [App\Http\Controllers\SessionsController] does not exist.
at /home/vagrant/code/vendor/laravel/framework/src/Illuminate/Container/Container.php:806 802| 803| try { 804| $reflector = new ReflectionClass($concrete); 805| } catch (ReflectionException $e) { > 806| throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e); 807| } 808| 809| // If the type is not instantiable, the developer is attempting to resolve 810| // an abstract type such as an Interface or Abstract Class and there is
Exception trace:
1 Illuminate\Foundation\Console\RouteListCommand::Illuminate\Foundation\Console{closure}(Object(Illuminate\Routing\Route)) [internal]:0
2 ReflectionException::("Class App\Http\Controllers\SessionsController does not exist") /home/vagrant/code/vendor/laravel/framework/src/Illuminate/Container/Container.php:804
3 ReflectionClass::__construct("App\Http\Controllers\SessionsController") /home/vagrant/code/vendor/laravel/framework/src/Illuminate/Container/Container.php:804
Up to now I just have a very simple web.php routes file:
Route::get('/', function () {
return view('index');
});
Route::prefix('app')->group(function () {
// Registration routes
Route::get('registration/create', 'RegistrationController@create')->name('app-registration-form');
});
// Templates
Route::get('templates/ubold/{any}', 'UboldController@index');
Any idea how I could debug this issue?
Upvotes: 26
Views: 120197
Reputation: 399
On top of ensuring that you use the updated route syntax:
Route::get('/', [RegistrationController::class, 'create']);
make sure that at the top of your routes/web.php the controller is included in the name space like:
use App\Http\Controllers\RegistrationController;
Upvotes: 1
Reputation: 31
you have to specify full path to the class
Previously it was
Route::get('/wel','Welcome@index');
Now it has changed to
use App\Http\Controllers\Welcome;
Route::get('wel',[Welcome::class,'index']);
Upvotes: 2
Reputation: 376
Simply add the following line in app->Providers->RouteServiceProvider.php
protected $namespace = 'App\\Http\\Controllers';
Upvotes: 5
Reputation: 31
This is the perfect answer, I think:
use App\Http\Controllers\HomepageController;
Route::get('/', [HomepageController::class, 'index']);
Route::get('/', 'App\Http\Controllers\HomepageController@index');
Upvotes: 3
Reputation:
on Larave 7 I had the same issue.
I checked the spelling of the controller name.
I recognize I have the wrong spelling in the "AlbumContoller" and I rename it to "AlbumController". so I forgot "r"
after I renamed the file and controller name and controller name in the web.php
Route::resource('albums', 'AlbumsController');
everything worked well
So You Don't need these two:
1- use App\Http\Controllers\IndexContoller;
2- Route::get('/', [MyModelController::class, 'myFunction']);
Upvotes: 2
Reputation: 3289
I was upgrading from Laravel 7 to Laravel 8 (Laravel 8 is still a few days in development) and also had this issue.
The solution was to use a classname representation of the controller in the route:
So in web.php instead of
Route::get('registration/create', 'RegistrationController@create')
it is now:
use App\Http\Controllers\RegistrationController;
Route::get('/', [RegistrationController::class, 'create']);
or as a string syntax (full namespaces controller name):
Route::get('/', 'App\Http\Controllers\RegistrationController@create');
As this should issue should only happen if you upgrade your application by creating a brand new laravel project you can also just add the default namespace to the RouteServiceProvider:
app/Providers/RouteServiceProvider.php
class RouteServiceProvider extends ServiceProvider
{
/* ... */
/** ADD THIS PROPERTY
* If specified, this namespace is automatically applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::middleware('web')
->namespace($this->namespace) // <-- ADD THIS
->group(base_path('routes/web.php'));
Route::prefix('api')
->middleware('api')
->namespace($this->namespace) // <-- ADD THIS
->group(base_path('routes/api.php'));
});
}
/* ... /*
}
See also https://laravel.com/docs/8.x/routing#basic-routing or https://laravel.com/docs/8.x/upgrade (search for "Routing").
Upvotes: 75
Reputation: 31
I am running Laravel 8.x on my pc. This error gave me headache. To recreate the error, this is what I did: First I created a controller called MyModelController.php Secondly, I wrote a simple function to return a blade file containing 'Hello World', called myFunction. Lastly, I created a Route: Route::get('/','MyModelController@myFunction'); This did not work.
This was how I solved it. First you would have to read the documentation on: (https://laravel.com/docs/8.x/releases#laravel-8)
At the 'web.php' file this was the Route i wrote to make it work:
use App\Http\Controllers\MyModelController;
Route::get('/', [MyModelController::class, 'myFunction']);
Upvotes: 2
Reputation: 297
You can define a route to this controller action like so:
use App\Http\Controllers\UserController;
Route::get('user/{id}', [UserController::class, 'show']);
Upvotes: 2
Reputation: 21
Now You Can use controller outside controller folder
use App\Http\submit;
Route::get('/', [submit::class, 'index']);
Now my controller excited in http folder
You have to change in controller file something
<?php
namespace App\Http;
use Illuminate\Http\Request;
use Illuminate\Http\Controllers\Controller;
class submit extends Controller {
public function index(Request $req) {
return $req;
}
}
Upvotes: 2
Reputation: 9201
In my case it was solved by running
php artisan optimize:clear
php artisan config:cache
The optimize:clear
commands clears everything
Upvotes: 6
Reputation: 2364
I had the same problem but with a middleware controller. So finally I linked that middleware in kerner.php file. It is located at app\Http\Kernel.php
I have added this line in route middleware.
'authpostmanweb' => \App\Http\Middleware\AuthPostmanWeb::class
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'authpostmanweb' => \App\Http\Middleware\AuthPostmanWeb::class
];
Upvotes: 1
Reputation: 48
replace-
Route::resource('/admin/UserOff','admin/UsersController');
with-
Route::resource('/admin/UserOff','admin\UsersController');
forward / with \
Upvotes: 1
Reputation: 972
I had this problem while leaving an empty middleware class in my middleware groups by mistake :
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:100,1',
'bindings',
'localization',
'' // Was empty by mistake
],
];
Upvotes: 1
Reputation: 25
I did all these
1: php artisan config:cache
2: checked for controller name spellings.
3: composer dump-autoload
4: Just changed the forward / slash to backward \ in route.
4th one worked for me.
Upvotes: 2
Reputation: 29
try to correct your controller name
my route was
Route::get('/lien/{id}','liensControler@show');
and my controller was
class liensController extends Controller
{
// all the methods of controller goes here.
}
Upvotes: 2
Reputation: 2716
In my case it was a matter of Linux's file name case sensitivity. For a file named IndexController
, having Indexcontroller
will work in windows but not in Linux
Upvotes: 5
Reputation: 3274
Alright i got similar problem, i was trying to be smart so i wrote this in my web.php
Route::group([
'middleware' => '', // Removing this made everything work
'as' => 'admin.',
'prefix' => 'admin',
'namespace' => 'Admin',
],function(){
});
All i had to do is just to remove all the unnecessary/unused option from group. That's all.
Upvotes: 2
Reputation: 857
For those who have similar issue with Illuminate\Contracts\Container\BindingResolutionException : Target class [<className>] does not exist.
message, this also could be helpful:
composer dump-autoload
Upvotes: 39
Reputation: 6272
In my case same error
occurred because of forward slash /
but it should be backward slash \
in defining route,
it happens when you have controller in folder
like as in my case controller was in api
Folder, so always use backward slash \
while mentioning controller name.
see example:
Error-prone code:
Route::apiResource('categories', 'api/CategoryController');
Route::apiResource('categories', 'api\CategoryController');
Upvotes: 3