Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

You have requested a non-existent service "user_service"

Iam trying to implement service for encoding password but it seems it doesn't work cause I get "You have requested a non-existent service user_service " error.

Here is my code :

Vendor/BundleNameBundle/Resources/config/services.yml

services:
    user_service:
         class:  Morescreens\VideomanagerBundle\Service\UserService
         arguments: ['@security.encoder_factory']

app/config/config.yml

imports:
    - { resource: "@morescreensVideomanagerBundle/Resources/config/services.yml" }

code for my service

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);
   }

}

Inside mine controller:

 $user=new User();
        $user->setPassword('password');

        $this->get('user_service')->setEncodedPassword($user);

EDIT:

I manually deleted cache folder and my service started to work.

like image 434
harisK92 Avatar asked Jul 02 '14 00:07

harisK92


1 Answers

Try looking at your src/{VendorName}/{BundleName}Bundle/DependencyInjection/{VendorName}{BundleName}Extension.php

This file should load your service definitions like this:

<?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');
    }
}

This will load the service definition from src/{VendorName}/{BundleName}Bundle/Resources/config/services.yml into the container. If that still doesn't work try doing php app/console cache:clear. For speed reasons, symfony will basically aggregate all your service (and other configuration files) into a cached file so it doesn't have to read those files every time. The files that actually cache the service definitions are located:

  • app/cache/{envName}/ (The cache directory can be configured to be located wherever you want, this is just the default).

The files that have the relevent info for container service definitions are:

  • app{EnvName}DebugProjectContainer.php
  • app{EnvName}DebugProjectContainer.php.meta
  • app{EnvName}DebugProjectContainer.xml
like image 54
echochamber Avatar answered Nov 15 '22 08:11

echochamber