--- layout: default title: Examples ---

Examples

Some code snippets to boost your daily fat-free development. bla bla bla, some more cool text here.


Your Start: the index.php and folders

This is your base, your control center, the headquarter - call it whatever you want.

{% highlight php %} set('AUTOLOAD', 'inc/'); // path to my custom classes which will be autoloaded by F3 $app->set('CACHE', false); // disable caching for developement $app->set('DEBUG', 3); // get all debug infos; to turn off use 0 (zero) $app->set('GUI', 'ui/'); // path for templates $app->set('DB', new DB('mysql:host=localhost;port=3306;dbname=mysqldb', 'admin', 'p455w0rD')); // global db connection $app->route('GET /', 'main->start'); // route for our root path $app->run(); // without this, the whole page won't be displayed {% endhighlight %}

Hint: You can use F3 as a static class too by using e.g. F3::set(). But this is not adviseable and will be deprecated in future releases.

We recommend the following layout for your folder structure.

  • /
    • /index.php
    • /lib
    • /inc
    • /ui
    • /temp (auto-generated)
    • /cache (auto-generated)

Still at the Start: Routing

The routing engine is a core feature of F3. Easy to use and minimal preparation to work with it.

{% highlight apache %} RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-l RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php [L,QSA] {% endhighlight %}

This is the core part of F3's routing engine. As you may recognize. This is no PHP or HTML code. You have to put this into your .htaccess file in your application folder. Don't forget to modify RewriteBase

E.g. your app is on http://example.org/myf3app, than you have to modify your .htaccess the following way:

{% highlight apache %} ... RewriteBase /myf3app/ ... {% endhighlight %}

Got it? Since you're done with the setup part for your routing engine, you can use F3's integrated routing engine which gives you URLs like /news/article/412. Do define normal routes, you have to use this:

{% highlight php startinline %} $app->route('GET /news/article/@id', 'news->showArticle'); {% endhighlight %}
  • GET indicates, we have a normal request. Others are POST, PUT, HEAD etc.
  • /news/article/@id we defined a variable called "id" which can be retrieved by get() everywhere your code
  • news->showArticle "news" is our PHP class which will be autoloaded (see your AUTOLOAD value) by F3 and afterwards its showArticle function will be called.

F3 loads all your files automatically. Class- and file name has to be same! In this case, it's our inc/news.php

Building the App

You know the basics, now add your own stuff.

Contribute!

Form Validation

External code in your app? No way! Use the inbound form validation to make your app more secure.

Contribute!

Login

How could a basic Login Class look like?

Contribute!

AJAX

Simple example of form post via AJAX

template has form with action and id {% highlight html %}
{% endhighlight %} template has javascript with ajax functions {% highlight javascript %} /* jQuery ajax function * post using the comment form & get back response from server & display on page. */ $(function() /* anonymous func */ { /* capture the form submit event */ $("#forumPostingForm").submit(function(event) { /* stop form from submitting normally */ event.preventDefault(); /* setup post function vars */ var targetUrl = $(this).attr('action'); var formData = $(this).serialize(); /* send the data using post and put the results in a div with id="result" */ /* post(url, postcontent, callback, datatype returned) */ var request = $.post( targetUrl, formData, formpostcompleted, "json" ); // end post function function formpostcompleted(data, status) { /* write server response to console for troubleshooting */ console.log(data); console.log(status); } }); // end submit function } {% endhighlight %} php function called by route does what ever you need, then responds with json formatted object {% highlight php startinline %} // return response using JSON for AJAX F3::route('POST /discuss/forums/@forum_id/@forum_name_uri/topics/@topic_id/@topic_subject_uri', function(){ // create assoc array object $return_array = array(); // set values $return_array['id'] = $posting->id; $return_array['parent_id'] = $posting->parent_id; $return_array['user_id'] = $posting->author_id; $return_array['author_name'] = $posting->author_name; $return_array['author_location'] = $posting->author_location; $return_array['posting_datetime'] = date('D F j, Y, g:i a'); $return_array['body_text'] = $posting->body_text; // send to browser as json encoded object echo json_encode($return_array); }); {% endhighlight %}