PHP Classes
elePHPant
Icontem

File: Func-Sample.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Yves Feupi Lepatio  >  Func  >  Func-Sample.php  >  Download  
File: Func-Sample.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Func
Register and call functions
Author: By
Last change: --
Date: 4 years ago
Size: 2,548 bytes
 

Contents

Class file image Download
<?php

/*- THE DEAL:
 * Suppose that for each value of query strings 'c' (controller - the class)
 * and 'a' (action - the method), we want to 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.
 */

// Define some controllers with their 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';

try
{
   
// Try to get and print the result for the specified controller action.
   
echo FLY_Func::FromMethod($controllerName, $actionName)->invoke();
}
catch(
FLY_Func_ClassNotFoundException $ex)
{
   
$message = "The controller '".$controllerName."' does not exit";
   
ShowError($message);
    exit;
}
catch(
FLY_Func_MethodNotFoundException $ex)
{
   
$message = "The action '".$actionName."' of 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;
}