Yii 1.1: metadata

Getting names of controllers/actions/modules/models in your app
13 followers

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:

  1. 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.

  2. getModules method can not find modules inside other modules. (will fixed in next ver.)

  3. 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.

Total 6 comments

#11163 report it
schmunk at 2012/12/20 06:47pm
Updated version

Hi,

thanks for the extension, I adopted your code for my project. * fixed module scans (dir detection) * added fatal error fix from vgfitzger

https://gist.github.com/4349624

Best regards, schmunk

#7815 report it
vgfitzger at 2012/04/19 07:02pm
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;

}
#5736 report it
pmaselkowski at 2011/11/08 07:13am
Class name

I think that class should have name EMetadata

#4311 report it
yiqing95 at 2011/06/24 12:13pm
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 .

#398 report it
juban_ at 2010/06/14 11:41am
Usefull extension

Very nice extension and smart code! Thanks.

#399 report it
Nayjest at 2010/06/14 10:51am
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 :)

Leave a comment

Please to leave your comment.

Create extension
  • Yii Version: 1.1
  • License: New BSD License
  • Developed by: Nayjest
  • Category: Others
  • Votes: +9 / -2
  • Downloaded: 1,448 times
  • Created on: Jun 14, 2010
  • Last updated: Nov 5, 2010
  • Tags: metadata