【问题标题】:Cannot override FOSOAuthServerBundle tokenAction无法覆盖 FOSOAuthServerBundle tokenAction
【发布时间】:2019-05-15 03:57:19
【问题描述】:

我正在使用以下软件包:

    "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 中使用什么模式将其指向正确的位置.

这是我的 services.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 我得到了同样的错误。

【问题讨论】:

    标签: php symfony oauth-2.0 fosoauthserverbundle


    【解决方案1】:

    原来答案是使用装饰器

    在我的 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();
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多