Welcome Guest, Not a member yet? Register   Sign In
Functions that need to be called on (almost) every page load
#1

[eluser]GregX999[/eluser]
I've got a library with three functions in it (lets say A, B and C)

Function A needs to be called every time a page is requested (aka - for every function in every controller that produces output)

For functions B and C, one of them always needs to be called for every page - usually B - so B should be default and C is used instead only sometimes.

How can I best do this without having to manually call these library functions in every controller function?

In RoR I can use the application controller to automatically do what I need to do with function A. But I don't know how to get an application class to load automatically if I have my controllers extend it instead of extending Controller.

And for functions B and C, in RoR I can use "before_filter" in my controllers to assign either B or C to each function all on just 2 lines.

What can I do with CI?

Thanks,
Greg
#2

[eluser]Colin Williams[/eluser]
You can use the application controller, just like you would with RoR. Copy system/libraries/Controller.php to application/libraries/Controller.php and add what you need. I can't imagine building anything properly without doing this.
#3

[eluser]brianw1975[/eluser]
i created an application_model and added it to the autoload for models and do all of my site-wide initialization there.

it does the following:

a)check for cookies, if the user is logged in according to the cookie (and the hash matches) log them in
b)load the site settings from the DB and add them to the $config array


I even went so far as to add a site-wide functions that I didn't think really needed its own model, but I could go back and change that so it would be less typing. But that function doesn't get called very often. Mostly use it for pages that are editable in the soon to be created admin section.
#4

[eluser]GregX999[/eluser]
Collin,
What exactly do you do in the Controller class? Call functions in other libraries from inside the Controller function? Put new functions in the class? How would I access the CI object (the same way as explained in the docs on making your own libraries?)? Or are you putting something in the Controller class that allows all the controllers to extend an Application class?

Brian,
How do you get functions inside of models to be executed automatically? It sounds like you're doing exactly what I want to - check sessions/cookies and pre-populate data before the code in the controller/function starts. How do you access the CI object from in the model, just use "$this"? And are you still having to create an instance of a model to use the functions in it?

This is starting to seem (to me) to be a bit of an over-site on the part of CI - it should be something built-in.

Greg
#5

[eluser]xwero[/eluser]
I would go with a hook because that way the functions get called automatically.

For the C and B function you create an array of controllers they need to be aware of. In the B function this stops the execution of the function, in the C function it triggers the execution.
#6

[eluser]Tom Schlick[/eluser]
all you have to do is create a library or a model and use the __construct
Code:
class Whatever extends Model
{
    function __construct()
    {
        parent:Model();
        do whatever function your little heart desires
    }
}

of course the __construct function is only a part of PHP 5 if you are using PHP 4 you need to call the function not construct but

function WhateverYourClassNameIS
#7

[eluser]Colin Williams[/eluser]
Quote:What exactly do you do in the Controller class?

Global stuff. Load current user. Load navigation. Etc.

Quote:How would I access the CI object ... ?

The controller class is the CI object. So you just do $this->load->library(), etc, like you would in a class that extends Controller.

Quote:Or are you putting something in the Controller class that allows all the controllers to extend an Application class?

No, I am using it as the Application class. You can go the other route and have multiple application classes that your controllers extend from, but it's not always necessary to do so.




Theme © iAndrew 2016 - Forum software by © MyBB