1

I want to be able to support multiple versioned endpoints from my api simultaneously, such as:

/api/v1.1/counties/get
/api/v1.2/counties/get

But in trying to implement the routing for this, a bit perplexed as to how Cake wants this as I keep getting a

Error: Controller class Counties could not be found.

Attempt 1:

Router::scope('/api', function ($routes) {

    $routes->setExtensions(['json']);
    $routes->fallbacks('DashedRoute');

    $versions = [
        1.1
    ];

    foreach ($versions as $version) {
        $routes->scope('/' . $version, function($routes) {

            $routes->resources('Counties', [
                'controller' => 'Counties',
                'prefix' => 'api',
                'map' => [
                    'get' => [
                        'action' => 'get',
                    ]
                ]
            ]);

        }
    }

});

Attempt 2:

Router::scope('/api', function($routes) {

    $routes->scope('/v1.1', function($routes) {
        $routes->resources('Counties', [
            'controller' => 'Counties',
            'map' => [
                'get' => [
                    'action' => 'get'
                ]   
            ]   
        ]); 
    }); 

    $routes->connect(
        '/v1.1/counties/get',
        [   
            'controller' => 'Counties',
            'action' => 'get',
        ]   
    );  
});

The directory structure I'm currently using (which is still open for debate):

src/Controller/Api/V1.1, which would use base controllers from src/Controller/Api and extend them with stub methods to override if needed. Most of my "fat" is in the models.

and src/Controller/Api/V1.1/CountiesController.php has:

namespace App\Controller\Api\V1.1;

class CountiesController extends AppController
{
}

Would appreciate any insight

4

1 回答 1

4

您不能在命名空间(文件夹)结构中使用点之类的字符,因为那是无效的 PHP。

您正在寻找的是使用前缀路由和path选项,以便您可以连接在命名空间中有效的前缀,并为路由提供自定义路径(URL 段),例如:

Router::prefix('api', function (RouteBuilder $routes) {
    // ...

    $routes->prefix('v11', ['path' => '/v1.1'], function (RouteBuilder $routes) {
        $routes->resources('Counties', [
            'map' => [
                'get' => [
                    'action' => 'get'
                ]  
            ]
        ]);
    });
});

这将连接以下路由(您可以通过检查 shell 中的连接路由bin/cake routes

+---------------------+-----------------------+--------------------------------------------------------------------------------------------------+
| Route name          | URI template          | Defaults                                                                                         |
+---------------------+-----------------------+--------------------------------------------------------------------------------------------------+
| v11:counties:index  | api/v1.1/counties     | {"controller":"Counties","action":"index","_method":"GET","prefix":"v11","plugin":null}          |
| v11:counties:add    | api/v1.1/counties     | {"controller":"Counties","action":"add","_method":"POST","prefix":"v11","plugin":null}           |
| v11:counties:view   | api/v1.1/counties/:id | {"controller":"Counties","action":"view","_method":"GET","prefix":"v11","plugin":null}           |
| v11:counties:edit   | api/v1.1/counties/:id | {"controller":"Counties","action":"edit","_method":["PUT","PATCH"],"prefix":"v11","plugin":null} |
| v11:counties:delete | api/v1.1/counties/:id | {"controller":"Counties","action":"delete","_method":"DELETE","prefix":"v11","plugin":null}      |
| v11:counties:get    | api/v1.1/counties/get | {"controller":"Counties","action":"get","_method":"GET","prefix":"v11","plugin":null}            |
+---------------------+-----------------------+--------------------------------------------------------------------------------------------------+

CountiesController然后预计该课程将在

src/Controller/Api/V11/CountiesController.php

命名空间为:

App\Controller\Api\V11

也可以看看

于 2018-01-26T18:20:31.710 回答