Monday, July 20, 2020

How to create controller in CakePHP 4?

Hi guys, in our last article we have seen how to setup database in MySQL. It is the first step to start application in CakePHP, or Database First approach. If you have not seen the article visit the link How to setup Database for CakePHP 4.x Application? Today, we will see, how to create our first controller in application. 

Before starting the coding, I will recommend to download any code editor which support the PHP development, my recommendations is to download Visual Studio Code, it is free community edition and has good support for plugins. After download install the PHP IntelliSense Extension. 

Once you are ready, open the command prompt and create new application, in my case, I have created new application “MyShop” in htdocs folder. If you don’t know how to create CakePHP 4.x application visit the link How to create your First CakePHP 4.0 Application

After creating the application, navigate in to root folder of your application and open the Visual Studio Code. Open the terminal window and move into the bin folder of your application. At the command prompt in the terminal window, type the command as shown below. 


In the command we have invoked the cake bake utility to bake our customer controller, where customers is the table name in my database. Once the command has completed its task you will see that it has created two files first under src\controller\ folder, which is “CustomersController” and the other under test\testcase\controlle\ folder is “CustomersControllerTest”, the first one is your main controller class, and second one is its test case class. We will not discuss the test case class here and leave the topic for other article. 

What is created inside the controller class? 

Let’s open the CustomersController Class in code editor. You will notice that this file contains the CutomersController class which extends the AppController class. AppController is the parent class and all the entity controllers inherits from this class. 

Under this class you will see five methods are automatically created to handle the CURD (Create, Update, Retrieve and Delete) operations. Index and View methods both performs the data retrieval operation but the first one dose pagination of all the available data in the table automatically, View method retrieves one record from the table at a time based on the primary key of table. Other three methods add, edit and delete obvious from their name are used for insert, update and delete. 

At this stage we have fully functional controller to handle CURD operation request. 

Now examine each method one by one. 

Index Method 

In index method on line #20 paginate controller retrieves the customer databased on default pagination settings, pagination settings can be changed by altering the paginate controller properties. After that on line #22 customer data is converted in to array and passed to the corresponding view associated with this view method. In later articles we will examine the views of each methods in this controller. This is the simple job of this method retrieve data from model and pass to view. 



View Method 

In this method on line #34 controller retrieves the single record based on the incoming primary key information and on line #38 it is converted to array and again set to its corresponding view. 



Add Method 
Add method is little interesting, considering the security mechanism this method checks that the incoming data is sent through the POST method of HTTP protocol or not, if it is valid data through POST method of HTTP protocol, then it tries to save the data on line #51, if all is well data will be saved and user will be redirected to index page otherwise operation failed message will be sent back to user. 


EDIT Method 

Edit method works in the similar way as Add method but it accepts the data through PATCH, POST, and PUT methods of HTTP protocol. It tries to save the data on line #75 if successful it redirects the user to index page with success message, otherwise sends the operation fail indication to user. 



Delete Method 

Delete method accepts the data via POST or DELETE methods of HTTP protocol, but it only accepts the primary key of the table to delete the record. It deletes one record at a time. 


In the later articles we will see, how the relevant Views of each method works and interact with the user. 

So guys today we have created our first controller and examined the code of controller. We learned the different method are available in the controller and their functions. This is fully functional controller. We can modify the details of each method depending upon the requirement of the application. But this bare bone structure is fully operational. 

Stay tuned with us, share, like, subscribe the article and my blog.

Related Posts:

  • How to create controller in CakePHP 4?Hi guys, in our last article we have seen how to setup database in MySQL. It is the first step to start application in CakePHP, or Database First approach. If you have not seen the article visit the link How to setup Database… Read More
  • How to create Model in CakePHP 4?Model is an important component of MVC design pattern, models are basically responsible for storing data and exchange with other layers in the application, in CakePHP models are fat layer and serve as business and data access… Read More
  • What is CakePHP 4 Development Strategy?Hi guys, in the last article we have discussed the design pattern of CakePHP 4.0, today we will discuss the development strategy. When we talk about strategy, first question comes in the mind from where we should start. So th… Read More

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.