skip to Main Content

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 AppHttpControllers;

use IlluminateHttpRequest;

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

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

2

Answers


  1. 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');
    
    Login or Signup to reply.
  2. The namespace is not correct: Capital A for App and use instead of /

    Route::get('/hello-controller', 'AppHttpControllersMyFirstController@index');
    

    or even better :

    Route::get('/hello-controller', [AppHttpControllersMyFirstController::class, 'index']);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search