Home >> Interviews Q & A >> CakePHP Interview Questions

CakePHP Interview Questions

CakePHP Interview Questions: CakePHP Interview Questions and Answers for Experienced. CakePHP is an open-source web framework. It follows the model–view–controller (MVC) approach and is written in PHP.
Q:- 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. There is number of in built component like Ajax, RequestHandler, Session etc. official website of cakephp is https://cakephp.org/
Q:- What is MVC (Model, View, and Controller) in CakePHP?
Model–view–controller (MVC) is an architectural pattern used in software engineering.
Model:
The model is responsible to manage the data, it stores and retrieves entities used by an application, usually from a database, and contains the logic implemented by the application.
View:
 Presentation -Design parts written here
Controller:
Business Logic - The controller handles the model and view layers to work together.
The controller receives a request from the client, invokes the model to perform the requested operations and sends the data to the View. The view formats the data to be presented to the user, in a web application as an HTML output
Q:- What is Composer? How to create a CakePHP Project using Composer?
Composer:
Composer is a tool for managing project dependencies.

You can create a CakePHP project using Composer by running below commands on terminal.

Before Install CakePHP 3 make sure following requirements are enabled and installed on your machine:

  1. PHP 5.6.0 or greater (including PHP 7.1).
  2. mbstring PHP extension installed and enabled
  3. intl PHP extension
  4. simplexml PHP extension
Install using Composer:
php composer.phar create-project --prefer-dist cakephp/app demoProject
Install from downloaded zip:
  • 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.
You may also like - PHP Developer Interview Questions
Q:- When CakePHP was developed?
  1. Cake PHP started at April 2005.
  2. A Polish programmer Michal Tatarynowicz wrote a minimal version of a Rapid Application Framework in PHP, dubbing it Cake.
  3. Cake PHP version 1.0 released in May 2006.
Q:- What is the current stable version of CakePHP?
3.x
Q:- Which is the first file that gets loaded when you run a application using cakephp? Can you change that file?
bootstrap.php is the file which is loaded first when application run. Yes, it can be changed either through index.php or .htaccess.
Q:- What is the name of Cakephp database configuration file name and its location?
Default file name is database.php.default. Its located in "/app/config/database.php.defaut"
You may also like - Core PHP Interview Questions
Q:- What is default function and default controller of cakephp which is called automatically?

Default controller is indexController.php and Default function is index.

Q:- How cakephp URL looks in address bar?
http://example.com/controller/action/param1/param2/param3
Q:- Why cakephp have two vendor folder?
There is two vendor folder, one folder in root and another is in "app" folder
  1. The root one is the global vendors folder - if multiple apps share the same root (cake + plugins + vendors)
  2. the APP one is the app specific one and takes precedence
Q:- List some database related functions in cakephp.
find, findAll, findAllBy, findBy, findNeighbours and query.
Q:-11 List some of the features in Cakephp
Following are some feature of Cakephp.
  1. MVC architecture
  2. Built-in validations
  3. Caching
  4. scaffolding
  5. Auth & ACL
  6. CSRF protection via Security Component.
Q:- Can you remember what is the directory structure when you download cakephp?
In CakePHP 3.x Folder Structure is as follows:
  • bin folder holds the Cake console executables.
  • config folder holds the (few) Configuration files CakePHP uses. Database connection details, bootstrapping, core configuration files and more should be stored here.
  • plugins folder is where the Plugins your application uses are stored.
  • logs folder normally contains your log files, depending on your log configuration.
  • src folder will be where you work your magic: it’s where your application’s files will be placed.
  • tests folder will be where you put the test cases for your application.
  • tmp folder is where CakePHP stores temporary data. The actual data it stores depends on how you have CakePHP configured, but this folder is usually used to store model descriptions and sometimes session information.
  • vendor folder is where CakePHP and other application dependencies will be installed. Make a personal commitment not to edit files in this folder. We can’t help you if you’ve modified the core.
  • webroot directory is the public document root of your application. It contains all the files you want to be publically reachable.

    Make sure that the tmp and logs folders exist and are writable, otherwise the performance of your application will be severely impacted. In debug mode, CakePHP will warn you, if it is not the case.

  • .htaccess
  • composer.json
  • index.php
  • README.md
The src folder

CakePHP’s src folder is where you will do most of your application development.

Let’s look a little closer at the folders inside src.

  1. Console: Contains the console commands and console tasks for your application. For more information see Shells, Tasks & Console Tools.
  2. Controller: Contains your application’s controllers and their components.
  3. Locale: Stores string files for internationalization.
  4. Model: Contains your application’s tables, entities and behaviors.
  5. View: Presentational classes are placed here: cells, helpers, and template files.
  6. Template: Presentational files are placed here: elements, error pages, layouts, and view template files.

...read more

Q:- Which function is executed before every action in the controller?

beforeFilter()

Q:- 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
You may also like - Yii2 Interview Questions Answers
Q:- How can we use ajax in cakephp?
By calling ajax helper and then using it in controller for rendering.
Q:- If you have to validate a registration module for a user, what all can be possible ways, which one is the best?
  1. Can be done on submission in the controller
  2. Using javascript/ajax while the user is still filling the data
Q:- How can you include a javascript menu throughout the site?
By adding the javascript files in webroot and call them in default views if needed everywhere or just in the related veiws.
Q:- 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.
Q:- What are commonly used components of cakephp?
  1. Security
  2. Sessions
  3. ACL(Access control lists)
  4. Auth(Authentication)
  5. Emails
  6. Cookies
  7. Request Handling
  8. MVC architecture
  9. Built-in validations
  10. Caching
  11. scaffolding
You may also like - Top 50 Laravel Interview Questions
Q:- What is a Helper? What are commonly used helpers of cakephp?

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

Below are the helpers which is used in cakephp:
  • FormHelper
  • HtmlHelper
  • JsHelper
  • CacheHelper
  • NumberHelper
  • Paginator
  • RSS
  • SessionHelper
  • TextHelper
  • TimeHelper
Q:- What is a Behavior?
Behaviors in CakePHP are associated with Models. Behaviors are used to change the way models behaves and enforcing model to act as something else.
Q:- What is the difference between a Component, a Behavior, and a Helper? Provide an example of where and when each might be used
Component is a Controller extension, Helpers are View extensions, Behavior is a Model Extension.
  • Component is used to extend a Controller
  • Behavior is used to extend a Model
  • Helper is used to extend a View

For example:

  • A shopping cart Component might offer functionality that can be used and shared across multiple Controllers.
  • A custom upload Behavior could be used to extend a Model, for example to add images uploads. Another common example of a Behavior would be to add extra validation functionality beyond that which CakePHP offers by default.
  • A Helper can be used to assist with View functionality.
Q:- What is a Element?
Element in cakephp are smaller and reusable bits of view code. Elements are usually rendered inside views.
Q:- What is a layout?
Layout in cakephp are used to display the views that contain presentational code. In simple views are rendered inside a layout.
Q:- 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";
Q:- How to include helpers in controller?
public $helpers = array('Form', 'Html', 'Js', 'Time'); //to in specific action use below code in that action
$this->helper[] ="helper_name";
Q:- How to include components in controller?
 public $components = array('Emails', 'ImageUploader', 'Sms');
Q:- How to write, read and delete the Session in cakephp?
//Write session
$this->Session->write('MysessionVar', "CakePHP Interview Questions and Answers for Experienced");

//Read session
$this->Session->read('MysessionVar');

//Delete session
$this->Session->delete('MysessionVar');
Q:- What is the use of $this->set()?

The set() method is used for creating a variable in the view file.

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.

Q:- 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.
Q:- What is the use of requestAction method?
The method requestAction is used to call a controller's action from any location and returns data from the action.
Q:- 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 the particular controller then add it into that controller only. 
You can also change it for the specific action of the controller by putting it in that action of 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

You may also like - MYSQL Interview Questions
Q:- What is the use of Security.salt and Security.cipherSeed in Cakephp? How to change its default value?
  1. The Security.salt is used for generating hashes. We can change the default Security.salt value in /app/Config/core.php
  2. Security.cipherseed is used for encrypt/decrypt strings. We can change the default Security.cipherSeed value by editing /app/Config/core.php.
Q:- What is Scaffolding in CakePHP? How to add Scaffolding in your application?

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

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

Scaffolding is Automatic CRUD creation, but i suggest not to use it because at any stage you will need to write your own code.

class PostsController extends AppController {
var $scaffold;
}
Q:- What is callback functions in Cakephp? Please explain?
Callback functions are simple functions which called automatically when are defined by the core CakePHP. Suppose you are inserting record in your database. Now you have two requirement.
  1. Validate the data before inserting into database.
  2. Want to do some changes after inserting into database.
Callback Methods
  1. beforeFind
  2. afterFind
  3. beforeValidate
  4. afterValidate
  5. beforeSave
  6. afterSave
  7. beforeDelete
  8. afterDelete
  9. onError
Q:- How callback functions works in CakePHP?
// Controller
$userData = array();
$this->User->InsertRecord($userData );


//Model
function InsertRecord($userData){
    $this->save($userData);
}

function beforeSave(){
}

function AfterSave(){
}

Q:- What are associations in CakePHP? What are different types of associations?

Associations - Linking Tables Together

Associations are the terms which means getting the data from database which are in different tables.

The four association types in CakePHP are: hasOne, hasMany, belongsTo, and belongsToMany.

Relationship Association Type Example
one to one hasOne A user has one profile.
one to many hasMany A user can have multiple articles.
many to one belongsTo Many articles belong to a user.
many to many belongsToMany Tags belong to many articles.

Associations are defined during the initialize() method of your table object.

For example if we wanted to define a belongsTo association in our ArticlesTable:

namespace App\Model\Table;

use Cake\ORM\Table;

class ArticlesTable extends Table
{
    public function initialize(array $config)
    {
        $this->belongsTo('Authors');
    }
}

If we had the UsersTable and AddressesTable classes made we could make the association with the following code:

class UsersTable extends Table
{
    public function initialize(array $config)
    {
        $this->hasOne('Addresses');
    }
}

For more details Click to read

Q:- What are are drawbacks of CakePHP?
The learning curve, and it loads the full application before it starts your task. It's not recommended for small projects because of its resource-heavy structure.
Q:- How to set Meta title from view files?
$this->set("title_for_layout", "CakePHP Interview Questions and Answers for Experienced");
You may also like - RESTful Web Services Interview Questions
Full Stack Tutorials

Author | Blogger @FullStackTutorials | View all articles