Laravel Error - Controller not found, but it is there

Viewed 966

Laravel 8

To be exact:

"laravel/framework": "^8.12",

I kept getting this error

enter image description here

As you guys can see I have that file in my project.

enter image description here

I also tried restarting my local MAMP server and clear the cache

php artisan view:clear
php artisan route:clear
php artisan cache:clear
php artisan config:clear

I also did

⚡️  mybabies  composer dumpauto                                                                                        
Generating optimized autoload files                                                                                   
> Illuminate\Foundation\ComposerScripts::postAutoloadDump                                                             
> @php artisan package:discover --ansi                                                                                
Discovered Package: facade/ignition                                                                                   
Discovered Package: fideloper/proxy                                                                                   
Discovered Package: fruitcake/laravel-cors                                                                            
Discovered Package: laravel/sail                                                                                      
Discovered Package: laravel/tinker                                                                                    
Discovered Package: nesbot/carbon                                                                                     
Discovered Package: nunomaduro/collision                                                                              
Package manifest generated successfully.                                                                              
Generated optimized autoload files containing 4683 classes 

Please let me know what else I can do.


route

Route::get('/', function(){ return Redirect::to('/baby/signin'); }); Route::get('/baby/signin','BabyAccountController@signin');

5 Answers

make sure you have these in your RouteServiceProvider:

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

public function boot()
{
    $this->configureRateLimiting();

    $this->routes(function () {
        Route::prefix('api')
            ->middleware('api')
            ->namespace($this->namespace) // here
            ->group(base_path('routes/api.php'));

        Route::middleware('web')
            ->namespace($this->namespace) // here
            ->group(base_path('routes/web.php'));
    });
}

Alternatively, you could comment out the $namespace in your RouteServiceProvider and use closure based route definitions like so:

use App\Http\Controllers\BabyAccountController;

Route::get('/baby/signin', [BabyAccountController::class, 'signin']);

I got the same error when I installed Laravel version 8.27.0: The error is as follow:

The way to define your routes in laravel 8 is either:

use App\Http\Controllers\BabyAccountController;

Route::get('/baby/signin', [BabyAccountController::class, 'signin']);

OR

If you want to stick to the old way, then you need to add a namespace property in the app\Providers\RouteServiceProvider.php and activate in the routes method. enter image description here

and

Route::get('/baby/signin','BabyAccountController@signin');

If you are using Laravel 8.0 or above, your Route code should be as below:

Your current code :

Route::get('/baby/signin','BabyAccountController@signin');

It should be :

Route::get('/baby/signin',[BabyAccountController::class, 'signin']);

and don't forget to import controller in web.php

use App\Http\Controllers\BabyAccountController;

Change your routes definition as below:

Route::get('/', function(){ return Redirect::to('/baby/signin'); }); Route::get('/baby/signin','App\Http\Controllers\BabyAccountController@signin');

Huge thanks to @p-k-tharindu

Adding this line into

app/Providers/RouteServiceProvider.php

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

Solved my problem.

Note : I don't need to add ->namespace($this->namespace) in boot() because it's already there. Just in case others face this issue to.

Related