A Symfony Quickie: Finding a File
It’s been a while, but I’ve finally made the plunge and did the first in the series of cookbook-related screencast tutorials for Symfony.
Edit: here’s the gist of the video.
== The sfFinder class ==
Symfony comes bundled with the sfFinder class that lets you conveniently search for files and directories. It has a simple fluent interface that lets you tell what you’re looking for and where to look for it:
sfFinder::type('file')->name('*.jpg')->in(sfConfig::get('sf_upload_dir'));
To illustrate this example, let’s use an action that lists the contents of the upload folder. Inside an action, we assign the array returned by sfFinder to the template:
$this->images = sfFinder::type('file')->name('*.jpg')->relative()->in(sfConfig::get('sf_upload_dir'));
Since, by default, sfFinder returns absolute paths, we tell it to return everything relative to the search directory - this will include subdirectories as well.
In a template, we can now iterate over the images to list them:
foreach($images as $image):
echo ‘<li>’ . image_tag(’/uploads/’ . $image); . ‘</li>’;
endforeach;




June 22nd, 2008 at 7:08 am
Nice initiative, it’s fun and efficient
June 22nd, 2008 at 10:55 am
and short
. This is very important, because I rarely watch a video over 1min long.
June 22nd, 2008 at 11:03 am
Yeah, I wanted to avoid being long-winded. Nobody has time for long tutorials - it’s mostly “I have a task to perform, not learn how to operate a concrete mixer truck.”
June 22nd, 2008 at 12:34 pm
But, come to think about it, this would be way more efficient in a form of one-liner and two screenshots. It is a one-liner and two screenshots (before and after), maybe three (a screenshot of the directory), would be quite enough.
June 22nd, 2008 at 1:13 pm
True, but where’s the Web-2.0-ishness in that?
It’s all about who’s got the screencasts and who doesn’t.
June 22nd, 2008 at 7:24 pm
… no matter the productivity, eh? You lose your time making the screencast, we lose ours watching it, and the work gets done itself.
But honestly, sometimes I prefer the old way. I hate funny pics slideshow glued together in a video … Give a document where I can click only those that seem interesting.
June 22nd, 2008 at 9:13 pm
Funny though, it got more traffic than any of my previously outlined posts.
But there is merit to have it transcribed, at least is SEO terms if not for usability - I’ll include the transcript and code with screenshots next time.
June 23rd, 2008 at 8:34 am
yay!
August 22nd, 2008 at 2:38 pm
nice work