Sharing technology, ideas, insights!
Call: +91 710 466 0336         Email:


View Cells in CakePHP 3 – A quick start!

The concept behind View Cells in CakePHP 3 has been around for a very long time. As per the book View cells are small mini-controllers that can invoke view logic and render out templates. Andy Dawson wrote about this in 2007. If I were to to summarise the use of View Cells I would say, “Think Widgets” – especially true if you are familiar with WordPress. CakePHP has always had requestAction() but calls to requestAction were needlessly CPU expensive. Over the period of time other alternatives have been tried, including instantiating the controller or creating custom helpers but these always felt kludgy. View Cells on the other hand are lightweight as well as modular.

Lets get down to using View Cell in a freshly baked app. I am assuming that you can install CakePHP 3 and Bake an app for Posts. The database table should have a Title field along with the mandatory Id, rest of the fields are optional and up to you.

I will show you how to create a Recent Posts widget using View Cells

Step 1

After you have baked your Posts app and ensured that it is running properly. Open the terminal again and bake a View Cell using the following command

The command will create files src/View/Cell/PostsCell.php and src/Template/Cell/Posts/display.ctp along with a file for writing tests. The file PostCell.php is the class which is the equivalent of mini-controller and the file display.ctp is the template file for rendering the display method of the PostsCell class. More about it later.

Step 2

Open the file PostsCell.php in your favorite editor. It will look something like

You will notice that there are a few conventions which the Cell classes follow.

  • Cells live in the App\View\Cell namespace.
  • Class names end in Cell.
  • Classes inherit from Cake\View\Cell.

Baking also creates an empty display() method. This is the default method which is called when a cell is rendered. Let us put some code into the display method. We are going to be creating a Recent Posts widget the following code is adequate for the purpose.

The code gets a count of the total posts along with the latest three posts and sets it out for rendering.

Step 3

Open the file src/Template/Cell/Posts/display.ctp and paste the following code in it.

Once again the code is simple enough and I won’t insult your intelligence by bothering to explain it. We are nearly done… One last step!

Step 4

The last step is to render the Cell into our view. I want to show this on every page so I used the default layout. Open the file src/Template/Layout/default.ctp and paste the following two lines just below the line which has $this->Flash->render().

The first line loads the cell and runs the display method and the second line renders the Cell. Thats it! Reload your app and you should see something similar to the screenshot below. Navigate to a different page the Widget will still be there in the left hand column 🙂

View Cell in CakePHP3

The View Cell appears in the top of the left column


View Cells is a flexible and powerful concept which is now built right into the CakePHP core. There of course is much more you can do with View Cells than creating widgets. I strongly recommend reading to fully understand their utility.

About the Author

Dr. Tarique Sani is a pediatrician and forensic expert by education. He is a PHP programmer of 'wrote the book' caliber and has to his credit several very popular open source as well as commercial PHP projects. He leads a team of dynamic programmers at SANIsoft who have in-depth understanding of Web scalability, development tools and usability practices with strong developmental skills in PHP, MySQL/PostgreSQL, HTML, Javascript, and Linux/Apache


  1. Sani Sir, this is superb post as well as your blog is awesome, I am new in cakePHP and have gained a lot knowledge just from your few blogs.

    Thanks Sir, keep us updating by posting blogs frequently.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.