![](/img/trans.png)
[英]How to override TokenController for FOSOAuthServerBundle?
[英]Cannot override FOSOAuthServerBundle tokenAction
我正在使用以下软件包:
"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,我得到相同的错误。
原来的答案是使用装饰器
在我的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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.