Yet Another Framework

Table of Contents

The Yaf_Application class

Introduction

Yaf_Application provides a bootstrapping facility for applications which provides reusable resources, common- and module-based bootstrap classes and dependency checking.

Note:

Yaf_Application implements the singleton pattern, and Yaf_Application can not be serialized or unserialized which will cause problem when you try to use PHPUnit to write some test case for Yaf.

You may use @backupGlobals annotation of PHPUnit to control the backup and restore operations for global variables. thus can solve this problem.

Class synopsis

Yaf_Application
final class Yaf_Application {
/* Properties */
protected $config ;
protected $dispatcher ;
protected static $_app ;
protected $_modules ;
protected $_running ;
protected $_environ ;
/* Methods */
public static void app ( void )
public void bootstrap ([ Yaf_Bootstrap_Abstract $bootstrap ] )
public Yaf_Application clearLastError ( void )
private void __clone ( void )
public __construct ( mixed $config [, string $envrion ] )
public void __destruct ( void )
public void environ ( void )
public void execute ( callable $entry , string $... )
public Yaf_Application getAppDirectory ( void )
public Yaf_Config_Abstract getConfig ( void )
public Yaf_Dispatcher getDispatcher ( void )
public string getLastErrorMsg ( void )
public int getLastErrorNo ( void )
public array getModules ( void )
public void run ( void )
public Yaf_Application setAppDirectory ( string $directory )
private void __sleep ( void )
private void __wakeup ( void )
}

Properties

config

dispatcher

_app

_modules

_running

_environ

The Yaf_Bootstrap_Abstract class

Introduction

Bootstrap is a mechanism used to do some intial config before a Application run.

User may define their own Bootstrap class by inheriting Yaf_Bootstrap_Abstract

Any method declared in Bootstrap class with leading "_init", will be called by Yaf_Application::bootstrap one by one according to their defined order.

Examples

Example #1 Bootstrap example

<?php
   
/* bootstrap class should be defined under ./application/Bootstrap.php */
   
class Bootstrap extends Yaf_Bootstrap_Abstract {
        public function 
_initConfig(Yaf_Dispatcher $dispatcher) {
            
var_dump(__METHOD__);
        }
        public function 
_initPlugin(Yaf_Dispatcher $dispatcher) {
            
var_dump(__METHOD__);
        }
   }

   
$config = array(
       
"application" => array(
           
"directory" => dirname(__FILE__) . "/application/",
       ),
   );
 
   
$app = new Yaf_Application($config);
   
$app->bootstrap();
?>

The above example will output something similar to:

string(22) "Bootstrap::_initConfig"
string(22) "Bootstrap::_initPlugin"

Class synopsis

Yaf_Bootstrap_Abstract
abstract class Yaf_Bootstrap_Abstract {
/* Properties */
/* Methods */
}

The Yaf_Dispatcher class

Introduction

Yaf_Dispatcher purpose is to initialize the request environment, route the incoming request, and then dispatch any discovered actions; it aggregates any responses and returns them when the process is complete.

Yaf_Dispatcher also implements the Singleton pattern, meaning only a single instance of it may be available at any given time. This allows it to also act as a registry on which the other objects in the dispatch process may draw.

Class synopsis

Yaf_Dispatcher
final class Yaf_Dispatcher {
/* Properties */
protected $_router ;
protected $_view ;
protected $_request ;
protected $_plugins ;
protected static $_instance ;
protected $_auto_render ;
protected $_return_response ;
protected $_instantly_flush ;
protected $_default_module ;
protected $_default_controller ;
protected $_default_action ;
/* Methods */
public Yaf_Dispatcher autoRender ([ bool $flag ] )
public Yaf_Dispatcher catchException ([ bool $flag ] )
private void __clone ( void )
public __construct ( void )
public bool disableView ( void )
public Yaf_Response_Abstract dispatch ( Yaf_Request_Abstract $request )
public Yaf_Dispatcher enableView ( void )
public Yaf_Dispatcher flushInstantly ([ bool $flag ] )
public Yaf_Application getApplication ( void )
public static Yaf_Dispatcher getInstance ( void )
public Yaf_Request_Abstract getRequest ( void )
public Yaf_Router getRouter ( void )
public Yaf_View_Interface initView ( string $templates_dir [, array $options ] )
public Yaf_Dispatcher registerPlugin ( Yaf_Plugin_Abstract $plugin )
public Yaf_Dispatcher returnResponse ( bool $flag )
public Yaf_Dispatcher setDefaultAction ( string $action )
public Yaf_Dispatcher setDefaultController ( string $controller )
public Yaf_Dispatcher setDefaultModule ( string $module )
public Yaf_Dispatcher setErrorHandler ( call $callback , int $error_types )
public Yaf_Dispatcher setRequest ( Yaf_Request_Abstract $request )
public Yaf_Dispatcher setView ( Yaf_View_Interface $view )
private void __sleep ( void )
public Yaf_Dispatcher throwException ([ bool $flag ] )
private void __wakeup ( void )
}

Properties

_router

_view

_request

_plugins

_instance

_auto_render

_return_response

_instantly_flush

_default_module

_default_controller

_default_action

The Yaf_Config_Abstract class

Introduction

Class synopsis

Yaf_Config_Abstract
abstract class Yaf_Config_Abstract {
/* Properties */
protected $_config ;
protected $_readonly ;
/* Methods */
abstract public mixed get ( string $name , mixed $value )
abstract public bool readonly ( void )
abstract public Yaf_Config_Abstract set ( void )
abstract public array toArray ( void )
}

Properties

_config

_readonly

The Yaf_Config_Ini class

Introduction

Yaf_Config_Ini enables developers to store configuration data in a familiar INI format and read them in the application by using nested object property syntax. The INI format is specialized to provide both the ability to have a hierarchy of configuration data keys and inheritance between configuration data sections. Configuration data hierarchies are supported by separating the keys with the dot or period character ("."). A section may extend or inherit from another section by following the section name with a colon character (":") and the name of the section from which data are to be inherited.

Note:

Yaf_Config_Ini utilizes the » parse_ini_file() PHP function. Please review this documentation to be aware of its specific behaviors, which propagate to Yaf_Config_Ini, such as how the special values of "TRUE", "FALSE", "yes", "no", and "NULL" are handled.

Class synopsis

Yaf_Config_Ini
class Yaf_Config_Ini extends Yaf_Config_Abstract implements Iterator , ArrayAccess , Countable {
/* Properties */
/* Methods */
public __construct ( string $config_file [, string $section ] )
public void count ( void )
public void current ( void )
public void __get ([ string $name ] )
public void __isset ( string $name )
public void key ( void )
public void next ( void )
public void offsetExists ( string $name )
public void offsetGet ( string $name )
public void offsetSet ( string $name , string $value )
public void offsetUnset ( string $name )
public void readonly ( void )
public void rewind ( void )
public void __set ( string $name , mixed $value )
public array toArray ( void )
public void valid ( void )
/* Inherited methods */
abstract public mixed Yaf_Config_Abstract::get ( string $name , mixed $value )
abstract public bool Yaf_Config_Abstract::readonly ( void )
abstract public Yaf_Config_Abstract Yaf_Config_Abstract::set ( void )
abstract public array Yaf_Config_Abstract::toArray ( void )
}

Properties

_config

_readonly

Examples

Example #1 Yaf_Config_Iniexample

This example illustrates a basic use of Yaf_Config_Ini for loading configuration data from an INI file. In this example there are configuration data for both a production system and for a staging system. Because the staging system configuration data are very similar to those for production, the staging section inherits from the production section. In this case, the decision is arbitrary and could have been written conversely, with the production section inheriting from the staging section, though this may not be the case for more complex situations. Suppose, then, that the following configuration data are contained in /path/to/config.ini:

; Production site configuration data
[production]
webhost                  = www.example.com
database.adapter         = pdo_mysql
database.params.host     = db.example.com
database.params.username = dbuser
database.params.password = secret
database.params.dbname   = dbname
 
; Staging site configuration data inherits from production and
; overrides values as necessary
[staging : production]
database.params.host     = dev.example.com
database.params.username = devuser
database.params.password = devsecret
<?php
$config 
= new Yaf_Config_Ini('/path/to/config.ini''staging');
 
var_dump($config->database->params->host); 
var_dump($config->database->params->dbname);
var_dump($config->get("database.params.username"));
?>

The above example will output something similar to:

string(15) "dev.example.com"
string(6) "dbname"
string(7) "devuser

The Yaf_Config_Simple class

Introduction

Class synopsis

Yaf_Config_Simple
class Yaf_Config_Simple extends Yaf_Config_Abstract implements Iterator , ArrayAccess , Countable {
/* Properties */
protected $_readonly ;
/* Methods */
public __construct ( string $config_file [, string $section ] )
public void count ( void )
public void current ( void )
public void __get ([ string $name ] )
public void __isset ( string $name )
public void key ( void )
public void next ( void )
public void offsetExists ( string $name )
public void offsetGet ( string $name )
public void offsetSet ( string $name , string $value )
public void offsetUnset ( string $name )
public void readonly ( void )
public void rewind ( void )
public void __set ( string $name , string $value )
public array toArray ( void )
public void valid ( void )
/* Inherited methods */
abstract public mixed Yaf_Config_Abstract::get ( string $name , mixed $value )
abstract public bool Yaf_Config_Abstract::readonly ( void )
abstract public Yaf_Config_Abstract Yaf_Config_Abstract::set ( void )
abstract public array Yaf_Config_Abstract::toArray ( void )
}

Properties

_config

_readonly

The Yaf_Controller_Abstract class

Introduction

Yaf_Controller_Abstract is the heart of Yaf's system. MVC stands for Model-View-Controller and is a design pattern targeted at separating application logic from display logic.

Every custom controller shall inherit Yaf_Controller_Abstract.

You will find that you can not define __construct function for your custom controller, thus, Yaf_Controller_Abstract provides a magic method: Yaf_Controller_Abstract::init.

If you have defined a init() method in your custom controller, it will be called as long as the controller was instantiated.

Action may have arguments, when a request coming, if there are the same name variable in the request parameters(see Yaf_Request_Abstract::getParam) after routed, Yaf will pass them to the action method (see Yaf_Action_Abstract::execute).

Note:

These arguments are directly fetched without filtering, it should be carefully processed before use them.

Class synopsis

Yaf_Controller_Abstract
abstract class Yaf_Controller_Abstract {
/* Properties */
public $actions ;
protected $_module ;
protected $_name ;
protected $_request ;
protected $_response ;
protected $_invoke_args ;
protected $_view ;
/* Methods */
final private void __clone ( void )
final private __construct ( void )
protected bool display ( string $tpl [, array $parameters ] )
public void forward ( string $action [, array $paramters ] )
public void getInvokeArg ( string $name )
public void getInvokeArgs ( void )
public string getModuleName ( void )
public Yaf_Request_Abstract getRequest ( void )
public Yaf_Response_Abstract getResponse ( void )
public Yaf_View_Interface getView ( void )
public void getViewpath ( void )
public void init ( void )
public void initView ([ array $options ] )
public bool redirect ( string $url )
protected string render ( string $tpl [, array $parameters ] )
public void setViewpath ( string $view_directory )
}

Properties

actions

You can also define a action method in a separate PHP script by using this property and Yaf_Action_Abstract.

Example #1 define action in a separate file

<?php
class IndexController extends Yaf_Controller_Abstract {
    protected 
$actions = array(
        
/** now dummyAction is defined in a separate file */
        
"dummy" => "actions/Dummy_action.php",
    );

    
/* action method may have arguments */
    
public indexAction($name$id) {
       
/* $name and $id are unsafe raw data */
       
assert($name == $this->getRequest()->getParam("name"));
       
assert($id   == $this->_request->getParam("id"));
    }
}
?>

Example #2 Dummy_action.php

<?php
class DummyAction extends Yaf_Action_Abstract {
    
/* a action class shall define this method  as the entry point */
    
public execute() {
    }
}
?>

_module

module name

_name

controller name

_request

current request object

_response

current response object

_invoke_args

_view

view engine object

The Yaf_Action_Abstract class

Introduction

A action can be defined in a separate file in Yaf(see Yaf_Controller_Abstract). that is a action method can also be a Yaf_Action_Abstract class.

Since there should be a entry point which can be called by Yaf (as of PHP 5.3, there is a new magic method __invoke, but Yaf is not only works with PHP 5.3+, Yaf choose another magic method execute), you must implement the abstract method Yaf_Action_Abstract::execute in your custom action class.

Class synopsis

Yaf_Action_Abstract
class Yaf_Action_Abstract extends Yaf_Controller_Abstract {
/* Properties */
protected $_controller ;
/* Methods */
abstract publicmixed execute ([ mixed $arg [, mixed $... ]] )
publicYaf_Controller_Abstract getController ( void )
/* Inherited methods */
final private void Yaf_Controller_Abstract::__clone ( void )
final private Yaf_Controller_Abstract::__construct ( void )
protected bool Yaf_Controller_Abstract::display ( string $tpl [, array $parameters ] )
public void Yaf_Controller_Abstract::forward ( string $action [, array $paramters ] )
public void Yaf_Controller_Abstract::getInvokeArg ( string $name )
public void Yaf_Controller_Abstract::getInvokeArgs ( void )
public string Yaf_Controller_Abstract::getModuleName ( void )
public Yaf_Request_Abstract Yaf_Controller_Abstract::getRequest ( void )
public Yaf_Response_Abstract Yaf_Controller_Abstract::getResponse ( void )
public Yaf_View_Interface Yaf_Controller_Abstract::getView ( void )
public void Yaf_Controller_Abstract::getViewpath ( void )
public void Yaf_Controller_Abstract::init ( void )
public void Yaf_Controller_Abstract::initView ([ array $options ] )
public bool Yaf_Controller_Abstract::redirect ( string $url )
protected string Yaf_Controller_Abstract::render ( string $tpl [, array $parameters ] )
public void Yaf_Controller_Abstract::setViewpath ( string $view_directory )
}

Properties

_module

_name

_request

_response

_invoke_args

_view

_controller

The Yaf_View_Interface class

Introduction

Yaf provides a ability for developers to use coustom view engine instead of build-in engine which is Yaf_View_Simple. There is a example to explain how to do this, please see Yaf_Dispatcher::setView.

Class synopsis

Yaf_View_Interface
class Yaf_View_Interface {
/* Methods */
abstract public bool assign ( string $name [, string $value ] )
abstract public bool display ( string $tpl [, array $tpl_vars ] )
abstract public void getScriptPath ( void )
abstract public string render ( string $tpl [, array $tpl_vars ] )
abstract public void setScriptPath ( string $template_dir )
}

The Yaf_View_Simple class

Introduction

Yaf_View_Simple is the built-in template engine in Yaf, it is a simple but fast template engine, and only support PHP script template.

Class synopsis

Yaf_View_Simple
class Yaf_View_Simple implements Yaf_View_Interface {
/* Properties */
protected $_tpl_vars ;
protected $_tpl_dir ;
/* Methods */
public bool assign ( string $name [, mixed $value ] )
public bool assignRef ( string $name , mixed &$value )
public bool clear ([ string $name ] )
final public __construct ( string $template_dir [, array $options ] )
public bool display ( string $tpl [, array $tpl_vars ] )
public string eval ( string $tpl_content [, array $tpl_vars ] )
public void __get ([ string $name ] )
public string getScriptPath ( void )
public void __isset ( string $name )
public string render ( string $tpl [, array $tpl_vars ] )
public void __set ( string $name , mixed $value )
public bool setScriptPath ( string $template_dir )
}

Properties

_tpl_vars

_tpl_dir

The Yaf_Loader class

Introduction

Yaf_Loader introduces a comprehensive autoloading solution for Yaf.

The first time an instance of Yaf_Application is retrieved, Yaf_Loader will instance a singleton, and registers itself with spl_autoload. You retrieve an instance using the Yaf_Loader::getInstance

Yaf_Loader attempt to load a class only one shot, if failed, depend on yaf.use_spl_auload, if this config is On Yaf_Loader::autoload will return FALSE, thus give the chance to other autoload function. if it is Off (by default), Yaf_Loader::autoload will return TRUE, and more important is that a very usefull warning will be triggerd (very usefull to find out why a class could not be loaded).

Note:

Please keep yaf.use_spl_autoload Off unless there is some library have their own autoload mechanism and impossible to rewrite it.

By default, Yaf_Loader assume all library (class defined script) store in the global library directory, which is defined in the php.ini(yaf.library).

If you want Yaf_Loader search some classes(libraries) in the local class directory(which is defined in application.ini, and by default, it is application.directory . "/library"), you should register the class prefix using the Yaf_Loader::registerLocalNameSpace

Let's see some examples(assuming APPLICATION_PATH is application.directory):

Example #1 Config example

// Assuming the following configure in php.ini:
yaf.library = "/global_dir"

//Assuming the following configure in application.ini
application.library = APPLICATION_PATH "/library"
Assuming the following local name space registerd:

Example #2 Register localnamespace

<?php
class Bootstrap extends Yaf_Bootstrap_Abstract{
     public function 
_initLoader($dispatcher) {
          
Yaf_Loader::getInstance()->registerLocalNameSpace(array("Foo""Bar"));
     }
?>
Then the autoload examples:

Example #3 Load class example

class Foo_Bar_Test =>
  // APPLICATION_PATH/library/Foo/Bar/Test.php
  
class GLO_Name  =>
  // /global_dir/Glo/Name.php
 
class BarNon_Test
  // /global_dir/Barnon/Test.php
As of PHP 5.3, you can use namespace:

Example #4 Load namespace class example

class \Foo\Bar\Dummy =>
   // APPLICATION_PATH/library/Foo/Bar/Dummy.php

class \FooBar\Bar\Dummy =>
   // /global_dir/FooBar/Bar/Dummy.php

You may noticed that all the folder wth the first letter capitalized, you can make them lowercase by set yaf.lowcase_path = On in php.ini

Yaf_Loader is also designed to load the MVC classes, and the rule is:

Example #5 MVC class loading example

Controller Classes =>
// APPLICATION_PATH/controllers/

Model Classes =>
// APPLICATION_PATH/models/

Plugin Classes =>
// APPLICATION_PATH/plugins/
Yaf identify a class's suffix(this is by default, you can also change to the prefix by change the configure yaf.name_suffix) to decide whether it is a MVC class:

Example #6 MVC class distinctions

Controller Classes =>
    // ***Controller

Model Classes =>
    // ***Model

Plugin Classes =>
    // ***Plugin
some examples:

Example #7 MVC loading example

class IndexController
    // APPLICATION_PATH/controllers/Index.php

class DataModel =>
   // APPLICATION_PATH/models/Data.php

class DummyPlugin =>
  // APPLICATION_PATH/plugins/Dummy.php

class A_B_TestModel =>
  // APPLICATION_PATH/models/A/B/Test.php

Note:

As of 2.1.18, Yaf supports Controllers autoloading for user script side, (which means the autoloading triggered by user php script, eg: access a controller static property in Bootstrap or Plugins), but autoloader only try to locate controller class script under the default module folder, which is "APPLICATION_PATH/controllers/".

also, the directory will be affected by yaf.lowcase_path.

Class synopsis

Yaf_Loader
class Yaf_Loader {
/* Properties */
protected $_local_ns ;
protected $_library ;
protected $_global_library ;
static $_instance ;
/* Methods */
public void autoload ( void )
public void clearLocalNamespace ( void )
private void __clone ( void )
private __construct ( void )
public static void getInstance ( void )
public Yaf_Loader getLibraryPath ([ bool $is_global = false ] )
public void getLocalNamespace ( void )
public static void import ( void )
public void isLocalName ( void )
public void registerLocalNamespace ( mixed $prefix )
public Yaf_Loader setLibraryPath ( string $directory [, bool $is_global = false ] )
private void __sleep ( void )
private void __wakeup ( void )
}

Properties

_local_ns

_library

By default, this value is application.directory . "/library", you can change this either in the application.ini(application.library) or call to Yaf_Loader::setLibraryPath

_global_library

_instance

The Yaf_Plugin_Abstract class

Introduction

Plugins allow for easy extensibility and customization of the framework.

Plugins are classes. The actual class definition will vary based on the component -- you may need to implement this interface, but the fact remains that the plugin is itself a class.

A plugin could be loaded into Yaf by using Yaf_Dispatcher::registerPlugin, after registerd, All the methods which the plugin implemented according to this interface, will be called at the proper time.

Examples

Example #1 Plugin example

<?php
   
/* bootstrap class should be defined under ./application/Bootstrap.php */
   
class Bootstrap extends Yaf_Bootstrap_Abstract {
        public function 
_initPlugin(Yaf_Dispatcher $dispatcher) {
            
/* register a plugin */
            
$dispatcher->registerPlugin(new TestPlugin());
        }
   }

   
/* plugin class should be placed under ./application/plugins/ */
   
class TestPlugin extends Yaf_Plugin_Abstract {
        public function 
routerStartup(Yaf_Request_Abstract $requestYaf_Response_Abstract $response) {
            
/* before router 
               in this hook,  user can do some url rewrite */
            
var_dump("routerStartup");
        }
        public function 
routerShutdown(Yaf_Request_Abstract $requestYaf_Response_Abstract $response) {
            
/* router complete 
               in this hook, user can do login check */
            
var_dump("routerShutdown");
        }
        public function 
dispatchLoopStartup(Yaf_Request_Abstract $requestYaf_Response_Abstract $response) {
            
var_dump("dispatchLoopStartup");
        }
        public function 
preDispatch(Yaf_Request_Abstract $requestYaf_Response_Abstract $response) {
            
var_dump("preDispatch");
        }
        public function 
postDispatch(Yaf_Request_Abstract $requestYaf_Response_Abstract $response) {
            
var_dump("postDispatch");
        }
        public function 
dispatchLoopShutdown(Yaf_Request_Abstract $requestYaf_Response_Abstract $response) {
            
/* final hoook
               in this hook user can do loging or implement layout */
            
var_dump("dispatchLoopShutdown");
        }
   }

   Class 
IndexController extends Yaf_Controller_Abstract {
        public function 
indexAction() {
            return 
FALSE//prevent rendering
        
}
   }

   
$config = array(
       
"application" => array(
           
"directory" => dirname(__FILE__) . "/application/",
       ),
   );
 
   
$app = new Yaf_Application($config);
   
$app->bootstrap()->run();
?>

The above example will output something similar to:

string(13) "routerStartup"
string(14) "routerShutdown"
string(19) "dispatchLoopStartup"
string(11) "preDispatch"
string(12) "postDispatch"
string(20) "dispatchLoopShutdown"

Class synopsis

Yaf_Plugin_Abstract
class Yaf_Plugin_Abstract {
/* Methods */
public void dispatchLoopShutdown ( Yaf_Request_Abstract $request , Yaf_Response_Abstract $response )
public void dispatchLoopStartup ( Yaf_Request_Abstract $request , Yaf_Response_Abstract $response )
public void postDispatch ( Yaf_Request_Abstract $request , Yaf_Response_Abstract $response )
public void preDispatch ( Yaf_Request_Abstract $request , Yaf_Response_Abstract $response )
public void preResponse ( Yaf_Request_Abstract $request , Yaf_Response_Abstract $response )
public void routerShutdown ( Yaf_Request_Abstract $request , Yaf_Response_Abstract $response )
public void routerStartup ( Yaf_Request_Abstract $request , Yaf_Response_Abstract $response )
}

The Yaf_Registry class

Introduction

All methods of Yaf_Registry declared as static, making it unversally accessible. This provides the ability to get or set any custom data from anyway in your code as necessary.

Class synopsis

Yaf_Registry
class Yaf_Registry {
/* Properties */
static $_instance ;
protected $_entries ;
/* Methods */
private void __clone ( void )
private __construct ( void )
public static void del ( string $name )
public static mixed get ( string $name )
public static bool has ( string $name )
public static bool set ( string $name , string $value )
}

Properties

_instance

_entries

The Yaf_Request_Abstract class

Introduction

Class synopsis

Yaf_Request_Abstract
class Yaf_Request_Abstract {
/* Constants */
const string Yaf_Request_Abstract::SCHEME_HTTP = http ;
const string Yaf_Request_Abstract::SCHEME_HTTPS = https ;
/* Properties */
public $module ;
public $controller ;
public $action ;
public $method ;
protected $params ;
protected $language ;
protected $_exception ;
protected $_base_uri ;
protected $uri ;
protected $dispatched ;
protected $routed ;
/* Methods */
public void getActionName ( void )
public void getBaseUri ( void )
public void getControllerName ( void )
public void getEnv ( string $name [, string $default ] )
public void getException ( void )
public void getLanguage ( void )
public void getMethod ( void )
public void getModuleName ( void )
public void getParam ( string $name [, string $default ] )
public void getParams ( void )
public void getRequestUri ( void )
public void getServer ( string $name [, string $default ] )
public void isCli ( void )
public void isDispatched ( void )
public void isGet ( void )
public void isHead ( void )
public void isOptions ( void )
public void isPost ( void )
public void isPut ( void )
public void isRouted ( void )
public void isXmlHttpRequest ( void )
public void setActionName ( string $action )
public bool setBaseUri ( string $uir )
public void setControllerName ( string $controller )
public void setDispatched ( void )
public void setModuleName ( string $module )
public void setParam ( string $name [, string $value ] )
public void setRequestUri ( string $uir )
public void setRouted ([ string $flag ] )
}

Properties

module

controller

action

method

params

language

_exception

_base_uri

uri

dispatched

routed

Predefined Constants

Yaf_Request_Abstract::SCHEME_HTTP

Yaf_Request_Abstract::SCHEME_HTTPS

The Yaf_Request_Http class

Introduction

Any request from client is initialized as a Yaf_Request_Http. you can get the rquest infomations like, uri query and post parameters via methods of this class.

Note:

For security, $_GET/$_POST are readonly in Yaf, which means if you set a value to these global variables, you can not get it from Yaf_Request_Http::getQurey or Yaf_Request_Http::getPost.

But there do is some usage need such feature, like unit testing. thus Yaf can be built with --enable-yaf-debug, which will allow Yaf read the value user set via script.

in such case, Yaf will throw a E_STRICT warning to remind you about that: Strict Standards: you are running yaf in debug mode

Class synopsis

Yaf_Request_Http
class Yaf_Request_Http extends Yaf_Request_Abstract {
/* Properties */
/* Methods */
private void __clone ( void )
public __construct ([ string $request_uri [, string $base_uri ]] )
public mixed get ( string $name [, string $default ] )
public mixed getCookie ( string $name [, string $default ] )
public void getFiles ( void )
public mixed getPost ( string $name [, string $default ] )
public mixed getQuery ( string $name [, string $default ] )
public void getRequest ( void )
public bool isXmlHttpRequest ( void )
/* Inherited methods */
public void Yaf_Request_Abstract::getActionName ( void )
public void Yaf_Request_Abstract::getBaseUri ( void )
public void Yaf_Request_Abstract::getControllerName ( void )
public void Yaf_Request_Abstract::getEnv ( string $name [, string $default ] )
public void Yaf_Request_Abstract::getException ( void )
public void Yaf_Request_Abstract::getLanguage ( void )
public void Yaf_Request_Abstract::getMethod ( void )
public void Yaf_Request_Abstract::getModuleName ( void )
public void Yaf_Request_Abstract::getParam ( string $name [, string $default ] )
public void Yaf_Request_Abstract::getParams ( void )
public void Yaf_Request_Abstract::getRequestUri ( void )
public void Yaf_Request_Abstract::getServer ( string $name [, string $default ] )
public void Yaf_Request_Abstract::isCli ( void )
public void Yaf_Request_Abstract::isDispatched ( void )
public void Yaf_Request_Abstract::isGet ( void )
public void Yaf_Request_Abstract::isHead ( void )
public void Yaf_Request_Abstract::isOptions ( void )
public void Yaf_Request_Abstract::isPost ( void )
public void Yaf_Request_Abstract::isPut ( void )
public void Yaf_Request_Abstract::isRouted ( void )
public void Yaf_Request_Abstract::isXmlHttpRequest ( void )
public void Yaf_Request_Abstract::setActionName ( string $action )
public bool Yaf_Request_Abstract::setBaseUri ( string $uir )
public void Yaf_Request_Abstract::setControllerName ( string $controller )
public void Yaf_Request_Abstract::setDispatched ( void )
public void Yaf_Request_Abstract::setModuleName ( string $module )
public void Yaf_Request_Abstract::setParam ( string $name [, string $value ] )
public void Yaf_Request_Abstract::setRequestUri ( string $uir )
public void Yaf_Request_Abstract::setRouted ([ string $flag ] )
}

Properties

module

controller

action

method

params

language

_exception

_base_uri

uri

dispatched

routed

The Yaf_Request_Simple class

Introduction

Yaf_Request_Simple is particularlly used for test puporse. ie. simulate some espacial request under CLI mode.

Class synopsis

Yaf_Request_Simple
class Yaf_Request_Simple extends Yaf_Request_Abstract {
/* Constants */
const string Yaf_Request_Simple::SCHEME_HTTP = http ;
const string Yaf_Request_Simple::SCHEME_HTTPS = https ;
/* Properties */
/* Methods */
private void __clone ( void )
public __construct ([ string $method [, string $module [, string $controller [, string $action [, array $params ]]]]] )
public void get ( void )
public void getCookie ( void )
public void getFiles ( void )
public void getPost ( void )
public void getQuery ( void )
public void getRequest ( void )
public void isXmlHttpRequest ( void )
/* Inherited methods */
public void Yaf_Request_Abstract::getActionName ( void )
public void Yaf_Request_Abstract::getBaseUri ( void )
public void Yaf_Request_Abstract::getControllerName ( void )
public void Yaf_Request_Abstract::getEnv ( string $name [, string $default ] )
public void Yaf_Request_Abstract::getException ( void )
public void Yaf_Request_Abstract::getLanguage ( void )
public void Yaf_Request_Abstract::getMethod ( void )
public void Yaf_Request_Abstract::getModuleName ( void )
public void Yaf_Request_Abstract::getParam ( string $name [, string $default ] )
public void Yaf_Request_Abstract::getParams ( void )
public void Yaf_Request_Abstract::getRequestUri ( void )
public void Yaf_Request_Abstract::getServer ( string $name [, string $default ] )
public void Yaf_Request_Abstract::isCli ( void )
public void Yaf_Request_Abstract::isDispatched ( void )
public void Yaf_Request_Abstract::isGet ( void )
public void Yaf_Request_Abstract::isHead ( void )
public void Yaf_Request_Abstract::isOptions ( void )
public void Yaf_Request_Abstract::isPost ( void )
public void Yaf_Request_Abstract::isPut ( void )
public void Yaf_Request_Abstract::isRouted ( void )
public void Yaf_Request_Abstract::isXmlHttpRequest ( void )
public void Yaf_Request_Abstract::setActionName ( string $action )
public bool Yaf_Request_Abstract::setBaseUri ( string $uir )
public void Yaf_Request_Abstract::setControllerName ( string $controller )
public void Yaf_Request_Abstract::setDispatched ( void )
public void Yaf_Request_Abstract::setModuleName ( string $module )
public void Yaf_Request_Abstract::setParam ( string $name [, string $value ] )
public void Yaf_Request_Abstract::setRequestUri ( string $uir )
public void Yaf_Request_Abstract::setRouted ([ string $flag ] )
}

Properties

module

controller

action

method

params

language

_exception

_base_uri

uri

dispatched

routed

Predefined Constants

Yaf_Request_Simple::SCHEME_HTTP

Yaf_Request_Simple::SCHEME_HTTPS

The Yaf_Response_Abstract class

Introduction

Class synopsis

Yaf_Response_Abstract
class Yaf_Response_Abstract {
/* Constants */
const string Yaf_Response_Abstract::DEFAULT_BODY = "content" ;
/* Properties */
protected $_header ;
protected $_body ;
protected $_sendheader ;
/* Methods */
public bool appendBody ( string $content [, string $key ] )
public bool clearBody ([ string $key ] )
public void clearHeaders ( void )
private void __clone ( void )
public __construct ( void )
public void __destruct ( void )
public mixed getBody ([ string $key ] )
public void getHeader ( void )
public bool prependBody ( string $content [, string $key ] )
public void response ( void )
protected void setAllHeaders ( void )
public bool setBody ( string $content [, string $key ] )
public void setHeader ( void )
public void setRedirect ( void )
private void __toString ( void )
}

Properties

_header

_body

_sendheader

The Yaf_Route_Interface class

Introduction

Yaf_Route_Interface used for developer defined their custom route.

Class synopsis

Yaf_Route_Interface
class Yaf_Route_Interface {
/* Methods */
abstract public string assemble ( array $info [, array $query ] )
abstract public bool route ( Yaf_Request_Abstract $request )
}

The Yaf_Route_Map class

Introduction

Yaf_Route_Map is a built-in route, it simply convert a URI endpoint (that part of the URI which comes after the base URI: see Yaf_Request_Abstract::setBaseUri) to a controller name or action name(depends on the paramter passed to Yaf_Route_Map::__construct) in following rule: A => controller A. A/B/C => controller A_B_C. A/B/C/D/E => controller A_B_C_D_E.

If the second parameter of Yaf_Route_Map::__construct is specificed, then only the part before delimeter of URI will used to routing, the part after it is used to routing request parameters (see the example section of Yaf_Route_Map::__construct).

Class synopsis

Yaf_Route_Map
class Yaf_Route_Map implements Yaf_Route_Interface {
/* Properties */
protected $_ctl_router ;
protected $_delimeter ;
/* Methods */
public string assemble ( array $info [, array $query ] )
public __construct ([ string $controller_prefer = false [, string $delimiter = "" ]] )
public bool route ( Yaf_Request_Abstract $request )
}

Properties

_ctl_router

_delimeter

The Yaf_Route_Regex class

Introduction

Yaf_Route_Regex is the most flexible route among the Yaf built-in routes.

Class synopsis

Yaf_Route_Regex
class Yaf_Route_Regex extends Yaf_Route_Interface implements Yaf_Route_Interface {
/* Properties */
protected $_route ;
protected $_default ;
protected $_maps ;
protected $_verify ;
/* Methods */
public string assemble ( array $info [, array $query ] )
public __construct ( string $match , array $route [, array $map [, array $verify [, string $reverse ]]] )
public bool route ( Yaf_Request_Abstract $request )
/* Inherited methods */
abstract public string Yaf_Route_Interface::assemble ( array $info [, array $query ] )
abstract public bool Yaf_Route_Interface::route ( Yaf_Request_Abstract $request )
}

Properties

_route

_default

_maps

_verify

The Yaf_Route_Rewrite class

Introduction

For usage, please see the example section of Yaf_Route_Rewrite::__construct

Class synopsis

Yaf_Route_Rewrite
class Yaf_Route_Rewrite extends Yaf_Route_Interface implements Yaf_Route_Interface {
/* Properties */
protected $_route ;
protected $_default ;
protected $_verify ;
/* Methods */
public string assemble ( array $info [, array $query ] )
public __construct ( string $match , array $route [, array $verify ] )
public bool route ( Yaf_Request_Abstract $request )
/* Inherited methods */
abstract public string Yaf_Route_Interface::assemble ( array $info [, array $query ] )
abstract public bool Yaf_Route_Interface::route ( Yaf_Request_Abstract $request )
}

Properties

_route

_default

_verify

The Yaf_Router class

Introduction

Yaf_Router is the standard framework router. Routing is the process of taking a URI endpoint (that part of the URI which comes after the base URI: see Yaf_Request_Abstract::setBaseUri) and decomposing it into parameters to determine which module, controller, and action of that controller should receive the request. This values of the module, controller, action and other parameters are packaged into a Yaf_Request_Abstract object which is then processed by Yaf_Dispatcher. Routing occurs only once: when the request is initially received and before the first controller is dispatched. Yaf_Router is designed to allow for mod_rewrite-like functionality using pure PHP structures. It is very loosely based on Ruby on Rails routing and does not require any prior knowledge of webserver URL rewriting. It is designed to work with a single Apache mod_rewrite rule (one of):

Example #1 Rewrite rule for Apache

RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php
or (preferred):

Example #2 Rewrite rule for Apache

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
If using Lighttpd, the following rewrite rule is valid:

Example #3 Rewrite rule for Lighttpd

url.rewrite-once = (
  ".*\?(.*)$" => "/index.php?$1",
  ".*\.(js|ico|gif|jpg|png|css|html)$" => "$0",
  "" => "/index.php"
)
If using Nginx, use the following rewrite rule:

Example #4 Rewrite rule for Nginx

server {
  listen ****;
  server_name  yourdomain.com;
  root   document_root;
  index  index.php index.html;

  if (!-e $request_filename) {
    rewrite ^/(.*)  /index.php/$1 last;
  }
}

Default route

Yaf_Router comes preconfigured with a default route Yaf_Route_Static, which will match URIs in the shape of controller/action. Additionally, a module name may be specified as the first path element, allowing URIs of the form module/controller/action. Finally, it will also match any additional parameters appended to the URI by default - controller/action/var1/value1/var2/value2.

Note:

Module name must be defined in config, considering application.module="Index,Foo,Bar", in this case, only index, foo and bar can be considerd as a module name. if doesn't config, there is only one module named "Index".

Some examples of how such routes are matched:

Example #5 Yaf_Route_Static(default route)example

// Assuming the following configure:
$conf = array(
   "application" => array(
      "modules" => "Index,Blog",
   ),
);

Controller only:
http://example/news
    controller == news
Action only(when defined yaf.action_prefer=1 in php.ini)
    action  == news
 
Invalid module maps to controller name:
http://example/foo
    controller == foo
 
Module + controller:
http://example/blog/archive
    module     == blog
    controller == archive
 
Module + controller + action:
http://example/blog/archive/list
    module     == blog
    controller == archive
    action     == list
 
Module + controller + action + params:
http://example/blog/archive/list/sort/alpha/date/desc
    module     == blog
    controller == archive
    action     == list
    sort       == alpha
    date       == desc

Class synopsis

Yaf_Router
class Yaf_Router {
/* Properties */
protected $_routes ;
protected $_current ;
/* Methods */
public bool addConfig ( Yaf_Config_Abstract $config )
public bool addRoute ( string $name , Yaf_Route_Abstract $route )
public __construct ( void )
public string getCurrentRoute ( void )
public Yaf_Route_Interface getRoute ( string $name )
public mixed getRoutes ( void )
public bool route ( Yaf_Request_Abstract $request )
}

Properties

_routes

registered routes stack

_current

after routing phase, this indicated the name of which route is used to route current request. you can get this name by Yaf_Router::getCurrentRoute.

The Yaf_Route_Simple class

Introduction

Yaf_Route_Simple will match the query string, and find the route info.

all you need to do is tell Yaf_Route_Simple what key in the $_GET is module, what key is controller, and what key is action.

Yaf_Route_Simple::route will always return TRUE, so it is important put Yaf_Route_Simple in the front of the Route stack, otherwise all the other routes will not be called.

Class synopsis

Yaf_Route_Simple
class Yaf_Route_Simple implements Yaf_Route_Interface {
/* Properties */
protected $controller ;
protected $module ;
protected $action ;
/* Methods */
public string assemble ( array $info [, array $query ] )
public __construct ( string $module_name , string $controller_name , string $action_name )
public bool route ( Yaf_Request_Abstract $request )
}

Properties

controller

module

action

The Yaf_Route_Static class

Introduction

Defaultly, Yaf_Router only have a Yaf_Route_Static as its default route.

And Yaf_Route_Static is designed to handle the 80% requirement.

please *NOTE* that it is unecessary to instance a Yaf_Route_Static, also unecesary to add it into Yaf_Router's routes stack, since there is always be one in Yaf_Router's routes stack, and always be called at the last time.

Class synopsis

Yaf_Route_Static
class Yaf_Route_Static implements Yaf_Router {
/* Methods */
public string assemble ( array $info [, array $query ] )
public void match ( string $uri )
public bool route ( Yaf_Request_Abstract $request )
}

The Yaf_Route_Supervar class

Introduction

Class synopsis

Yaf_Route_Supervar
class Yaf_Route_Supervar implements Yaf_Route_Interface {
/* Properties */
protected $_var_name ;
/* Methods */
public string assemble ( array $info [, array $query ] )
public __construct ( string $supervar_name )
public bool route ( Yaf_Request_Abstract $request )
}

Properties

_var_name

The Yaf_Session class

Introduction

Class synopsis

Yaf_Session
class Yaf_Session implements Iterator , ArrayAccess , Countable {
/* Properties */
protected static $_instance ;
protected $_session ;
protected $_started ;
/* Methods */
private void __clone ( void )
private __construct ( void )
public void count ( void )
public void current ( void )
public void del ( string $name )
public void __get ( string $name )
public static void getInstance ( void )
public void has ( string $name )
public void __isset ( string $name )
public void key ( void )
public void next ( void )
public void offsetExists ( string $name )
public void offsetGet ( string $name )
public void offsetSet ( string $name , string $value )
public void offsetUnset ( string $name )
public void rewind ( void )
public void __set ( string $name , string $value )
private void __sleep ( void )
public void start ( void )
public void __unset ( string $name )
public void valid ( void )
private void __wakeup ( void )
}

Properties

_instance

_session

_started

The Yaf_Exception class

Introduction

Class synopsis

Yaf_Exception
class Yaf_Exception extends Exception {
/* Inherited properties */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* Methods */
public __construct ( void )
public void getPrevious ( void )
/* Inherited methods */
final public string Exception::getMessage ( void )
final public Throwable Exception::getPrevious ( void )
final public mixed Exception::getCode ( void )
final public string Exception::getFile ( void )
final public int Exception::getLine ( void )
final public array Exception::getTrace ( void )
final public string Exception::getTraceAsString ( void )
public string Exception::__toString ( void )
final private void Exception::__clone ( void )
}

The Yaf_Exception_TypeError class

Introduction

Class synopsis

Yaf_Exception_TypeError
class Yaf_Exception_TypeError extends Yaf_Exception {
/* Properties */
/* Methods */
/* Inherited methods */
public Yaf_Exception::__construct ( void )
public void Yaf_Exception::getPrevious ( void )
}

The Yaf_Exception_StartupError class

Introduction

Class synopsis

Yaf_Exception_StartupError
class Yaf_Exception_StartupError extends Yaf_Exception {
/* Properties */
/* Methods */
/* Inherited methods */
public Yaf_Exception::__construct ( void )
public void Yaf_Exception::getPrevious ( void )
}

The Yaf_Exception_DispatchFailed class

Introduction

Class synopsis

Yaf_Exception_DispatchFailed
class Yaf_Exception_DispatchFailed extends Yaf_Exception {
/* Properties */
/* Methods */
/* Inherited methods */
public Yaf_Exception::__construct ( void )
public void Yaf_Exception::getPrevious ( void )
}

The Yaf_Exception_RouterFailed class

Introduction

Class synopsis

Yaf_Exception_RouterFailed
class Yaf_Exception_RouterFailed extends Yaf_Exception {
/* Properties */
/* Methods */
/* Inherited methods */
public Yaf_Exception::__construct ( void )
public void Yaf_Exception::getPrevious ( void )
}

The Yaf_Exception_LoadFailed class

Introduction

Class synopsis

Yaf_Exception_LoadFailed
class Yaf_Exception_LoadFailed extends Yaf_Exception {
/* Properties */
/* Methods */
/* Inherited methods */
public Yaf_Exception::__construct ( void )
public void Yaf_Exception::getPrevious ( void )
}

The Yaf_Exception_LoadFailed_Module class

Introduction

Class synopsis

Yaf_Exception_LoadFailed_Module
class Yaf_Exception_LoadFailed_Module extends Yaf_Exception_LoadFailed {
/* Properties */
/* Methods */
/* Inherited methods */
public Yaf_Exception::__construct ( void )
public void Yaf_Exception::getPrevious ( void )
}

The Yaf_Exception_LoadFailed_Controller class

Introduction

Class synopsis

Yaf_Exception_LoadFailed_Controller
class Yaf_Exception_LoadFailed_Controller extends Yaf_Exception_LoadFailed {
/* Properties */
/* Methods */
/* Inherited methods */
public Yaf_Exception::__construct ( void )
public void Yaf_Exception::getPrevious ( void )
}

The Yaf_Exception_LoadFailed_Action class

Introduction

Class synopsis

Yaf_Exception_LoadFailed_Action
class Yaf_Exception_LoadFailed_Action extends Yaf_Exception_LoadFailed {
/* Properties */
/* Methods */
/* Inherited methods */
public Yaf_Exception::__construct ( void )
public void Yaf_Exception::getPrevious ( void )
}

The Yaf_Exception_LoadFailed_View class

Introduction

Class synopsis

Yaf_Exception_LoadFailed_View
class Yaf_Exception_LoadFailed_View extends Yaf_Exception_LoadFailed {
/* Properties */
/* Methods */
/* Inherited methods */
public Yaf_Exception::__construct ( void )
public void Yaf_Exception::getPrevious ( void )
}