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;

  • sl.rReddit
  • del.icio.us
  • co.mments
  • Ma.gnolia

9 Responses to “A Symfony Quickie: Finding a File”

  1. NiKo Says:

    Nice initiative, it’s fun and efficient :)

  2. gasper_k Says:

    and short :) . This is very important, because I rarely watch a video over 1min long.

  3. Krof Drakula Says:

    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.”

  4. gasper_k Says:

    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. :)

  5. Krof Drakula Says:

    True, but where’s the Web-2.0-ishness in that? :) It’s all about who’s got the screencasts and who doesn’t.

  6. gasper_k Says:

    … no matter the productivity, eh? You lose your time making the screencast, we lose ours watching it, and the work gets done itself. :D

    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.

  7. Krof Drakula Says:

    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.

  8. gasper_k Says:

    yay! :D

  9. Chat Says:

    nice work :)

Leave a Reply