Cakephp interview questions

Cakephp interview questions | Freshers & Experienced

  • Sharad Jaiswal
  • 12th Feb, 2021
  • 725 Followers

About Cakephp

Started in April of 2005 by Michal Tararynowicz who wrote a minimum version of speedy application development in PHP and then dubbed it Cake. He opened his work for the online community of developers by publishing the framework under MIT. After the wait of one year, it was finally released in May 2006.

Introducing CakePHP, it is an open-source web framework which supports the MVC (Model- view- controller). It is written in PHP and formed after the ideas of Ruby on rails. This language uses the software design patterns such as model–view–controller, active record, convention over configuration, association data mapping, etc. This is the brief introduction of CakePHP, but if you are preparing for the interview then you must check our online interview questions. We have collected all types of questions in one set to make it easier and understandable. Also, you get equipped with the best CakePHP interview question which is very often asked in interviews.

Key Features of Cakephp

Below are few major features of Cakephp

MVC Pattern

Built-in Validation

CRUD Scaffolding

Smarter router prefixes

Rapid Web Development

ORM

Robust Performance

Clean MVC Conventions

Fast and secure

Q1. What is Cakephp?

Written in PHP, CakePHP is an open-source web framework that is developed by using the concepts of Ruby on Rails. It requires less code to develop web applications using CakePHP while also making the application faster. Developed by Michal Tatarynowicz, CakePHP is based on MVC architecture offering Models, Views, and Controllers. CakePHP offers several libraries to make web development tasks easier while giving the developer a flexible database with a powerful scaffolding system.

Q2. What are controllers in cakephp?

CakePHP follows MVC architecture. Here ‘C’ stands for Controller which 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 data in the web application are called and also 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 Controller class. Based on the requested parameters, the CakePHP application uses convention to convert it into view to be displayed in the browser.

Q3. 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 can be used to do logic just before or after database operation.

Eg: beforeFind(array $query) – it is called before any find that you do with the database. If you want to check something before find an operation, you can use this. Based on the result of the $query, that is true or false the find operation is then done. Some other hooks are afterFind(), beforeValidate(), afterValidate(), beforeSave(), afterSave(), beforeDelete(), afterDelete(), and onError().

Q4. What are server requirements to install CakePHP?

CakePHP installation requirements are the following,

  • 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.

Although a database engine is not necessary for CakePHP application, the following are the supported engines by CakePHP,

  • MySQL with a minimum version of 5.5.3
  • MariaDB with a minimum version of 5.5 
  • PostgreSQL 
  • SQLite 3 
  • Microsoft SQL server with a minimum of 2008 version

Q5. How to do cakephp version check?

There are two ways to find CakePHP version Type

 echo Configure::version()

in the controller to get the current version. Or, go to the following path to find the version in the VERSION.txt file

/path/to/cake/lib/Cake/VERSION.txt 

Q6. 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 is officially supported by CakePHP. 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 composer.phar file in your current directory.
  1. php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
  2. php -r "if (hash_file('sha384', 'composer-setup.php') === 'baf1608c33254d00611ac1705c1d9958c817a1
    a33bce370c0595974b342601bd80b92a3f46067da89e3b06bff421f182') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
  3. php composer-setup.php
  4. php -r "unlink('composer-setup.php');"
  • The above command will download the composer installer, verify if it is 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. composer create-project --prefer-dist CakePHP/app:^3.8 my_app_name.

This command downloads all the necessary files and libraries required for the CakePHP application.

Q7. What is scaffolding in CakePHP?

Scaffolding in CakePHP is an application creation technique that is 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.

Q8. List types of cache supported by CakePHP?

Cache in CakePHP makes your application run faster by storing complex query results for faster access. The types of cache supported by CakePHP are,

File cache - it’s the simplest and slowest cache to store large elements.

APCu cache - it’s a fast cache and uses shared memory to store elements. It provides basic read and writes features.

Wincache - it’s the same APCu cache but it is optimized for Windows.

Redis – provides fast and persistent cache.

Array – a run-time storage cache for storing elements in order.

Q9. What is use of $this->set() in CakePHP?

Set() method in CakePHP is used to pass variables to the views.

$this->set('variable','value') // syntax of set 

Set in conjunction with the compact function is used to pass multiple variables to the view.

$this->set(compact('varible1','variable2','variable2',…....)); // syntax is set with compact. 

Q10. How to get current URL in CakePHP?

To get the complete URL from the hostname, type the following command,

echo $this->Html->url(null, true);

Here, we set the full option to true. So, we get the full scheme, hostname, and the project path.

For eg, the above command produces the following result,

http://localhost/project/controller/action/1

Q11. List few helpers in cakephp?

Helpers in CakePHP contains logic that helps in the creation of view in the application layer. There are many helpers available in CakePHP,

  • Breadcrumbs – this helper is used to easily create and render app.
  • Flash – helps in rendering the flash messages.
  • Forms – helps in quickly creating forms that support re-population, validation, and layout.
  • HTML – makes HTML options faster, flexible, and easier.
  • Numbers – it has methods to display numbers in your views.
  • Paginator – it helps in outputting page number and next/previous links.
  • RSS – it is used to generate XML for RSS feeds.
  • Text – this helper has methods to make the text more usable in your views.
  • Time helper – it makes the time-related information process quickly.
  • URL – it is used to generate URLs from other helpers.

Q12. What do you understand by HABTM?

HABTM, expanded as 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. There are many types of associations in CakePHP that links are used to link different models. 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.

Q13. Enlist 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 types of delete like cascading delete, bulk delete, strict delete with the data.
  • Retrieving Data & Result set – With the help of get() or find() function, you can retrieve the data. With the first() function, you can retrieve the resulting set.
  • Association – with different association types available in CakePHP, you can like two or more databases.

Q14. What is "Security.salt" and "Security.cipherSeed" in CakePHP?

CakePHP provides various security features to keep data, or the passwords safe.

Security.salt is a hash that is used to encrypt/decrypt text messages in CakePHP.

Eg: Security::encrypt($text, $key, $hmacSalt = null) 

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/dedcrypt the message in Security::cipher() function.

Eg: Security::encrypt($text, $key, $hmacSalt = null) 

Here, $key is the 256 bit/32 byte cipher key or Security.cipherSeed

Q15. 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())

Used to do any logic before saving operation.

beforeSave(array $options = array())

Used to do logic after the save operation. 

beforeDelete(boolean $cascade = true)

Used to do pre-deletion logic.

afterDelete()

Used to do logic after deletion.

onError()

It is called if any error occurs.

Q16. Explain different Layers of CakePHP?

CakePHP follows the MVC architecture in building web applications. So, it has three layers.

Model Layer

This layer has the part that has business logic implemented for the application. For eg: retrieving data, converting or processing data, validating data comes under this layer.

View Layer

This layer has the part that is responsible for rendering the front-end interface from the information provided from the model data. It is separate from the model layer.

Controller Layer

This layer is used to handle the request from the user. It gets requests from clients, checks it’s validity, fetches data accordingly, allocated resources, selects presentational data accordingly, and finally delegates the rendering process.

Q17. List some components of CakePHP Framework?

Components are nothing but a logic package that is shared between the controllers. There are many such components available in CakePHP. They are,

  • Security – used to set security and manage HTTP authentication
  • Cookies – used to create cookies in your code easily.
  • Access control lists – provides an interface for database and access control list like ini.
  • Request handling – used to examine and inform about application content to the app.
  • Emails – provides the interface to send emails.
  • Sessions – it provides a wrapper to PHP sessions.
  • Authentication – used for authenticating users.

Q18. What logging levels are available in CakePHP?

In CakePHP, logging can be done very easily by using the log() function.

CakePHP offers various levels of logging function. They are,

  • Emergency – infers that the system is unstable.
  • Critical – used to announce critical conditions.
  • Notice – used to announce significant conditions.
  • Error – used to announce the error conditions.
  • Alert – used to infer an alarm condition and to take action.
  • Debug – announces the debug messages.
  • Info – announces informational messages.

Q19. How do you display the schema of the model in CakePHP?

You can use the following code to get the schema of the model.

$this->ModelName->schema();

Providing the ModelName, you can get the structure.

Q20. How to create session and cookie in CakePHP?

Creat Session in CakePHP

The session is used to identify users and store data specific to them. Session object is used to create a session. Session object can be created using the following code,

$session = $this->request->session(); // here $session is the session object. 

With the session object, you can write, read, delete, session data. Also, you can destroy, renew, and complete a session

Creat Cookie in CakePHP

CakePHP offers an easy and secure way to manage cookies. CookieComponent class in CakePHP is used to manage cookie. To configure a cookie, you need to use config() method. It sets the cookies path, expiration, domain, key, encryption, and many more.

$this->Cookie->config([ 
   'expires' => '+10 days', 
   'httpOnly' => true 
]); 

The above function is used to create and configure the cookie. After configuring the cookie, you can write, read, delete, and check cookie data with various methods provided by the CookieComponent class.

Laravel vs CakePHP

CakePHP Laravel
In the case of small projects or work, CakePHP provides better performance than the Laravel. Laravel is known to be one of the open sources MVC frameworks and supports impactful performance for larger projects as it proffers two-way data binding services
It uses the HMVC architecture. It uses the MVC architecture.
As it does not support any binding process, therefore it is a very simple and easier approach toward routing approach than the Laravel As it supports two-way data binding process, it is very complex and hence it is less preferable than the CakePHP.
It works on directly MVC. It works on HTML and dynamic elements.

CakePHP vs Yii

CakePHP Yii
The database model is object-relational documents-oriented The database model is Relational object-oriented.
It is written in PHP Programming language It is written in the PHP JavaScript language.
Its framework is under the MIT license. Its framework is under the BSD license.
The programming paradigm of CakePHP is object-oriented functional event-driven The programming paradigm of Yii is object-oriented functional event-driven

Major Advantages

Supporting Software's:

The main advantage of using CakePHP is that it supports the wide range of software's that are heavily used by the developers including Model-View-Controller. So, it is great for the developers that they are able to use the MVC in keeping their business logic and interface separately. In addition to this, they are able to manage and update a large web application without working from scratch in a very easy way.

Reusable Code:

The developers can create prototypes and reusable codes due to the presence of features like scaffolding and code generations. Reusability fo code not only makes the programmers more productivity but also decrease the overall development time. Moreover, they are able to create new PHP applications for different projects with the help of reusable code.

Fast Templating System:

Another main advantage of CakePHP is the templating system. It provides a fast and flexible templating system which helps the programmer to increase the application look and can also create custom templates. Many developers also create design-made custom CakePHP templates for the enterprises in a very short period of time.

Classes:

As CakePHP deals with the different classes, it is a boon for those developers who find it difficult to work with standard PHP class. This feature allows the programmers to directly access Core and App directories. So, it is now easy for them to use this feature for defining the functions of each class in an easy way.

Tools:

CakePHP has many standard and innovative tools for the safety and security of web applications as compared to the other frameworks. Developers are able to use the built-in-tools to block the SQL injection and cross-site scripting (XSS) attacks. Moreover, these tools are beneficial to the developers to validate user input and prevent tampering and CSRF.

Disadvantages Of CakePHP:

Needs Refinement:

Many developers believe that CakePHP needs better documentation and needs improvement. Although it is simple and easy-to-learn still it is more complex than CodeIgniter. A lot of time and efforts are required to learn the various aspect of CakePHP.

Default routes:

CakePHP does not support default routes for Fancy URLs as the other frameworks do. Developers have to update the default routes while creating Fancy URLs.

Update:

Many developers find it difficult to update the version from 2.x to 3.x of CakePHP. It seems that CakePHP does not support some backward agreeable changes. This is the reason that many developers do not find it better to use the updated version. Nevertheless, the developers are able to develop applications with new features it updates version.

About Author :

  • Author of Cakephp interview questions

    Sharad Jaiswal

    My name is Sharad Jaiswal, and I am the founder of Conax web Solutions. My tech stacks are PHP, NodeJS, Angular, React. I love to write technical articles and programming blogs.

Leave A Comment :

Valid name is required.

Valid name is required.

Valid email id is required.