首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法覆盖FOSOAuthServerBundle tokenAction

无法覆盖FOSOAuthServerBundle tokenAction
EN

Stack Overflow用户
提问于 2019-05-14 02:28:32
回答 1查看 244关注 0票数 1

我正在使用以下包:

代码语言:javascript
复制
    "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方法,但是遇到了这个错误:

代码语言:javascript
复制
"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"

我尝试了几种不同的方法(自动线,自动注入),但我总是回到上面描述的错误上来。似乎"use OAuth2\ OAuth2 ;“引用在捆绑包的TokenController中的名称空间是正确的,但是当我尝试覆盖它时,它无法正确地解析OAuth2类的位置,并且我不确定在类或services.yaml中使用什么模式来将它指向正确的位置。

这是我的services.yaml

代码语言:javascript
复制
services:

    ...

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

和我的自定义TokenController类

代码语言:javascript
复制
?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;
    }

}

如果我试着做一些显而易见的事情,加上下面这行

代码语言:javascript
复制
use OAuth2\OAuth2;

对于我的自定义TokenController,我得到了同样的错误。

EN

回答 1

Stack Overflow用户

发布于 2019-05-15 03:57:19

事实证明答案是使用装饰器

在我的services.yaml

代码语言:javascript
复制
    App\Controller\OAuth\OAuthTokenController:
        decorates: FOS\OAuthServerBundle\Controller\TokenController
        arguments: ['@fos_oauth_server.server']

用于覆盖TokenController的任何自定义类

代码语言:javascript
复制
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();
        }
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56117974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档