Updated CakePHP Interview Questions And Answers - 2023

What is Cakephp?


CakePHP is a free, open-source, rapid development framework for PHP. It’s a foundational structure for programmers to create web applications. CakePHP's goal is to enable developers to work in a structured and rapid manner–without loss of flexibility. CakePHP takes the monotony out of web development.

 

When CakePHP was developed?


CakePHP started in April 2005. When a Polish programmer Michal Tatarynowicz wrote a minimal version of a Rapid Application Framework in PHP, dubbing it Cake.CakePHP version 1.0 released in May 2006.  (source:http://en.wikipedia.org/wiki/CakePHP)

 

What is the current stable version of CakePHP?


3.4 (on date 2017-04-01).

 

What is MVC in CakePHP?


Model view controller (MVC) is an architectural pattern used in software engineering.
Model      : Database functions exist in the model
View        : Design parts written here
Controller : Business Logic goes here
MVC Architecture of CakePHP

 

Server requirements for CakePHP.


Here are the requirements for setting up a server to run CakePHP:
An HTTP server (like Apache) with the following enabled: sessions, mod_rewrite (not absolutely necessary but preferred)
PHP 4.3.2 or greater. Yes, CakePHP works great in either PHP 4 or 5.
A database engine (right now, there is support for MySQL 4+, PostgreSQL, and a wrapper for ADODB).

 

How to install CakePHP?


step1: Go to cakephp.org and download the latest version of CakePHP.
step2: Cakephp comes in a .zip file, so unzip it.
step3: Extract the files in the localhost in the desired folder (for example cakephp).
step4: Open the browser and run the URL localhost/cakephp
step5: Just Follow the instructions display on the page.

 

What is the folder structure of CakePHP?


cakephp/
app/
Config/
Console/
Controller/
Lib/
  Locale/
  Model/
  Plugin/
  Test/
  tmp/
  Vendor/
  View/
  webroot/
    .htaccess
  index.php
  
 lib/
 plugins/
 vendors/
 .htaccess/
 index.php/
 README.md/

 

What is the name of the Cakephp database configuration file name and its location?


Default file name is database.php.default.
Its located at /app/config/database.php.default.To connect with the database it should be renamed to database.php

 

What is the first file that gets loaded when you run an application using CakePHP?can you change that file?


bootstrap.php
yes, it can be changed. Either through index.php or through .htaccess

 

What is the use of Security.salt and Security.cipherSeed in CakePHP? How to change its default value?


- The Security.salt is used for generating hashes.we can change the default Security.salt value in /app/Config/core.php.  
- The Security.cipherseed is used for encrypt/decrypt strings. We can change the default Security.cipherSeed value by editing /app/Config/core.php

 

What are the controllers?


A controller is used to manage the logic for a part of your application. Most commonly, controllers are used to managing the logic for a single model. Controllers can include any number of methods which are usually referred to as actions. Actions are controller methods used to display views. An action is a single method of a controller.

 

What is the default function for a controller?


index() function

 

Which function is executed before every action in the controller?


function beforeFilter()

 

List some of the features in CakePHP


- Compatible with versions 4 and 5 of PHP
- MVC architecture
- Built-in validations
- Caching
- Scaffolding
- Access Control Lists and Authentication.
- CSRF protection via Security Component.

 

Using CakePHP, what all are drawbacks.


It loads full application before it starts your task. It's not recommended for small projects because of its resource-heavy structure.

 

What is the naming convention in CakePHP?


Table names are plural and lowercased, model names are singular and CamelCased: ModelName, model filenames are singular and underscored: model_name.php, controller names are plural and CamelCased with *Controller* appended: ControllerNamesController, controller filenames are plural and underscored with *controller* appended: controller_names_controller.php,  

 

What is Scaffolding in Cakephp?


Scaffolding is a technique that allows a developer to define and create a basic application that can create, retrieve, update, and delete objects.

 

How to add Scaffolding in your application?


To add scaffolding to your application, just add the $scaffold variable in the controller,

<?php
class PostsController extends AppController {
    var $scaffold;
}
?>

Assuming you’ve created Post model class file (in /app/Model/post.php), you’re ready to go. Visit http://example.com/posts to see your new scaffold.

 

What is a Component in CakePHP?


Components are packages of logic that are shared between controllers. They are useful when a common logic or code is required between different controllers.

 

What are the commonly used components of CakePHP?


  • Security
  • Sessions
  • Access control lists
  • Emails
  • Cookies
  • Authentication
  • Request handling
  • Scaffolding

 

What is a Helper?


Helpers in CakePHP are associated with the Presentation layers of the application. Helpers mainly contain presentational logic which is available to share between many views, elements, or layouts

 

What are commonly used helpers of CakePHP?


  • FormHelper
  • HtmlHelper
  • JsHelper
  • CacheHelper
  • NumberHelper
  • Paginator
  • RSS
  • SessionHelper
  • TextHelper
  • TimeHelper

 

What is Behavior?


Behaviors in CakePHP are associated with Models. Behaviors are used to change the way models behave and enforcing a model to act as something else.

 

What is the difference between Component, Helper, Behavior?


The component is a Controller extension, Helpers are View extensions, Behavior is a Model Extension.

 

What is an Element?


Elements in CakePHP are smaller and reusable bits of view code. Elements are usually rendered inside views.

 

What is the layout?


Layout in CakePHP is used to display the views that contain presentational code. In simple views are rendered inside a layout.

 

How to set layout in the controller?


var $layout = ‘layout_name’;
to overwrite for a specific action use below code in that action
$this->layout =”layout_name”;

 

How to include helpers in the controller?


public $helpers = array(‘Form’, ‘Html’, ‘Js’, ‘Time’);
to in specific action use below code in that action
$this->helper[] =”helper_name”;

 

How to include components in the controller?


public $components = array(‘Emails’, ‘ImageUploader’, ‘Sms’);

 

How to write, read, and delete the Session in CakePHP?


$this->Session->write(‘Bird.Color’, ‘Black’);
$black = $this->Session->read(‘Bird.Color’);
$this->Session->delete(‘Bird’);

 

What is the use of $this->set();


The set() method is used for creating a variable in the view file. Say for example if we write, $this->set('posts',$posts); in controller fie, then the variable $posts will be available to use in the view template file for that action.

 

What is the use of $this->set(compact());


Using $this->set(compact()) , we can pass multiple parameters to access into the view file.

For example, $this->set(compact('posts','users','reports')); Now all these variables will be available in the respective view files.

 

What are the advantages of each? which would you use and why?


An advantage of the first case $this->set('posts', $posts); is that it allows two different names for the view file and controller file. For example, you could write something like $this->set('postData', $posts);. Now the variable name in the view file would be $postData.The advantage with the second approach $this->set(compact()); is easier to write, and useful especially when we are setting several variables to the view.No need to add a separate line for each variable as we have with $this->set();

For example,

$this->set(compact('posts','users','reports'));

 

Is it possible to have Multiple validation Rules per Field in CakePHP?


Yes, it's possible.

 

What is wrong with the below validation rule?


'email' => array(
    'rule' => array(
        'rule' => 'notEmpty',
        'message' => 'Please Enter Email address.'
    ),
    'rule' => array(
        'rule' => 'email',
        'message' => 'Entered Email address is invalid.'
    )
)

The problem is the first rule notEmpty will never be called because email rule will overwrite it. While using multiple validation rules for the same field you must keep the rule key "unique". In this case, if we want to use multiple rules then, we can simply change the rule key names like,

'email' => array(
    'rule1' => array(
        'rule' => 'notEmpty',
        'message' => 'Please Enter Email address.'
    ),
    'rule2' => array(
        'rule' => 'email',
        'message' => 'Entered Email address is invalid.'
    )
)

 

What is the difference between required and notEmpty in CakePHP?


Difference between required and notEmpty

 

How to Get the current URL in CakePHP?


To get current URL in CakePHP use, 

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

This will give a full URL with a hostname. If you want to get a relative path instead of full URL, then use the following code:

echo $this->here;

This will produce absolute URL excluding hostname i.e. /controller/abc/xyz/

 

How can you make URLs search engine friendly while using CakePHP?


It's an automatic task that is done by CakePHP.

 

Can you list some database related functions in CakePHP?


find, findAll , findAllBy , findBy , findNeighbours and query.

 

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.

 

What is the use of requestAction method?


The method requestAction is used to call a controller’s action from any location and return data from the action.

 

What is recursive in CakePHP?


To understand this topic follow this post: Recursive in CakePHP

 

How can we use ajax in CakePHP?


By calling ajax helper and then using it in the controller for rendering.

 

What is habtm?


Has and belongs to many is a kind of associations that can be defined in models for retrieving associated data across different entities.

 

How CakePHP URL looks in the address bar?


http://example.com/controller/action/param1/param2/param3

 

How can you include a javascript menu throughout the site? Give steps.


By adding the javascript files in webroot and call them in default views if needed everywhere or just in the related views.

 

Why CakePHP have two vendor folders? what is the difference between the two vendors folder available in CakePHP?


There will be two vendor folders available in the CakePHP framework. One in app folder and one in the root folder

- The vendor folder in the app folder is used to place the third-party libraries which are application-specific.

- The vendor folder in the root folder is used to place the third-party libraries which are used for multiple applications.

 

What is the default extension of view files in CakePHP? Can we change it? if yes then how?


The default extension of view files is .ctp

Yes we can change it by writing public $ext = '.yourext'; in AppController.If you want to change it for a particular controller then add it to that controller only. You can also change it for the specific action of the controller by putting it in that action of the controller.

public $ext = '.yourext'; in AppController
 - you can change all the views extentions.

 public $ext = '.yourext';  in specific controller like, PostsController
- you can change all the views extentions of PostsController.

public $ext = '.yourext';  in specific controller action like, index()
- you can change the view extention of index.ctp 

Note: You cannot specify multiple extensions, however, it seems like there is a fall back to .ctp if no .php file is found.

 

 

How can you set a custom page title for the static page?


To set a custom page title, copy-paste following code anywhere in your static page (.ctp) file:

$this->set("title_for_layout", "My page title");

 

How to display the schema of the model?


If you want to display the schema of a particular model then you just need to add the following single line of code. For example, we have “Posts” Controller.

pr($this->Post->schema());

 

To learn more about CakePHP, start reading our CakePHP Tutorials Series


 

Ketan Patel

As a backend and ecommerce developer, I have extensive experience in implementing robust and scalable solutions for ecommerce websites and applications. I have a deep understanding of server-side technologies and have worked with various programming languages. I have experience in integrating with payment gateways, and other third-party services.