Skip to content
Matija Folnovic edited this page Sep 22, 2010 · 10 revisions

Getting Started

Download & Install

First, you need to have php 5.x installed and configured.

In production, it is recommended for you to run APC (Alternative PHP Cache) because it is also used internally for all sorts of caching, like caching configuration files. Next, you need to download Swift Framework. Currently, you can only download it using git, since it is still in heavy development. So, run:

git clone git://github.com/mfolnovic/swift.git

Now you have it, you can try our hello world application by opening your favourite browser and go to:

http://localhost/path_to_swift/

You should see Hello and welcome to Swift!

MVC (Model-View-Controller)

In Swift, we are using MVC pattern, which speeds up a development a lot. Application is divided in three parts: models, controllers and view.

Controller

We start from controllers since they use models, and they are run before views. Controllers are in app/controllers/ and named welcome.php if you'd like to have controller Welcome. Let's see how it would look:

    <?php
    class WelcomeController extends Controller_Base {
        function __construct() {
            parent::__construct();
        }

        function index() {
            echo "Hello world";
        }
    }
    ?>

So first, we create new class called WelcomeController that inherits Controller_Base. Function __construct is optional in this case, but later, we'll see how we use it. All other function are actions. If we go to url: http://localhost/path_to_swift/welcome/index, it would run method index declared in class WelcomeController.

Let's see common use of this approach. This is how Blog controller would look like:

    <?php
    class BlogController extends Controller_Base {
        function index() {
            $this -> posts = model('post'); // OR
            $this -> posts = model('post') -> all(); // later, we'll explain what's difference
        }
        
        function show() {
            $this -> post = model('post') -> find_by_id($this -> data['id']);
        }
        
        function new() {
            $this -> post = model('post');
        }
        
        function create() {
            $this -> post = model('post') -> values($this -> data['post']);
            
            if($this -> post -> save()) {
                $this -> redirect('blog/show/' . $this -> post -> id);
            } else {
                $this -> render('blog/new');
            }
        }
        
        function edit() {
            $this -> post = model('post') -> find_by_id($this -> data['id']);
        }
        
        function update() {
            $this -> post = model('post') -> find_by_id($this -> data['id']) -> values($this -> data['post']);
            
            if($this -> post -> save()) {
                $this -> redirect('blog/show/' . $this -> post -> id);
            } else {
                $this -> render('blog/edit/' . $this -> post -> id);
            }
        }
        
        function destroy() {
            model('post') -> find_by_id($this -> data['id']) -> delete();
        }
    }
    ?>

Oh, we have lots of new stuff here. First, we can see new methods inherited from Controller_Base: render and redirect. Redirect is used when you want to redirect to other url. It sends http location header. Render is used when you don't want to redirect, you just want to render other action. Most important difference is you lose globals you define in action.

So, what are globals? In controller, globals are variables which are also available in views. You define them as properties for that controller. Example:

    <?php
    class WelcomeController extends Controller_Base {
        function index() {
            $this -> title = "Hello world";
        }
    }
    ?>

creates new property called title with value Hello world. Soon, we'll see how we can access them in views.

Model

Also, you could see function model used quite often (in each action). What are models anyway? Models are way to represent database (mysql, pqsql, ldap etc.) rows/entries as objects. Let's describe each model call in each action, and see what happens. In index action, you want to get all posts. There are two possible solutions. One is to initiate model('post') and call all() which queries database and gets all rows. But, model implements interface called IteratorAggregate, so it is possible to iterate through it, so you can run:

    <?php
    $posts = model('post');
    foreach($posts as $post) {
        ...
    }
    ?>

Next, we can get first and last post in table by calling:

    <?php
    $first = model('post') -> first();
    $last  = model('post') -> last();
    ?>

And we can find it by id:

    <?php
    $post = model('post') -> find_by_id(5);
    // OR
    $post = model('post') -> where("`ID`=5");
    // OR
    $post = model('post') -> where(array('ID'=1'));
    ?>

When we have post, we can:

  • update it:
    <?php
    $post -> values(array('title' => 'New title')) -> save();
    ?>
  • delete it:
    <?php
    $post -> delete();
    ?>

View

Last but not least, view is responsible for showing data you created in controllers in user-viewable way. View usually generate HTML, but in future, we'll also support JSON, XML and others. View are written in templating language. Here, in swift, we are using HAML, but we plan to support raw php and twig.

Haml

For more information about haml, you can visit PHP Haml website

Clone this wiki locally