Target class controller does not exist - Laravel 8

前端 未结 14 2146
你的背包
你的背包 2020-11-22 05:43

Here is my controller:



        
相关标签:
14条回答
  • 2020-11-22 06:07
    • Yes in laravel 8 this error is occur..
    • After try many solutions I got these perfect solution
    • Just follow the steps...

    CASE - 1

    We can change in api.php and in web.php files like bellow..
    The current way we write syntex is

    Route::get('login', 'LoginController@login');
    

    should changed to

    Route::get('login', [LoginController::class, 'login']);
    

    CASE - 2

    1. First go to the file: app > Providers > RouteServiceProvider.php
    2. In that file replace the line
      protected $namespace = null; with protected $namespace = 'App\Http\Controllers';
    3. Then After add line ->namespace($this->namespace) as shown in image..
    0 讨论(0)
  • 2020-11-22 06:07

    If you are using laravel 8

    just copy and paste my code

    use App\Http\Controllers\UserController;
    
    Route::get('/user', [UserController::class, 'index']);
    
    0 讨论(0)
  • 2020-11-22 06:10

    You are using Laravel 8. In a fresh install of Laravel 8, there is no namespace prefix being applied to your route groups that your routes are loaded into.

    "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." Laravel 8.x Docs - Release Notes

    You would have to use the Fully Qualified Class Name for your Controllers when referring to them in your routes when not using the namespace prefixing.

    use App\Http\Controllers\UserController;
    
    Route::get('/users', [UserController::class, 'index']);
    // or
    Route::get('/users', 'App\Http\Controllers\UserController@index');
    

    If you prefer the old way:

    App\Providers\RouteServiceProvider:

    public function boot()
    {
        ...
    
        Route::prefix('api')
            ->middleware('api')
            ->namespace('App\Http\Controllers') // <---------
            ->group(base_path('routes/api.php'));
    
        ...
    }
    

    Do this for any route groups you want a declared namespace for.

    The $namespace property:

    Though there is a mention of a $namespace property to be set on your RouteServiceProvider in the Release notes and commented in your RouteServiceProvider this does not have any effect on your routes. It is currently only for adding a namespace prefix for generating URLs to actions. So you can set this variable, but it by itself won't add these namespace prefixes, you would still have to make sure you would be using this variable when adding the namespace to the route groups.

    This information is now in the Upgrade Guide

    Laravel 8.x Docs - Upgrade Guide - Routing

    With what the Upgrade Guide is showing the important part is that you are defining a namespace on your routes groups. Setting the $namespace variable by itself only helps in generating URLs to actions.

    Again, and I can't stress this enough, the important part is setting the namespace for the route groups, which they just happen to be doing by referencing the member variable $namespace directly in the example.

    Update:

    If you have installed a fresh copy of Laravel 8 since version 8.0.2 of laravel/laravel you can uncomment the protected $namespace member variable in the RouteServiceProvider to go back to the old way, as the route groups are setup to use this member variable for the namespace for the groups.

    // protected $namespace = 'App\\Http\\Controllers';
    

    The only reason uncommenting that would add the namespace prefix to the Controllers assigned to the routes is because the route groups are setup to use this variable as the namespace:

    ...
    ->namespace($this->namespace)
    ...
    
    0 讨论(0)
  • 2020-11-22 06:12

    Just uncomment below line from RouteServiceProvider (If does not exists then add)

    protected $namespace = 'App\\Http\\Controllers';
    
    0 讨论(0)
  • 2020-11-22 06:14

    In laravel 8 you just add your controller namespace in routes\web.php

    use App\Http\Controllers\InvoiceController; // InvoiceController is controller name Route::get('invoice',[InvoiceController::class, 'index']);

    Or

    go `app\Providers\RouteServiceProvider.php` path and remove comments

    protected $namespace = 'App\\Http\\Controllers';

    0 讨论(0)
  • 2020-11-22 06:16

    In case if you prefer grouping of this routes, you can do as :

    Route::group(['namespace' => 'App\Http\Controllers\Api'], function () {
        Route::resource('user', 'UserController');
        Route::resource('book', 'BookController');
    });
    
    
    0 讨论(0)
提交回复
热议问题