Welcome Guest, Not a member yet? Register   Sign In
Getting All Controller Functions
#1

[eluser]cpscdave[/eluser]
Hi everyone,
I'm working on implementing some Role Based Security for a site, and one of the things I would like to do is have the code figure out all of the methods that role access needs to be set for.

How can I go about doing this?

I originally tried to instantiate a instance of the controller(s) but when I do I get an error as CI is assuming I'm trying to load a library.

Any suggestions??
#2

[eluser]WanWizard[/eluser]
Load your controller using 'include', then use get_class_methods('controller_class_name') to list all its methods?
#3

[eluser]cpscdave[/eluser]
that method kinda works.
However there is a couple problems.
First it gives you the base class functions as well,
and it doesnt then let you know whether the method is private or protected.

I actually figured out a method to get what I need:

$theDir is set to where the controllers are on the local server
Code:
$theControllers = array();
if ($dh = opendir($theDir)) {
   while (($file = readdir($dh)) !== false) {
    if(strstr($file, ".php"))
        {
      $theControllers[] = $file;
    }
   }
}
closedir($dh);
$uris = array();
foreach($theControllers as $controller)
{
  if($controller == "auth.php") //not sure how to handle this yet...
    continue;
  $class = str_replace(".php","",$controller);
  $fp = fopen($theDir . $controller, "r");
  $content = fread($fp, filesize($theDir . $controller));
  fclose($fp);
  $theLines = explode("\n",$content);
  foreach($theLines as $line)
   {
    if(stristr($line, "private"))
    continue;
    if(stristr($line, "protected"))
    continue;
    if(stristr($line, "function"))
     {
    $myLine = trim(str_replace("function","",$line));
    $myLine = str_replace(strchr($myLine, "("),"",$myLine);
    if(strtolower($class) == strtolower($myLine))
     {
      //is constructor
      continue;
    }
    if(substr($myLine,0,1)=="_")
    {
    //is private
     continue;
    }
    $uris[] = $class . "/".$myLine;
   } //end if line is function
} //end foreach lines
}//end foreach controller
print_r($uris);




Theme © iAndrew 2016 - Forum software by © MyBB