Why I love CakePHP? And You’d too!

Manish Demblani
CakePHP
Published in
5 min readJun 28, 2016
CAKEPHP LOGO

Over the past year and a half, I have used a few Php based frameworks such as CakePHP, Laravel, Silex and Phalcon. Of all these CakePHP is the one that I prefer.

For a beginner, it is not easy to learn CakePHP. At the beginning, it is overwhelming to start. CakePHP requires patience. CakePHP has a steep learning curve, which gradually becomes linear over time, but never stands to become stable(You always learn something new).

But wait, after a certain point in time, to code in CakePHP, it feels like A PIECE OF CAKE.

A Piece of Cake

CakePHP provides an MVC architecture.

  1. M for Model implements the business Login
  2. V for View represents the data to the client
  3. C for Controller handles the client request and helps in communication between the model and the view.

Listed below are a few points about CakePHP that really help this Framework in standing out.

  1. Convention over Configuration

CakePHP prefers convention over configuration, which means when using CakePHP there are a few rules that must be followed. Although initially you might think what is going on, but over the long run they help a lot and drastically save the time it would take to manage your application.

2. Inheritance Model

CakePHP provides a very good inheritance structure, where the reusable function are kept in the parent class and the Class specific function are kept in the child class. For example if you have a function which only gets a particular field from the database, put it in the AppModel if it is used by multiple classes i.e. Model and if the function is used by many controllers put it in the AppController.

3. Environment Specific Configuration

Every application requires a different set of configurations, i.e. The production environment and the development environment. To achieve this in CakePHP, create a separate file named, config.php containing all the basic configurations and the API keys and then load this file in your applications core.php file as follows:

Configure::load(‘config.php’, ‘default’);

Furthermore, CakePHP provides you with 2 files that are specific to you local machine environment, database.php and email.php.

To read more about different software environment, Click here.

4. Save functions

CakePHP save functions are very easy to use and provide you with the flexibility of either updating a record or creating a new record based on the value of the ID key.

Save works in a way that it first refers the schema of the model and creates a sample model structure with their default values( it only uses those with default value), and then replaces those values with the data passes.

However, there is a small catch when you send an empty array to the function. So if an empty array is passed, the save function would work perfectly and return a proper response, but the problem would be that an empty row would be created in your table.

5. Callbacks — before/after(SAVE, FIND, FILTER, VALIDATE)

One of the best things I found out and like about CakePHP, are the callbacks it provides. Both the Controllers and the Models have their specific callbacks which are called in a specific sequence. For example, In your application, the controller is the one which would handle the requests that your application receives, every requests needs to be validated, For this CakePHP provides the beforeFilter callback (which is called before any request reaches the desired endpoint), the beforeFilter function provides the best ground to authenticate and authorize each request as it is. Similarly, the beforeFind provides a way to add a few default conditions before each save.

Note: When using update functions, the callbacks are not invoked so you have to invoke them manually.

6. Routing

CakePHP routing provides proper direction for each request and endpoint.

CakePHP provides a nice way to route your requests to a Controller Endpoint. It provides a separate file(routes.php) where you can specify all the routes, and even provide a list of custom routes. Furthermore, with routes, it even enables you to have api versioning using the CakePHP prefixing. It even provides you with a default set of rest-routes that could come in handy and let you kick-start your application.

7. Bake Module — Automatic Code Generation

CakePHP’s Bake console is another effort to get you up and running in CakePHP — fast. The Bake console can create any of CakePHP’s basic ingredients: models, views and controllers. And we aren’t just talking skeleton classes: Bake can create a fully functional application in just a few minutes. In fact, Bake is a natural step to take once an application has been scaffolded.

8. Find

CakePHP provides you with a very easy to use and understand Find function. The use of this function is to perform READ operations in your database and retrieve data from it. By providing the FIND function, it helps you in getting rid of forming the various different types of SQL queries, be it multiple fetching, creating a View, performing a Join. CakePHP ensures that you use the FIND function and leave the rest to the CakePHP ORM to form the query and send back the data in an appropriate format.

A rough analogy for the CakePHP Find function is that, it acts as a Detective. You give the detective the conditions and your expectations and it will give you what you desire with the right information.

9. Components and Behaviours

Plug and Play

CakePHP provides you with a set of Plug and Play packages called Components and Behaviours which are used in the Controllers and Models respectively. If you think a large portion of the code you use is the same and can be reused to such an extent that even different projects can use it as well, then Components and Behaviours are a perfect fit. Components enable sharing code between Controllers and Behaviours enable the same for Models. For example, Consider you are using Type A authentication to validate a requests, and wish to change to Type B. You only need to remove Type A component from your controller and replace it with Type B(Considering Authentication was done using a Component)

There are many more points that can be covered about CakePHP, such as its ORM, Sanitization, ACL, Caching and so on. But keeping it short. I hope by reading this you get a brief insight into how CakePHP helps you and would be motivated to try it out.

Feedback and Comments are appreciated. Until we meet again.

Cheers,
Manish M. Demblani

--

--

Manish Demblani
CakePHP

Archduke of Computer Engineering. Code to enjoy, like to build stuff, software architecture, DevOps, and learn new technologies. LFC Fan #YNWA.