Draco
Draco

Reputation: 460

Function as service in Symfony2

I am storing a string in database that I want to access from various places in my application. I figure out that the best solution will be create a function that is taking that string from database and register it as a service.

Function:

public function shopUrlAction()
{ 
  return new Response($this->getDoctrine()->getRepository('AppBundle:Settings')->find(1)->getName());
}

service.yml

services:
  app.default_controller:
    class: AppBundle\Controller\DefaultController

output in other controller:

$return['base_url'] = $this->forward('app.default_controller:shopUrlAction');

Unfortunately I am constantly getting

CRITICAL - Uncaught PHP Exception Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: "You have requested a non-existent service "app.default_controller"." at /app/bootstrap.php.cache line 2099 Context: {"exception":"Object(Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException)"}

I've cleared cache.

Upvotes: 0

Views: 169

Answers (1)

Michael Sivolobov
Michael Sivolobov

Reputation: 13340

As I see from your question you have service.yml instead of services.yml (in plural form).

You should include your service.yml in main config.yml in imports section or use standard path to it (AppBundle/Resources/config/services.yml)

Upvotes: 1

Related Questions