scott
scott

Reputation: 43

Symfony2 Adding 3rd party Bundles to a Service Controller

I am looking to get some help on how to add a 3rd party bundle to an existing service controller, specifically KnpSnappyBundle https://github.com/KnpLabs/KnpSnappyBundle.

The bundle was installed with composer, and registerBundles() function in app/AppKernel.php was updated.

I am having trouble with passing the bundle to a service controller, via its constructor.

1: Unsure how to figure out the path to use

--SomeController.php file--
...
use Symfony\Component\???\???

class FormDataController
{
    ...
    private $pdf;

2: Also unsure how to figure out what the object type is named.

    public function __construct(..., KnpSnappyBundle? $pdf )
    {
        ...
        $this->pdf= $pdf;

        return $this;
    }

3: Then in the servives.yml file i add the following argument to the correct controller

- "@knp_snappy.pdf"

After doing the above, the controller should be able to do the following to access the bundles functions, correct?

$this->pdf->...

Upvotes: 2

Views: 896

Answers (1)

dmnptr
dmnptr

Reputation: 4304

Why are you doing this?

Quote:

The bundle registers two services:

  • the knp_snappy.image service allows you to generate images;
  • the knp_snappy.pdf service allows you to generate pdf files.

Just access them like this (if you use standard Symfony controller):

$this->get('knp_snappy.pdf')->...;
$this->get('knp_snappy.image')->...;

UPDATE:

If you are injecting knp_snappy.pdf service into your custom service, you should use Knp\Bundle\SnappyBundle\Snappy\LoggableGenerator class.

--SomeController.php file--
...
use Knp\Bundle\SnappyBundle\Snappy\LoggableGenerator;

class FormDataController
{
    // ... //

    private $pdf;

    public function __construct(..., LoggableGenerator $pdf )
    {
        ...
        $this->pdf = $pdf;

        return $this;
    }

    // ... //

}

When you run ./app/console container:debug command, it lists all available services and corresponding classes. Very helpful.

Upvotes: 5

Related Questions