The Benefits of a View-First Approach
With MVC web programming, I really enjoy taking a View First approach. This essentially means two things:
- Development starts with the View.
- Requests are routed directly to Views, not Controllers.
Typically, in MVC you would route to a controller, which would interface with the model and render the view. With View first, the view is responsible for interfacing with controllers and returning itself (or passing the request to another view).
A traditional MVC request cycle would look like this:
A View First request cycle could look like this:
In playing with this approach, I’ve come to see a few advantages:
Requests are easy to follow
Just as traditional PHP page controllers act, a URL can be mapped one to one with a file on the actual filesystem. Routes (or rewrites) are still required for advanced functionality, but they become the exception to the rule. This is a very easy pattern for non-engineers to follow. Defining routes becomes the exception to the rule. This is very similar to the approach that Craft CMS takes where a URL is automatically routed to a Twig template.
In a typical MVC stack, controllers are responsible for both rendering the view and populating it with data, but with a View First approach, they are only responsible for the latter. The views themselves call controllers to retrieve the data they require. Controllers then become very composable things as more than one can be called on any given request.
Re-Usable Prototypes and a Natural Workflow
With development focusing on the view initially, template designers have the opportunity to start their work in the final environment without the support of a developer. From the start, they can prototype with the exact same templates that will be used in the final product. To go one further, we’ve been playing with a rapid-prototyping framework that would allow our template designers to write the final template logic from scratch before a single line of code is written by a developer.
To me, this feels like a more natural workflow. Projects can to from Strategy and Wireframes directly to Creative and UX, as opposed to Creative and UX waiting for a developer to set up the environment or tools required.
Are there any frameworks that take this approach?
The Lift Framework (written in Scala) is the only one in the mainstream. All PHP frameworks tend to take a very traditional approach like the one found in Ruby on Rails. I think this approach has a lot in common with MVVM, but with a slightly different request flow. We’re playing with and building some of these ideas into a system built from best of breed PHP libraries and components. I feel like the PHP community would benefit immensely from a View First framework so stay tuned!