2
所以通常我可以做類似訪問用戶管理器中的控制單元,如何在Symfony中使用FosUserBundle作爲服務訪問用戶管理器?
$this->get('fos_user.user_manager');
但是我已經需要訪問這一項服務。所以這是我做過什麼:
在我config.yml文件:
fos_user:
db_driver: orm
firewall_name: main
user_class: Main\UserBundle\Entity\User
services:
userbundle_service:
class: Main\UserBundle\Controller\UserBundleService
arguments: [@fos_user]
在我UserBundleService.php「:
<?php
namespace Main\UserBundle\Controller;
use FOS\UserBundle\Entity\User;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Bridge\Monolog\Logger;
class UserBundleService
{
protected $securityContext;
public function __construct(User $user)
{
$this->user = $user;
}
public function validateRequest(){
$userManager = $this->container->get('fos_user.user_manager');
$this ->logger->warn("user is : ".$userManager);
exit;
}
}
我得到的錯誤是:
ServiceNotFoundException: The service "userbundle_service" has a dependency on a non-existent service "fos_user".
所以接下來我要做的就是將fos_user作爲服務移動到我的config.yml中:
services:
userbundle_service:
class: Main\UserBundle\Controller\UserBundleService
arguments: [@fos_user2]
fos_user2:
user_class: Main\UserBundle\Entity\User
我得到的錯誤:
RuntimeException: The definition for "fos_user2" has no class. If you intend to inject this service dynamically at runtime, please mark it as synthetic=true. If this is an abstract definition solely used by child definitions, please add abstract=true, otherwise specify a class to get rid of this error.
ma nager被命名爲'fos_user.user_manager',所以你必須把整個兩個單詞放在你的參數中:''參數:[@ fos_user.user_manager]''userbundle_service'。 – Sgoettschkes 2012-08-04 17:55:23