Error in symfony when accessing getParameter

0

I'm immersed in a project in symfony and I'm doing some tests with different bundles.

In the first CatalogBundle bundle, it is responsible for managing two entities, Producto and Proveedor with their respective form and so on. When I add a provider, I keep their image in a directory. In this bundle I add an event that has been removed by a provider supplier_deleted .

In the second FileManagerBundle bundle, you have an event listener that listens for a supplier_deleted event. If so, delete the associated image.

When I try to do it, symfony returns the error

  

Call to a member function getParameter () on null

This is the code of FileManagerBundleController

namespace FileManagerBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class FileManagerController extends Controller
{

    public function deleteSupplierImage (Supplier $supplier)
    {
        $supplierImage = $supplier->getLogo();
        unlink($this->getParameter('kernel.root_dir').'/../web/uploads/suppliers/'.$supplierImage);
    }
}

FileManagerBundle \ Resources \ config \ services.yml

services:
    FileManagerBundle.FileManagerController:
        class: FileManagerBundle\Controller\FileManagerController
    FileManagerBundle.delete_supplier_image_event_listener:
        class: FileManagerBundle\EventListener\DeleteSupplierImageEventListener
        arguments:
            - "@FileManagerBundle.FileManagerController"
        tags:
            - { name: kernel.event_listener, event: supplier_deleted, method: deleteSupplierImage }

FileManagerExtension.php

namespace FileManagerBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;

class FileManagerExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new YamlFileLoader(
            $container,
            new FileLocator(__DIR__.'/../Resources/config/')
        );
        $loader->load('services.yml');
    }
}

Any idea what can be wrong?

    
asked by Shinzu 05.03.2017 в 17:59
source

2 answers

0

Why are you injecting the driver? The driver is not injected, and in case you want to do it for some reason (first you should consider the architecture), not by extending the FrameworkBundle Controller means that $ this-> gtParameter should work.

$ this-> getParameter only works when you are in a controller and you have not defined it as a service. In case you define it as a service, you must manually inject the container "@service_container" with the setter "setContainer".

But, of course, you can not find "container".

Again, injecting the controller does not make any sense.

    
answered by 05.03.2017 в 19:48
0

In symfony 3.3 the use of 'kernel.root_dir' is depreciated and the following code should be used

$this->getContainer()->getParameter('kernel.project_dir')

I think you fail because of the lack of -> getContainer ()

    
answered by 12.10.2017 в 05:28