Pluf : PHP 5 Framework   Download | Bug Tracking | Source | Documentation | Forum | Latest Changes | Home

Minimalist Hello World! application

This is a minimalist tutorial to show how to create the simplest Hello World! application.

In this tutorial, you will learn:

  • The basic directory structure of a Pluf application.
  • The concepts of:
    • The views.
    • Response and request objects.
    • The dispatcher.

Installation of Pluf

Just perform a checkout of the latest version Pluf somewhere on your system, you do not have to do anything else to have it installed.

Hello World! Application Structure

To facilitate the development, the files you are going to create are to be placed in your Apache document root in a folder that will name $HELLO_ROOT in the rest of this tutorial.

At the end of the tutorial, you will have the following files:

  • $HELLO_ROOT/index.php - The entry point of the application.
  • $HELLO_ROOT/Hello/Views.php - The definition of the views.
  • $HELLO_ROOT/Hello/conf/urls.php - The definition of the URLs.
  • $HELLO_ROOT/Hello/conf/hello.php - The configuration of the application.

The Hello World! View

A view is a public method of a class. That's it, you do not need your class to extend any other class.

Here is the minimal "Hello World!" view:

<?php
class Hello_Views
{
    public function hello($request, $match)
    {
        return new Pluf_HTTP_Response('Hello World!');
    }
}

What you need to note here:

The name of the class is Hello_Views and is in the file Hello/Views.php. This is very important as the classes are automatically loaded based on their name. The same way, you will find the class Pluf_HTTP_Response in the file Pluf/HTTP/Response.php.

The parameters are a request object Pluf_HTTP_Request and a match array. You can ignore the match array for the moment.

The return value is a response object. Here we are returning the simplest object possible, but a lot of practical response objects are available. The content of the response is 'Hello World!'.

URLs Definition

The definition is by convention in $HELLO_ROOT/Hello/conf/urls.php.

<?php
return array(
             array(
                   'regex' => '#^/hello/$#',
                   'base' => '',
                   'priority' => 4,
                   'model' => 'Hello_Views',
                   'method' => 'hello'
                   )
             );

This file just return an array of URLs definition. A definition is composed of 3 important elements:

  • regex: The regular expression that will run against the PHP path info.
  • model: The class containing the view.
  • method: The view itself. If the regular expression match, this is the method that will be called. The matching results will be available as the second argument of the method.

Here, if we access http://localhost/pathto/index.php/hello/ then the hello method from the Hello_Views class will be called. This approach means that you have full control on your urls, they can end with /, .html, .pdf, etc.

Read more in the URL definition page of the manual.

Application Configuration

In that case, we have a very simple application, so the configuration is also very simple:

<?php
return array('hello_urls' => dirname(__FILE__).'/urls.php');

We just return an associative array with the path to the url file. Nothing more to do.

Read more in the settings page of the manual.

Application Entry Point

<?php
require 'Pluf.php';
Pluf::start(dirname(__FILE__).'/Hello/conf/hello.php');
Pluf_Dispatcher::loadControllers(Pluf::f('hello_urls'));
Pluf_Dispatcher::dispatch(Pluf_HTTP_URL::getAction()); 

Line by line:

  1. We first include the base Pluf file. If it is not in your include path, you can modify it with a call to set_include_path.

  2. We start the Pluf application with the configuration.

  3. We load the dispatcher with the URL definition.

  4. We dispatch the request.

Done, you have created your first application with Pluf.

Testing

Go to http://localhost/pathto/index.php/hello/

Enjoy!

Download the code

The full code can be seen in this commit.

Report issues in the documentation

If you find errors or issues in the documentation, report a bug. We will update the documentation accordingly.

Pluf © 2005-2008 Loïc d'Anterroches, supported by Céondo Ltd.