admin管理员组文章数量:1122832
In my routes.php
I currently have this:
$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);
});
and in my Application.php
I do this:
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;
}
This applies an OAuth2 authentication to all the controllers with the /api/
prefix.
But, I don't actually need authentication on all of my API controllers, just some of them.
So I tried moving the controllers that do need authentication into a nested namespace, from namespace App\Controller\Api
to namespace App\Controller\Api\Input
.
So I changed my routes.php
file:
$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);
});
What I would also like to do is change the getAuthenticationService
accordingly, so that only the controllers under /api/input
are affected by it, IE:
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;
}
However, checking with a debugger, even though I'm calling, for example, http://localhost/api/input/questionnaires/update
, the prefix
is still Api
, the controller
is Input
, the action
is questionnaires
and update
is the fist element in the pass
array.
This is not what I'm expecting, so I guess I am setting up my routes incorrectly, even though the documentation mentions that it is possible to nest multiple prefixes if necessary.
本文标签: phpNested routes prefixes in CakePHPStack Overflow
版权声明:本文标题:php - Nested routes prefixes in CakePHP - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308537a1933696.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论