0

I got my some custom classes in my vendor folder which I use in my Symfony project. Now I need to access some parameters from my parameters.yml which is located in

C:\xampp\htdocs\myproject\app\config\parameters.yml

In my regular Symfony code I just do

$this->getParameter('myparameter');

and all set, but not in vendor folder. I guess I need to import some namespaces, but could not find which? Any help would be appreciated. Thank you.

UPD1 The issue was solved by adding the following code to AppBundle.php

class AppBundle extends Bundle
{
private static $containerInstance = null;

public function setContainer(\Symfony\Component\DependencyInjection\ContainerInterface $container = null)
{
    parent::setContainer($container);
    self::$containerInstance = $container;
}

public static function getContainer()
{
    return self::$containerInstance;
}
}

and then calling the container from my vendor code with the following:

use AppBundle\AppBundle;
AppBundle::getContainer()->getParameter('myparameter');

Thanks everyone for help.

Jack
  • 857
  • 14
  • 39
  • [Inject the service container](http://stackoverflow.com/questions/12056178/how-to-access-service-container-in-symfony2-global-helper-function-service) and use [`->getParamter()`](http://symfony.com/doc/current/service_container/parameters.html)? – kero Apr 20 '17 at 09:38
  • Do I need to define it as a service, or using use Symfony\Component\DependencyInjection\ContainerInterface as Container; is enough? Btw, this approach does not work saying Undefined property $container – Jack Apr 20 '17 at 09:46
  • @Jack Your updated code is exactly what **not** to do. Statically getting parameters from the container is defeating the whole purpose of the container. Your classes in the vendor folder will now be tightly coupled to the AppBundle class and the container. Please do not do this. Inject the required parameters into the class through its constructor when it's instantiated. That's what the dependency injection container is for. My answer below explains. – daviomey Apr 20 '17 at 14:17

2 Answers2

0

DependencyInjection/YourBundleExtension.php

$container->setParameter('myparameter', $config);

https://symfony.com/doc/current/create_framework/dependency_injection.html#main

After that, add this config to your service (for example)

Resources/config/services.yml

services:
your.service:
    class: App\YourBundle\Service\YourService
    arguments:
        - %myparameter%
Alexey Samara
  • 131
  • 1
  • 13
  • yes, but how can I access it from my class which is located in C:\xampp\htdocs\myproject\vendor\myotherclass\myotherclass.php? It knows nothing about Symfony container. – Jack Apr 20 '17 at 11:10
  • If you'll use Injection it will know about containers. I think there is no other ways ... you can parse yml file from your class, but this is incorrect using of Symfony – Alexey Samara Apr 20 '17 at 11:24
  • any examples how injection could be used here are welcome. Thank you – Jack Apr 20 '17 at 11:33
  • http://stackoverflow.com/questions/21523481/symfony2-creating-own-vendor-bundle-project-and-git-strategy – Alexey Samara Apr 20 '17 at 11:37
  • You can check my bundle for example: https://github.com/wow-apps/symfony-slack-bot how to use configs and how to create public bundle that will be downloadable from composer – Alexey Samara Apr 20 '17 at 11:40
0

You can either define your services in

  1. C:\xampp\htdocs\myproject\app\config\services.yml
  2. The Resources\config directory of a bundle. See How to Load Service Configuration inside a Bundle.

You simply define your services by giving them a name, specifying the class and then listing any arguments the class has to be injected into it when it is instantiated. To reference a parameter from parameters.yml you wrap the name of the parameter in %% as I did below.

services:
   my_project.some_service:
      class: Vendor\Class\Name\Here
      arguments: ['%some.parameter.name%']

In 99% of scenarios you definitely should not inject the dependency injection container into your own classes. Having your classes depend on the container is not good - have them depend on other services from your project or simple values such as strings, integers etc.

daviomey
  • 364
  • 3
  • 5