2016-01-11 46 views
3

我正在嘗試爲Sulu CMF前端進行用戶註冊。 我想散列密碼。但出於任何原因,我無法獲得該服務。訪問Sulu前端的安全服務:散列用戶密碼

You have requested a non-existent service "security.encoder_factory". 

或者

You have requested a non-existent service "security.password_encoder" 

如果我要

app/console container:debug | grep encode 

兩種服務都是有

$ app/console container:debug | grep encode      
263: security.encoder_factory              Symfony\Component\Security\Core\Encoder\EncoderFactory          
266: security.password_encoder              Symfony\Component\Security\Core\Encoder\UserPasswordEncoder         

你對我有什麼提示? 我的控制器是知道的集裝箱和我的代碼如下:

$this->container->get('security.password_encoder') 

繼承人一些從conainer輸出:調試

$ app/console container:debug security.password_encoder 
[container] Information for service security.password_encoder 
Service Id  security.password_encoder 
Class   Symfony\Component\Security\Core\Encoder\UserPasswordEncoder 
Tags    - 
Scope   container 
Public   yes 
Synthetic  no 
Lazy    no 
Synchronized  no 
Abstract   no 


$ app/console container:debug security.encoder_factory 
[container] Information for service security.encoder_factory 
Service Id  security.encoder_factory 
Class   Symfony\Component\Security\Core\Encoder\EncoderFactory 
Tags    - 
Scope   container 
Public   yes 
Synthetic  no 
Lazy    no 
Synchronized  no 
Abstract   no 

我目前安裝的symfony的版本是v2.6.12

sulu提供的命令使用這種方法。它適用於環境,開發和產品。

/** 
* Encodes the given password, for the given password, with he given salt and returns the result. 
* 
* @param $user 
* @param $password 
* @param $salt 
* 
* @return mixed 
*/ 
private function encodePassword($user, $password, $salt) 
{ 
    /** @var PasswordEncoderInterface $encoder */ 
    $encoder = $this->getContainer()->get('security.encoder_factory')->getEncoder($user); 

    return $encoder->encodePassword($password, $salt); 
} 
+0

我在打電話給控制器。在那裏我擴展了Symfony \ Bundle \ FrameworkBundle \ Controller \ Controller(它擴展了ContainerAware)。 – psren

+0

你使用哪個Symfony版本?你可以顯示'php app/console容器的輸出:debug security.password_encoder'和'php app/console container:debug security.encoder_factory'嗎? – xabbuh

+0

感謝您的回答。我在上面添加了更多細節。 – psren

回答

2

正如xabbuh已經在評論中已經指出的那樣,我們在蘇祿兩個不同的內核。如果您使用app/webconsole命令,則您正在使用網站的內核。

在標準安裝中,我們不包括SecurityBundle,因爲它增加了一些開銷,並且對簡單網站不是必需的。但是,如果您正在構建更復雜的應用程序,則可能還需要保護您的網站。在這種情況下,您可以簡單地將Symfony\Bundle\SecurityBundle\SecurityBundle的新實例添加到app/WebsiteKernel$bundles陣列中。之後還會有您要求的服務。

1

蘇魯CMF有名爲app /控制檯Web控制檯額外的控制檯應用程序,它使用一個不同的內核:-)

我搜索服務沒有存在那裏。 相反,我能夠使用

$userManager = $this->container->get('sulu_security.user_manager'); 
+0

這是一個可能的解決方案,取決於你想要做什麼。將增加另一個答案來實現你實際要求的。 –

+0

謝謝你的回答(全部)。 A現在已經完成了我的任務。我的問題是我沒有看到SULU有不同的內核:-) – psren