SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Controller PreDespatch

    Hi,

    I'm trying to set up a controller with PreDispatch and looking for the the best solution to implement this. It needs to run 2 PreDispatches such as Login and Permission.

    I also want to implement the composite pattern in the controller.

    I have found this example but now sure how to add a composite with it.

    PHP Code:
    <?php
    if (! class_exists('A_Locator')) include 'A/Locator.php';
    if (! 
    class_exists('A_DL')) include 'A/DL.php';

    class 
    A_Controller_Front {
        var 
    $mapper;
        var 
    $error_action;
        var 
    $pre_methods;

        function 
    A_Controller_Front(&$mapper, &$error_action$pre_methods=array()) {
            
    $this->mapper =& $mapper;
            
    $this->error_action =& $error_action;
            
    $this->pre_methods $pre_methods;
        }

        function 
    addPreMethod($method, &$action) {
            
    $this->pre_methods[$method] =& $action;
        }

        function 
    run(&$locator) {
            
    $action =& $this->mapper->doMapping($locator);
            while (
    $action) {
                
    $class  $this->mapper->getClass($action->class);
                
    $method $this->mapper->getMethod($action->method);
                if (
    $action->dir == '') {
                    
    $dir $this->mapper->getDir();
                } else {
                    
    $dir $action->dir;
                }
                
    $action null;
                
    $result $locator->loadClass($class$dir);
                if (
    $result) {
                    
    $class str_replace('-''_'$class);
                    
    $controller =& new $class($locator);
        
                    if (
    $this->pre_methods) {
                        foreach (
    array_keys($this->pre_methods) as $pre_method) {
                            if (
    method_exists($controller$pre_method)) {
                                
    $change_action =& $controller->{$pre_method}($locator);
                                if (
    $change_action) {
                                    if (
    is_object($change_action)) {
                                        
    $action =& $change_action;
                                    } elseif (
    is_object($this->pre_methods[$pre_method])) {
                                        
    $action =& $this->pre_methods[$pre_method];
                                    } else {
                                        
    $action =& $this->error_action;
                                    }
                                    continue 
    2;
                                }
                            }
                        }
                    }
                    
                    if (! 
    method_exists($controller$method)) {
                        
    $method $this->mapper->default_method;
                    }
                    if (
    method_exists($controller$method)) {
                        
    $action =& $controller->{$method}($locator);
                    }
                } else {
                    
    $action =& $this->error_action;
                }
            }
        }

    }
    ?>

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Filter chain!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •