Category: PHP (page 2 of 2)

RequireJs followup : The creation of a module


Last time I wrote about a possible require JS module for zend framework 2. After receiving a fair amount of interest, feedback and suggestions in general I decided to give it a go.

I proceeded to spend a decent amount of time reading all of requirejs’s documentation, mapping out how I’d like to fit that structure into the zend framework modular structure. It’s pretty much the same as described in the previous blog post, with only one big change… I’ve actually written the code. I know, shocker right.

So now what?

Well, I know this news might not be as interesting as I hoped it would be, but it is something. By publishing this blog post I’m hoping to receive more feedback, and perhaps some nice PRs. I’ve written some usage examples in the wiki in case you’re interested.


RequireJS (from here on RJS) did an awesome job supplying a modular structure. However, when working with ZF2 you’re already in a modular structure. This means we’re bound together with other modules if we want to share RJS modules. Every ZF2 module’s RJS module should have the possibility to get executed. Imagine having a RJS module that doesn’t get executed because the main application doesn’t know about it. That would take away a lot of flexibility.

I kept in mind that every module should share the same config map when it comes to loading RJS modules. This means that one way or another, we’ll still need a main application to provide all modules with this information.

SxRequireJs sets up a main application, which essentially sets all “module bootstraps” as a dependency, waits for them to load, and then executes them. This will be your module’s entry point, and should have its own dependencies.


Yes yes, I’ll stop talking. But I’d like to ask all of you to take a look, leave feedback and / or submit improvements.


RequireJS implementation for Zend framework 2

I have written a followup on this post, where I explain that the module is already available.

This is my initial idea for a requireJS module that could be used in zend framework 2. There’s not a lot of details yet, but the logic is there. Implementing this will be the easiest part, as I do believe that the architecture for this should be just right because you otherwise lose the flexibility and structure that both zend framework 2 and requireJS offer. I guess I’ll just start of by explaining my personal zend framework 2 architecture a little bit.

I do not store all of my assets in one directory. 

My approach is slightly different, in the sense that I supply assets on a per module basis. Every module has a public directory, and I use AliasMatch and RewriteEngine to map requests to their assets:


So basically I rewrite requests from this “/mymodule/js/main.js” to this “/path/to/application/module/Mymodule/”. This way my modules actually supply their own assets.


The reason I started using requireJS should be obvious to everyone that knows what it is.

In case you still want to know why,  it’s because I like structuring and setting up dependencies in a proper manner. This allows me to have a “main” module that will take care of global modules / dependencies such as jquery, allowing me to quickly scan for modules that don’t support jquery1.7.3 yet (example).


The problem with requireJS is the structure it expects.

With the way zf2 modules are set up the optimizer will not work. Also, being able to have one entry point (for the “main” requireJS) will be rather difficult with a bunch of modules having their own “main”. This could be solved by mapping those to the main application but then you’d not be working modular.


My solution is an implementation that will figure all of this out for you.

I was thinking of building a zf2 module with a viewhelper that allows you to register a path for your zf2 module as well as the main requireJS module. zf2 modules register requirejs modules, and at the end of the run the viewHelper will simply set up the requirejs config (paths, so that /zf2module/* links to the actually resource) and dependencies to make sure that all requirejs modules get loaded.

When enabled, this run could also automatically create a build file for requirejs. This, however, will work event based (zf2) and will ask every module for their requirejs modules (because a build takes all js). This allows you to leave out very specific requirejs modules from the build. Along with this approach, you could also have a build.json / build.js for every zf2 module which would be scooped out by the optimizer (a zf2 layer that will create the build file based on these files).


The reason I think this approach is awesome is because of its flexibility.

Simply because using this approach allows you to not have to think about your structure too much (the main goal behind requireJS and zf2 modules) and focus on doing what you know, thus your logic. It’s flexible enough for you to write your own implementations and use this the way you think fits best, while still keeping all advantages of requireJS.


Some examples, maybe?

I do not have a working module yet, as I’m still collecting feedback and ideas. I do however have some really simple uses for the view helper.

I do realize this illustrates the usage for your main module view, however it might give you a better idea of the structure I’m thinking about.


Any questions, tips or feedback?

Do tell me. This is just the theory and an initial proposal. So all feedback both positive and negative are well appreciated.


©SpoonX 2018