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

后端 未结 17 1811
無奈伤痛
無奈伤痛 2021-02-05 10:36

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\\

相关标签:
17条回答
  • 2021-02-05 11:05

    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;
        }
    }
    
    0 讨论(0)
  • 2021-02-05 11:06

    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
    
    0 讨论(0)
  • 2021-02-05 11:06

    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');
    

    Solution code:

    Route::apiResource('categories', 'api\CategoryController');
    
    0 讨论(0)
  • 2021-02-05 11:07

    Run this command

      php artisan config:cache 
    
    0 讨论(0)
  • 2021-02-05 11:07

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

    0 讨论(0)
  • 2021-02-05 11:07

    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']);

    0 讨论(0)
提交回复
热议问题