Welcome Guest, Not a member yet? Register   Sign In
How to get name of current class and function?
#1

[eluser]Unknown[/eluser]
I started using CodeIgniter a week ago and this is my first post, so greetings all!

What I want to do:
For every function within a controller, I'd like to be able to access the name of the controller, the name of the function and the URI segments.

What my current solution is:

//In codeigniter/Codeigniter.php, I initialized the controller class with the desired parameters. So instead of:
$CI=new $class();

//I have:
$CI=new $class($method,$URI->rsegments);

//END of Codeigniter.php

//In controllers/some_class.php, I receive the new parameters in the constructor, declare them as class variables, then get the class variables within each of the class' methods.
class Some_class{
protected $method;
protected $uri_rsegments;
function Some_class($method,$uri_rsegments)
{
$this->method=$method;
$this->uri_rsegments=$uri_rsegments;
}

function index()
{
$method=$this->$method
$uri_rsegments=$this->uri_rsegments;
}

function another_method()
{
$method=$this->$method
$uri_rsegments=$this->uri_rsegments;
}
}
//END OF some_class.php

What I would like help on:

A suggestion for a better, more rigorous, less invasive approach to accomplishing this than the "hack" I did. Hooks? config.php? $GLOBALS? $_SESSION? Which of these would work (and if you have time, why not)?

For the curious - what do I want this for?
- Have my anchor links update themselves relatively when I decide to reroute my URI's and omit the class from the URI.
- Log activity (the uri segments used) in my DB when a page is being visited.

Thanks!

-Adrian
#2

[eluser]Pascal Kriete[/eluser]
This should work:

Code:
$Rtr =& load_class('Router');

$current_folder = $Rtr->fetch_directory();
$current_class = $Rtr->fetch_class();
$current_func = $Rtr->fetch_method();
$params = $this->uri->rsegments;

I wrote a helper a long time ago:
http://ellislab.com/forums/viewthread/71440/
#3

[eluser]Colin Williams[/eluser]
Meh... another subject that repeatedly gets brought up, and repeatedly gets attacked with a sledgehammer when the URI class has functions specifically for this...

Code:
$c = $this->uri->rsegment(1); // The Controller
$f = $this->uri->rsegment(2); // The Function

Using rsegment() as opposed to segment() means you get the final request, not necessarily what's in the URI (like when using routes).
#4

[eluser]Mirage[/eluser]
You can just retrieve the segments in the constructor of your controller using the URI class. If you want it in every controller, create a master controller and subclass your controllers from that.

In your constructor of the master simply do what Colin suggests.

If you don't want to use a master controller you can use a post_controller_contstructor hook to set the properities in the controllers after the constructor runs.

For your logging needs the hook is the best way to automate it although you may want to do the logging at a different time (later in the process).

I really don't understand the automagic anchor update with class omission, but I'm sure you have a solid idea behind it...

Hope this helps -
-m
#5

[eluser]Colin Williams[/eluser]
Sorry, xmooth. Looking back at your request, it looks like you already knew how to get the rsegment entries. I guess I was responding more to this topic's title and inparo's response. Really no need to modify codeigniter.php though, given the URI class member functions available to you in the controller.
#6

[eluser]Pascal Kriete[/eluser]
/hides sledgehammer
Definitely use Colin's solution.
#7

[eluser]Colin Williams[/eluser]
:-P
#8

[eluser]chrisbratlien[/eluser]
Colin's solution works great for me. I can dynamically put an extra CSS class in my layouts, such as <body class="store-checkout"> and then scope certain CSS rules to my controller-function classes.

Watch out for the typo "rseqment" above in Colin's code snippet. Here it's corrected.

Code:
$c = $this->uri->rsegment(1); // The Controller
$f = $this->uri->rsegment(2); // The Function
#9

[eluser]Colin Williams[/eluser]
Whoops! Edited my post too.




Theme © iAndrew 2016 - Forum software by © MyBB