Recently I wrote about the Flight PHP micro-framework. One of the challenges I ran into when launching one of our sites in it was that we had some database information that needed to be pulled out in the layout.
It was the same information on every page so it didn’t make sense to pass it in as a variable as this would have meant using the same code on every route to do the same thing. Instead, it was simpler to create a view helper which would pull out the information needed.
class Helper { public static function menuTags () { $tagModel = Flight::tagModel(); return $tagModel->findDistinct(); } }
Nothing fancy here, just a static method on a class which hooks into one of the model classes I has already registered with Flight and returns the result. I then just needed to reference this from the layout view.
<?php foreach (Helper::menuTags() as $tag) { ?> <?=htmlspecialchars($tag["tag"])?> <?php } ?>
Now it appears on every page without me having to pass it in as a variable.
Don't have time to check my blog? Get a weekly email with all the new posts. This is my personal blog, so obviously it is 100% spam free.
Tags: flight, helpers, micro framework, PHP
This entry was posted on Monday, June 18th, 2012 at 12:06 pm and is filed under Limited, Programming. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.