3

i'm using laravel 5 , in rutes.php i have this code :

Route::get('about',"homeController@about");

and in App\Http\Controllers\ i have file homeController.php that contains :

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller as BaseController;

class homeController extends BaseController{
public function about(){
    return view::make('about');
}
}

but it throws this error : Class App\Http\Controllers\homeController does not exist . how can i fix it ?

here is structure of the project and controllers : enter image description here

ako
  • 2,000
  • 2
  • 28
  • 34

12 Answers12

8

First, check if you write the name of the controller correctly. If it is, there are 3 methods:

  1. Route::get('/about', 'App\Http\Controllers\homeController@about'); Write all the paths where your controller there is.

  2. Route::get('/about', [HomeController::class, 'about');

    • Go to file RouteServiceProvider.php in Providers folder
    • Search for //protected $namespace = 'App\\Http\\Controllers'; ( You will find it in comment)
    • Remove // of comment and save. with this method, you will able to use the name of the controller directly without writing all the paths.
shaedrich
  • 5,457
  • 3
  • 26
  • 42
  • Using the second method inside an IDE would help the thread owner to see that the controller class has to be written starting with an uppercase letter as you've pointed out. – shaedrich Apr 01 '21 at 15:37
3

Change all

homeController

To

HomeController
KmasterYC
  • 2,294
  • 11
  • 19
3

change your routing like below:

use App\Http\Controllers\HomeController;

Route::get('/about',  [HomeController::class, 'index'])->name('home');

for more info look at this page: https://laravel.com/docs/8.x/routing

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
milad
  • 93
  • 1
  • 6
2

For HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function welcome()
    {
        return view('welcome');
    }
}

For web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;

Route::get('/', [HomeController::class, 'welcome']);

Its work for me

1
<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller as BaseController;

class homeController extends BaseController{
  public function about(){
    return view::make('about');
  }
}
Rohit Khatri
  • 1,980
  • 3
  • 25
  • 45
1
<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller as BaseController;

class homeController extends BaseController{
  public function about(){
    return view::make('about');
  }
}

Should works perfectly. Are you sure that name of file is homeController.php ?

  • but in your screenshot `here is structure of the project and controllers :` The name of file starts from capital letter – user3720250 Aug 17 '16 at 11:41
  • Change `Route::get('about',"HomeController@about");` to `Route::get('about',"homeController@about");` and try – user3720250 Aug 17 '16 at 11:43
  • If you want to mess up project structure, go ahead. If you have a very good reason to do so, well, go ahead. But if not, classes should start with an uppercase letter. – shaedrich Apr 01 '21 at 15:40
1

I faced a similar issue with the Home controller, in my case I saw the route was configured as following:

Route::get('/home',"homeController@index"); 

when I changed the above code to the following then it worked.

Route::get('/home',"App\Http\Controllers\HomeController@about");

In your case first, check the spelling first, whether it should be HomeController or homeController.

You can change your route code to the following

so you can try to change the following code

Route::get('about',"HomeController@about");

to

Route::get('about', 'App\Http\Controllers\HomeController@about');

or

Route::get('/about', 'App\Http\Controllers\HomeController@about');

Hope this will work.

Hriju
  • 728
  • 1
  • 16
  • 27
1

It can happen because of these two causes:

  1. Typo Error
  2. Laravel Cache

1) Type Error For this check, please check your HomeController file name and the class name in that file. Both should be same with case sensitivity.

HomeController.php

class HomeController extends

2) Laravel Cache Laravel stores file caches with previous configurations. To refresh the cache, do this commands in the command window and then try again

php artisan cache:clear
php artisan view:clear
php artisan optimize

Hope it solved!.

Karthik SWOT
  • 1,129
  • 1
  • 11
  • 15
0

Yes. That is right because you are not giving right path to the action in the routes. Either you update core files for path or provide manually in the route. e.g you have

Route::get('about',"homeController@about");

try this route

Route::get('/about', [App\Http\Controllers\homeController::class,'about']);

or you can type route as

Route::get('/about', 'App\Http\Controllers\homeController@about');

furthermore you can check if you have right code in the controller function.

user14063792468
  • 839
  • 12
  • 28
0

You are trying Laravel 7.x and before routing schema. Refer to 8.x documentation.

New syntaxis using as [HomeController::class, 'index'] or you need to add namespace before Controller name like App\Http\Controllers\HomeController.

Obsidian
  • 3,719
  • 8
  • 17
  • 30
zehyr
  • 41
  • 5
0

Update the path to your route by adding .

Route::get('/home', [\App\Http\Controllers\HomeController::class, 'index'])->name('home');
Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
-1

Please update your route

use App\Http\Controllers\HomeController;

Route::get('/about', [HomeController::class, 'about']);