Welcome Guest, Not a member yet? Register   Sign In
Getting controller, view names from helper or library
#1

[eluser]welded[/eluser]
Is there a clean way to get the name of the controller and the view from within a helper or library class? I'm making a library that builds an HTML menu which is different based on what page the user is on. I would like to avoid passing variables to it or parsing the URL since both may be prone to errors.

Thanks for any tips!
#2

[eluser]welded[/eluser]
This seems to do the trick. So my follow-up is this: is there a better or recommended method?

Code:
$CI =& get_instance();
echo $CI->uri->rsegments[1].$CI->uri->rsegments[2];
#3

[eluser]jedd[/eluser]
Well, that method does what you said you didn't want to do - parse the URL! Smile

You can use PHP's native get_class() call - that should tell you the Controller's name (haven't tested this - no idea what happens when you do this from within helpers or libraries).

For telling what view you're in .. that's a bit trickier. I think there are PHP variables that you can call - __FILE__ - that kind of thing. But I'd be really wary about doing that.

The problem with uri segments, or any kind of self-analysis by scripts, is that they're prone to break if you move things around, and they're prone to become less useful the more you abstract your code.

Can you perhaps look at using session data to store the key bits of information you need? You could write a couple of helpers to set and get this information more easily, but I suspect it'd result in more readable code.
#4

[eluser]TheFuzzy0ne[/eluser]
The view's going to be a tricky one, because generally you'll be loading multiple views in any given request. However:

Code:
$CI =& get_instance();
$controller = $CI->Router->class;

EDIT: This was re-edited as a piece was missing.
#5

[eluser]welded[/eluser]
jedd, actually those aren't the URL segments although I acknowledge the similarities. If you examine the contents of the $CI object you'll get a TON of data (I dumped it to screen and it basically crashed Safari) and the uri object contains the URL segments as well as the rsegments (route segments, real segments? not sure what the 'r' is for), which is what I was looking for. In my test I was loading the home page so the segments array was empty but the rsegments array had the controller and view names plain as day. Smile

I had thought of get_class and get_parent_class but neither returned anything helpful, not in my test anyway.

TheFuzzyOne - can you elaborate about this? I understand the relationship to be that domain.com/controller/view would trigger a single function within the controller that could then call other functions if necessary, but that the relationship would never the less remain controller/view. How do you load multiple views?

Thanks for the ideas!
#6

[eluser]jedd[/eluser]
Ah, cool. Hadn't played with php's self-class identifiers .. I usually know where I am Smile

With rsegment - yes - it's the re-routed segment, and is the one you should be deferring to I expect.

Have a read of the differences in the [url="http://ellislab.com/codeigniter/user-guide/libraries/uri.html"]URI section[/url] of the manual.
#7

[eluser]welded[/eluser]
Ooh, handy! I hadn't noticed that page of the docs yet.




Theme © iAndrew 2016 - Forum software by © MyBB