laravel 8 target class controller does not exist
In this tutorial, we will tell you how we can solve the target class controller does not exist in Laravel 8. You know that Laravel 8 was released a few days ago. They modified the routing system a bit from the different version of Laravel.
In this example, we will solve the routing problem in Laravel 8, because there are no target class apphttpcontrollers. so we have two solutions. one is to use the class name in the route file and the second is to define the namespace in RouteServiceProvider.php.
use the class name in the route file
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\StudentController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); Route::get('/student', [StudentController::class, 'index']); // or Route::get('/student', 'App\Http\Controllers\StudentController@index'); |
define the namespace in RouteServiceProvider.php
app/Providers/RouteServiceProvider.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | <?php namespace App\Providers; use Illuminate\Cache\RateLimiting\Limit; use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; use Illuminate\Http\Request; use Illuminate\Support\Facades\RateLimiter; use Illuminate\Support\Facades\Route; class RouteServiceProvider extends ServiceProvider { /** * The path to the "home" route for your application. * * This is used by Laravel authentication to redirect users after login. * * @var string */ public const HOME = '/home'; 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) ->group(base_path('routes/web.php')); Route::prefix('api') ->middleware('api') ->group(base_path('routes/api.php')); }); } .... |
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?php use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); Route::get('student', 'StudentController@index'); |
Read Also
Laravel 8 Get Current URL With Parameters
Laravel 8 Cron Job Scheduling Example Tutorial
Laravel 8 Import Export Excel & Csv File Example Tutorial
Please follow and like us: