Welcome Guest, Not a member yet? Register   Sign In
Executing a controller based on criteria other than URL?
#1

[eluser]Unknown[/eluser]
For each visit to my app, regardless of the requested page, I need to execute some common code. It is quite possible that this code will determine that a controller other than the controller dictated by the route should be executed.

Unfortunately, after digging through the core code, I've found no way of doing the above. I would have thought that there would be some kind of hook that lets you change the controller on-the-fly before it is actually executed, but it doesn't look like this is the case. Am I missing something obvious?

I'd really prefer to not have to do a redirect for various reasons.
#2

[eluser]esra[/eluser]
Would a precontroller hook work?
#3

[eluser]Mirage[/eluser]
What you want is a pre-controller hook. From it you might be able to modify $RTR so that it alters the controller loading. Haven't look in detail if this is possible...

I could help if you were more specific about what you're trying to accomplish. There may be other ways.
#4

[eluser]Unknown[/eluser]
I'd really like to use a hook, but it seems that forces that be are working against me with regard to how hooks are implemented and more importantly, how libraries are implemented.

By changing the $class and $method globals in the pre_controller hook, I can easily remap on the fly what I want my controller to be. The problem is that I need the session and database libraries to be loaded in order to determine if I should remap or not, but libraries are loaded as part of the controller itself. So there's a bit of a chicken and an egg situation going on.

I thought about reproducing minimal library loading logic from within the hook itself, but I worry about future portability and that this is really descending into the sort of hack that CI should be isolating me from. I'm uncomfortable as it is mucking about with $class and $method.

In the simplest terms, here's what I want to do:

1) A page is requested.
2) The session and database libraries are loaded.
3) Conditional logic is performed.
4) Based on the outcome of the logic:
5) The requested page controller will be executed.
6) A remapped page controller will be executed.

Admittedly, I haven't yet dug too deeply into the library logic of CI to see if autoload libraries are loaded prior to the controller being created. I have a feeling they're not, but that's the next place I'm looking.

Any thoughts would be a great help!
#5

[eluser]thurting[/eluser]
You could create a class (e.g. ApplicationController) that inherits from Controller and from which all of your application controllers will inherit. You could call parent::__construct(); in your application controllers and handle all of the logic through the super class (e.g. ApplicationController). In my opinion, the logic of mapping one request to a method on another controller should be paralleled by a proper redirect. Otherwise, your application structure doesn't really make much sense. If you just want to use a conditional to map requests to methods in a controller than you shouldn't use a redirect, but it seems appropriate if you are actually moving from one controller to another.

This is just one way you could do it. It may not be right for you and there may be a better way.

would post_controller_constructor work?
#6

[eluser]esra[/eluser]
It sounds like you're trying to mimic the operation of a case statement with switches where each switch routes to a different page based on the url syntax.




Theme © iAndrew 2016 - Forum software by © MyBB