Phil Sturgeon

Web developer, kayaker, outdoors madman and part-time alcoholic.


CodeIgniter Packages != Modules

Posted CodeIgniter at Apr 12, 2010

So many people are asking about the new Packages feature in CodeIgniter 2.0 that I need to put this to bed:

Packages are not Modules.

These are two entirely different concepts and should not be confused with each other.

Modules

By modules I mean any HMVC implementation such as Modular Separation or Matchbox. These essentially allow you to split your application down into folders of self-contained logic, so your news module will contain all your news controllers, helpers and models, kept hidden away from your user system.

Modules essentially allow you to build your application exactly the same as you normally would, but you put you have a slightly cleaner file system.

The key differentiating feature is that modules are called by the URL.

http://example.com/module/controller/method

All current modular systems extend the Router to pick up a module name from the URL and then use that to find the controller. They also extend the Loader to give the current module prefference when loading. So in your news controller if you write $this->load->model('news_m'); it will first look in the current module, then the main application directory, then - if you are using them - Packages.

Packages

A wonderful but mis-understood feature added in by the EllisLab team to CodeIgniter 2.0 development branch that will allow you to put your models, libraries, helpers and config into a shared "repository". This is VERY helpful, but is nothing more than creating a dynamic list of fallback directories for Loader to look in if something does not exist in /application.

Packages in no way map to the URL but instead have to be loaded via:

$this->load->add_package_path('/usr/local/codeigniter/shared');

Doing this would allow several useful things:

Things Packages do not do:

But surely...?

With all that said, Packages are fairly close to allowing modular applications but not out of the box. Theoretically you could create a MY_Router that would tie to the URL and check if a module was being used. If so it would dynamically load the module as a package and Bob's your Uncle, you have a modular system.

There is plenty of code out there that could make this very easy and If I was a little less knackered or busy I might consider having a go just for the hell of it. When it comes to my applications I very much doubt I would use a system like that as I don't see the point. Modular Separation is patched to work with CodeIgniter 2.0 and offers many more features than this theoretical bodged Packages-HMVC such as 404 routing and some very smart routing tweaks for routing withing a module not at a global level like normal routes.php.

But I want modules!

I don't know if anything has changed in the last few months, but at EECI2009 Derek Allard assured me that at some point, some form of HMVC would probably make it into the CodeIgniter codebase. Right now EE2 has a custom modular system and they have no need to mess with that. Until then we have Modular Sepration which is a very elegant and lightweight system compared to the older Modular Extensions or Matchbox.

Summary

We have an awesome new way to store our code in more places than just /application or /system, nothing more. Let's not get over-excited and stick to using existing modular systems if we need them.

Further reading: For more information on what Packages can do, take a look at Jamie Rumbelow's post on the subject. Notice even he gets a little confused between Modules and Packages, but submits to my reasoning in the end. ;-)

Comments

User comments
  • Gravatar Rick Jolly

    Jul 26, 2010

    First, HMVC has nothing to do with modules or packages. It is the ability to call a controller and get its response internally. That is, without a new http request.

    As it stands, a package is a simple solution that doesn't solve all of the module requirements. Why not allow controllers to be called? I agree that wiredesingz's modular separation goes much further. Kohana's modules with the cascading file system is the complete package.

  • Gravatar Phil Sturgeon

    Apr 14, 2010

    I think by "the whole HMVC route" you are referring to "Modular Extensions (HMVC)" which has lots of crazy features that are nothing to do with HMVC so I'm not talking about directly calling modules from other modules. Is that what you meant when you said "it's pretty easy for "modules" to call other "modules" when they are both libraries."?

    As an example, if I write a page module I might want to call a library from the user module to check it a user is logged in. All current modular systems support this, but Packages does not.

    So your hook my get basic loading of that current modules content, but you would have to extend the Loader to do anything useful. By the time you've done that you've recreated most of the useful features in Modular Separation!

  • Gravatar Colin Williams

    Apr 13, 2010

    Well, it's pretty easy for "modules" to call other "modules" when they are both libraries. And app packages really just let you group and locate resources your own way. My module_invoke example works equally as well with old fashioned libraries, but having models, helpers, etc, grouped in makes it cleaner.

    I just get the feeling that App Packages were EllisLab's way of solving the module issue without going the HMVC route. The mob was screaming for modules, in a sense, and EllisLab put on their framework hats and said, "Let's improve the environment for module-like solutions"

  • Gravatar Phil Sturgeon

    Apr 13, 2010

    Thanks for stopping by Colin. As I said you could use Packages as the foundation for building your own HMVC/Modular system, but out of the box this is not a feature that people should be considering.

    Your hook is interesting but it would need work to make it flexible. For starters it would assume everything is a module and even though you could load from the /application and /application/module/[modname] you would not be able to cross-load content from other modules.

    All that aside, views are not supported by the Packages code so your modules would be pretty useless.

    We can hack something together for Packages and eventually make something cool that resembles existing modular systems, but this article was intended to highlight the differences and point out what Packages can and cannot do right now, out of the box.

  • Gravatar Colin Williams

    Apr 12, 2010

    My first impression of App Packages was that it could be seen as a framework for doing modules in your CI app. Think of your root/core controller as more of a router that delegates user requests to different app package libraries (where one of the files is essentially the controller). The controller fires off events that, through naming conventions or configuration, app package files can listen and respond to.

    http://gist.github.com/364035

  • Gravatar Brennan Novak

    Apr 12, 2010

    I am so pumped for Modules! Thanks for the recap and explanation. I can't wait for 2.0 to come out. Do you happen to know when that may be?

  • Gravatar Udi

    Apr 12, 2010

    Yep.
    Thats my opinion exactly.

Post a comment