Wolfie
Wolfie

Reputation: 41

Laravel Class Controller not found because route is weirdly case sensitive

I wanted to add a controller to an already existing project of mine. Wrote a controller file, added the link in the admin_api route file and adjusted the view I needed.

On my local machine everything works fine.

On production it failed (it's supposed to retrieve a collection, which is then handled and displayed by a vue js component) and the network monitor showed an error 500.

I ssh into the website and ran php artisan route:list which gave me the error

In Container.php line 779:

  Class App\Http\Controllers\Admin\Api\StatsController does not exist

Although the file StatsController is in App\Http\Controllers\Admin\API\

After playing around I realized that the problem was with the folder name "API", as laravel was looking for "Api". So apparently it's case sensitive. Placing the controller in a new folder "Api" solved the issue. Moving all the other "API" controllers to "Api" broke their routes. when running php artisan route:list and in RouteServiceProvider all routes appear as under Admin/Api.

What I don't get is where is this being registered? Is there a way to change this? I find it quite annoying that the controllers sit now in separate folders and it seems quite absurd to me. I don't quite get how the original controllers worked if they seem to contradict the other configurations.

Working with Laravel 5.7

I would appreciate any insights!

Thanks

Upvotes: 0

Views: 1043

Answers (2)

mdexp
mdexp

Reputation: 3567

Just put all the relevant api controllers in a folder named Api or API (just pick a convention and stick with it), then make sure that each file's namespace (declared at the top) is correct and reflects the folder structure you have chosen.

For example, if your controllers are in app/Http/Controllers/Admin/Api, check that each controller file begins with:

<?php

namespace App\Http\Controllers\Admin\Api;

// Rest of code...

Upvotes: 1

Modscleo4
Modscleo4

Reputation: 1

StatsController.php must have the namespace as App\Http\Controllers\Admin\API. In Controller.php you have to import as App\Http\Controllers\Admin\API\StatsController. After that, run composer dump-autoload

Upvotes: 0

Related Questions