CakePHP Interview Questions and Answers

Questions

30

Last updated

Feb 10, 2022

CakePHP is a modern, open-source PHP 7 framework that makes building web applications simpler and faster. It is based on MVC architecture that is powerful and quick to grasp. The in-built models, controllers, and views allow natural separation of logic from presentation layers. This framework requires less code and offers a flexible database access layer with a powerful scaffolding system. A lot of CakePHP interview questions were asked about the basic overview.

Advantages

  • Build apps quickly
  • Complicated XML and YAML files not required.
  • Ideal for making commercial apps.
  • Secure, scalable and stable
  • Unique built-in features like translation, caching, database access, and authentication.

Here are some of the CakePHP Interview Questions for freshers as well as experienced candidates:

Development History

Michal Tatarynowicz, a Polish programmer, started CakePHP in April 2005. The framework got published under the MIT license. L. Masters and G. J. Woodworth founded the Cake Software Foundation in Dec 2005 to promote the development of CakePHP and released Version 1.0 in May 2006.

Latest Version: Version CakePHP 3.7.2 got released in Jan 2019.

Most Frequently Asked CakePHP Interview Questions

Here in this article, we will be listing frequently asked CakePHP Interview Questions and Answers with the belief that they will be helpful for you to gain higher marks. Also, to let you know that this article has been written under the guidance of industry professionals and covered all the current competencies.

Q1. Explain CakePHP and why it is used?
Answer

CakePHP is a modern, open-source PHP 7 framework that makes building web applications simpler and faster. It is based on MVC architecture that is powerful and quick to grasp. It is used to develop web applications.

Q2. What are the key features of cakePHP?Explain
Answer
  • Build apps quickly
  • Complicated XML and YAML files not required.
  • Ideal for making commercial apps.
  • Secure, scalable and stable
  • Unique built-in features like translation, caching, database access, and authentication.
Q3. Write the latest version of cakePHP?
Answer

CakePHP 3.7.2

Q4. How to install cakePHP with composer?
Answer

Composer is a tool used for project dependencies.

To install cakePHP we can use
Execute this "php composer.phar create-project –prefer-dist cakephp/app MyProject"

Q5. What are the Server Requirements for installing cakePHP?
Answer

CakePHP is quite simple and easy to install.

  • HTTP Server should Have mod_rewrite is preferred.
  • PHP 5.6.0 or greater including PHP 7.2 (PHP latest version)
  • mbstring PHP extension
  • intl PHP extension
  • simplexml PHP extension

For more details you can visit cakePHP official website Click here

Q6. Why we used $this->set() in cakePHP?
Answer

It is used for creating a variable in the view file with $this->set('variable1','bestinterviewquestion.com'); in controller fie and then that variable $variable1 will be available to use in the view template file for that action.

Q7. How to use session in cakePHP?
Answer

It allows us to manage unique users across requests and also helps to stores data for specific users. We can access session from controllers, views, helpers, cells, and components.

We can use session in cakePHP in following ways :-

 

How to create session?

We can use the write() to create or write session.
Example : $session->write('username', 'bestinterviewquestion.com');

 

How to read session?

We can use the read() to get stored data from session.
Example : $session->read('username');

 

How to Check session?

We can use the check() to check this data is exists or not in session.
Example :

if ($session->check('username')) {
  // name exists and is not null.
}

 

How to delete session?

We can use the delete() to delete data from session.
Example : $session->delete('username');

 

How to destroy session?

We can use the destroy() to destroy session.
Example : $session->destroy();

Q8. How to use pagination in cakePHP?
Answer

1. Open your controller file & put below code.
public $paginate = [
'limit' => 10,
'order' => [
'Users.name' => 'asc'
]
];



2. After this now load Paginator in initialize (). public function initialize()
{
parent::initialize();
$this->loadComponent('Paginator');
}



3. Please set Paginate in Index function.
public function index() {
$this->layout=false;
$details=$this->Users->find('all');
$this->set('users', $this->paginate($details));
}



4. You can write this code in your "index.ctp" page.
echo $this->Paginator->prev('< ' . __('previous'), array('tag' => 'li', 'currentTag' => 'a', 'currentClass' => 'disabled'), null, array('class' => 'prev disabled'));

echo $this->Paginator->numbers(array('separator' => '','tag' => 'li', 'currentTag' => 'a', 'currentClass' => 'active'));
echo $this->Paginator->next(__('next').' >', array('tag' => 'li', 'currentTag' => 'a', 'currentClass' => 'disabled'), null, array('class' => 'next disabled'));
?>

 

Q9. What is a Helper and list some common helpers name used in cakePHP?
Answer

It is the component like classes for the presentation layer of our application. Helpers contain presentational logic that is shared between any views, elements, or layouts in cakePHP.

Most common helpers used in cakePHP, is given below:-

  • FormHelper
  • HtmlHelper
  • JsHelper
  • CacheHelper
  • NumberHelper
  • Paginator
  • RSS
  • SessionHelper
  • TextHelper
  • TimeHelper etc
Q10. What do you mean by Component in cakephp? List some commonly used components.
Answer

It is a class file that contains the common code and logic. It can be shared between the application's controllers. We can perform various common tasks like session handling, cookies and security related things with the help of components.

In CakePHP, we can use various components that are given below:-

  • Authentication
  • Cookie
  • Cross-Site Request Forgery
  • Flash
  • Security
  • Pagination
  • Request Handling etc
Q11. How we can set layout in the controller file using cakePHP?
Answer

$this->layout ="layout_name"; You can use this in your controller's action.

Q12. How to pass multiple parameters to access into the view files?
Answer
We can use $this->set(compact()) to pass multiple parameters to access into the view file.

$this->set(compact('variable1','variable2','variable3'));

Q13. How we can get current URL in CakePHP?
Answer

<?php echo Router::url( $this->here, true ); ?>

Q14. Can we use ajax in cakephp?
Answer

Yes, we can use ajax with by calling ajax helper.

Q15. What is the default extension of view files? How we can change it?
Answer

Default extension of view file is ".ctp".

We can change default extension to write public $ext = '.yourextension' in AppController. If you want to change it for particular controller then please add it into that particular controller only. You can also change it for the specific action of the controller by putting it in that action only.

Q16. How many types of caches does CakePHP support? Explain
Answer
  • APCu
  • File-Based
  • Memcached
  • Redis
  • Wincache
  • XCache
Q17. How we can set custom page title in cakePHP?
Answer

To set a custom page title, copy & paste following code anywhere in your (.ctp) file.
$this->set("title_for_layout", "Home Page | bestinterviewquestion.com");

Q18. List some key features in cakaPHP 3 over cakePHP2?
Answer
  • It improvements it's ORM feature.
  • It enhanced components and helpers
  • Best proficiency in cakePHP3
  • It improved session Management in cakePHP3.
  • It improved consistency of conventions in cakePHP3
  • It improved bug-fixing tool in cakePHP3
Q19. Explain the difference between Component, Helper, Behavior in cakePHP?
Answer

Component : It is a Controller extension in cakePHP.
Helpers : Helpers are View extensions in cakePHP.
Behavior : It is a Model Extension in cakePHP

Q20. Please write the name of Cakephp database configuration file name and its location?
Answer

It's default file name is database.php.default. We can use this file to configure with database. This file is located in "/app/config/database.php.defaut".

Q21. What do you mean by HABTM?
Answer

It stands for "Has And Belongs To Many" and it is a kind of associations that can be defined in models for retrieving associated data across different entities in cakePHP.

Q22. What do you mean by Scaffolding used in CakePHP?
Answer

It is a technique that allows a user to define and create a basic application that can create, retrieve, update and delete objects in cakePHP.

Q23. How we can call a model from view in cakePHP?
Answer

App::import('Model', 'Price');
$price = new Price();

Q24. In cakePHP, which function is first executed before every action in the controller?
Answer

beforeFilter()

Q25. List some database related query function used in cakePHP.
Answer
  • find()
  • findAll()
  • findAllBy()
  • findBy()
  • findNeighbours() etc
Q26. What is the term "Security.salt" and "Security.cipherSeed" in CakePHP?
Answer

Security.salt : It is used for generating hashes. We can change it's default value in /app/Config/core.php.
Security.cipherseed : It is used for encrypt/decrypt strings. We can change it's default value by editing /app/Config/core.php.

Q27. Explain the callback functions in CakePHP?
Answer

Callback functions just before or after a CakePHP model operation. These callback functions can be defined in model classes.These are very simple functions which called automatically when are defined by the core CakePHP.

  • beforeFind()
  • afterFind()
  • beforeValidate()
  • afterValidate()
  • beforeSave()
  • afterSave()
  • beforeDelete()
  • afterDelete()
  • onError()
Q28. What do you mean by Hooks in CakePHP ?
Answer

These are the functions that we can call before and after doing any task in Models like after finding data, before saving data etc.
For Example : beforeSave(), afterSave(), beforeFind(), afterFind().

Q29. What is Validation Model in CakePHP?
Answer

CakePHP provide a very simple but powerful validation model so that we can easily manage our data validation. To do validation in CakePHP we can just need to declare a $validate array in your model class for required fields.


public $validate = array(
'email' => array(
'rule' => 'email',
'message' => 'Can you please enter a valid email address.',
'required' => true
),
'phone' => array(
'rule' => array('minLength', '10'),
'message' => 'Can you please enter a valid mobile number.',
'allowEmpty' => true
)
);

Q30. Explain the difference between beforeRender() and beforeFilter() in cakePHP?
Answer

beforeFilter() is executed before every action in the controller call but beforeRender() is executed before the view is rendered.