Chili Pepper Design

Web development relleno

| Comments

We have an interesting use case with our platform at SplashLab Social where we need render the same View from the Yii MVC framework in different contexts. For instance sometimes a View is rendered in a Iframe on Facebook, other times it’s rendered alone on Mobile, and still other times it’s rendered in our own Iframe widgets. Often there needs to be context-specific JavaScript loaded (for resizing the Iframe container), and sometimes an entirely different version of the View needs to be rendered (a mobile optimized version). We also want to collect analytics about which context the View is loading in, and cache the View on a per-context basis (in case it’s a special version of the View being rendered). Finally we need a single URL for each context (since once a Widget or Facebook App is set up on a third party it’s difficult to update the URL), and a single URL that can be shared via social media.

There are probably many ways to accomplish this, but the solution we have settled on is to create a separate Controller for each context, and create a new Renderer class with the shared View rendering code, which we inject the Controller in to.

  • We have a separate Controller for each context
  • The default Action of the Controller loads the Renderer

Having separate controllers for each context allows up to specify caching rules for each context. The Renderer class

Based on which Controller is requested, we can load a context-specific Renderer class which extends the base Renderer class with the context-specific changes such as additional JavaScript.

We use Yii’s Router to add a static (short) URL for each context, which we route to the proper Controller.

Unit testing using the Renderer class is simplified as well. Since we pass in the Controller, $GET and $POST variables we can easily mock the request environment for each context, and ensure that the proper changes for each Context are being made.