Description ¶
Yii Metadata component helps to get information about models,controllers and actions from your application
Installation & Usage ¶
1. Place Metadata.php to directory with components of your application (your_app_dir/protected/components/
) or to extensions (your_app_dir/protected/extensions/
)
2. Add 'metadata' element to 'components' in your application config (your_app_dir/protected/config/main.php
):
...
'components'=>array(
'metadata'=>array('class'=>'Metadata'),
...
),
If you place Metadata.php to extensions, then you should write 'class'=>'ext.Metadata' instead of 'class'=>'Metadata'
3. Use:
$user_actions = Yii::app()->metadata->getActions('UserController');
var_dump($user_actions); #Get actions of 'UserController'
$controllers = Yii::app()->metadata->getControllers(); #You can specify module name as parameter
var_dump($controllers); #Get list of application controllers
$controllersWithActions = Yii::app()->metadata->getControllersActions('user'); #if no $module param, controllers&actions of application will returned
var_dump($controllers); #Get controllers and their actions of module 'user'
$models = Yii::app()->metadata->getModels(); #You can specify module name as parameter
var_dump($models); #Get list of models application
$modules = Yii::app()->metadata->getModules();
var_dump($modules);
Issues ¶
v0.2:
If some modules in your app have controllers with same names, MetaData will raise fatal error, when you will try to get information about controllers of both this modules.
getModules method can not find modules inside other modules. (will fixed in next ver.)
MetaData cannot find information about controller actions, specified by actions() method (will fixed in next ver.)
Resources ¶
Requirements ¶
- Yii 1.0 or above
Change Log ¶
November 04, 2010 v0.2 released (fixed some bugs)
June 13, 2010 Initial release.
Usefull extension
Very nice extension and smart code!
Thanks.
designation of this exstension
For example you can use it in your Yii based CMS to get list of available modules/controllers/actions and actions dinamically.
p.s.: Sorry for my english, it isn't good. Usually i speak russian. Correct me please if you see stange text in documentation :)
nested module may not support
great job! useful for auth system , auto list all available actions , controllers ,modules , and then assign it to some role ,you don't need input it manually anymore and can choice it from a tree list view under this extension helping .:)
and............
any one test it on nested modules circumstance? the nest version should support the nested module situation . i roughly read the code and found that it doesn't support nested modules .
Class name
I think that class should have name EMetadata
Issue Fix
You wont get that fatal error every time if you parse the .php file as text instead of including it and reflecting it. Replace the getActions function with this:
public function getActions($controller, $module=null) { if ($module!=null){ $path=join(DIRECTORY_SEPARATOR,array(Yii::app()->modulePath,$module,'controllers')); $this->setModuleIncludePaths($module); }else{ $path='protected'.DIRECTORY_SEPARATOR.'controllers'; } if(!is_file($path.DIRECTORY_SEPARATOR.$controller.'.php')){ throw new Exception('no file found at '.$path.DIRECTORY_SEPARATOR.$controller.'.php'); } $actions = array(); $file = fopen($path.DIRECTORY_SEPARATOR.$controller.'.php', 'r'); $lineNumber = 0; while( feof($file)===false ){ ++$lineNumber; $line = fgets($file); preg_match('/public[ \t]+function[ \t]+action([A-Z]{1}[a-zA-Z0-9]+)[ \t]*\(/', $line, $matches); if( $matches!==array() ){ $name = $matches[1]; $actions[] = $matches[1]; } } return $actions; }
Updated version
Hi,
thanks for the extension, I adopted your code for my project.
https://gist.github.com/4349624
Best regards,
schmunk
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.