Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Laravel Basics MVC (Controller) Using Controllers to Return Views

Target Class [AppController] does not exist error

In the Laravel Basics course and the lesson 'Using Controllers to Return Views' I created the AppController just like in the tutorial and changed the route to reference it again like in the tutorial, however, when I go to the web page, I get a Target Class [AppController] does not exist error. the controller code and the route are given below:


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

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

}

route code:

Route::get('/', 'AppController@index')

I have MAMP installed and am using the default port 8888. Valet is also installed and the directory of my project is parked so that I start the web page with treehouse.test. Just to be sure it wasn't Valet that was a problem, I also fired up the artisan server and attempted to see the webpage through localhost:8000.

If I remove the controller and return the route to have the function statement I can view the welcome page no problems, but as soon as I put the AppController in I get the error.

3 Answers

I believe this is something that has changed in Laravel 8. This video series was made with an earlier version. Adapting the example from the Laravel 8 Docs, given here: https://laravel.com/docs/8.x/routing#basic-routing

This works for me:

Route::get('/', [AppController::class, 'index']);

I would like to add that I found that In order for above to work I had to do:

use App\Http\Controllers\Appcontroller;

I had this same problem as Mike on Sep 10

In this file routes/web.php file

I had to use this Route Statement: Route::get('/', 'App\Http\Controllers\AppController@index');

And it worked. Thank you for that Mike! Although it would be helpful for an update and explanation of why it works

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi, Mike Womack! I feel like there's not quite enough information to give you a definitive answer here. It could be something as simple as the file structure or possibly even the autoloading. Do you happen to have this pushed up to GitHub anywhere where I could take a look?

Let me know :sparkles:

Jennifer, Thanks for your response. I have since got it working, although I am not sure exactly why. I found an answer in StackOverflow that helped at https://stackoverflow.com/questions/63807930/target-class-controller-does-not-exist-laravel-8. It looks like something changed in laravel 8. It talks about something to do with Namesapce and the RouteServiceProvider. The bottom line is I added the full path including the Namespace "App\Http\Controllers\AppController@index' to the route statement and it worked. Thanks

Ashley Gordon
Ashley Gordon
10,827 Points

I too had to use the Stack overflow that Mike is referring to. Seems the course needs a note adding to update.

Had to use the full file path for the AppController.