简体   繁体   中英

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

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.

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%

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM