Iam尝试实施密码编码服务,但似乎它没有工作因为我得到了#34;你已经请求了一个不存在的服务user_service"错误。
这是我的代码:
services:
user_service:
class: Morescreens\VideomanagerBundle\Service\UserService
arguments: ['@security.encoder_factory']
imports:
- { resource: "@morescreensVideomanagerBundle/Resources/config/services.yml" }
class UserService {
/**
* @var EncoderFactoryInterface
*/
private $encoderFactory;
public function __construct(EncoderFactoryInterface $encoderFactory)
{
$this->encoderFactory=$encoderFactory;
}
public function setEncodedPassword(User $user)
{
$encoder=$this->encoderFactory->getEncoder($user);
$password=$encoder->encodePassword($user->getPassword(),$user->getSalt());
$user->setPassword($password);
}
}
$user=new User();
$user->setPassword('password');
$this->get('user_service')->setEncodedPassword($user);
编辑:
我手动删除了缓存文件夹,我的服务开始工作。
答案 0 :(得分:5)
尝试查看你的src / {VendorName} / {BundleName} Bundle / DependencyInjection / {VendorName} {BundleName} Extension.php
此文件应加载您的服务定义,如下所示:
<?php
namespace Vendor\{BundleName}Bundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class Vendor{BundleName}Extension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
//Load our YAML resources
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}
这会将服务定义从src/{VendorName}/{BundleName}Bundle/Resources/config/services.yml
加载到容器中。如果仍然无效,请尝试php app/console cache:clear
。出于速度原因,symfony基本上会将您的所有服务(和其他配置文件)聚合到一个缓存文件中,因此它不必每次都读取这些文件。实际缓存服务定义的文件位于:
具有容器服务定义的相关信息的文件是: