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