Location: PHPKode > scripts > Func > Func-Sample.php
<?php

/*- THE DEAL:
 * Suppose that for each value of query strings 'c' (controller - the class)
 * and 'a' (action - the class method), we want to properly call the action
 * of the controller class. Example: for the address http://site/?c=Home&a=index
 * the action 'index' of the controller 'Home' will be processed if *possible*.
 * 
 * Here is a way to do the work using the Func class.
 */

// Definition of some controller classes with actions

class HomeController
{
    public function indexAction()
    {
        return 'Home - Index Page';
    }
    
    public function aboutAction()
    {
        return 'Home - About Page';
    }
    
    private function cannotProcessAction()
    {
        return 'Denied';
    }
}

class GalleryController
{
    public function indexAction()
    {
        return 'Gallery - Index Page';
    }
    
    public function albumAction()
    {
        return 'Gallery - Album Page';
    }
    
    protected function cannotProcessAction()
    {
        return 'Denied';
    }
}

// ----------------------------------------------------------------------------

function ShowError($message)
{
    $style = 'style="float:left; padding:10px; font: 14px/1.5em Verdana; border:1px solid red;"';
    echo '<div ', $style, '>', $message . ' -- PAGE NOT FOUND</div>';
}

// ----------------------------------------------------------------------------

/** @see FLY_Func */
require_once 'FLY/Func.php';


$controllerName = (empty($_GET['c']) ? 'Home'                      /* Default controller */
                                     : $_GET['c']) . 'Controller';
$actionName     = (empty($_GET['a']) ? 'index'                     /* Default action */
                                     : $_GET['a']) . 'Action';

$func = null;

try
{
    // Try to register the action of the controller
    $func = FLY_Func::FromMethod($controllerName, $actionName);
}
catch(FLY_Func_ClassNotFoundException $ex)
{
    $message = 'The controller `' . $controllerName . '` does not exit';
    ShowError($message);
    exit;
}
catch(FLY_Func_MethodNotFoundException $ex)
{
    $message = 'The action `' . $actionName . '` for the controller `' . $controllerName . '` '
             . 'does not exist';
    ShowError($message);
    exit;
}
catch(FLY_Func_MethodNotPublicException $ex)
{
    $message = 'The action `' . $actionName . '` for the controller `' . $controllerName . '` '
             . 'is private. Could not process the request';
    ShowError($message);
    exit;
}

// Everything is correct, process with
// the call of the controller action.
echo $func->invoke();
Return current item: Func