0

I am trying to access my configuration variables outside of the controller.

When I try:

class pdfFooter extends \TCPDF
{ 
public function footer()
{
    $config = $this->get('core_parameters'); 
}
}

I get this error:

Uncaught PHP Exception Symfony\Component\Debug\Exception\UndefinedMethodException: "Attempted to call an undefined method named "get" of class "Plugin\PrintBundle\Controller\pdfFooter".

Simply calling:

$this->writeHTMLCell($config->getParameter('heading_color_config'));

triggers it. Most other topics with this issue I've encountered suggest to globalize it. Surely there's a better way?

5
  • 1
    just inject the container, stackoverflow.com/questions/40692433/…
    – Federkun
    Nov 21, 2016 at 20:41
  • 1
    Whatever you do, don't inject the container. Instead, learn a bit about services and then inject the config object. symfony.com/doc/current/service_container.html
    – Cerad
    Nov 21, 2016 at 22:45
  • Why would injecting the container be bad? (Dumb question I'm sure, I'm new to Symfony)
    – billblast
    Nov 21, 2016 at 22:59
  • The idea is to only inject what you need. Makes things easier to test and maintain. There are a few classes like controllers and listeners where you don't know in advance what the class will end up needing and thus injecting the container is fine. But avoid it if possible.
    – Cerad
    Nov 21, 2016 at 23:27
  • try this one i hope its working fine for get configuration parameter $this->container->getParameter('core_parameters'); Nov 22, 2016 at 8:04

1 Answer 1

3

You have to inject the container so you can access services and parameters that you will need.

But, Like what @Cerad said, The main reason (Among many : No Type Hinting, No control over used services, RunTime compilation Errors, Missing dependencies, etc) why injecting the container is not a good idea is because Dependencies replacement : If service is defined in library, you wont be able to replace it dependencies with local ones filling your needs [1] .

You should avoid it if possible. Here what it looks like to inject only the parameters that you need:

(The params must be definef in advance in the config file)

services:
    yourapp.bundle.pdffooter:
        class: App\Bundle\Foo\pdfFooter
        arguments: ['%param1%','%param2%',...]

In your class:

class pdfFooter
{    
private $param1;
private $param2;
// ...

public function __construct($param1,$param2,...)
{
    $this->param1 = $param1;
    $this->param2 = $param2;
    // ...
}

public function footer()
{

    // you can access your params directly here
}
1
  • Not quite. First, the ContainerInterface does nothing for you here. Secondly, the reason for not using the container has nothing to do with size and memory usage. Consider adjusting your answer to show core_parameters being injected?
    – Cerad
    Nov 22, 2016 at 14:04

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.