We Write Code logo
We Write Code

Recently, I got to dive into a project that was making use of CakePHP, which is an open-source MVC web framework. To make things interesting, PHP itself was almost entirely new to me going into the project. But after working with it using this framework, I think there are a few reasons you might want to consider Cake for your PHP projects (even aside from the fact that they offer cool bakery-themed swag on their website)!

CakePHP has been around for over a decade and has plenty of community support, and updated versions continue to bring better performance, plugins, and other improvements. The framework focuses on “convention over configuration,” with the idea being that the developer won’t have to spend much time worrying about where certain code needs to live because default rules for this are already in place. As I quickly learned, PHP itself is fairly lax in terms of these conventions, and adding the Cake layer (no pun intended) brings some helpful consistency. One example of this in my project was database schemas. CakePHP expects or assumes certain things, such as the name of a primary key field being id, and the names of any foreign key fields being the name of the table they are referencing followed by _id. The magic of Cake then automatically knows what keys to look for when getting associated records! It’s also worth noting that these conventions can be fairly easily overridden if you don’t want to follow a particular pattern in your project.

Besides a defined structure, there are other other helpful features of CakePHP including ORM (object-relational mapping) and various built-in libraries. I found it easier than I expected to create a new model, write to a table, and simply use predefined methods like get(), find() or save() inside a controller or table method to interact with the data. 

Two of the CakePHP “Helpers” I used were FormHelper and FlashHelper. FormHelper is designed to do a lot of the heavy-lifting of form creation and make things like validation and even layout a bit easier. By calling create() in the view, an html form tag will be rendered on the UI. An options array can be passed into the Form methods for customization. Even JavaScript can be passed into some of these options arrays, which was extremely useful in my case to write some simple jQuery to show/hide content on click. When the form is submitted, we could call $this->Flash->render() to see FlashHelper render a FlashComponent one-time notification like a toast message. There are also many other Components and Helpers that are part of CakePHP that can help eliminate tedious tasks in your project.

There’s so much that could be discussed about this framework, and if you’re considering CakePHP for your project the docs are pretty extensive and go into quite a bit of detail. In my opinion there’s somewhat of a learning curve, maybe depending on your familiarity with PHP to begin with. But there are definitely a lot of great features that I’m sure I will continue to discover the more I work with CakePHP!