Search code examples
phpsymfonysymfony-2.6

Can I reach "/web" path from within a container in Symfony2?


I need to save a file in /web/import path and I am asking which is the best way to achieve this. I have a class defined in /Service/SyncController.php and it extends from Controller:

class SyncController extends Controller { ... } 

That class is defined as a service also:

sync.service:
    class: PDI\PDOneBundle\Service\SyncController
    arguments: ["@service_container", "@doctrine.orm.entity_manager", "@request_stack", "%kernel.root_dir%"]

Do I need to pass the argument "%kernel.root_dir%" in order to access web path as $this->webRoot = realpath($rootDir . '/../web') or I can do it from @service_container? Which is the right way to get the path to /web/import in order to save files in there?


Solution

  • we use this configuration to do just this.

    in parameters.yml

    upload_path: "/images/uploads"
    upload_dir: "%kernel.root_dir%/../web%upload_path%"
    

    we split path from dir just for simplicity of reading.

    in the controller:

    $work_dir = $this->container->getParameter('upload_dir');