Welcome Guest, Not a member yet? Register   Sign In
Getting value from hook to controller.
#1

[eluser]drewbee[/eluser]
Hello everyone I am setting up a referral system on my current project, and as such have a requirement that the referring query string can be added to any url.

This is no problem through the pre_system hook. The query string is succesfully recongnized and avoids 'crashing' (404 displaying). Unfortunately during the pre_system, I have NOTHING available to me at this point. In trying to stay consistent with using all of code igniters features, I want to use the cookie helpers later on during the controller calls.

Unfortunately, I have no way of moving said cookie value from the controller (Through global vars or even the hook class itself, as it seems to be unitialized by the time it gets to the controller);

Does anyone have any advice on this one? In its most basic form, my question I am asking is how to I get a value from a hook class to a controller?

Or is this one of those times that I should just deal with the cookie handling within the hook manually? (I can do that, I just wanted to stay true to CI) Big Grin
#2

[eluser]Michael Wales[/eluser]
Why not just write your routes so the referral code can be appended to any of your URLs and bypass the whole hook mess? An extended Controller class with a quick referral variable check in the Constructor takes care of any processing you need to accomplish with that variable.

Just trying to toss some other ways to "skin this cat" out there - I've never even looked at the code behind hooks before (never found a reason to use them).
#3

[eluser]drewbee[/eluser]
With that method above, wouldn't I have to have a custom route for each and every controller/method setup? That sounds like even more of a mess then the hook mess Big Grin (Unless there is some global way of handling it that I dont know about).

I came across working with the hook because I still want the query_string check to be off, which tends to throw some very bad or not found pages when adding the query string on to the end of a controller / method.
#4

[eluser]Mirage[/eluser]
Hey Drew -

1. What is your 'uri_protocol' config setting?

2. Option 1: Run your hook 'pre' system, store info in a static var and run it again 'post_controller_constructor' to shove it into the controller.

3. Option 2: Run you hook 'pre' system, store info in a GLOBAL var and access that from the controller constructor or whereever.

EDIT:
With regards to 'staying true to CI' - A hook is something expected to be 'foreign' to CI. Ergo it's totally 'legit' not to use CI helpers, etc. to complete the hook task. It's also not true that you don't have access to anything. You just can't use the Loader, because it's not been loaded. A simple include directive can go a long way here. Finally - you do have access to everything defined in index.php.

HTH,
m
#5

[eluser]drewbee[/eluser]
uri_protocol is set to AUTO.

For the 3rd option, I tried storing the value by global $var, as well as $_GLOBAL['var'], however the values were not accessible (does CI trash these as well as GET?)

Above anything else, the easiest method way would be to just set the silly cookie within the pre_system hook. This would be the minimum amount of work (and the cleanest IMO).

Also, at the time of the pre_system, I did i dump of $this and had only the hooks class and one other irrelevant class available to me. Everything is pretty much non-existant during the pre-system load.
#6

[eluser]Mirage[/eluser]
I'm not aware of CI clearing globals. But it could be...

If handling cookies is the most straightfoward option for you, I'd run with it.

Cheers,
-m




Theme © iAndrew 2016 - Forum software by © MyBB