Welcome Guest, Not a member yet? Register   Sign In
Dynamic Footer problem
#1

[eluser]iainco[/eluser]
Hey guys, currently making a project that is based on sports news, with users being able to rate articles and so on.

The footer for each page will display articles that users have archived, top rated articles, tags for the current sport and a little search form.

This footer is used on all pages.

The problem arises where users who have not registered visit a publicly accessible page - (for example the 'About' page)

Footer view code
Code:
<? if($this->session->userdata('email')): ?>
// Show content that registered users should see
<? endif; ?>

</div>
&lt;/body&gt;
&lt;/html&gt;


Code for making the footer content available.
Code:
function __construct()
    {
        parent::__construct();

        if($this->session->userdata('email'))
        {
            $this->load->model('FooterModel');
        }
    }
    
    function index()
    {
        $footerContent = array
        (
            'archived'     => $this->FooterModel->fetchArchived(),
            'tags'         => $this->FooterModel->fetchTags(),
            'topRated'     => $this->FooterModel->fetchTopRated()
        );        

        $this->load->view('common/header', array('title' => 'About'));
        $this->load->view('about/main');
        $this->load->view('common/footer', $footerContent);
    }

But it means I need to have this code on every page... and I just can't think of a better way to overcome this problem.

If I haven't explained the problem well please let me know and I will try to elaborate.

Thanks
#2

[eluser]missionsix[/eluser]
I've had a similar problem.

All my pages are wrapped with similar header / footers. So calling those views on every controller page seemed like a lot of code duplication.


So what I did was create a Parent controller that all my other controllers would extend. Then I created 1 method called Display that would load the header / footer views, or any other 'global' view files and then spit everything out into a container view.


Example: Public_Controller.php
Code:
&lt;?php   if (!defined('BASEPATH')) exit('No direct script access allowed');

class Public_Controller extends Controller {

    public $container;
    public $data = array();
    
    public function Public_Controller() {
        parent::Controller();
        $this->container = 'container';
    }
    
    private function _prepare() {
        // Here I assign global data, like userinfo, ad management, ect...
        [...]
    }
    
    public function display($data = array()) {
        
        $this->data = array_merge($this->data, $data);
        
                // do any global data preparation
        $this->_prepare();
        
        $this->data['header'] = $this->load->view('includes/header', $this->data, true);
        $this->data['footer'] = $this->load->view('includes/footer', $this->data, true);
        
        $this->load->view($this->container, $this->data);
    }
    
}


?&gt;

And then in my other controllers I do something like this:

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

include_once 'public_controller.php';

class Front extends Public_Controller {
    
    public function __construct()
    {
        parent::Public_Controller();
        [...]
    }

    public function index() {
      // do index page

      $this->display();
    }

}
#3

[eluser]iainco[/eluser]
Thanks for your reply, this looks like my solution Smile
#4

[eluser]missionsix[/eluser]
Your welcome!

There is probably a better way to do it so you don't have to include the file in every controller, but its a lot less code duplication.

I've also edited my post to reference the display method in the example controller to complete the example.
#5

[eluser]Colin Williams[/eluser]
Quote:There is probably a better way to do it so you don’t have to include the file in every controller

Create the parent controller in application/libraries/MY_Controller.php

CodeIgniter will expect that it's an extension to the controller library and load it automatically

But to the main question raised, and I'm not saying extending the controller won't work, this is partly the reason I developed the Template library, and why so many others have written and distributed similar solutions. If you approach output handling as being the sole responsibility of the given controller method, you're going to run into this problem over and over again. What Template does is allow the given controller method access to modify and control the ultimate output, while also providing access to any other component running for the given request (a library, a hook, etc.) In this way, your app is more flexible with regards to what is output.




Theme © iAndrew 2016 - Forum software by © MyBB