我正在使用以下软件包:
"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软件包的象征方法,但我陷入了错误:
"Cannot autowire service AppControllerTokenController argument $server of method FOSOAuthServerBundleControllerTokenController::__construct() references class OAuth2OAuth2; but no such service exists. You should maybe alias this class to the existing fos_oauth_server.server service"
我尝试了几种不同的方法(Autowire,自动注射(,但是我一直回到上述错误。看来"使用oauth2 oauth2;"参考在捆绑包的TokenController中得到了正确的命名,但是当我尝试覆盖时,它无法正确解析OAuth2类位置,我不确定在课堂或服务中使用哪种模式。。
这是我的services.yaml
services:
...
AppControllerTokenController:
resource: '../src/Controller/TokenController.php'
arguments: ['@fos_oauth_server.server']
和我的自定义TokenController类
?php
namespace AppController;
use FOSOAuthServerBundleControllerTokenController as BaseController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
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 OAuth2OAuth2;
对于我的自定义TokenController,我遇到了相同的错误。
事实证明答案是使用装饰器
在我的服务中
AppControllerOAuthOAuthTokenController:
decorates: FOSOAuthServerBundleControllerTokenController
arguments: ['@fos_oauth_server.server']
我的任何自定义类都覆盖TokenController
namespace AppControllerOAuth;
use FOSOAuthServerBundleControllerTokenController as BaseController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
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();
}
}
}