Question

I am trying to give a shot to HMVC in Codeigniter. Here is my folder structure.

-ROOT
--APPLICATION
---MODULES
----Module_Email
-----Controllers
-----Models
-----Views
-----Assets
------JS
------CSS
------IMG

To render the Module i have to use

Module::run('Module_Email');

This method will output the rendered output, an example is given below

<script type="text/javascript" src="PATH/TO/EMAIL_MODULE/JS/JS_FILE.JS"></script>
<div data-module-name="Module_Email" class="Email_wrapper">

  //RENDERED HTML CONTENT 

</div>

Now here my problem start. Normally i would like to put all my resources to header. So when i call any module, its dependence need to be added in header instead of from where its get called.

I searched a lot but i couldn't find any good methods.

Please help.

Update

Currently i have a function on my header called get_assets() which will output predefined resources to header. But i cant say which modules is going to use in pages, so the system need to check which modules are used in this page, and if its used then its dependencies need to be added on header.

Was it helpful?

Solution 2

I added an array to the Module class to store the assets and two functions to store/retrieve the items. Here is the source (updated Modules.php)

        # Register your assets
        public static function register_asset( $asset )
        {
           if( in_array($asset,self::$assets) === FALSE )
           {
              self::$assets[] = $asset;
           }
        }

       public static function assets()
       {
             return self::$assets;
       }

and now you can register your assets like this inside your module

Modules::register_asset('myslider.js');

You can retrieve all your assets using

Modules:assets();

Which will return an array of assets that can be processed depending up on the situation.

OTHER TIPS

Seems like your main problem then is trying to figure out what modules were used.

Unfortunately as far as I can tell with the default Wiredesignz modular extension there is no way to access the module name unless you write some sort of hack to get at that data. The module being used is stored in the protected variable $module in the MX_Router class, however, there is no public method to allow you to get access to it. So your only choice would be to extend the class and create a public function.

Alternatively you could use a forked version of Wiredesignz implementation which I did which provides numerous other features including a public function to get at the $module variable. Using the forked version I wrote you could then use code such as this:

<?php $module_name = $this->router->fetch_module(); ?>

However, that will only record the last module you loaded, so you would still need to do work to store all the modules, and then have your function use this information to determine what assets to load. If I were doing something like you I would probably fork my version and then create an additional data structure to store every module that was loaded that you could then later get access to.

I don't think this is exactly what you were hoping for, but might be something to get you on the right track to finding a solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top