{"id":1309,"date":"2018-01-19T19:22:41","date_gmt":"2018-01-19T19:22:41","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/the-php-fat-free-framework-slim-down-your-php-development\/"},"modified":"2018-01-19T19:24:41","modified_gmt":"2018-01-19T19:24:41","slug":"the-php-fat-free-framework-slim-down-your-php-development","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/the-php-fat-free-framework-slim-down-your-php-development\/","title":{"rendered":"The PHP Fat-Free Framework: Slim Down Your PHP Development"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By W. Jason Gilmore<\/div>\n<div class=\"\">on April 5, 2011<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<p>The Perl community has long laid claim to the motto &#8220;<a href=\"http:\/\/en.wikipedia.org\/wiki\/There%27s_more_than_one_way_to_do_it\" target=\"_blank\">There&#8217;s more than one way to do it<\/a>&#8220;. As a long time member of the PHP community, I often wonder whether our motto should be, &#8220;There&#8217;s more than ten ways to do it.&#8221; The number of competing PHP projects can be staggering at times, as is evidenced by the ten lightweight frameworks I introduced in last year&#8217;s article, <a href=\"http:\/\/www.developer.com\/lang\/php\/article.php\/3878476\/Top-10-Lightweight-Frameworks-for-PHP-Development.htm\" target=\"_blank\">Top 10 Lightweight Frameworks for PHP Development<\/a>. While sorting through such a broad selection of solutions can at times be overwhelming, the advantage is that with some patience you will eventually come across a solution which perfectly suits your particular tastes.<\/p>\n<p>While I argue that you should consider using a framework for practically every new project you tackle, precisely which framework you choose isn&#8217;t so clear-cut. For instance, robust solutions such as the <a href=\"http:\/\/framework.zend.com\/\" target=\"_blank\">Zend Framework<\/a> and <a href=\"http:\/\/www.symfony-project.org\/\" target=\"_blank\">symfony<\/a> offer every imaginable feature one could want in a framework, but come at the cost of a fairly significant learning curve. If you&#8217;re keen to start capitalizing upon the great framework-oriented features you&#8217;ve heard so much about but aren&#8217;t yet ready to invest the significant time required to master one of the more involved solutions, consider investigating one of the lightweight solutions such as the <a href=\"http:\/\/fatfree.sourceforge.net\/\" target=\"_blank\">PHP Fat-Free Framework<\/a>.<\/p>\n<p>In this article I&#8217;ll show you just how easy it is to get started building Web sites using this deceptively tiny (55kB) framework.<\/p>\n<p>Incidentally, Fat-Free takes advantage of several features available only to PHP 5.3.0 and newer, therefore if you haven&#8217;t yet taken the time to upgrade your PHP installation, you&#8217;ll need to do so in order to follow along with the examples found in this tutorial.<\/p>\n<h2>Introducing the PHP Fat-Free Framework<\/h2>\n<p>The PHP Fat-Free Framework manages to pack an incredible set of features into an incredibly small code base, offering native support for caching, bandwidth throttling, and a template engine. A suite of plugins further enhance Fat-Free&#8217;s capabilities, providing developers with the opportunity to integrate databases including MongoDB, process forms, generate CAPTCHAs, and manage user authentication, among other features.<\/p>\n<p>To follow along with the ensuing examples, download <a href=\"http:\/\/fatfree.sourceforge.net\/\" target=\"_blank\">Fat-Free<\/a> and place the unarchived files within a directory accessible by your Web server. You&#8217;ll also need to rename the <code>htaccess<\/code> file included with the download, placing the required period in front of the file name (<code>.htaccess<\/code>). You&#8217;ll also need to enable Apache&#8217;s mod_rewrite module if it isn&#8217;t already enabled, as Fat-Free uses mod_rewrite to map the website&#8217;s various routes to their respective handlers. I also suggest renaming the sample <code>index.php<\/code> file to something like <code>index.php-orig<\/code>. The <code>index.php<\/code> file plays a special role within Fat-Free websites, as it is where you&#8217;ll define the website routes and associated route handlers.<\/p>\n<h2>Using Clean URLs with PHP Fat-Free<\/h2>\n<p>Fat-Free might be used to implement a website consisting of solely static HTML pages and requiring little more than clean URLs. For instance, the following <code>index.php<\/code> file defines three routes (homepage, <code>\/about<\/code>, and <code>\/contact<\/code>), rendering a static page in response to each respective route request. Notice the use of PHP 5.3&#8217;s powerful <a href=\"http:\/\/php.net\/manual\/en\/functions.anonymous.php\" target=\"_blank\">anonymous function<\/a> feature, which can significantly reduce the amount of code you&#8217;d have to otherwise write.<\/p>\n<div class=\"example\">\n<pre><code>&lt;!--p\n\nrequire_once 'F3\/F3.php';\n\nF3::route('GET \/about',\n  function() {\n    echo F3::serve('pages\/about.html');\n  }\n);\n\nF3::route('GET \/contact',\n  function() {\n    echo F3::serve('pages\/contact.html');\n  }\n);\n\nF3::route('GET \/', \n  function() {\n    echo F3::serve('pages\/index.html');\n  }\n);\n\nF3::run();<\/code><\/pre>\n<\/div>\n<p>Save this file in your project home directory as <code>index.php<\/code>, create a directory called <code>pages<\/code> which contains the three respective HTML pages, and you&#8217;re done.<\/p>\n<h2>Creating Dynamic Pages with PHP Fat-Free<\/h2>\n<p>Of course, a framework&#8217;s advantages really become apparent when creating dynamic pages. You can identify parts of a route as being dynamic by defining a token within the route definition. For instance, the previous <code>index.php<\/code> file can be refactored to define a dynamic token parameter called <code>@page<\/code> which could subsequently be used to retrieve the desired page:<\/p>\n<div class=\"example\">\n<pre><code>&lt;!--p\n\nrequire_once 'F3\/F3.php';\n\nF3::route('GET \/', \n  function() {\n    echo F3::serve('pages\/index.html');\n  }\n);\n\nF3::route('GET \/@page',\n  function() {\n    $page = F3::get('PARAMS[\"page\"]');\n    echo F3::serve(\"pages\/{$page}.html\");\n  }\n);\n\nF3::run();<\/code><\/pre>\n<\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">1<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"Jason_Gilmore040520114658.html?page=2\">2<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"Jason_Gilmore040520114658.html?page=2\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The lightweight PHP Fat-Free Framework packs an incredibly large set of features into an incredibly small code<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1309","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1309"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1309\/revisions"}],"predecessor-version":[{"id":3199,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1309\/revisions\/3199"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}