Welcome Guest, Not a member yet? Register   Sign In
Setting a globally used function to run automatically
#1

[eluser]hightower[/eluser]
Hi folks,

I'm working on a project and have stumbled across something I need to sort before I proceed further. I'll try my best to explain this, so stay with me Smile

I have various controllers - here is the example code from one of them:

Code:
class Welcome extends Controller()
{

    function index()
    {
        $data = custom_helper_to_process_data_used_in_header_view();

        $this->load->view('header',$data);
        $this->load->view('content');
        $this->load->view('footer');
    }

}

As you can see, a custom function is called which returns some data. This data is then sent to the header view and then the content and footer are loaded after. My custom helper function is to be used on every page that is loaded, and the data retrieved is then always passed to the header view - this is because some of the menu's from the header view require information from the database, and this information needs to be displayed on each page.

My question is how would I go about processing this automatically for each page (i.e. calling the function and assigning the returned data to a variable before passing it to the header view) as it's a bit tedious to write this code in every page.

I know I could have a controller like this:

Code:
class Welcome extends Controller()
{
    function Welcome()
    {
        $data = custom_helper_to_process_data_used_in_header_view();
        $this->load->view('header',$data);
    }

    function index()
    {
        $this->load->view('content');
        $this->load->view('footer');
    }

}

But I would need to repeat this for each controller I make, which could be quite a few.

Any ideas?
#2

[eluser]TheFuzzy0ne[/eluser]
Uhm... I think this is resolved [url="http://ellislab.com/forums/viewthread/121558/"]here[/url]?
#3

[eluser]hightower[/eluser]
No, it isn't Sad
#4

[eluser]TheFuzzy0ne[/eluser]
By using a main template (as I'd suggested previously in the other thread), all you have to do is load the vars, load the content, then load the template. That's what I do in most of my controller methods, anyway. If you want something to run on every request, you can use [url="http://ellislab.com/codeigniter/user-guide/general/hooks.html"]hooks[/url] (as Dam1an suggested in the other thread), or you can [url="http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html"]extend the controller class[/url].
#5

[eluser]hightower[/eluser]
So is it a done thing to have this in my header_view.php file:

Code:
$var = $some_custom_function();

echo $var['test'];
echo $var['test2'];

Because that would seemingly be the answer to my question, but I didn't know whether it was considered bad coding practice to run the function from my helper within a view file?
#6

[eluser]TheFuzzy0ne[/eluser]
No. Technically the variables should be passed into the view. If you can give me an example of the type of information you'd like to be loaded globally, I might be able to give you an example.
#7

[eluser]hightower[/eluser]
Ok Smile

I have a header view called header_view.php - this basically shows my HTML before content. A cut down (for simplisity) version is:

Code:
<html>
<head>
    <title><?= $page_title ?></title>
</head>
<body>
    <div class="leftmenuitem">
        <p>&lt;?= $date ?&gt;</p>
        <p>&lt;?= $h_team ?&gt;</p>
        <p>&lt;?= $a_team ?&gt;</p>
    </div>

    <div class="content">

    &lt;!-- Rest of content follows which is loaded by controller --&gt;

The above header_view.php uses 4 bits of information passed by the controller (it will in essence be many, many more bits of info when live). The page title is set in the controller, where as the last three bits of information are grabbed from the database using my custom function in the custom helper I created and autoloaded.

My controller looks something similar to this:

Code:
class Welcome extends Controller()
{

    function index()
    {
        $data = my_function_that_pulls_from_db();
        $data['page_title'] = 'Page Title 1';

        $this->load->view('header',$data);
        $this->load->view('content');
        $this->load->view('footer');
    }

    function another()
    {
        $data = my_function_that pulls_from_db();
        $data['page_title'] = 'Page Title 2';

        $this->load->view('header',$data);
        $this->load->view('another');
        $this->load->view('footer');
    }

}

I know I can put this code into a contructor statement, so I only need to insert it once per controller:

Code:
$data = my_function_that pulls_from_db();
$data['page_title'] = 'Page Title 2';

$this->load->view('header',$data);

But my app is going to have quite a few controllers which means I will have to put that code in each one in order for my menu (in the header view file) to display its contents correctly - seems tedious.

So my question is, surely there is a location I can put:

Code:
$data = my_function_that pulls_from_db();
$data['page_title'] = 'Page Title 2';

$this->load->view('header',$data);

So each page automatically loads the header (without me having to type extra code) so all I am left to do is:

Code:
class Welcome extends Controller()
{

    function index()
    {
        $this->load->view('content');
        $this->load->view('footer');
    }

    function another()
    {
        $this->load->view('another');
        $this->load->view('footer');
    }

}

I'm sorry if I'm not making sense - I just have a hard time putting into words the thoughts that go through my head Smile

All help is greatly appreciated.

Thanks!
#8

[eluser]jedd[/eluser]
It's already been suggested - twice - to extend your controller. There's a [url="http://ellislab.com/forums/viewthread/109698/"]thread over here[/url] that I point people at on this subject.

The database calls are going to be trickier, but not insurmountable, particularly if you're happy to call in a library to do this. If they're from multiple models, then you might find your performance is in jeopardy, but so much of that depends on other factors.

The code you mentioned most recently doesn't include this requirement, and extending your controller also removes the need to have those extra load->view lines in each of your controller's methods.
#9

[eluser]hightower[/eluser]
Ok, thanks for your help everyone - will take a look at that thread and see how I get on.




Theme © iAndrew 2016 - Forum software by © MyBB