Welcome Guest, Not a member yet? Register   Sign In
404 Page Not Found using MY_Controller class
#1

[eluser]Hitankar[/eluser]
Hi!

I am trying to implement the concept of using common header and footer for all the pages. I have look thru and found out that this can be done using a MY_Controller class derived from the base Controller class, and inheriting this class for whatever class I am creating.

I see the the view for the header and footer opens up good. However, I see a 404 Page Not Found error message as well. Could help me out where I am going wrong.


The html markup for the resulting page I add in the reply.

Code:
$<html>
<head>
<title>404 Page Not Found</title>
<style type="text/css">

body {
background-color:    #fff;
margin:                40px;
font-family:        Lucida Grande, Verdana, Sans-serif;
font-size:            12px;
color:                #000;
}

#content  {
border:                #999 1px solid;
background-color:    #fff;
padding:            20px 20px 12px 20px;
}

h1 {
font-weight:        normal;
font-size:            14px;
color:                #990000;
margin:             0 0 4px 0;
}
</style>
</head>
<body>
    <div id="content">
        <h1>404 Page Not Found</h1>
        <p>The page you requested was not found.</p>    </div>

&lt;/body&gt;
&lt;/html&gt;
<h1>Home</h1>
&lt;title&gt;Home&lt;/title&gt;
&lt;link rel="stylesheet" href="/assets/css/main.css" type="text/css"&gt;


This is footer
#2

[eluser]Hitankar[/eluser]
Here is the MY_Controller class I used.

Code:
&lt;?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;

Please help me out, thanks.




Theme © iAndrew 2016 - Forum software by © MyBB