2

In a Symfony 3.3 Project I defined a class at

../AppBundle/Helper/ProgramHelper

like this

class ProgramHelper implements ContainerAwareInterface
{
    use ContainerAwareTrait;
    protected $em;
    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }
}

In services.yml I added it like this

services:
    ...
    app.helper.program_helper:
        class: AppBundle\Helper\ProgramHelper
        tags:
             - { name: app.helper, alias: 'container_aware' }
        arguments: [ "@doctrine.orm.entity_manager" ]
        calls:
            - [setContainer, ['@service_container']]

Now - trying to access the class from a controller like

$ph = $this->get('app.helper.program_helper');

results in this error

ServiceNotFoundException
You have requested a non-existent service "app.helper.program_helper".

Any hint on the issue is much appreciated.

7
  • Please make sure debug mode is enabled, if production mode enabled then clear cache then refresh your page
    – Manmohan
    Feb 14, 2018 at 12:37
  • @Manmohan I did clear the cache before using cache:clear -e dev - I run the app on the URL ../app_dev.php/... which should put me in dev mode right? So - cache does not seem to be the problem. Feb 14, 2018 at 12:43
  • 1
    Add public: true to the service definition. Services are now private by default which means that you can no longer access them via get. bin/console debug:container will confirm the service is available.
    – Cerad
    Feb 14, 2018 at 13:38
  • @Cerad Thank you! That was the problem. If you post it as an answere you get the tick :-) Feb 14, 2018 at 14:01
  • It's not really much of an answer. Unless this is a big legacy project then you should consider upgrading to at least 3.4 (3.3 is no longer supported) and spend a bit of time reviewing the service container portion of the docs. Lots and lots of changes on how to use services.
    – Cerad
    Feb 14, 2018 at 14:21

1 Answer 1

4

@Cerad answered the question - thank you!

Add public: true to the service definition. Services are now private by default which means that you can no longer access them via get. bin/console debug:container will confirm the service is available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.