PHPBuilder - The PHP Fat-Free Framework: Slim Down Your PHP Development Page 2



RSS Twitter
Articles Tools

The PHP Fat-Free Framework: Slim Down Your PHP Development - Page 2

by: W. Jason Gilmore
|
April 5, 2011

Building Page Templates with PHP Fat-Free

If you were building a database which contained a great deal of information your favorite video games then it certainly won't make any sense to create one static page for each game. Instead, you'll want to create a page template which is dynamically updated based on the user's request to learn more about a particular game. You can pass variables from the route function into your page like this:

F3::route('GET /game',
  function() {
    F3::set('title', 'Super Mario Brothers');
    F3::set('publisher', 'Nintendo');
    F3::set('year', '1985');
    echo F3::serve("pages/game.html");
  }
);

The game.html file must include placeholders for each variable. For instance, you could output each of the variables defined within the route function by adding the following text to game.html:

{@title} was created by {@publisher} in {@year}.

With both pieces in place, requesting /game to the browser will result in the following string being output to the browser:

Super Mario Brothers was created by Nintendo in 1985. 

Integrating a MySQL Database with PHP Fat-Free

In order to make the video game website fully dynamic you'll need to retrieve the data from your database. This is incredibly easy to do using Fat-Free. The following example uses a dynamic title slug passed via the route to query a MySQL database table. All of this should be quite straightforward except for the AUTOLOAD-related call. This call is responsible for identifying the location of the sqldb plugin which is packaged with the Fat-Free download. If you look inside this directory you'll find a list of 20 different plugins which you can use to enhance your website's capabilities.

<!--p

require_once 'F3/F3.php';


F3::route('GET /game/@slug',
  function() {

    F3::set('AUTOLOAD','/home/wjgilmore/Downloads/fatfree-1.4.4/autoload/');

    F3::set('DB',
      array(
        'dsn-->'mysql:host=localhost;port=3306;dbname=fat_games',
        'user'=>'root',
        'password'=>'jason'
      )
    );

    $slug = F3::scrub(F3::get('PARAMS["slug"]'));

    F3::sql("SELECT title, publisher, released FROM games WHERE slug='$slug'");

    echo F3::serve("pages/game.html");
  }
);

F3::run();

The game.html template is then modified to iterate over any records returned from the query, which in the case of this particular example is exactly one:

{@game.title} was created by {@game.publisher} in {@game.released}.

Conclusion

Fat-Free provides developers with an incredibly rich set of functionality while staying true to its goal of keeping a trim waistline. If you're looking for a no-nonsense solution to building dynamic websites fast, Fat-Free is well worth a look.

About the Author

Jason Gilmore is founder of the publishing, training, and consulting firm WJGilmore.com. He is the author of several popular books, including "Easy PHP Websites with the Zend Framework", "Easy PayPal with PHP", and "Beginning PHP and MySQL, Fourth Edition". Follow him on Twitter at @wjgilmore.

« Previous Page
1
|
2

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
W. Jason Gilmore

Comment:



Comment:

(Maximum characters: 1200). You have characters left.