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 <kylee.tilley@gmail.com>
*/

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

/**
* @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;

#2
[eluser]brianw1975[/eluser]
heheh not willing to look through all the code to evaluate it, but you should be able to make "MY_FEC.php" and put it in application/libraries/

and for code do the same as before:

Code:
&lt;?php

class MY_FEC extends MY_Controller {

    function MY_FEC(){
        parent::MY_Controller();
    }

    function something(){
        echo "i *should* be accessible";
    }
}

and in the controllers that use the front end controller you will have:

Code:
&lt;?php
class thatonepage extends MY_FEC{
    function thatonepage(){
        parent::MY_FEC();
    }

    function index(){
        parent::something();
        $this->something();
        echo "I should work";
    }
}

note that MY_FEC was arbitrary...

#3
[eluser]kyleect[/eluser]
That did not work. My front controller couldn't be found, which means I could include it manually but is that the only way to do it? Is there a way to do this natively within CI?

#4
[eluser]brianw1975[/eluser]
Interesting... I thought this would work, I'm surprised that once the base Controller is extended it wouldn't allow further extending...

anyways, sit back and relax, someone else will have the correct answer.

#5
[eluser]devbro[/eluser]
I had this problem before you only have one of the following options:
1. put all classes that you extend from Controller in MY_controller.php
2. put them in their own file and do include inside MY_controller.php

These are the only solutions at this point.

worst come to worse look up HMVC and matchbox


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.