Welcome Guest, Not a member yet? Register   Sign In
Same tasks in each controller: controller inheritance?
#10

[eluser]gon[/eluser]
I also use inheritance from a base controller.

The base controller has a _remap method, so every action will call _remap first.

The _remap method checks the controller and the action, and checks if the user has permission to execute that controller / action.
If access control is successful, the controller action is executed.

Note that _remap only check ACL. Auth is checked in the base_controller constructor, and we get redirected out if not successful.

This is the _remap method in base_controller:

Code:
/////////////////////////////////////////////
    // catch-all function                        //
    //                                            //
    // dispatches to action after checking ACL //
    /////////////////////////////////////////////
    function _remap($action) {
        
        if (!method_exists($this, $action))
            show_404();
        
        $segments = $this->uri->segment_array();
        if (count($segments)<2)
            show_404();
        
        $controller = $segments[2];        
        $args = array_slice($segments, 3);
        
        if ($this->menu_section) {
            $menu_section = $this->menu_section;    
        } else {
            $menu_section = $controller;
        }
        $this->cl_navi_model->set_route($menu_section, $action, $args);
                    
        $authorization = $this->cl_acl->getAuthorization($controller, $action);                
        if (!$authorization) {            
            show_404();
        }
        
        if (method_exists($this, $controller."_init")) {
            call_user_func_array(array($this, $controller."_init"), array($action, $args));
        }    

        ob_start();
        call_user_func_array(array($this, $action), $args);
        $content = ob_get_contents();        
        ob_end_clean();
        
        echo $this->cl_layout->render($content);
    }

I also configure the menu, and call an optional XXX_init method which some controllers implement, but you can skip that.

The ob_start(), ob_end() is another part you can skip if you want. I use it for getting the content that the action renders, and injecting it into the common layout.

Regards.


Messages In This Thread
Same tasks in each controller: controller inheritance? - by El Forum - 10-10-2008, 05:57 AM
Same tasks in each controller: controller inheritance? - by El Forum - 10-10-2008, 06:16 AM
Same tasks in each controller: controller inheritance? - by El Forum - 10-10-2008, 06:40 AM
Same tasks in each controller: controller inheritance? - by El Forum - 10-10-2008, 06:45 AM
Same tasks in each controller: controller inheritance? - by El Forum - 10-10-2008, 06:48 AM
Same tasks in each controller: controller inheritance? - by El Forum - 10-10-2008, 09:24 AM
Same tasks in each controller: controller inheritance? - by El Forum - 10-10-2008, 09:41 AM
Same tasks in each controller: controller inheritance? - by El Forum - 10-10-2008, 09:54 AM
Same tasks in each controller: controller inheritance? - by El Forum - 10-11-2008, 01:26 AM
Same tasks in each controller: controller inheritance? - by El Forum - 10-11-2008, 03:38 AM



Theme © iAndrew 2016 - Forum software by © MyBB