So, this is not the first time I am creating the service but I just can't resolve the error
You have requested a non-existent service "global_settings".
Steps I took to ensure service is properly setup:
My AppBundleExtension.php
namespace AppBundle\DependencyInjection; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Loader; class AppBundleExtension extends Extension { public function load(array $configs, ContainerBuilder $container) { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('settings.xml'); } }
My settings.xml
<?xml version="1.0" encoding="UTF-8" ?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> <services> <service id="global_settings" class="AppBundle\Services\GlobalSettings"> <call method="setEntityManager"> <argument type="service" id="doctrine.orm.default_entity_manager" /> </call> </service> </services> </container>
My GlobalSettings
service
namespace AppBundle\Services; use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityRepository; class GlobalSettings { /** * @var EntityManager */ protected $em; /** * @var EntityRepository */ protected $repo; public function setEntityManager(EntityManager $em) { $this->em = $em; $this->repo = null; } /** * @return array with name => value */ public function all() { return $this->$this->getRepo()->findAll(); } /** * @param string $name Name of the setting. * @return string|null Value of the setting. * @throws \RuntimeException If the setting is not defined. */ public function get($name) { $setting = $this->$this->getRepo()->findOneBy(array( 'name' => $name, )); if ($setting === null) { throw $this->createNotFoundException($name); } return $setting->getValue(); } /** * @param string $name Name of the setting to update. * @param string|null $value New value for the setting. * @throws \RuntimeException If the setting is not defined. */ public function set($name, $value) { $setting = $this->$this->getRepo()->findOneBy(array( 'name' => $name, )); if ($setting === null) { throw $this->createNotFoundException($name); } $setting->setValue($value); $this->em->flush($setting); } /** * @return EntityRepository */ protected function getRepo() { if ($this->repo === null) { $this->repo = $this->em->getRepository('AppBundle:Settings'); } return $this->repo; } /** * @param string $name Name of the setting. * @return \RuntimeException */ protected function createNotFoundException($name) { return new \RuntimeException(sprintf('Setting "%s" couldn\'t be found.', $name)); } }
Then inside my controller I trying to access the service using the following code
$data = $this->get('global_settings')->get('paypal_email');
What am I doing wrong? Any help will be really appreciate as I am out of idea.
The reason why I kept getting this error was that my default setting for services was public: false
So to fix that I needed to set the public
property to true
for my service
services: # default configuration for services in *this* file _defaults: # automatically injects dependencies in your services autowire: true # automatically registers your services as commands, event subscribers, etc. autoconfigure: true # this means you cannot fetch services directly from the container via $container->get() # if you need to do this, you can override this setting on individual services public: false my_service: class: AppBundle\Service\MyService public: true
You wrote:
Steps I took to ensure service is properly setup
My
AppBundleExtension.php
And:
I know
AppBundleExtension
is not loading, what do I need to do to load it? What am I missing?
So it was clear that the AppBundleExtension
class was not loaded.
According to the official documentation you should remove the Bundle
in the file name and class name:
The name is equal to the bundle name with the
Bundle
suffix replaced byExtension
(e.g. the Extension class of the AppBundle would be calledAppExtension
and the one for AcmeHelloBundle would be calledAcmeHelloExtension
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With