Hello everyone! Several years ago, I embarked on a journey to build an app in PHP 8. To have some fun along the way, I decided to throw out ‘established’ and ‘entrenched’ ideas in software development. So, I experimented and questioned everything.
The original app still isn’t done. I’m not sure when, if ever, it will be done. However, I’ve extracted a framework from my most successful experiment, and today, I’d like to announce the Swytch PHP Framework, along with a brief introduction to what it is, what it can do for you, and how you can get started with it.
What is the Swytch Framework?
This framework was borne out of several ‘small team’ pain points that I wanted to solve:
- I hate having to implement the same logic in more than one language.
- I don’t want to write Javascript.
- It needs to be scalable.
- It needs to be fast.
- And secure, by default.
- Easy enough to maintain that a junior programmer could use it.
- Powerful enough to accomplish anything.
I honestly think I hit most, if not all, of those points…
The Swytch Framework is a framework that allows you to write regular HTML5, in line with your business logic, in a composable, easy-to-maintain way; much like React or Vue in the Javascript world. It’s built on top of htmx, a library that lets you access AJAX, CSS transitions, Server Sent Events, etc, directly in HTML. It also uses html5-php, a library that handles HTML parsing and manipulation.
The main approach behind The Swytch Framework is to simplify web development by reducing the need for separate templates, controllers, and views. Instead, you write your entire web-app in modular components that can be tested in isolation and stacked together into a cohesive whole. With built-in CSRF/XSS protection and translations as a first-class citizen (vs. an afterthought), you can truly blaze through application development.
What does it look like?
I’ll immediately invite you to take a look at Once and its source code. This is an example project using the Swytch Framework. According to WakaTime, I wrote the whole thing in two weeks, working just a couple of hours a night (including rewrites and refactors due to improvements in the framework). The total number of hours is far less than a true work week.
Why use this?
The Swytch Framework has several advantages over a traditional framework:
It’s pretty fast. Faster than you’d think it should be if you knew how it worked. A page with several hundred PHP components will render faster than a regular Symfony API request, a Twig template, or this (uncached) WordPress page. I haven’t done any official benchmarks, yet, but I understand why it is faster. That’s a blog post for another day though; and when the Framework is a bit more mature and actually optimized.
It’s easy to maintain. You write regular HTML and you get regular HTML back, right beside any relevant API requests. This saves a huge amount of cognitive complexity because you don’t have to jump around through directories and files, trying to understand how data flows.
It’s flexible. The Swytch Framework doesn’t impose any conventions or restrictions on how you structure your project or code. You can completely replace the engine lifecycle and only use basic parts if you so desire.
It’s secure. Most PHP templating languages require you to remember to escape in certain contexts. In those languages, you must escape differently when rendering an attribute vs. inside <script>
tags. Even though these template languages usually have an ‘auto-escape’ option, in the small print, it usually mentions that it only escapes in one mode and you have to remember to manually switch the mode depending on the context. The Swytch Framework contains a recursive-descent parser and rendering stack so it will actually escape your output based on the actual context you are writing in. Not to mention, every <form>
and API request handler contains CSRF logic built-in.
Translations are first-class citizens. Often projects are started without considering translations. This results in a ridiculous amount of time spent bolting it on as an afterthought. By making it part of the design, right from the beginning, you can write text in your native language, and easily add translations later on.
Getting Started
If the project sounds interesting to you, go give it a star on GitHub, and check out the documentation. There’s a template repository if you want to give it a spin as well.
What’s Next?
The Swytch Framework is still new, so there are probably bugs, and there are some half-finished features (such as caching and increasing test coverage). If you have any suggestions or issues, please feel free to open an issue or pull request on GitHub.
I hope you enjoy using The Swytch Framework as much as I do!
Happy coding!