Yii Framework Forum: Retrieve all modules - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Retrieve all modules Rate Topic: -----

#1 User is offline   manilodisan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 80
  • Joined: 17-September 09
  • Location:Bucharest

Posted 17 December 2009 - 06:11 AM

I'm building a application where I give the admin the option to activate/deactivate modules using the web administration so he doesn't have to edit the configuration files manually so I need to know what modules are inside the module/ folder. I could parse the folder and see what's inside but it may be a better solution.

Something like Yii::app ()->getModules () but with a result which includes the loaded/unloaded modules. Is there such a method I can use?
0

#2 User is offline   Sander 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 169
  • Joined: 03-November 09
  • Location:Amsterdam, Netherlands

Posted 17 December 2009 - 06:26 AM

Did you try that? I think that exact method should be available: http://www.yiiframew...tModules-detail (CApplication extends CModule).
0

#3 User is offline   manilodisan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 80
  • Joined: 17-September 09
  • Location:Bucharest

Posted 17 December 2009 - 06:28 AM

Yii::app ()->getModules () only returns the modules which are manually added in the config file:
'modules'=>array('forum',...)


But this is not very friendly for noobs so I need a way to know exactly all the modules available in the modules/ dir...loaded or not.
0

#4 User is offline   jerry2801 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 504
  • Joined: 18-November 09

Posted 17 December 2009 - 07:29 AM

View Postmanilodisan, on 17 December 2009 - 06:11 AM, said:

I'm building a application where I give the admin the option to activate/deactivate modules using the web administration so he doesn't have to edit the configuration files manually so I need to know what modules are inside the module/ folder. I could parse the folder and see what's inside but it may be a better solution.

Something like Yii::app ()->getModules () but with a result which includes the loaded/unloaded modules. Is there such a method I can use?



try this:

  /**
   * Find a module searching in application modules and if it's not found there
   * looks in modules' modules
   * @param String $moduleID The model to find
   * @return The module, if it's found else null
   */
  public static function findModule($moduleID) {
    if(Yii::app()->getModule($moduleID)) {
      return Yii::app()->getModule($moduleID);
    }
    $modules = Yii::app()->getModules();
    foreach ($modules as $mod=>$conf) {
      if(Yii::app()->getModule($mod)) {
        return self::findInModule(Yii::app()->getModule($mod), $moduleID);
      }
    }
    return null;
  }

  /**
   * Search for a child module
   * @param String $parent The parent module
   * @param String $moduleID The module to find
   * @return The module, if it's not found returns null
   */
  private static function findInModule($parent, $moduleID) {
    if ($parent->getModule($moduleID)) {
      return $parent->getModule($moduleID);
    } else {
      $modules = $parent->getModules();
      foreach ($modules as $mod => $conf) {
        return $this->findInModule($parent->getModule($mod), $moduleID);
      }
    }
    return null;
  }

0

#5 User is offline   manilodisan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 80
  • Joined: 17-September 09
  • Location:Bucharest

Posted 17 December 2009 - 07:35 AM

This is a findModule function. I need something that returns all the available modules not a find function.
0

#6 User is offline   manilodisan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 80
  • Joined: 17-September 09
  • Location:Bucharest

Posted 18 December 2009 - 10:20 AM

Bump..before I do anything stupid that I might regret later :P
0

#7 User is offline   markslor 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 30-October 13

Posted 24 July 2014 - 03:34 AM

Corrections

Thanks to jerry2801, your functions put me in the right way.

I've noticed errors when I was trying to find a nested module. I'll figured it out, I posted my code for those who need it too.

/**
    * Find a module searching in application modules and if it's not found there
    * looks in modules' modules
    * @param String $moduleID The model to find
    * @return The module, if it's found else null
    */
    public static function findModule($moduleID) {
        if(Yii::app()->getModule($moduleID)) {
            return Yii::app()->getModule($moduleID);
        }
        $modules = Yii::app()->getModules();
        foreach ($modules as $mod => $conf) {
            if(Yii::app()->getModule($mod)) {
                $module = self::findInModule(Yii::app()->getModule($mod), $moduleID);
                if($module)
                    return $module;
            }
        }
        return null;
    }

    /**
    * Search for a child module
    * @param String $parent The parent module
    * @param String $moduleID The module to find
    * @return The module, if it's not found returns null
    */
    private static function findInModule($parent, $moduleID) {
        if ($parent->getModule($moduleID)) {
            return $parent->getModule($moduleID);
        } else {
            $modules = $parent->getModules();
            if(!empty($modules)) { // Here was the principal error, when $modules is empty, it returns a CBaseController object.
                foreach ($modules as $mod => $conf) {
                    return $this->findInModule($parent->getModule($mod), $moduleID);
                }
            }
        }
        return null;
    }

0

#8 User is offline   Joblo 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 616
  • Joined: 12-September 10
  • Location:Austria

Posted 24 July 2014 - 04:29 AM

I never would call
Yii::app()->getModule($mod) or $parent->getModule($mod)
in a loop method on finding a module.
That means, all that all these modules will be instantiated and initialized...
Do you really want to instantiate all the modules on finding a module?

Why not only parse the array of the configured modules returned by 'getModules()'?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users