SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP MVC: Improving my page controller

    Hi,

    I am practising MVC to build my website, this is my page controller,

    PHP Code:
    /**
         * this file handles the retrieval and serving of page
         */ 
        
    class controller_core 
        
    {
            public 
    $connection null;
            public 
    $page_model null;
            public 
    $authentication_admin null;
            public 
    $authentication_member null;
            public 
    $constant null;
            
            public function 
    __construct($connection)  
            {  
                
    $this->connection $connection;
                
    $this->page_model = new __page_model($connection);
                
    $this->authentication_admin = new __authentication_admin($connection);
                
    $this->authentication_member = new __authentication_member($connection);
                
    $this->constant = new __constant_model($connection);
            } 
            
            public function 
    render_page($authenticated_admin$authenticated_member$backbone)
            {
                
    # ob_start - Turn on output buffering. It lets you put output into a buffer instead of sending it directly to the client.
                # in computing, a buffer is a region of memory used to temporarily hold data while it is being moved from one place to another.
                
    ob_start();
                
                
    # set variable for users
                
    $user_primary null;
                
    $user_secondary null;
                
    $user_tertiary null;
                
    $user_quartary null;
                
                
    # set variable for members
                
    $member_primary null;
                
    $member_secondary null;
                
    $member_tertiary null;
                
                
    # store the db connection object in the variable
                
    $connection $this->connection;
        
                
    # determine it is a root user or a sub user from the class of authentication_admin
                
    $category_admin $this->authentication_admin->get_admin_category($authenticated_admin);
                
    $category_member $this->authentication_member->get_member_category($authenticated_member);
                
                
    # set either one of the user type to true
                
    switch($category_admin
                {
                    case 
    'user_primary':
                        
    $user_primary true;
                        break;
                    case 
    'user_secondary':
                        
    $user_secondary true;
                        break;
                    case 
    'user_tertiary':
                        
    $user_tertiary true;
                        break;
                    case 
    'user_quartary':
                        
    $user_quartary true;
                        break;
                }
                
                
    # set either one of the user type to true
                
    switch($category_member
                {
                    case 
    'member_primary':
                        
    $member_primary true;
                        break;
                    case 
    'member_secondary':
                        
    $member_secondary true;
                        break;
                    case 
    'member_tertiary':
                        
    $member_tertiary true;
                        break;    
                }
                
                
    # get the constant values from the class of constant
                
    $cst_value_site_title $this->constant->get_constant('site_title')->cst_value;
                
    $cst_value_site_slogan $this->constant->get_constant('site_slogan')->cst_value;
                
    $cst_value_meta_description $this->constant->get_constant('meta_description')->cst_value;
                
    $cst_value_meta_keywords $this->constant->get_constant('meta_keywords')->cst_value;
                
                    
                
    # if $_REQUEST pg exists
                
    if(isset($_REQUEST['pg_url']))
                {
                    
    # show the requested page
                    # always send the value of $authentication_admin to the class of page:
                    # if $authentication_admin has a value, you can see this page even if it is hidden
                    # if $authentication_admin has a value, you can see this page only if it is published
                    
    $page $this->page_model->get_page($_REQUEST['pg_url'],$category_admin);
                    
    $parent $this->page_model->get_parent($page);
                    
                    
    # store the date into the variable
                    
    $parent_id $page->parent_id;
                    
    $tmp_path $page->tmp_path;
                    
                    
    # get the main template/ html document
                    
    include $backbone;
                    
    //print_r($authentication_admin);
                
    }
                else
                {    
                    
    # if no special page is requested, we'll show the default page
                    
    $page $this->page_model->get_page(DEFAULT_PAGE,$category_admin);
                    
    $parent $this->page_model->get_parent($page);
                    
                    
    #store the date into the variable
                    
    $parent_id $page->parent_id;
                    
    $tmp_path $page->tmp_path;
                    
                    
                    
    #get the main template/ html document
                    
    include$backbone;
                    
    #print_r($parent);
                
    }
                
                
    #Return the contents of the output buffer.
                
    return ob_get_contents();
                    
                
    #Clean (erase) the output buffer and turn off output buffering.
                
    ob_end_clean();
            }
        } 

    below is the class that extended from the parent controller class above, but you can see that I am repeating some(lots!) of variables from the parent class,

    PHP Code:
    class controller_extended extends controller_core
            
    {
                function 
    __construct($connection
                {
                    
    parent::__construct($connection);
                }
                
                public function 
    render_page($authenticated_admin$authenticated_member$backbone
                {
                    
    # set variable for users
                    
    $user_primary null;
                    
    $user_secondary null;
                    
    $user_tertiary null;
                    
    $user_quartary null;
                    
                    
    # set variable for members
                    
    $member_primary null;
                    
    $member_secondary null;
                    
    $member_tertiary null;
                    
                    
    # store the db connection object in the variable
                    
    $connection $this->connection;
            
                    
    # determine it is a root user or a sub user from the class of authentication_admin
                    
    $category_admin $this->authentication_admin->get_admin_category($authenticated_admin);
                    
    $category_member $this->authentication_member->get_member_category($authenticated_member);
                    
                    
    # if $_REQUEST tag_name exists 
                    
    if(isset($_REQUEST['tag_name']))
                    {
                        
    # get the value from the request
                        
    if(isset($_REQUEST['pg_url'])) $pg_url $_REQUEST['pg_url'];
                        if(isset(
    $_REQUEST['tag_name'])) $tag_name $_REQUEST['tag_name'];
                        if(isset(
    $_REQUEST['str_id'])) $str_id $_REQUEST['str_id'];
                        
                        
    # show the requested page
                        # always send the value of $authentication_admin to the class of page:
                        # if $authentication_admin has a value, you can see this page even if it is hidden
                        # if $authentication_admin has a value, you can see this page only if it is published
                        
    $page $this->page_model->get_page($pg_url,$category_admin);
                        
    $parent $this->page_model->get_parent($page);
                        
                        if(empty(
    $str_id))
                        {
                            
    # get the included template
                            
    switch($pg_url
                            {
                                case 
    'publications':
                                    
    $tmp_path 'resources_publication_subitem.php';
                                    break;
                                case 
    'tender-opportunities':
                                    
    $tmp_path 'resources_tender_opportunitie_subitem.php';
                                    break;
                                case 
    'research-topics':
                                    
    $pg_url $tag_name;
                                    
    $tmp_path 'item_content_research_topics.php';
                                    break;
                                case 
    'videos':
                                    
    $tmp_path 'video_tagged.php';
                                    break;
                                case 
    'forum':
                                    
    $tmp_path 'forum_subitem.php';
                                    break;    
                                case 
    'ener-exchange':
                                    
    $tmp_path 'exchange_subitem.php';
                                    break;
                            }
                        }
                        else
                        {
                            
    # store the date into the variable
                            
    $parent_id $page->parent_id;
                            
                            
    # get the included template
                            
    switch($pg_url
                            {
                                case 
    'forum':
                                    
    $tmp_path 'item_forum.php';
                                    break;    
                                case 
    'ener-exchange':
                                    
    $tmp_path 'item_exchange.php';
                                    break;
                            }
                            
                        }
                        
                        
    # get the main template/ html document
                        
    include $backbone;
                        
                    }
                    else
                    {
                        
    parent::render_page($authenticated_admin$authenticated_member$backbone);
                    }
                }
            } 
    How can I work around this repetitive variables? Maybe I have made the controller incorrectly?

    Thanks.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code is good enough for a basic MVC pattern to start with. There are a few things I would take in consideration:

    1. I would split the render function in 2 functions to separate the actions. One to read the model and populate the values to be displayed and the other one to select and display the page. That way you could ensure the controller implementation will "decouple" even more between model code and view code.
    2. I would make a change to get rid of the parent code in the if block. I'm not sure if it violates any oo design principles, but for sure this is not the way classes should be used.
    3. Replace the case block with a structured code(maybe a state pattern or another design pattern would fit well; if not you could make it like an associative array with pg_url as key and php file name as value) or at least move it in a separate file to reuse it in many page controllers.
    4. Handle now the relevant $_get and $_post variables or add an automatic mechanism to sanitize input. It's better to do it now, than to modify later the code.

  3. #3
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by adiian View Post
    The code is good enough for a basic MVC pattern to start with. There are a few things I would take in consideration:

    1. I would split the render function in 2 functions to separate the actions. One to read the model and populate the values to be displayed and the other one to select and display the page. That way you could ensure the controller implementation will "decouple" even more between model code and view code.
    2. I would make a change to get rid of the parent code in the if block. I'm not sure if it violates any oo design principles, but for sure this is not the way classes should be used.
    3. Replace the case block with a structured code(maybe a state pattern or another design pattern would fit well; if not you could make it like an associative array with pg_url as key and php file name as value) or at least move it in a separate file to reuse it in many page controllers.
    4. Handle now the relevant $_get and $_post variables or add an automatic mechanism to sanitize input. It's better to do it now, than to modify later the code.
    Thanks for the reply, Adiian.

    I have split the code into classes like below, I hope that I am not violating the oo principles?

    PHP Code:
    /**
     * get the site constant information
     *
     * @var $constant
     * @var $site_title
     * @var $site_slogan
     * @var $site_meta_description
     * @var $site_meta_keywords
     *
     * @param object $connection
     *
     * @func __construct
     *
     * @return string
     *
    */ 
    class helper_constant 
    {
        
    # set the property/ variable of this class
        
    public $constant null;
        public 
    $site_title null;
        public 
    $site_slogan null;
        public 
    $site_meta_description null;
        public 
    $site_meta_keywords null;
        
        public function 
    __construct($connection)  
        {  
            
    $this->connection $connection;
            
    $this->constant = new constant($connection);
            
            
    # get the constant values from the class of constant
            
    $this->site_title $this->constant->get_constant('site_title')->cst_value;
            
    $this->site_slogan $this->constant->get_constant('site_slogan')->cst_value;
            
    $this->site_meta_description $this->constant->get_constant('meta_description')->cst_value;
            
    $this->site_meta_keywords $this->constant->get_constant('meta_keywords')->cst_value;
        }
    }

    /**
     * get the authentication member's/ user's complete info
     *
     * @var $authentication_admin
     * @var $authentication_member
     * @var $user_primary
     * @var $user_secondary
     * @var $user_tertiary
     * @var $user_quartary
     * @var $member_primary
     * @var $member_secondary
     * @var $member_tertiary
     * @var $info_admin
     * @var $info_member
     * @var $category_admin
     * @var $category_member
     *
     * @param object $connection
     * @param string $authenticated_admin
     * @param string $authenticated_member
     *
     * @func __construct
     *
     * @return string
     * @return boolean
     *
    */ 
    class helper_authentication 
    {
        
    # set the property/ variable of this class
        
    public $authentication_admin null;
        public 
    $authentication_member null;
        public 
    $user_primary null;
        public 
    $user_secondary null;
        public 
    $user_tertiary null;
        public 
    $user_quartary null;
        public 
    $member_primary null;
        public 
    $member_secondary null;
        public 
    $member_tertiary null;
        public 
    $info_admin null;
        public 
    $info_member null;
        public 
    $category_admin null;
        public 
    $category_member null;
        
        public function 
    __construct($connection,$authenticated_admin,$authenticated_member)  
        { 
            
    $this->authentication_admin = new __authentication_admin($connection);
            
    $this->authentication_member = new __authentication_member($connection);
            
    $this->category_admin $this->authentication_admin->get_admin_category($authenticated_admin);
            
    $this->category_member $this->authentication_member->get_member_category($authenticated_member);
            
    $this->info_admin $this->authentication_admin->get_admin_info($authenticated_admin);
            
    $this->info_member $this->authentication_member->get_member_info($authenticated_member);
            
            
    # set either one of the user type to true
            
    switch($this->category_admin
            {
                case 
    'user_primary':
                    
    $this->user_primary true;
                    break;
                case 
    'user_secondary':
                    
    $this->user_secondary true;
                    break;
                case 
    'user_tertiary':
                    
    $this->user_tertiary true;
                    break;
                case 
    'user_quartary':
                    
    $this->user_quartary true;
                    break;
            }
            
            
    # set either one of the user type to true
            
    switch($this->category_member
            {
                case 
    'member_primary':
                    
    $this->member_primary true;
                    break;
                case 
    'member_secondary':
                    
    $this->member_secondary true;
                    break;
                case 
    'member_tertiary':
                    
    $this->member_tertiary true;
                    break;    
            }
        }
    }

    /**
     * handle the retrieval and serving of page
     *
     * @var $connection
     * @var $authentication
     * @var $model_page
     * @var $model_configuration
     * @var $helper_constant
     *
     * @param object $connection
     * @param string $authenticated_admin
     * @param string $authenticated_member
     * @param string $backbone
     *
     * @func __construct
     * @func render_page
     *
    */ 
    class controller_core 
    {
        public 
    $connection null;
        public 
    $model_page null;
        public 
    $model_configuration null;
        public 
    $helper_constant null;
        public 
    $authentication null;
        
        public function 
    __construct($connection,$authentication)  
        {  
            
    $this->connection $connection;
            
    $this->authentication $authentication;
            
    $this->model_page = new __page_model($connection);
            
    $this->model_configuration = new __configuration_cms_model($connection);
            
    $this->helper_constant = new helper_constant($connection);
        }
        
        public function 
    render_page($backbone)
        {
            
    # store the db connection object in the variable
            
    $connection $this->connection;
            
    $authentication $this->authentication;
            
    $site $this->helper_constant;

            
    # get the hide value (0 or 1) from the class of model_configuration
            
    $cfg_hide_edit_as_you_go $this->model_configuration->get_configuration_cms('edit_as_you_go')->cfg_hide;

            
    # check if it is 0 then set it to true
            
    if ($cfg_hide_edit_as_you_go == '0'$edit_as_you_go true;
            else 
    $edit_as_you_go null;
            
            if(isset(
    $_REQUEST['pg_url']))
            {
                
    # show the requested page
                # always send the value of $authentication_admin to the class of page:
                # if $authentication_admin has a value, you can see this page even if it is hidden
                # if $authentication_admin has a value, you can see this page only if it is published
                
    $page $this->model_page->get_page($_REQUEST['pg_url'],$authentication->category_admin);
                
    $parent $this->model_page->get_parent($page);
                
                
    # store the date into the variable
                
    $parent_id $page->parent_id;
                
    $tmp_path $page->tmp_path;
                
                
    # get the main template/ html document
                
    include $backbone;
                
    //print_r($authentication_admin);
            
    }
            else
            {    
                
    # if no special page is requested, we'll show the default page
                
    $page $this->model_page->get_page(DEFAULT_PAGE,$authentication->category_admin);
                
    $parent $this->model_page->get_parent($page);
                
                
    #store the date into the variable
                
    $parent_id $page->parent_id;
                
    $tmp_path $page->tmp_path;
                
                
                
    #get the main template/ html document
                
    include$backbone;
                
    #print_r($parent);
            
    }
        }
    }

    /**
     * controller extended class 
     * handle the retrieval and serving of page
     *
     * @param object $connection
     * @param string $authenticated_admin
     * @param string $authenticated_member
     * @param string $backbone
     *
     * @func __construct
     * @func render_page
     *
    */
    class controller_extended extends controller_core
    {
        function 
    __construct($connection,$authentication
        {
            
    parent::__construct($connection,$authentication);
        }
        
        public function 
    render_page($backbone
        {    
            
    # store the db connection object in the variable
            
    $connection $this->connection;
            
    $authentication $this->authentication;
            
    $site $this->helper_constant;

            
    # get the hide value (0 or 1) from the class of model_configuration
            
    $cfg_hide_edit_as_you_go $this->model_configuration->get_configuration_cms('edit_as_you_go')->cfg_hide;

            
    # check if it is 0 then set it to true
            
    if ($cfg_hide_edit_as_you_go == '0'$edit_as_you_go true;
            else 
    $edit_as_you_go null;
            
            
    # if $_REQUEST tag_name exists 
            
    if(isset($_REQUEST['tag_name']))
            {
                
    # get the value from the request
                
    if(isset($_REQUEST['pg_url'])) $pg_url $_REQUEST['pg_url'];
                if(isset(
    $_REQUEST['tag_name'])) $tag_name $_REQUEST['tag_name'];
                if(isset(
    $_REQUEST['str_id'])) $str_id $_REQUEST['str_id'];
                
                
    # show the requested page
                # always send the value of $authentication_admin to the class of page:
                # if $authentication_admin has a value, you can see this page even if it is hidden
                # if $authentication_admin has a value, you can see this page only if it is published
                
    $page $this->model_page->get_page($pg_url,$authentication->category_admin);
                
    $parent $this->model_page->get_parent($page);
                
                if(empty(
    $str_id))
                {
                    
    # get the included template
                    
    switch($pg_url
                    {
                        case 
    'publications':
                            
    $tmp_path 'resources_publication_subitem.php';
                            break;
                        case 
    'tender-opportunities':
                            
    $tmp_path 'resources_tender_opportunitie_subitem.php';
                            break;
                        case 
    'research-topics':
                            
    $pg_url $tag_name;
                            
    $tmp_path 'item_content_research_topics.php';
                            break;
                        case 
    'videos':
                            
    $tmp_path 'video_tagged.php';
                            break;
                        case 
    'forum':
                            
    $tmp_path 'forum_subitem.php';
                            break;    
                        case 
    'ener-exchange':
                            
    $tmp_path 'exchange_subitem.php';
                            break;
                    }
                }
                else
                {
                    
    # store the date into the variable
                    
    $parent_id $page->parent_id;
                    
                    
    # get the included template
                    
    switch($pg_url
                    {
                        case 
    'forum':
                            
    $tmp_path 'item_forum.php';
                            break;    
                        case 
    'ener-exchange':
                            
    $tmp_path 'item_exchange.php';
                            break;
                    }
                    
                }
                
                
    # get the main template/ html document
                
    include $backbone;
                
            }
            
            
    #if $_REQUEST cat_name or $_REQUEST pg_archive exists 
            
    if((isset($_REQUEST['cat_name']))||(isset($_REQUEST['pg_archive'])))
            {
                
    # store the value
                
    if(isset($_REQUEST['cat_name'])) $cat_name $_REQUEST['cat_name'];
                if(isset(
    $_REQUEST['pg_archive'])) $pg_archive str_replace('-'' '$_REQUEST['pg_archive']);
                
                
    # show the requested page
                # always send the value of $authentication_admin to the class of page:
                # if $authentication_admin has a value, you can see this page even if it is hidden
                # if $authentication_admin has a value, you can see this page only if it is published
                
    $page $this->model_page->get_page('portfolio',$authentication->category_admin);
                
    $parent $this->model_page->get_parent($page);
                
                
    # store the date into the variable
                
    $parent_id $page->parent_id;
                
    $tmp_path 'blog.php';
                
                
    # get the main template/ html document
                
    include $backbone;
            }
            
            
    #if $_REQUEST cat_key exists 
            
    elseif(isset($_REQUEST['cnt_key']))
            {
                
    # store the value
                
    if(isset($_REQUEST['cnt_key'])) $cnt_key $_REQUEST['cnt_key'];
                
                
    # show the requested page
                # always send the value of $authentication_admin to the class of page:
                # if $authentication_admin has a value, you can see this page even if it is hidden
                # if $authentication_admin has a value, you can see this page only if it is published
                
    $page $this->model_page->get_page('home',$authentication->category_admin);
                
    $parent $this->model_page->get_parent($page);
                
                
    # store the date into the variable
                
    $parent_id $page->parent_id;
                
    $tmp_path 'verify.contact.php';
                
                
    # get the main template/ html document
                
    include $backbone;
            }

            
    # if $_REQUEST mem_verify exists 
            
    elseif(isset($_REQUEST['mem_verify']))
            {
                
    # store the value
                
    if(isset($_REQUEST['mem_verify'])) $mem_verify $_REQUEST['mem_verify'];
                
                
    # show the requested page
                # always send the value of $authentication_admin to the class of page:
                # if $authentication_admin has a value, you can see this page even if it is hidden
                # if $authentication_admin has a value, you can see this page only if it is published
                
    $page $this->model_page->get_page('home',$authentication->category_admin);
                
    $parent $this->model_page->get_parent($page);
                
                
    # store the date into the variable
                
    $parent_id $page->parent_id;
                
    $tmp_path 'verify.member.php';
                
                
    # get the main template/ html document
                
    include $backbone;
            }
            
            
    # if $_REQUEST msg_key exists 
            
    elseif(isset($_REQUEST['msg_key']))
            {
                
    # store the value
                
    if(isset($_REQUEST['msg_key'])) $msg_key $_REQUEST['msg_key'];
                
                
    # show the requested page
                # always send the value of $authentication_admin to the class of page:
                # if $authentication_admin has a value, you can see this page even if it is hidden
                # if $authentication_admin has a value, you can see this page only if it is published
                
    $page $this->model_page->get_page('home',$authentication->category_admin);
                
    $parent $this->model_page->get_parent($page);
                
                
    # store the date into the variable
                
    $parent_id $page->parent_id;
                
    $tmp_path 'verify.message.php';
                
                
    # get the main template/ html document
                
    include $backbone;
            }
                
            
    # if $_REQUEST unsubscribe_key exists 
            
    elseif(isset($_REQUEST['unsubscribe_key']))
            {
                
    # store the value
                
    if(isset($_REQUEST['unsubscribe_key'])) $unsubscribe_key $_REQUEST['unsubscribe_key'];
                
                
    # show the requested page
                # always send the value of $authentication_admin to the class of page:
                # if $authentication_admin has a value, you can see this page even if it is hidden
                # if $authentication_admin has a value, you can see this page only if it is published
                
    $page $this->model_page->get_page('home',$category_admin);
                
    $parent $this->model_page->get_parent($page);
                
                
    # store the date into the variable
                
    $parent_id $page->parent_id;
                
    $tmp_path 'unsubscribe.contact.php';
                
                
    # get the main template/ html document
                
    include $backbone;
            }
            
            
    # if $_REQUEST unsubscribe_key exists 
            
    elseif(isset($_REQUEST['content_option']))
            {
                
    //echo $_REQUEST['content_option'];
                # store the value
                
    if(($_REQUEST['content_option'] == 'html')) $html true;
                if((
    $_REQUEST['content_option'] == 'flash')) $flash true;
                
                
    # show the requested page
                # always send the value of $authentication_admin to the class of page:
                # if $authentication_admin has a value, you can see this page even if it is hidden
                # if $authentication_admin has a value, you can see this page only if it is published
                
    $page $this->model_page->get_page('asia chic',$authentication->category_admin);
                
    $parent $this->model_page->get_parent($page);
                
                
    # store the date into the variable
                
    $parent_id $page->parent_id;
                
    $tmp_path 'global.php';
                
                
    # get the main template/ html document
                
    include $backbone;
            }
            
            
    # run the parent method instead
            
    else
            {
                
    parent::render_page($backbone);
            }
        }

    Thanks

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would strongly recommend to replace the if blocks from controller_extended render method. Here is what you have:

    PHP Code:
    function render()
    {
         
    some generic code
         
    if((isset($_REQUEST['cat_name']))||(isset($_REQUEST['pg_archive'])))
            
    bla bla
         
    else if
            
    bla bla
         
    else 
            
    parent::render_page($backbone);

    Let's consider the following approach:

    the generic code will be moved in base class, then for each if block we'll create a controller class. The code that is implemented in each if block will be moved in the render method of each controller. Let's consider one controller called CategoryController.

    A factory will give us the required controller object. Let's consider a simple static factory(similar to the if blocks):

    PHP Code:
    if((isset($_REQUEST['cat_name']))||(isset($_REQUEST['pg_archive'])))
         return new 
    CategoryController
    else if ...
    ...
    else
         return new 
    BaseController() 
    then in the main block:

    PHP Code:
    $controller $factory->createController();
    $controller->render(); 
    In this case the factory will decide which controller should be used. Main code will be the same, for adding new controllers you will have to change only the factory class and to add a new controller class. The advantage will be that existing controllers and main code will remain unchanged so you don't have to change/test them for each new concrete controller(Open Closed Principle).

  5. #5
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by adiian View Post
    I would strongly recommend to replace the if blocks from controller_extended render method. Here is what you have:

    PHP Code:
    function render()
    {
         
    some generic code
         
    if((isset($_REQUEST['cat_name']))||(isset($_REQUEST['pg_archive'])))
            
    bla bla
         
    else if
            
    bla bla
         
    else 
            
    parent::render_page($backbone);

    Let's consider the following approach:

    the generic code will be moved in base class, then for each if block we'll create a controller class. The code that is implemented in each if block will be moved in the render method of each controller. Let's consider one controller called CategoryController.

    A factory will give us the required controller object. Let's consider a simple static factory(similar to the if blocks):

    PHP Code:
    if((isset($_REQUEST['cat_name']))||(isset($_REQUEST['pg_archive'])))
         return new 
    CategoryController
    else if ...
    ...
    else
         return new 
    BaseController() 
    then in the main block:

    PHP Code:
    $controller $factory->createController();
    $controller->render(); 
    In this case the factory will decide which controller should be used. Main code will be the same, for adding new controllers you will have to change only the factory class and to add a new controller class. The advantage will be that existing controllers and main code will remain unchanged so you don't have to change/test them for each new concrete controller(Open Closed Principle).
    Thanks. It's a great principle. But I cannot get it applied to my code as I have to pass two objects into the controller each time:
    1. db connection object
    2. user authentication object

    PHP Code:
    $controller = new factory($connection,$authentication);
    $controller->get_controller($backbone AP_SITE.RP_VIEW_LOCAL.'index.php');


    /**
     * controller extended class 
     * handle the retrieval and serving of page
     *
     * @param object $connection
     * @param string $authenticated_admin
     * @param string $authenticated_member
     * @param string $backbone
     *
     * @func __construct
     * @func render_page
     *
    */
    class controller_tag extends controller_base
    {
        function 
    __construct($connection,$authentication
        {
            
    parent::__construct($connection,$authentication);
        }
        
        public function 
    render_page($backbone
        {    
            
    # store the db connection object in the variable
            
    $connection $this->connection;
            
    $authentication $this->authentication;
            
    $site $this->helper_constant;

            
    # get the hide value (0 or 1) from the class of model_configuration
            
    $cfg_hide_edit_as_you_go $this->model_configuration->get_configuration_cms('edit_as_you_go')->cfg_hide;

            
    # check if it is 0 then set it to true
            
    if ($cfg_hide_edit_as_you_go == '0'$edit_as_you_go true;
            else 
    $edit_as_you_go null;
            
            
    # get the value from the request
            
    if(isset($_REQUEST['pg_url'])) $pg_url $_REQUEST['pg_url'];
            if(isset(
    $_REQUEST['tag_name'])) $tag_name $_REQUEST['tag_name'];
            if(isset(
    $_REQUEST['str_id'])) $str_id $_REQUEST['str_id'];
            
            
    # show the requested page
            # always send the value of $authentication_admin to the class of page:
            # if $authentication_admin has a value, you can see this page even if it is hidden
            # if $authentication_admin has a value, you can see this page only if it is published
            
    $page $this->model_page->get_page($pg_url,$authentication->category_admin);
            
    $parent $this->model_page->get_parent($page);
            
            if(empty(
    $str_id))
            {
                
    # get the included template
                
    switch($pg_url
                {
                    case 
    'publications':
                        
    $tmp_path 'resources_publication_subitem.php';
                        break;
                    case 
    'tender-opportunities':
                        
    $tmp_path 'resources_tender_opportunitie_subitem.php';
                        break;
                    case 
    'research-topics':
                        
    $pg_url $tag_name;
                        
    $tmp_path 'item_content_research_topics.php';
                        break;
                    case 
    'videos':
                        
    $tmp_path 'video_tagged.php';
                        break;
                    case 
    'forum':
                        
    $tmp_path 'forum_subitem.php';
                        break;    
                    case 
    'ener-exchange':
                        
    $tmp_path 'exchange_subitem.php';
                        break;
                }
            }
            else
            {
                
    # store the date into the variable
                
    $parent_id $page->parent_id;
                
                
    # get the included template
                
    switch($pg_url
                {
                    case 
    'forum':
                        
    $tmp_path 'item_forum.php';
                        break;    
                    case 
    'ener-exchange':
                        
    $tmp_path 'item_exchange.php';
                        break;
                }
                
            }
            
            
    # get the main template/ html document
            
    include $backbone;
        }
    }

    /**
     * a factory will decide which controller should be used
     *
    */
    class factory extends controller_base
    {
        function 
    __construct($connection,$authentication
        {
            
    parent::__construct($connection,$authentication);
        }
        
        public function 
    get_controller($backbone
        {
            
    # if $_REQUEST tag_name exists 
            
    if(isset($_REQUEST['tag_name']))
            {
                return new 
    controller_tag($connection,$authentication);
            }
            
            
            
    # run the parent method instead
            
    else
            {
                return new 
    controller_base($connection,$authentication);
            }
        }


    Below is how I evoke the controller originally.

    PHP Code:
    $controller = new controller_extended($connection,$authentication);
    $controller->render_page($backbone AP_SITE.RP_VIEW_LOCAL.'index.php'); 
    It works fine with this but I would love to follow the correct principle

    Thanks.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    doesn't really matter if you has to pass parameters. You could add the connection as a member in the base controller class. That way the connection will be available from all the extended controller classes. You can set the connection details inside the factory after you instantiate the object.


    PHP Code:
    if((isset($_REQUEST['cat_name']))||(isset($_REQUEST['pg_archive'])))
         
    $controller = new CategoryController
    else if ...
    ...
    else
         
    $controller = new BaseController()

    $controller->setConnection(..connection details.); 


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
  •