Beginners Guide

Welcome to the Swift PHP beginners guide. This guide will explain all of the basic functionality of Swift PHP and show you how to create a simple web application.

Step 1: Downloading and Installing Swift PHP

First, you must download a copy of Swift PHP. Once downloaded, you can unzip and begin using it. You must place the all files into your root web folder. Generally, when using Apache this will be the 'htdocs' or 'public_html' directory. You may place all of the files directly into your root web folder if you intend to host it at your domain's root (http://mydomain.com). Otherwise, you may place the files into a sub-directory (http://mydomain.com/Swift-PHP).

Step 2: Configuring the .htaccess File

The .htaccess file is used by Apache to determine different rules for displaying your website's files. By default, Swift PHP comes with a .htaccess file configured to clean up URL's, such as removing the 'www' prefix, removing extra slashes (Swift-PHP//home), and adding a single trailing slash (Swift-PHP/home/). After this it sends the user's request to the index.php file for Swift PHP to handle.

The only setting you may need to change in your .htaccess file is at the top where it says:

# Specify our directory for rewrites
RewriteBase /

The RewriteBase needs to be set to the root location of your website. For example, if my website was at (http://mydomain.com/) then I would leave .htaccess as it currently is. If I was to host my website in a sub-directory (http://mydomain.com/Swift-PHP/), I would change it to be like below:

# Specify our directory for rewrites
RewriteBase /Swift-PHP/

You essentially want to change this to match the directory where your Swift PHP's index.php file is located at.

Step 3: Setup the index.php File

By default, the .htaccess file discussed above will direct all requests to the index.php file. This is the entry point into your web application. Inside the index.php file we will load Swift PHP into our application, configure Swift PHP for our website, and the finally run Swift PHP.

Include Swift PHP

By default, you should be able to include Swift PHP class by using the file path provided below ('Swift/Swift.php'). However, if you move the 'Swift' directory somewhere else, then you may need to update the file path below.

require_once 'Swift/Swift.php';

Get an instance of Swift PHP

The core of the Swift PHP Framework is the Swift static class. Here we simply get an instance of this class by calling the Swift::getInstance() static function.

$swift = Swift::getInstance();

Configure your Swift PHP app

By calling the config() function using our Swift PHP object we can set the first param to be the setting we want to change and optionally add a second parameter to assign a value to this setting.

$swift->config('app_url', 'http://localhost/');
$swift->config('app_view_dir', 'view');


Add routes to Swift PHP

Before running the Swift PHP Framework, we need to define some URL routes for our application. We use the map() function to associate a specific request URI or regular expression with a callback function. If the user enters a request that matches the regular expression, then it will be stored and accessible with the $swift->param($key) function, where $key is the index of all matches.

$swift->map('/', loadHomePage);
$swift->map('/user/([a-bA-Z0-9_\-]+)/', loadUserPage);


Create functions for loading pages

Create functions for rendering our pages that we referenced above when mapping our URI's to a callback function.

function loadHomePage() {
global $swift;
$swift->render('home.php');
}

function loadUserPage() {
global $swift;
$data['username'] = $swift->param(0);
$swift->render('user.php', $data);
}


Run Swift PHP

Lastly, we run the Swift PHP Framework by calling the run() function. This function should be called last and is responsible for executing the application using the configuration and routes defined above.

$swift->run();

Step 4: Creating the View

Now, we must create and setup the files needed to load our web application's view. Earlier, we created a function to load our home page and a user page. Inside these functions, we call the render() function and pass it the names of the files we want loaded from our view directory. The default directory for our view files is located in the 'view' directory but we may modify this by changing the 'app_view_dir' setting. The initial setup of Swift-PHP comes with these files in the view directory which you can review.

Step 5: Visiting our Web Application

You are now finished setting up your web application. If you did not modify any of the default settings or views that come with Swift PHP you should be able to visit the pages below in your web browser.

http://localhost/ (Our home page)
http://localhost/user/Derek/ (A user page)
http://localhost/user/Colleen/ (Another user page)