CodeIgniter Forums
Is there a standard way to get the application/controllers path? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Is there a standard way to get the application/controllers path? (/showthread.php?tid=9630)



Is there a standard way to get the application/controllers path? - El Forum - 07-02-2008

[eluser]Dave Stewart[/eluser]
I'm writing a utility Library that needs to scan the folder and print out a list of all the controller names and methods.

Cheers,
Dave


Is there a standard way to get the application/controllers path? - El Forum - 07-02-2008

[eluser]mironcho[/eluser]
Hi Dave,
You can use the APPPATH constant:
Code:
$controllers_path = APPPATH . 'controllers';



Is there a standard way to get the application/controllers path? - El Forum - 07-02-2008

[eluser]xwero[/eluser]
controller names isn't to difficult : APPPATH.'controllers'. But if i was you i leave the controller path up to the developers. They can either add their own path or an array of paths.

Extracting the methods is a little harder. You can read the files and regex the function names but that is going to take some time if there are a bunch of controllers.


Is there a standard way to get the application/controllers path? - El Forum - 07-02-2008

[eluser]Dave Stewart[/eluser]
[quote author="xwero" date="1215020423"]Extracting the methods is a little harder.[/quote]

Really? I was going to read the classes, load them one by one, then call a get_class_methods(). If that doesn't work within the CI structure, I'd just "new" each class.

The regexp route is also fine. The utility will only be run a couple of times, and only in development, never in production.


Is there a standard way to get the application/controllers path? - El Forum - 07-02-2008

[eluser]xwero[/eluser]
That's better Smile But i think there will be a collision if you load the controller where you call the library method in because all the classes are loaded in the global namespace. If you run the library in an independent script you shouldn't have any trouble.

More visual explanation
Code:
class a {}

class a {}
Results in an error : class a can not be redeclared.


Is there a standard way to get the application/controllers path? - El Forum - 07-02-2008

[eluser]Dave Stewart[/eluser]
Thanks for the input.

If I get what you're implying, I guess that will only happen for the controller that is actually doing the scan? I'm sure it will be fairly trivial to filter it out.

I've got some proper work to do for the rest of the day, but I'll probably have a crack tonight, and I'll post back my efforts.

Cheers!
Dave


Is there a standard way to get the application/controllers path? - El Forum - 07-02-2008

[eluser]Dave Stewart[/eluser]
As usual, I can't resist having a play (I have next to no will power) so I had a quick crack.

As it turns out, the regexp route was far more successful and flexible, as loading classes and such like created a lot of issues with classes (and previously-included classes) being redeclared.

Problem solved though!

Cheers,
Dave