In a fresh laravel 8 install, I have this in my web.php file:
Route::get('/', 'HomeController@index');
and this in the index method of the HomeController.php
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('welcome');
}
error page:--
The way to define your routes in laravel 8 or above is
use App\Http\Controllers\HomeController;
// Using PHP callable syntax...
Route::get('/', [HomeController::class, 'index']);
OR
// Using string syntax...
Route::get('/', 'App\Http\Controllers\HomeController@index');
If you want to stick to the old way, then you need to add a namespace property in the RouteServiceProvider.php (This property was present in previous versions) and activate in the routes method. /**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
// Edit boot method
/**
* 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')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
});
in laravel 8 you can use the new syntax to define your routes or stick to the old way.
![FireShot Capture 058 - 🧨 Target class [HomeController] does not exist. - localhost](https://res.cloudinary.com/practicaldev/image/fetch/s--PAYhFtFk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/322gdctshjwqwx7xxj1n.png)
Comments
Post a Comment