Symfony2 Define Media Uploaded Directory in Config file and access it it in controller twig

509 Views Asked by At

In my project i have lot of different types of document of user to be uploaded. I am using local drive to save media and ony save the file. What it want is to define the uploaded directory in only configuration file (category wise) access it in controller and twig files. so that if i changed the media path later, i have to change it only once and should work everywhere?

I know lot bundle available for this, but i am not using any bundle I used doctrine to upload this media. More, later i also move this media to Amazon S3.

1

There are 1 best solutions below

3
scoolnico On

You can do it directly in parameters.yml:

parameters:
    /.../
    media_directory: '%kernel.root_dir%/../web/uploads'

And you can have access in controller like all other parameters:

public function someAction() 
{
    $directory = $this->getParameter('media_directory');
}

EDIT

For twig, try like that:

<p>Media Directory: {{ media_directory }}</p>

Before, I think you have to declare twig global var in app/config/config.yml:

# Twig Configuration
twig:
    globals:
        media_directory: %media_directory%