米克說的是真的。要跟進(重寫服務),請繼續閱讀。
在app/config/services.yml
,添加以下內容:
services:
fos_oauth_server.controller.token:
class: OAuth2Bundle\Controller\TokenController
arguments: [@fos_oauth_server.server]
哪裏OAuth2Bundle\Controller\TokenController
是擴大從FOS束的一個自定義控制器。
例如:
namespace OAuth2Bundle\Controller;
use FOS\OAuthServerBundle\Controller\TokenController as BaseController;
use OAuth2\OAuth2;
use Symfony\Component\HttpFoundation\Request;
class TokenController extends BaseController {
public function tokenAction(Request $request)
{
// Do whatever you like here
$result = parent::tokenAction($request);
// More custom code.
return $result;
}
}
當然,你可以做任何你喜歡的$result
。您甚至可以省略parent::
調用並執行您自己的身份驗證來返回令牌。
EDIT
這是上述的後續。我必須訪問一些實體並在用戶收到令牌時進行一些更新。這要求我有權訪問實體管理器。在我的情況下,我必須檢查OAuth客戶端是否仍處於活動狀態;它不會被刪除,但只是暫時禁用來阻止一組用戶。
因爲控制器是一種服務,你可以添加額外的參數,如實體管理器:
服務定義現在看起來是這樣的:
fos_oauth_server.controller.token:
class: ApiBundle\Controller\OAuth\TokenController
arguments: [@fos_oauth_server.server, @doctrine.orm.entity_manager]
當然控制器現在有一個構造函數。它現在看起來像這樣:
namespace OAuth2Bundle\Controller;
use Doctrine\ORM\EntityManager;
use FOS\OAuthServerBundle\Controller\TokenController as BaseController;
use OAuth2\OAuth2;
use Symfony\Component\HttpFoundation\Request;
class TokenController extends BaseController {
/**
* @var EntityManager
*/
private $em;
/**
* @param OAuth2 $server
*/
public function __construct(OAuth2 $server, EntityManager $entityManager)
{
parent::__construct($server);
$this->em = $entityManager;
}
public function tokenAction(Request $request)
{
// Do whatever you like here
$result = parent::tokenAction($request);
// More custom code.
return $result;
}
}
如何覆蓋oauth.xml? – Jeboy
請參閱[如何覆蓋包的任何部分](http://symfony.com/doc/current/cookbook/bundles/override.html) – Mick