Hi There,
I am newbie in yii framework, i have a query
How can i get a list of all controller and its methods of my application dynamically?
Thanks
Hi There,
I am newbie in yii framework, i have a query
How can i get a list of all controller and its methods of my application dynamically?
Thanks
You can use this function get all controllers and action methods of your project.
Response will be controller as key and values as methods
public function getControllers() {
$path = Yii::app()->controllerPath;
$data = array();
$files = CFileHelper::findFiles($path, array("fileTypes" => array("php")));
foreach ($files as $file) {
include_once $file;
$filename = basename($file, '.php');
if (($pos = strpos($filename, 'Controller')) > 0) {
$class_name = $controllers[] = substr($filename, 0, $pos);
$f = new ReflectionClass($filename);
$methods = array();
foreach ($f->getMethods() as $m) {
if ($m->class == $class_name . "Controller"
&& preg_match('/^action+\w{2,}/', $m->name)) {
$methods[] = str_replace("action", "", $m->name);
}
}
$data[$filename] = $methods;
}
}
return $data;
}
Response :
Array
(
[SiteController] => Array
(
[0] => Index
[1] => Error
[2] => Contact
[3] => Login
[4] => Logout
)
)