Welcome Guest, Not a member yet? Register   Sign In
Ok, I have MY_Controller but now I want to create Front_End_Controller
#1

[eluser]kyleect[/eluser]
I've created MY_Controller inside of Application/Libraries and now I want to create a front end controller that extends off MY_Controller. This will be used by any controller that outputs HTML. I did some searching but didn't find any info on where to place that front end controller file and how CI will handle my extending off that controller.

I guess I have a related question, do you see this is as needed. I have a lot of code in my MY_Controller that has to do with prepping views and outputting HTML however not all my controllers would output HTML. Take a look:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* MY_Controller
*
* Consider this the application controller.
*
* @package Firestarter
* @subpackage Core Extension
* @author Kylee Tilley <[email protected]>
*/

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

/**
* @package Firestarter
* @subpackage Core Extension
**/

/* TODO:

    I need to create an assest management library so clean up the code in $this->embed_assests

*/

/*

    NOTE: Using this controller will change how you are using views.
    You will need to load a view in to a variable called $this->views['content']

*/

    class MY_Controller extends Controller
    {
        protected $view_data;
        protected $assests;
        protected $views;
        
        public function __construct()
        {
            parent::Controller();
            
            $this->config->load('application', TRUE);            
            
            //SET DEFAULT VIEW VARIABLES
            $this->view_data['page_title'] = 'Home';
            $this->view_data['application'] = $this->config->item('application');
                //To help standardize page specific CSS, we are setting the body id of the page to "controller__method"            
            $this->view_data['body_id'] = ($this->router->method = 'index') ? ($this->router->class) : ($this->router->class).'__'.(($this->router->method));
            
            //SET DEFAULT ASSESTS
            
            /* ---- Stylesheets ----
            
                In order to include a stylesheet on to the page, you must define it in the $this->assests['css'] variable.
                
                By default, you will set just a single string. You can set an array if you need to set options like media type.
                
                EXAMPLE:
                
                    $this->assests['css'][] = array('print', media="print");
            
            */
            $this->assests['css'] = array('main');
            $this->assests['javascript'] = array();

            //LOAD VIEWS
            //The header and footer views have to be loaded in to variables so they can be outputted on the controllers destruction
            $this->views['header'] = $this->load->view('header', $this->view_data, TRUE);
            $this->views['footer'] = $this->load->view('footer', $this->view_data, TRUE);
            $this->views['view_content'] = NULL;
        }
        
        private function embed_assests()
        {
            $stylesheets = NULL;
            $javascripts = NULL;
            
            
            
            //STYLESHEETS
            
            if(!empty($this->assests['css']))
            {            
                foreach($this->assests['css'] as $stylesheet)
                {
                    if(is_array($stylesheet))
                    {
                        if(isset($stylesheet['media']))
                        {
                            $stylesheets .= '&lt;link rel="stylesheet" href="'.$this-&gt;view_data['application']['assests_dir'].'css/'.$stylesheet[0].'.css" media="'.$stylesheet['media'].'" type="text/css">'."\n";
                        }
                        else
                        {
                            $stylesheets .= '&lt;link rel="stylesheet" href="'.$this-&gt;view_data['application']['assests_dir'].'css/'.$stylesheet.'.css" type="text/css">'."\n";
                        }
                    }
                    else
                    {
                        $stylesheets .= '&lt;link rel="stylesheet" href="'.$this-&gt;view_data['application']['assests_dir'].'css/'.$stylesheet.'.css" type="text/css">'."\n";
                    }
                }
            }
            
            
                
            //JAVASCRIPT
            
            if(!empty($this->assests['javascript']))
            {    
                foreach($this->assests['javascript'] as $javascript)
                {
                    $javascripts .= '[removed][removed]'."\n";
                }
            }
            
            
            
            $this->views['header'] = str_replace('#load_css#', $stylesheets, $this->views['header']);
            $this->views['header'] = str_replace('#load_javascript#', $javascripts, $this->views['header']);    
        }
        
        public function __destruct()
        {    
            $this->embed_assests();
            
            echo $this->views['header'];
            echo $this->views['view_content'];
            echo $this->views['footer'];
        }
    }
?&gt;


Messages In This Thread
Ok, I have MY_Controller but now I want to create Front_End_Controller - by El Forum - 09-01-2009, 06:19 PM



Theme © iAndrew 2016 - Forum software by © MyBB