Код: Выделить всё
$routes->prefix('Api', function (RouteBuilder $routes) use ($apiCache) {
$routes->registerMiddleware('apiCache', $apiCache);
$routes->applyMiddleware('apiCache');
$routes->connect('/', ['plugin' => 'OAuth2', 'controller' => 'OAuth', 'action' => 'oauth']);
$routes->connect('/token', ['plugin' => 'OAuth2', 'controller' => 'OAuth', 'action' => 'accessToken', '_ext' => 'json']);
$routes->connect('/o_auth2/{controller}', ['plugin' => 'OAuth2', '_ext' => 'json']);
$routes->fallbacks(DashedRoute::class);
});
Код: Выделить всё
public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
{
$service = new AuthenticationService();
if ($request->getParam('prefix') !== 'Api') {
// stuff for user authentication here, not relevant
} else {
// set up my OAuth2 API authentication here
}
return $service;
}
Но на самом деле мне это не нужно аутентификация на всех моих контроллерах API, только на некоторых из них.
Поэтому я попытался переместить контроллеры, которым требуется аутентификация, во вложенное пространство имен, из пространства имен App\Controller\Api в пространство имен. App\Controller\Api\Input.
Поэтому я изменил файл router.php:
Код: Выделить всё
$routes->prefix('Api', function (RouteBuilder $routes) use ($apiCache) {
$routes->registerMiddleware('apiCache', $apiCache);
$routes->applyMiddleware('apiCache');
$routes->prefix('Input', function (RouteBuilder $routes) use ($apiCache) {
$routes->connect('/{controller}'); // not sure if this line is needed
$routes->connect('/', ['plugin' => 'OAuth2', 'controller' => 'OAuth', 'action' => 'oauth']);
$routes->connect('/token', ['plugin' => 'OAuth2', 'controller' => 'OAuth', 'action' => 'accessToken', '_ext' => 'json']);
$routes->connect('/o_auth2/{controller}', ['plugin' => 'OAuth2', '_ext' => 'json']);
});
$routes->fallbacks(DashedRoute::class);
});
Код: Выделить всё
public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
{
$service = new AuthenticationService();
if (!str_contains($request->getParam('prefix'), 'Api')) {
// stuff for user authentication here, not relevant
} else {
if ($request->getParam('prefix') === 'Api/Input') { // not sure what the correct prefix would be
// set up my OAuth2 API authentication here
}
return $service;
}
code> по-прежнему является Api, контроллер — это вход, действие — это опросники, а обновление — первый элемент в проходе код> массив.
Это не то, чем я занимаюсь ожидаю, поэтому думаю, что неправильно настраиваю маршруты, хотя в документации упоминается, что при необходимости можно вложить несколько префиксов.
Подробнее здесь: https://stackoverflow.com/questions/792 ... in-cakephp
Мобильная версия