fpet
fpet

Reputation: 73

cakephp3: namespace error with prefix route

I am trying to implement a REST api with cakephp 3.

In order to give an easily reproducible example of my problem, I start with a fresh installation of cakephp 3.1.11. In config/routes.php, I add:

Router::prefix('/api', function ($routes) {
    $routes->fallbacks('DashedRoute');
});

If I access http://mysite/prefixtest/api/Accounts I get the (expected) "Missing Controller error":

Error: AccountsController could not be found.

Error: Create the class AccountsController below in file: src/Controller//Api/AccountsController.php

<?php
namespace App\Controller\\Api;

use App\Controller\AppController;

class AccountsController extends AppController
{

}

Notice the double slashes in the path and the double backslashes in the namespace.

Creating the AccountsController in src/Controller/Api/AccountsController.php (single / in the path) with the suggested code results in the error:

Error: syntax error, unexpected '\' (T_NS_SEPARATOR), expecting identifier (T_STRING)
File src/Controller/Api/AccountsController.php
Line: 2 

Not too surprising, since the double backslash in the namespace isn't allowed in php. Removing the additional backslash gets me back to the "Missing Controller error", probably since the controller is not in the namespace cakephp expects it in.

What am I doing wrong here?

Upvotes: 0

Views: 293

Answers (1)

ndm
ndm

Reputation: 60453

Opposed to scopes or routes, prefixes are not supposed to be connected with a leading slash.

See also Cookbook > Routing > Prefix Routing

Upvotes: 1

Related Questions