无法覆盖FOSOAuthServerBundle tokenAction

时间:2019-05-13 18:28:32

标签: php symfony oauth-2.0 fosoauthserverbundle

我正在使用以下软件包:

    "friendsofsymfony/oauth-server-bundle": "^1.6",
    "friendsofsymfony/rest-bundle": "^2.5",
    "friendsofsymfony/user-bundle": "^2.1",
    "symfony/framework-bundle": "4.2.*",
    "symfony/http-foundation": "4.2.*",
    "symfony/http-kernel": "4.2.*"

我正在尝试覆盖FOSOAuthServerBundle包的tokenAction方法,但是我被错误卡住了:

"Cannot autowire service App\Controller\TokenController argument $server of method FOS\OAuthServerBundle\Controller\TokenController::__construct() references class OAuth2\OAuth2; but no such service exists. You should maybe alias this class to the existing fos_oauth_server.server service"

我尝试了几种不同的方法(自动接线,自动注入),但我一直绕着上面描述的错误反复讨论。看来“使用OAuth2 \ OAuth2;”引用在包的TokenController中已正确命名,但是当我尝试覆盖它时,无法正确解析OAuth2类的位置,而且我不确定在类或services.yaml中使用哪种模式将其指向正确的位置

这是我的服务。yaml

services:

    ...

    App\Controller\TokenController\:
        resource: '../src/Controller/TokenController.php'
        arguments: ['@fos_oauth_server.server']

还有我的自定义TokenController类

?php

namespace App\Controller;

use FOS\OAuthServerBundle\Controller\TokenController as BaseController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class TokenController extends BaseController
{

    /**
     * @param Request $request
     *
     * @return Response
     */
    public function tokenAction(Request $request)
    {
        $token = parent::tokenAction($request);

        // my custom code here 

        return $token;
    }

}

如果我尝试做显而易见的事情并添加行

use OAuth2\OAuth2;

对于我的自定义TokenController,我遇到相同的错误。

1 个答案:

答案 0 :(得分:0)

原来的答案是使用装饰器

在我的services.yaml

    App\Controller\OAuth\OAuthTokenController:
        decorates: FOS\OAuthServerBundle\Controller\TokenController
        arguments: ['@fos_oauth_server.server']

任何自定义类都可以覆盖TokenController

namespace App\Controller\OAuth;

use FOS\OAuthServerBundle\Controller\TokenController as BaseController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class OAuthTokenController extends BaseController
{

    /**
     * @param Request $request
     *
     * @return Response
     */
    public function tokenAction(Request $request)
    {
        try {
            $token = $this->server->grantAccessToken($request);

            // custom code here

            return $token;
        } catch (OAuth2ServerException $e) {
            return $e->getHttpResponse();
        }
    }

}