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:
- PHP 5.4.16 or greater
- mbstring extension library
- intl extension
- MySQL with a minimum version of 5.5.3
- MariaDB with a minimum version of 5.5
- SQLite 3
- Microsoft SQL server with a minimum of 2008 version
5) What are the server requirements to install CakePHP?
Here are the following server requirements for the installation of CakePHP:
- An HTTP Server such as Apache or Microsoft IIS.
- PHP with a minimum version of 5.6.
- intl, mbstring, simplexml, PDO PHO extension is required.
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:
- It follows MVC architecture.
- Built-in validations
- Auth & ACL
- CSRF protection via Security Component etc.
- Rapid development
- Secure, scalable, and stable
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:
- Model layer: This layer has business logic implemented for the application, such as retrieving data, converting or processing data, validating data that comes under this layer.
- View layer: This layer is responsible for rendering the front-end interface from the information provided from the model data.
- Controller layer: This layer is used to handle the request from the user. It gets requests from clients, checks its validity, fetches data accordingly, allocated resources, selects presentational data accordingly, and finally delegates the rendering process.
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,
- File cache:It is the simplest and slowest cache to store large elements.
- APCu cache:It is a fast cache and uses shared memory to store elements. It provides basic read and writes features.
- Wincache:It is the same APCu cache, but it is optimized for Windows.
- Redis: It provides a fast and persistent cache.
- Array:It is a run-time storage cache for storing elements in order.
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,
- Validating Data:With the validator object, you can check the type, shape, and size of the data before converting them into entities.
- Saving Data:After loading and making changes to the data, you can use the save() function to insert or update the data into the database.
- Deleting Data: With the delete() function, you can do different delete types like cascading delete, bulk delete, strict delete with the data.
- Retrieving Data & Result set:With the help of the get() or find() function, you can retrieve the data. With the first() function, you can retrieve the resulting set.
- Association:In CakePHP, different association types are available. With the help of this, you can like two or more databases.
17) How many types of associations are supported by CakePHP?
CakePHP supports four associations:
- hasOne: One to One relationship
- hasMany: One to Many relationship
- belongsTo: Many to One relationship
- hasAndBelongsToMany (HABTM): Many to Many relationship
18) What is “Security.salt” and “Security.cipherSeed” in CakePHP?
CakePHP provides various security features to keep data or passwords safe.
- Security.salt is a hash that is used to encrypt or decrypt text messages in CakePHP.
Here, $hmacSalt() is the salt to use for HMAC process. Null is used to defined to use Security.salt
- Security.cipherSeed is nothing but the key used to encrypt or dedcrypt the message in Security::cipher() function.
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:
- Cross-Site Request Forgery
- Request Handling
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.
- The root one is the global vendor folder. It is used to place the third-party libraries which are used for multiple applications. The multiple apps share the same root (Cake + plugins + vendors)
- The APP one is the app-specific one and takes precedence. It is used to place the third-party libraries which are application-specific.
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.
- Bootstrap: It is used to load configuration files, define constants, and other global functions.
- Routes: It is used to load routes.
- Middleware: It is used to add middleware to the application.
- Console: It is used to add console commands to the application.
- Events: It is used to add event listeners to the application event manager.
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?
- Session::read($key)function is used to read specific session data in CakePHP.
- Session::write($key, $value)function is used to write session data in CakePHP.
- Session::delete($key)function is used to delete specific session data in CakePHP.
28) What is the class name for email configuration transport?
There are various class names for email configuration transport:
- Mail: It is used to send mail by using the PHP mail function.
- SMTP:Send using SMTP
- Debug:Do not send the email, just return the result
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:
- Emergency:It infers that the system is unstable.
- Critical:It is used to announce critical conditions.
- Notice:It is used to announce important conditions.
- Error: It is used to announce the error conditions.
- Alert:It is used to infer an alarm condition and to take action.
- Debug: It is announced the debug messages.
- Info:It announces informational messages.
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:
- Run the following command in your terminal, and you’ll have phar file in your current directory.
- The above command will download the composer installer, verify SHA-384, run the installer to download the Composer, and delete the installer.
- After installing the Composer, move it to your working directory.
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,
- beforeFind(array $query): This is called before doing find operation to execute the query that is passes to the function.
- afterFind(array $results, boolean $primary = false): This is called after executing the find operation and is used to modify the results from it.
- beforeValidate(array $options = array()): It is used to modify data before it is validated.
- afterValidate(): It is used to do data clean up after validation process.
- beforeSave(array $options = array()): It is used to do any logic before saving operation.
- beforeSave(array $options = array()): It is used to do logic after the save operation.
- beforeDelete(boolean $cascade = true): It is used to do pre-deletion logic.
- afterDelete(): It is used to do logic after deletion.
- onError(): It is called if any error occurs.
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:
|The database model is object-relational documents-oriented.||The database model is Relational object-oriented.|
|The programming paradigm of CakePHP is object-oriented, functional event-driven.||The programming paradigm of Yii is object-oriented, functional event-driven.|
|Its framework is under the MIT license.||Its framework is under the BSD license.|
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.