1
votes

I have a very simple routes.php file:

<?php

Route::get('/', 'TracksController@index');

And a simple TracksController.php file located at App\Http\Controllers:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Track as Track;

class TracksController extends Controller
{
    function index(){

        $tracks = Track::latest->get();
        return view('tracks')->with(compact('tracks'));

    }

}

But whenever I try to access that route I get this error:

ReflectionException in Route.php line 280:
Class App\Http\Controllers\TracksController does not exist

I have no idea what else I can do. I have:

  • Run composer dumpautoload
  • Run php artisan clear-compiled
  • Checked permissions on the storage/* folders
  • Forcing the namespace and/or fullpath of the TracksController in the routes file

But nothing seems to be working.

I even checked the vendor/composer/autoload_classmap.php file generated by composer and I cannot find the TracksController file there.

Any clues?

3
check your function function index(){} should be public function index(){}`Andrew Nolan
make the function public, and remove everything inside of it and put this dd('test'); and remove the use App\Track as Track; and if you see the word test on your page then the answer is obviousAchraf Khouadja
It may have been a typo in your question, but the command should be composer dump-autoload, with a hyphen. If it wasn't a typo, try that and see if that resolves the issue.patricus
composer allows for composer dump-autoload or composer dumpautoloadAndrew Nolan
@AndrewNolan Huh. Just tested that and it works. I did not know that. Thank you for the information.patricus

3 Answers

1
votes

So my previous answer was incorrect as patricus put about the public modifier. The issue does lie with your $tracks = Track::latest->get();

change to $tracks = Track::latest()->get(); and you should be set.

0
votes

If you're on linux, file names and paths are case sensitive. Assuming you haven't changed the autoloading in the default composer.json file, PHP is expecting the file to be located at app\Http\Controllers\TracksController.php. In your question, you specified it is located at App\....

Assuming this wasn't a typo in the question, you either need to rename your App directory back to app, or you need to update your composer.json file to let it know to autoload from the App directory, and not the app directory:

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "App/"
    }
},
0
votes

If you are using resource controllers, you must declare the use of the controller in the routes/web.php file, as following:

<?php
use App\Http\Controllers\UsersController;
...
Route::resource('users', UsersController::class);

This works here using Laravel 8.