PHPLights/Documentation/Quickstart

From Linuxnetworks
< PHPLights‎ | Documentation
Revision as of 10:57, 24 October 2007 by Nose (Talk | contribs)

Jump to: navigation, search

Application structure

The "app" directory in the phpLights package provides a good starting point for your new application. It contains the necessary directories and the entry point (index.php) with the bootstrapping code.

There are only a few files and directory which are absolutely necessary:

  • index.php (start point)
  • base.php (basic configuration)
  • lib/ (for implemented classes controllers and models)
  • template/ (for template files)
  • template/base.php (basic view for output)
  • template/error/internal.php (internal server error page)
  • template/error/notfound.php (404 error page)

Optional files and directories are:

  • config/ (configuration files)
  • config/resource.php (available resources, e.g for databases)
  • data/ (if needed)
  • skin/ (css and image files)
  • skin/default/css/debug.css (css for easy debugging)
  • template/debug.php (view for easy debugging)

Calling the application

If the application is called without additional path information, defaults for controller and action are assumed. Therefore, the following URLs (assuming that "app" is the name of your application directory) are considered the same:

.../app
.../app/index.php
.../app/index.php/default
.../app/index.php/default/index

The first parameter after "index.php" specifies the controller, the second the action. By default, the class "DefaultController" is used and its "index" member function is called.

The first character of the first parameter (controller) is upper-cased and the string "Controller" is appended. Internally, the framework does a lookup for a .php file of this name in the lib/ directory of the application which must include a class of the same name. Contrary to the controller parameter, the action parameter is used unmodified to find a corresponding method name.

To fill your application with life, first implement the "index" method in the "DefaultController" class and add further public methods for more actions. The next step may include creating more controller classes with some methods implemented for providing more functionality.

Implementing a controller with actions

To get convenient access to often needed functionality, you should extend your own controller from the "LsController" base class. It provides usefull shortcuts for forwarding request to other controllers, creating new objects, adding views and creating subviews for generating the output, access configuration values and write messages to the log. The available functions are:

$this->forward( $controllername, $actionname );
$this->create( $classname, $interfacename, ... );
$this->addView( $viewname, $templatename );
$this->createView( $templatename );
$this->conf( $keypath, $defaultvalue = NULL );
$this->log( $message, $loglevel = LsLogger::Error );

Furthermore, you can access the complete context of the application which contains all necessary objects provided by for phpLights applications. There are shortcut functions available which return the objects:

LsFactory() 
creates class instances, $this->create() is an alias for LsFactory()->create()
LsLogger() 
log messages, $this->log() is an alias for LsLogger()->log()
LsConfig() 
get, add or remove configuration values, $this->conf() is an alias to LsConfig()->get()
LsRequest() 
provides access to user input and file uploads
LsResponse() 
holds HTTP header informations, cookies and views for generating output
LsUser() 
access to session data and authorization possibilities
LsDB() 
provides database connections via LsDB()->acquire()
LsTimer() 
enables performance measures via LsTimer()->add()

A basic application may look like this:

// get user input and make sure it is an integer (0 if not available)
$myval = LsResponse()->get 'request/myval', 0, LsStorage::Int );

// do something

// Add new view named 'vbody' with template 'app/template/default/index.php'
$vbody = $this->addView( 'vbody', 'default/index' );

// Add a variable named 'statusvar' to the 'vbody' view
$vbody->statusvar = 'Success';



Back to Overview