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.

user3440145
  • 793
  • 10
  • 34
  • Please make sure debug mode is enabled, if production mode enabled then clear cache then refresh your page – Manmohan Feb 14 '18 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. – user3440145 Feb 14 '18 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 '18 at 13:38
  • @Cerad Thank you! That was the problem. If you post it as an answere you get the tick :-) – user3440145 Feb 14 '18 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 '18 at 14:21
  • @Cerad well - it solved my problem. So thanks. I will consider upgrading to 3.4 – user3440145 Feb 14 '18 at 16:57
  • Why you bother creating service definitions? Autowiring works great in modern Sf. – Mike Doe Feb 14 '18 at 17:11

1 Answers1

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.

user3440145
  • 793
  • 10
  • 34