Running multiple sites with CakePHP 3 using plugins

I had an interesting concept for a business that required running multiple sites from within the same vertical. I also wanted the ability for the core backend to span into other verticals. To do this I needed a highly scaleable and flexible core. The premise was the core code would just serve as light-weight CRM, with additional functionality being built as plugins (including the front-ends). Sites would then import these plugins and be configured via some JSON settings. This blog will focus on just running multiple sites off the CakePHP 3 framework, but if interest is expressed I’ll go into further detail.

IF you must

I have a lot of gripes in life. Most of them relate to driving. Thankfully as a mostly work from home developer I only need to drive into the office once a week. During that drive to Park City I consistently contend with slow drivers in the passing lane, which annoys me almost as much as a people who think using their turn signal is optional. This post isn’t about driving though, its about a bigger gripe: people who do IF statements wrong. Let’s look at the common types of badly written if statements that make me want to fire developers.

Poor appendChild and removeChild Performance on Internet Explorer

Recently we noticed that conversion rates on Internet Explorer are quite low. Internet Explorer was converting at just .30% compared to 1.5% for Safari, Chrome, and mobile. I’ve blogged before on how Google Analytics can assist developers in finding bugs and once again GA had saved the day or at least pointed to an issue. Unfortunately this bug had persisted for weeks and since I just assumed browser stack was being slow, it was never addressed. It doesn’t help that no one in our office uses Internet Explorer (even Windows users are on Chrome) and only 13% of our traffic comes from Internet Explorer. Worse, its widely known Internet Explorer lags far behind Chrome and Safari in DOM performance.

Safari Doesn’t Care About Your JavaScript On Submit

Recently I was implementing some new code to show a user that an order was processing. Pretty standard stuff with overlays, hiding the submit button etc… While going through routine testing in various browsers I noticed that the implementation was not working in Safari. Oddly it turns out that when you submit a form via JavaScript in Safari it decides to pretty much not execute anything else. A stackoverflow thread touched up on this a bit, along with a potential solution.

Here we have a sample of the code that doesn’t work as expected in Safari:

The solution is to use setTimeout as a poor mans queue.

I can’t understand why Safari operates differently in this respect. All other major browsers work as expected including Internet Explorer. With some other odd behavior I’ve seen out of Safari lately, I think its quickly becoming the new IE6.

Reorder DOM Elements Without Removing and Replacing

I had an interesting problem recently that required I re-order some DOM elements without doing a remove and replace. The existing implementation had a bug involving some filtering based on html attributes and I wanted to avoid rewriting some legacy code. Typically you would do something like this:

The problem is you are replacing everything including any events you have binded to those elements. The solution comes courtesy of this blog post and utilizes the jQuery detach method. Per the jQuery manual:

The .detach() method is the same as .remove(), except that .detach() keeps all jQuery data associated with the removed elements. This method is useful when removed elements are to be reinserted into the DOM at a later time.

Our initial example becomes this:

This handy solution easily replaces what you might be more familiar with.