Welcome Guest, Not a member yet? Register   Sign In
Overall controller
#1

[eluser]codex[/eluser]
Hi, in the header I have a part where if you have new messages it is displayed as 'you have x new messages', and it's visible on every page you visit. But to call this for every page, do I have to put it in all controllers? Or is there an overall option?
#2

[eluser]thurting[/eluser]
Assuming the variable "x" is pulled from the database, you may want to create a private method on your controller that handles retrieving this information. If this behavior needs to be site wide, you should subclass "Controller" and have all of you application controllers inherit from this subclass. For presentation, you should create a view partial (RoR calls it this), e.g. "_header.php" that contains the markup you will reuse. Then within any views that need this markup you simply call load->view(partial) and everything should be fine. This keeps everything DRY and maintainable.
#3

[eluser]codex[/eluser]
[quote author="thurting" date="1193637821"]Assuming the variable "x" is pulled from the database, you may want to create a private method on your controller that handles retrieving this information. If this behavior needs to be site wide, you should subclass "Controller" and have all of you application controllers inherit from this subclass. For presentation, you should create a view partial (RoR calls it this), e.g. "_header.php" that contains the markup you will reuse. Then within any views that need this markup you simply call load->view(partial) and everything should be fine. This keeps everything DRY and maintainable.[/quote]

Hi, while this sounds good, I have little idea of how this works. Could you give a brief example of what this would look like? The partial part I can probably figure out, but I need help with the subclassing of the controller.
#4

[eluser]thurting[/eluser]
Sorry, but I don't really have the time to write up an example. Also, it probably wouldn't help much if you didn't understand the underlying syntax. I suggest you pick up a book on PHP and familiarize yourself with the language and OOP concepts. Good luck. Hope you get it working.
#5

[eluser]codex[/eluser]
[quote author="thurting" date="1193643430"]Sorry, but I don't really have the time to write up an example. Also, it probably wouldn't help much if you didn't understand the underlying syntax. I suggest you pick up a book on PHP and familiarize yourself with the language and OOP concepts. Good luck. Hope you get it working.[/quote]

You could have left it at 'Sorry, but I don't really have the time to write up an example'. You probably didn't mean it that way, but the rest almost feels like an insult. 8-/

And I've figured it out, thanks. Tongue
#6

[eluser]jjbakker[/eluser]
How about you example codex? Smile

I'm struggling with the same.
#7

[eluser]codex[/eluser]
[quote author="jjbakker" date="1196136215"]How about you example codex? Smile

I'm struggling with the same.[/quote]

Hey,

Make a new controller called MY_Controller that extends the base controller and put it in the Libraries dir. The prefix is default MY_, but you can change that in the config.
Code:
<?php
class MY_Controller extends Controller {

    function MY_Controller()
    {
        parent::Controller();
        
// do check for new messages here
        $data['new_messages'] = $this->new_messages_check();
        $this->load->vars($data);
    }

function new_messages_check()
    {
        // code here to check for messages
    }
?>

Now, In every controller that you need this function change 'Controller' to 'MY_Controller' like so:

Code:
class your_classname extends MY_Controller
And your main base controller will be extended with your new function(s).

Hope this helps!!
#8

[eluser]wakextreme[/eluser]
You could also do this with a hook.

http://ellislab.com/codeigniter/user-gui...hooks.html




Theme © iAndrew 2016 - Forum software by © MyBB