Peter
Peter

Reputation: 9143

How to use relative paths in symfony2?

This is going to be a short, nevertheless complicated question. Let me try and explain what I am trying to accomplish.

I've got a config file config.yml

# src/Company/HappyBundle/Resources/config/config.yml
company_happy:
    import:
        attachments_path: "@CompanyHappyBundle/Resources/public/attachments"

I'm trying to get the attachments_path parameter in a Command. I do this using the following line:

$this->container->getParameter('company_happy.import.attachments_path');

Now, how can I convert @CompannyHappyBundle to the actual path?

Update

If you have any information on better ways to do this, this is always welcome. What I'm trying to do is to save attachments to the bundle's public folder.

Upvotes: 1

Views: 2715

Answers (2)

jcroll
jcroll

Reputation: 7165

Set a value in your parameters.yml as following:

# parameters.yml

parameters:
    company_happy.import.attachments_path: "@CompanyHappyBundle/Resources/public/attachments"

Then call it the same in your controller as you currently are.

Upvotes: 0

Michael Sivolobov
Michael Sivolobov

Reputation: 13340

You don't need to store your attachments at the bundle's home directory. Also you have wrong definition for parameter. All parameters in Service container must be located under parameters section.

As you can see here you need to create Form with file field. Then fill this form with Request data and then use move() method upon it to move file in any location you need. The most popular place for it - web/uploads directory.

public function uploadAction()
{
    $form = $this->createFormBuilder()
        ->add('attachment', 'file')
        ->getForm();

    if ($form->isValid()) {
        $dir = $this->container->getParameter('kernel.root_dir').'/../web/uploads';
        $someNewFilename = '...';

        $form['attachment']->getData()->move($dir, $someNewFilename);

        // ...
    }

    // ...
}

Upvotes: 1

Related Questions