Developing with Kirby
A year or so ago, I taught myself PHP by way of WordPress. I didn’t consider myself a developer, but I knew how to make things happen. When I learned Ruby on Rails, I was thrilled to realize that I actually understood the basic concepts of programming, and I picked up Ruby really quickly.
After learning Rails this winter, I was curious to see how much more I understood about PHP now that I had begun to work with Ruby, so I built a new site using Kirby. I’ve loved working with Kirby. It’s really easy to extend the app and it’s API feels very natural.
By default, Kirby does not have a database. It’s a file-based system, meaning that each page has a directory with a text files and images. This is not unlike Jekyll, except that Kirby serves PHP, rather than static pages.
This structure works nicely for simple sites but fortunately you can hook Kirby up to MySQL and Sqlite if you need.
Learning Rails, I found the concept of MVC really helpful for understanding the roles of each part of the application. Kirby is not an MVC app, but it has some aspects of this.
The closest thing you might find to a mole is the
blueprint. Each template has a related
blueprint that lays out the fields for this template. It also determines the layout of the page editor in the back end, called the Panel.
However, Kirby can be run without the Panel and you could add any field to a page you like. You can also add any file to the page directory and it becomes part of that
$page object, even if it is a a directory with another
$page in it. So the “model” in Kirby is somewhere between the
blueprint and the directory itself.
Templates are Kirby’s views. Kirby will check the name of the text file in a directory and go look for a template with the same name. If one doesn’t exist, it’ll use the
default.php template. In the site that I made last week, I left much more logic in the templates than I am comfortable with. I’ll have to refactor this to use the controllers instead.
Kirby does have controllers. Although, I don’t remember seeing this in earlier versions of the app. The BlogPrint plugin uses controllers to distinguish between
Controllers are documented in Kirby, but many of the code examples in the docs or on the forum use quite a bit of logic in the templates.
When Kirby loads it looks through the
/site/plugins directory and loads any PHP files here. This is where you can place helper functions. Frankly, I’d rather if there were two separate directories, “Helpers” and “Plugins”, or “Vendor”. To me, a helper is a function you write yourself for the web app you’re building whereas a “Plugin” is third-party code that you’re plugging into your app.
In the site I made last week, I used the BlogPrint plugin, which uses a handful of helper functions to get post authors and meta data. I also wrote a method that collects images and puts them into a gallery using Foundation’s Clearing Lightbox
Kirby comes with a collection of functions that extend some basic PHP classes and give you a handful of useful helpers for working with PHP. The docs on this are very limited, but the source code is written very clearly.
When I was writing a function to build an image gallery, I used the a couple of these helpers and when things didn’t work as I expected, I just followed the string of functions to figure out what was going on. If I work on a non-Kirby related project in the future, I might want to use the Toolkit.
I’m excited about working with Kirby in the future. It looks like a really excellent choice for my freelance work. I sent the site I built to a friend who will be using it and she replied saying, “I’m LOVING working on this site. So logical and easy.” I can’t take credit for that. I laid out some of the back end pages, but Kirby’s Panel just makes sense.
I launched the new site this week because the charity I made it for is being featured in a news article, but I haven’t finished working on it. At the moment it’s using the base Foundation styles because I was focused on getting something functional out there for their publication deadline. I’ll post a link to the site once it’s done.