'Controller Target Class does not exist?
Im trying to follow a larval tutorial and make a controller. This is what I have so far and it works for the guy in the video but mine says controller not found. I don't know what to do to fix it. Thank you!
web.php file:
Route::get('/', [PagesController::class, 'home']);
PagesController.php file:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\PagesController;
class PagesController extends Controller
{
public function home()
{
return view('welcome', [
'foo' => 'bar'
]);
}
}
Solution 1:[1]
I think somethings it happens due to cache of routes you added before
php artisan optimize:clear
Command to clear all cache
And then check does this get('/') route is binded to home() method on conrtoller using
php artisan route:list
I hope its resolve your issue..
Solution 2:[2]
Do you have use App\Http\Controllers\PagesController in web.php file?
If it doesn't exist, you should type it top of the file.
Solution 3:[3]
why did you use this in PagesController.php :
use App\Http\Controllers\PagesController;
This is wrong. you must remove this line of code in PagesController.php
Solution 4:[4]
I figured it out, added.
use App\Http\Controllers\PagesController
to the top of my web.php file and changed the get route to
Route::get('/', [PagesController::class, 'home']);
Solution 5:[5]
Instead of Route::get('/', [PagesController::class, 'home']);
you should use Route::get('/', 'App\Http\Controllers\PagesController@home');
If you do not want to type full namespace location just open RouteServiceProvider.php and add following $namespace = 'App\Http\Controllers';
Hope this solves your problem
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Saravana Sai |
| Solution 2 | Suzushio |
| Solution 3 | Keyvan Gholami |
| Solution 4 | Karl Hill |
| Solution 5 | r0pe-12 |
