Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 6.0 php artisan route:list returns “Target class [App\Http\Controllers\SessionsController] does not exist.”

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?

like image 821
Andreas Avatar asked Sep 10 '19 06:09

Andreas


3 Answers

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:

Solution 1: Classname representation

use App\Http\Controllers\RegistrationController;

Route::get('/', [RegistrationController::class, 'create']);

Solution 2: String syntax

or as a string syntax (full namespaces controller name):

Route::get('/', 'App\Http\Controllers\RegistrationController@create');

Solution 3: Return to previous behaviour

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").

like image 138
Alexander Taubenkorb Avatar answered Nov 09 '22 23:11

Alexander Taubenkorb


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
like image 44
Boris N Avatar answered Nov 10 '22 00:11

Boris N


Run this command

  php artisan config:cache 
like image 23
mukesh kumar Avatar answered Nov 09 '22 23:11

mukesh kumar