CSV Export with Symfony 2

Avatar
By:
Checking Credit Card

After creating a new “Posts” function of the site, ironically I encountered something that a Tutorial would serve better. Oh well, here is a brand new tutorial on creating a CSV Export feature in Symfony 2. Enjoy!

“One of the frameworks we’ve been taking a serious look at and a serious development interest in has been Symfony 2. I’m not really going to go over what Symfony 2 can do, it’s strengths and weaknesses – for now at least. But after needing to create a CSV function for a site, I found a very easy way to do it and doing it in the framework.”

 
 

One of the advantages I have working at the company I do, is using a whole lot of different frameworks and websites. (4 frameworks in 3 months, had seen part of one in my previous experience as a PHP dev) One of the frameworks we’ve been taking a serious look at and a serious development interest in has been Symfony 2. I’m not really going to go over what Symfony 2 can do, it’s strengths and weaknesses – for now at least. But after needing to create a CSV function for a site, I found a very easy way to do it and doing it in the framework. Instead I’ll be going over a very short and easy tutorial on developing a CSV Export for your Symfony 2 build.

I should note also that this pertains to retreiving a database listing, where I needed to do it was in an admin panel, but it is easily changeable if you simply have a static list or an array somewhere feeding data.

Controller Action


First up, here’s the code:

public function adminCsvAction() {

$repository = $this->getDoctrine()->getRepository('AcmeTestBundle:Test');
$query = $repository->createQueryBuilder('s');
$query->orderBy('s.id', 'DESC');

$data = $query->getQuery()->getResult(); $filename = "export_".date("Y_m_d_His").".csv";

$response = $this->render('AcmeTestBundle:Default:adminCsv.html.twig', array('data' => $data));
$response->headers->set('Content-Type', 'text/csv');

$response->headers->set('Content-Disposition', 'attachment; filename='.$filename);
return $response;
}

This contains some sample bundle names and actions, but you could easily keep the adminCsv action name and html. The first 4 lines after the function declaration is simply a query to grab data from the database, fill in with whatever you wish. After that, it get’s very simple. We’re defining a filename to save as when the browser visits the CSV page and we are rendering an html file which we’ll be creating shortly. That html file will contain the data that will be in the CSV export.

After that all we do is set the headers for the rendering of the html file so that instead of displaying, the browser knows to download the file (or prompt to download).

The Route

So the tutorial is complete, here is a sample route you can insert into your routing file:

admin_csv:
pattern: /admin/csv
defaults: { _controller: AcmeTestBundle:Default:adminCsv }

The Template File

The last step, I have a very basic (albeit for a CSV, it is basic) template setup with twig taking the data variable set in that controller action and using a for loop in twig to print out the data values along with the top header columns.

id, field1, field2, field3
{% for row in data %}
{{ row.id }},{{ row.field1 }},{{ row.field2 }},{{ row.field3 }}
{% endfor %}

See, simple Enough?

In Conclusion

If you have any questions feel free to post a comment. I know I didn’t go incredibly indepth, I wanted to limit this to a tutorial on utilizing a CSV Export function with Symfony 2, but at the same time if you aren’t decently familiar with the basics of this system – it will look like what PHP does to a non-programmer. (gibberish) It was definitely a refreshing experience finding that despite some things in this framework, the CSV capability is extremely easy and also easy to expand to whatever you need it to do, without having to download a plugin or create a whole classic and such to just generate a simple csv list of data. Enjoy!

 
 
>