Home > Blockchain >  Laravel controller not existing
Laravel controller not existing

Time:06-28

I wanna redirect to my controller instead of a view but it says: "Target class [app/Http/Controllers/MyFirstController] does not exist. "

here is the code (web.php file):

//just a view
Route::get('/', function () {
    return view('index');
});

//just a view
Route::get('/final', function () {
    return view('welcome');
});

//the controller is interested in
Route::get('/hello-controller', 'app/Http/Controllers/MyFirstController@index');

Controller code (app/Http/controllers/MyFirstController.php) :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MyFirstController extends Controller
{
    public function index(){
        return "viva";
    }
}

additional information: Laravel Framework version: 8.83.17 PHP version : PHP 7.4.29

CodePudding user response:

The namespace is not correct: Capital A for App and use \ instead of /

Route::get('/hello-controller', 'App\Http\Controllers\MyFirstController@index');

or even better :

Route::get('/hello-controller', [\App\Http\Controllers\MyFirstController::class, 'index']);

CodePudding user response:

try to clear cache your controller by using

php artisan route:cache

and also maybe u should use

//the controller is interested in
Route::get('/hello-controller', 'App/Http/Controllers/MyFirstController@index');
  • Related