My favorites | Sign in
Project Logo
                
Search
for
Updated May 13, 2008 by nsbucky
Labels: Featured
GettingStarted  
how get up and going

Introduction

The simplest way to explain how this works, that request methods, such as GET and POST are mapped via an array of urls to classes that you define. Also included is an AJAX method that will detect ajax requests (works with most popular libraries that send the HTTP_X_REQUESTED_WITH header).

Getting Started

If you checked out Webphp from svn, you will not have the dependencies of Savant3, ADODB, and Inspekt, so make sure you download those separately from the downloads section here, or from their respective sites.

ADODB is only required if you need database access, and you want to use the included recordset pagers. Otherwise you can just use the mysql type of functions from PHP core.

To get started with web-php, create a file called index.php.

The urls are a key=>value pair of regex pattern => classname.

In that file, try the following just to see how it works.

require 'init.php';

$urls = array(
              '#^$#'      => 'hello',
              '#^named/(?P<namedparam>[a-zA-Z_]+)/?$#' => 'named',
             );


class hello {        
    
    function GET($p)
    {
        echo 'request via GET';
        // or
        $vars['message'] = 'requested via get';
        echo Web::render("name-of-file.html", $vars);
    }                          
    
    function POST($p)
    {
        echo 'request via POST';        
    }  
    
    function AJAX($p)
    {
        echo "requested via AJAX";
    }                            
}                          

                                                             

class named {

    function preRun()
    {
        echo "executed before request method";
    }

    function postRun()
    {
        echo "executed after request method";
    }

    function GET($p)
    {
        var_dump($p);
        echo "this is a captured var from the URI: ".$p['namedparam'];
    }
}


try {
    /* debug?
    Web::instance()->debug(true);
    */
    Web::run($urls); 
    // Web::instance()->debugDisplay();
} catch (RequestErrorException $e) {
    // errorCode gives you the 404 or 500 code etc.
    // echo $e->errorCode();                       
    // viewError will print out a basic 404 page
    // catch the RequestErrorException and do whatever you want.
    // viewError will send a header() to the browser fyi.
    $e->ViewError();
}                         

Comment by timmy.bart, May 19, 2009

Hi Kenrick,

Thanks for the effort you've put into porting web.py to php. it's really useful to have a lightweight "framework" in PHP.

I have a suggestion though, you should allow passing configurations parameters to the controllers, so we don't have to rely on global variables. Could be implemented as :

index.php:
Web::run($urls,$conf);

class hello {        
    
    private $conf;
    function hello($conf=null)
    {
    	$this->conf = $conf;
    }
    function GET(){}
}

web.php
public static function run(array $urls,$conf = null, $baseURLPath = null)
...
// instantiate class
$loaded_class = new $class_to_load($conf);

Relying on dependency injection allows for configuration manipulation deep down the stack. what do you think ?

Comment by timmy.bart, May 19, 2009

correction

//web.php
public static function run(array $urls,$conf, $baseURLPath = null)

Sign in to add a comment
Hosted by Google Code