我想按照以下方式对api路线进行分组:
$app->mount('/api/v1.0', function($api) use($app) {
$api->mount('/documents', new DocumentsApiControllerProvider());
});
DocumentsApiControllerProvider
的定义如下:
use Silex\Application;
use Silex\Api\ControllerProviderInterface;
use App\Controllers\DocumentsApiController;
class DocumentsApiControllerProvider implements ControllerProviderInterface {
public function connect(Application $app) {
$app['api.documents.controller'] = function() use($app) {
return new DocumentsApiController();
};
$controller = $app['controllers_factory'];
$controller->get('/folders/{folder_id}/content/pages/{page}', 'api.documents.controller:getPage');
return $controller;
}
}
这会导致抛出以下异常:
致命错误:未捕获的异常' LogicException'消息'" mount"方法采用" ControllerCollection"实例或可赎回。'
非递归安装按预期工作:
$app->mount('/api/v1.0/documents', new DocumentsApiControllerProvider());
有关于该主题的讨论:https://github.com/silexphp/Silex/pull/1345,但我无法理解添加或忽略的能力?
我如何实现这一目标?
更新
好吧,我决定将api路由直接插入 Silex\Application
实例(嵌套/分组!),只是使用相同的前缀 - ' api / v1 0.0 /' :
$app->mount("/api/v1.0/documents", new DocumentsApiControllerProvider());
$app->mount("/api/v1.0/otherstuff", new OtherStuffApiControllerProvider());