Welcome Guest, Not a member yet? Register   Sign In
Controller and Method being called
#1

[eluser]Armorfist[/eluser]
Hello,
I was wondering if the controller and method that's being called is stored in some variable in the system so I don't have to go fetch them on the URI. I use some routings so it becomes difficult to do that.

Thanks!
#2

[eluser]Armorfist[/eluser]
Ok, after some digging I found that there are two functions in the CI_Router class where I can retrieve the current controller/method, they are fetch_class() and fetch_method(), but i'm having some problems accessing them inside the controller. How can I access these functions within the controller constructor? They are on the system/libraries/Router.php lib.

Thanks!
#3

[eluser]sophistry[/eluser]
you must be looking for the CI manual... the information you seek is to be found in the manual.

you must look in the manual in the URI page:

i have discovered the URI class here.

because you are using routing, there is some information you may need regarding routed segments.

enjoy. and please let us know how you are coming along.
#4

[eluser]Rick Jolly[/eluser]
Code:
$RTR =& load_class('Router');
$method = $RTR->fetch_method();
#5

[eluser]Armorfist[/eluser]
No, that's not what I want.
As I mentioned on the first post, I want to get the controller and method that's being called without having to find them manually on the URI.
If I understood right, the fetch_class() and fetch_method() functions on the CI_Router class return these values after processing the URI, so if these functions already exist and return the values that I want, I just want to know how to call them within the controller, if possible.

Thanks!
#6

[eluser]Armorfist[/eluser]
[quote author="Rick Jolly" date="1218137834"]
Code:
$RTR =& load_class('Router');
$method = $RTR->fetch_method();
[/quote]

It works! Sorry for my previous post saying it didn't, i was doing it wrong! Big Grin
Thanks RIck!
#7

[eluser]nmweb[/eluser]
Do a var_Dump($this) in your controller. The stuff you arelooking for might be stored somewhere in the superobject. Place the var_dump between <pre> tags.
#8

[eluser]sophistry[/eluser]
is it really necessary to bring the $RTR object into it when the URI class (loaded automatically by CI) already has the controller in segment 1 and the method in segment 2? i know it's a matter of style vs. substance, but if you have no specific need to invoke the $RTR object, then why do it when there is an API to get the URI data?
Code:
echo $this->uri->rsegment(1);
echo $this->uri->rsegment(2);

just a thought... no warranties expressed or implied.
#9

[eluser]Rick Jolly[/eluser]
I guess when using the uri class you'd have to know when to use "segment" vs "rsegment" and you'd have to know if the controller were in a subdirectory.
#10

[eluser]sophistry[/eluser]
if no routes are being used then rsegment has the same info as segment. so you can always use rsegment() whether or not you are using any routes.

also, rsegments() seems to ignore the subdirectory.

so, when you have a controller in a subdirectory $this->uri->rsegment() still has

controller in rsegment 1
method in rsegment 2

while segment() has

subdir in segment 1
controller in segment 2
method in segment 3 (but only if explicitly passed in the URI - does not hold the default method index for instance)

HTH




Theme © iAndrew 2016 - Forum software by © MyBB