我正在使用以下包:
"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"我尝试了几种不同的方法(自动线,自动注入),但我总是回到上面描述的错误上来。似乎"use 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,我得到了同样的错误。
发布于 2019-05-15 03:57:19
事实证明答案是使用装饰器
在我的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();
}
}
}https://stackoverflow.com/questions/56117974
复制相似问题