CakePHP Interview Questions
The following are the most popular and useful CakePHP interview questions and answers for fresher and experienced candidates. These questions are created specifically to familiarize you with the types of questions you might encounter during interviews.
1) What is CakePHP?
CakePHP is an open-source, free web framework written in PHP scripting Language for rapid web development of web applications and APIs. CakePHP follows the model view controller (MVC) approach. CakePHP is inspired by the Ruby on Rails framework and distributed under the MIT License.
2) When was CakePHP developed?
In April 2005, CakePHP was developed by Michal Tatarynowicz. As a Polish programmer, he wrote a minimal version of a Rapid Application Framework in PHP, dubbing it Cake. CakePHP version 1.0 was released in May 2006.
3) What is CakePHP used for?
CakePHP is used for rapid development for PHP-based web applications, etc.
4) What are the system requirements for CakePHP installation?
System requirements for CakePHP are:
5) What are the server requirements to install CakePHP?
Here are the following server requirements for the installation of CakePHP:
6) Where does the database configuration file reside in CakePHP?
CakePHP stores configure file here: /app/config/database.php.default
7) What are the features of CakePHP?
CakePHP provides various significant features, such as:
8) Explain layers of CakePHP?
CakePHP works on MVC structure. MVC stands for the model view controller. MVC is an architectural pattern that describes a way to structure our application and explains the responsibilities and interactions of each part in that structure:
9) What are controllers in CakePHP?
The Controller acts as a layer between Model and Views. The Controller handles the incoming request data from the routing and makes sure correct models for the web application data are called and the correct view is called.
You can create your Controller to handle the model and your views. Each manually created Controller extends from the AppController class. AppController, in turn, extends from the Controller class. The CakePHP application uses convention to convert it into a view to be displayed in the browser-based on the requested parameters.
10) Explain hooks in CakePHP?
Hooks in CakePHP are callback functions that can be called before or after a database-related operation like accessing, modifying, deleting, or saving the data in the database. This method is used to do logic just before or after database operation.
Some other hooks are afterFind(), beforeValidate(), afterValidate(), beforeSave(), afterSave(), beforeDelete(), afterDelete(), and onError().
11) How do you check the CakePHP version?
There are two ways to find the CakePHP version:
Or, go to the following path to find the version in the VERSION.txt file
12) What is scaffolding in CakePHP?
Scaffolding in CakePHP is an application creation technique used to create a basic application that does data operations such as create, read, update, & delete.
It is used to create a temporary quick application that is not in any way flexible. With scaffolding, you can also create or break the link between objects. To create scaffolding, you need to have a model and Controller defined. Then with a $scaffold variable in the Controller, you can create your application quickly.
13) How many types of cache are supported by CakePHP?
In CakePHP, Cache makes your application run faster by storing complex query results for faster access. The types of cache supported by CakePHP are,
14) How to get the current URL in CakePHP?
To get the complete URL from the hostname, write the following command,
Here, we set the full option to true. So, we get the full scheme, hostname, and project path. Suppose the above command gives the following result,
15) What are the sessions in CakePHP?
PHP Sessions allows you to identify unique users across requests and store persistent data for specific users against a unique session ID. It is used to persist state information between page requests.
Session IDs are normally sent to the browser via session cookies, and the ID is used to retrieve existing session data.
You can access the session data any place you have access to a request object. It means the session is accessible from:
16) List some database-related functions in CakePHP?
CakePHP database layer provides help in working with a relational database like making a connection, building queries, making changes to the structure, preventing SQL injections, and many more. The following are some of the basic database functions that CakePHP offers,
17) How many types of associations are supported by CakePHP?
CakePHP supports four associations:
18) What is "Security.salt" and "Security.cipherSeed" in CakePHP?
CakePHP provides various security features to keep data or passwords safe.
Here, $hmacSalt() is the salt to use for HMAC process. Null is used to defined to use Security.salt
Here, $key is the 256 bit/32 byte cipher key or Security.cipherSeed
19) What are the components in CakePHP?
In CakePHP, components are packages of logic that are shared between controllers. CakePHP comes with a fantastic set of core components you can use to aid in various common tasks.
Using components in your application makes your controller code cleaner and allows you to reuse code between different controllers. Below are some commonly used CakePHP components, such as:
20) What is a Helper in CakePHP?
Helpers are associated with Presentation layers of application. It contains presentational logic, which is available to share between many views, elements, or layouts. Here are some commonly used helpers, such as:
21) What is Behavior in CakePHP?
In CakePHP, Behaviors are associated with Models. It is used to organize and enable horizontal reuse of Model layer logic. They are similar to traits. However, behaviors are implemented as separate classes. This allows them to hook into the life-cycle callbacks that models emit while providing trait-like features.
Behaviors provide a convenient way to package up a behavior that is common across many models.
22) Why does CakePHP have two vendor folders?
Two vendor folders are available in the CakePHP, one folder in the root and another is in the "app" folder.
23) Explain the Application feature in CakePHP.
The Application feature of CakePHP controls how the application is configured and what plugins, middleware, console commands, and routes are included.
24) What is the name of the CakePHP database configuration file?
In CakePHP, database.php.default file is used for database configuration. It is located in /app/config/ directory of CakePHP.
25) What is the use of $this->set() in CakePHP?
In CakePHP, this method is used to create variables in the view file.
Set in conjunction with the compact function is used to pass multiple variables to the view.
And then that variable will be available to use in the view template file for that action.
26) Which function is executed before every action in the Controller?
beforeFilter() function is executed before every action in the controller.
27) How to read, write, and delete the Session in CakePHP?
28) What is the class name for email configuration transport?
There are various class names for email configuration transport:
29) What are logging levels available in CakePHP?
In CakePHP, logging can be done very easily by using the log() function. CakePHP offers various levels of logging function, such as:
30) How do to display the schema of the model in CakePHP?
You can use the following code to get the schema of the model.
And by providing the ModelName, you can get the structure.
31) How to create Cookie in CakePHP?
CakePHP offers an easy and secure way to manage cookies. In CakePHP, CookieComponent class is used to manage cookies. It sets the cookie path, expiration, domain, key, encryption, and many more. To configure a cookie, you need to use the config() method.
The above function is used to create and configure the cookie. After configuring the cookie, you can read, write, delete, and check cookie data with various methods provided by the CookieComponent class.
32) What is Pagination in CakePHP?
In CakePHP, the controller Pagination component is used to building paginated queries. The PaginatorHelper is used to generate pagination links and buttons in view.
33) What is Composer? How to create a CakePHP project using the Composer?
A composer is a tool for managing project dependencies. You can create a CakePHP project using Composer by running the below commands on the terminal.
34) What is the default extension of view files? How can you change it?
.ctp is the default extension of the view file. You can change default extension to write public $ext = '.yourextension' in AppController. If you want to change it for a particular controller, then add it to that particular Controller only.
You can also change it for the specific action of the Controller by putting it in that action only.
35) What are Elements in the CakePHP framework?
In CakePHP, Elements are smaller and reusable bits of view code. Elements are usually rendered inside views.
36) What is a Layout in the CakePHP framework?
In CakePHP, Layout is used to display the views that contain presentational code. In simple views are rendered inside a layout.
37) How to set Layout in the Controller?
You can set Layout in the Controller by using the following command:
To overwrite for a specific action use below command:
38) How to include Helpers in Controller?
You can include Helpers in controller by using this command:
In specific action use below code:
39) What is the use of the RequestAction Method?
The method requestAction is used to call a controller's action from any location and returns data from the action.
40) How to include Components in the Controller?
You can include the components in controller by using the following command:
41) How to change the structure of the URL in an application without modifying the whole code?
The following code is used to change the structure of the URL in an application.
42) How can you encrypt and decrypt data in CakePHP?
In CakePHP, we can encrypt and decrypt data by using security library methods.
43) How can you create a validator in the Controller?
We can create a validator in Controller by using the following command:
44) How to install CakePHP using Composer?
After checking that you have the requirements needed for the CakePHP installation, you can use the Composer to install it. Composer is nothing but a dependency management tool that CakePHP officially supports.
But first, you have to install Composer to create a CakePHP project. To download Composer on Mac or Linux:
To create your first CakePHP project using Composer, type the following command.
This command downloads all the necessary files and libraries required for the CakePHP application.
45) List few callback functions in CakePHP?
Callback function is used to so some logic before or after doing a database related operation. Some of the callback methods are,
46) What do you understand by HABTM?
There are many types of associations in CakePHP that links are used to link different models. HABTM (Has And Belongs To Many) is a type of model association in CakePHP. HABTM is used to associate two models in many ways and repeatedly.
The unique thing about HABTM is that the link between the models is no exclusive. Also, the data in HABTM is a complete set. So, each time a new data association is added, the complete set in HABTM is dropped and created again.
47) What is the difference between CakePHP and Yii?
Below are some major differences between CakePHP and Yii, such as:
49) Which methods are used to create and destroy model associations on the fly?
The bindModel() and unbindModel() Model methods are used to create and destroy model associations on the fly.
50) What are the drawbacks of CakePHP?
The learning curve and loads the full application before it starts your task. It's not recommended for small projects because of its resource-heavy structure.