DBnR
DBnR

Reputation: 175

CakePHP 3 "Controller class XYZ could not be found." only on production server

I have a separate AppController for an external API to my site. I can connect to the API just fine using Postman on my local development machine, but once I move the files to the test site or production site, I get 404 errors like "Controller class Users could not be found."

{
    "message": "Controller class Users could not be found.",
    "url": "/api/tv/users/index",
    "code": 404,
    "file": "/var/www/html/site/vendor/cakephp/cakephp/src/Http/ControllerFactory.php",
    "line": 100
}

Ubuntu 16.04, Apache, PHP 7.

The code and environment on all servers (local dev, test, and production) should be the same, yet it only works on the local machine. I don't even know what code to post but here's some:

Test code Api/TV/UsersController:

namespace App\Controller\Api\TV;

class UsersController extends AppController
{
    public function index()
    {
        $this->set([
            'message' => 'Why wont you work?',
            '_serialize' => [
               'message'
            ]
        ]);
    }
}

Routes:

Router::prefix('api', function(RouteBuilder $routes) {
    $routes->connect('/v2', [
        'controller' => 'users',
        'action' => 'login',
        '_method' => 'GET'
    ]);

    ...

    $routes->fallbacks('DashedRoute');
});

Router::prefix('api/v3', function(RouteBuilder $routes) {
    $routes->setExtensions(['json']);

    $routes->fallbacks('DashedRoute');
});

Router::prefix('api/tv', function(RouteBuilder $routes) {
    $routes->setExtensions(['json']);

    $routes->fallbacks('DashedRoute');
});

As you can see I have several different API's on different prefixes. Only the last one isn't working (except locally).

I'm at my wit's end here. I know the answer is staring me right in the face but I need someone else to point it out for me.

Upvotes: 1

Views: 1950

Answers (1)

ndm
ndm

Reputation: 60503

All parts of your prefixes (separated by /) are being inflected using Inflector::camelize(), which means api/tv becomes Api/Tv for the filesystem lookup, and Api\Tv for the namespace lookup, so your path and your namespace are wrong, as Tv won't match TV on case sensitive filesystems.

Long story short, rename your TV folder and namespace part to Tv.

Upvotes: 1

Related Questions