Yii2 framework interview questions

Yii2 framework is one of the best PHP frameworks. This page contains Yii2 framework interview questions for both freshers and experienced candidate.

Define yii2?

  1. yii2 is an open-source PHP framework.
  2. It can be used for immediately developing web applications such as blogs and e-commerce websites.
  3. It follows the MVC architecture.
  4. It provides secure and professional features to create healthy projects.

What are the features of yii2 framework?

These are the list of some features of yii2 framework:

  1. Unit and functionality testing
  2. Automatic code generation
  3. Error handling and logging
  4. Internationalization and localization
  5. Authentication and authorization
  6. Extension library
  7. Detailed documentation
  8. Model-View-Controller (MVC) design pattern
  9. Form input and validation
  10. Skinning and theming mechanism
  11. Layered caching scheme
  12. Database Access Objects(DAO), Query Builder, Active Record, DB Migration
  13. AJAX-enabled widgets

In yii2 framework, list some database related functions?

These are the list of database related functions used in yii2 framework:

find()->Creates an Yii dbActive QueryInterface instance for query purpose.
Example:Getting first record matching with condition
$user = User::find()->where([‘name’ => ‘Abc’])->one();

2:findall()->Returns a list of active record models which match the detailed primary key value(s) or a set of column values.
Example:find the customers whose primary key value is 5
$customers = Customer::find All (5);
find customers whose age is 20 and whose status is active
$customers = Customer::find All ([‘age’ => 20, ‘status’ => ‘active’]);

3:insert()->Inserts a row into the associated database table using the attribute values of this record.
Example:$customer = new Customer;
$customer->name = $name;
$customer->email = $email;

4:delete()->Deletes the table row equivalent to this active record.
Example:$models = Customer::find()->where(‘status = 3’)->all();
foreach ($models as $model) {
5:deleteall()-> Deletes rows in the table using the given conditions.
Example:Customer::delete All (‘status = 3’);
save() :Saves the current record. Usages:
$customer = new Customer; // or $customer = Customer::find One ($id);
$customer->name = $name;
$customer->email = $email;

4.Q:In yii2,define “gii” and for what, it is used?

Ans:1: Yii framework provides a more powerful module, which is called Gii.
2: To create and generate fully customized models, forms, CRUD(create, read, update and delete) for database and more by using gii module.
3: To enable Gii by configuring it in the modules property of the application.
Depending upon how the application is created,
Following code is already provided in the config/web.php configuration file:
$config = [ … ];

if (YII ENV DEV) {

$config[‘bootstrap’][] = ‘gii’;
$config[‘modules’][‘gii’] = [
‘class’ => ‘yii Module’,

5.Q:When yii framework starts,what is the name of the first file loaded?

Ans:When yii framework starts,Index.php is first file that can be loaded.
it will create a new object of new YiiwebApplication and start the application.
require(__DIR__ . ‘/../vendor/autoload.php’);
require(__DIR__ . ‘/../vendor/yiisoft/yii2/Yii.php’);
// load application configuration
$config = require(__DIR__ . ‘/../config/web.php’);
// instantiate and configure the application
(new yiiwebApplication($config))->run();

6.Q:In Yii2 Framework,explain Naming Convention?

Ans:1: To define a table prefix by using gii, we need to set it to tbl_.Then it should set up the User Controller instead of TblUserController.
2: It occupies a class naming convention whereby the names of the classes directly map to the directories in which they are stored.
3: All classes are stored hierarchically in the root level directory of the Yii Framework that is the “framework/” directory.
4: Class names may only contain alphanumeric characters. Numbers are allowed in class names but are deterred. A dot (.) is only allowed in place of the path separator.

7.Q: In yii2 framework, explain the request life cycle?

Ans: In Yii2 framework, these are the following stages when handling a request:
1: Pre-initialize the application with Application::preinit ();
2: Set up the error handling;
3: Register core application components;
4: Load application configuration;
5: Initialize the application with Application::init()
a: Register application behaviors;
b: Load static application components;
6: Raise an onBeginRequest event;
7: Process the user request.
a: Collect information about the request;
b: Create a controller;
c: Run the controller;
8: Raise a onEndRequest event;

8.Q: Define yii helpers?

Ans:1: In yii2 framework,helpers are static classes which simplify common coding tasks, these are string or array manipulations, HTML code generation, and so on.
2: All helpers are stored in yii helpers namespace.
3: In yii framework,to use the helper class by directly calling one of its static methods, like the following:
use yii helpers Html;
echo Html::encode(‘Test > test’);

9.Q: In yii2 framework, what are components?

Ans: Components are an independent set of code written to perform a unique task in controllers.
1: In the Yii2 framework, all the applications are constructed upon components that are objects written to a specification.
2: A component is an instance of a derived class.
3: Using a component, it mainly involves accessing its properties and raising/handling its events.
4: The base class Component determines how to define properties and events.

10.Q: Define C Model class in yii2 framework?

Ans:1: In yii2 framework, C model is the base class.
2: It gives the common features needed by data model objects.
3:C Model defines the essential framework for data models which need to be validated.
4: In yii2 framework, all Models extends C Model class.

11.Q: Define Active record in yii2 framework?

Ans:1: It gives an object-oriented interface for accessing and manipulating data stored in databases.
2: An Active Record class is combined with a database table, an Active Record instance correlate to a row of that table, and an attribute represents the value of a particular column in that row.
3: In place of writing SQL statements, to access and manage the data stored in database tables, you would access using Active Record attributes and
call Active Record functions.

12.Q In Yii, what is the difference between “render” and “render Partial”?

Ans: In a yii framework, the Render method is used to render a view with described layout while render partial is used to render only view layout is not involved in view.

13.Q: In Yii, how to get Current Url?

Ans:In yii framework,using Yii::app () ->request->getUrl () method to get the current url.

14.Q: Explain how to configure yii application with database?

Ans: In Yii framework, open the default file main.php which is present in protected/config/main.php and add host-name, database name, username, and password of your database server in DB.

15.Q: Define habtm?

habtm indicates to HasAndBelongsToMany.
It is a kind of associations that can be defined in models for retrieving associated data across different entities.

16.Q: In your application how you can work with different layouts?

Ans: You can change the layout to a different one, by overriding/setting the $this->layout in your controller.

Example:class Controller extends CController
public $layout=’//layouts/column2′ ; / / change the layouts here with column2 or others

public function actionIndex(){
$this->layout=”//layouts/column3″; / / change the layouts here with column3 or others
// your code goes here

18.Q:what is the basic server requirements to install Yii 2 Framework ?

Ans: In Yii2 framework, desires PHP 5.4 or above with mbstring extension and PCRE-support.

19.Q: Define formatter in yii2?

Ans:1: Formatter is a component which is used to format view data in readable format for users.
2: The formatter is implemented by yii\i18n\Formatter by default.
3: Formatter gives a set of functions to format data as numbers, currencies, date/time and other commonly used formats.
Example:$formatter = \Yii::$app->formatter;

// output: January 1, 2014
echo $formatter->asDate(‘2014-01-01’, ‘long’);

// output: 12.50%
echo $formatter->asPercent(0.125, 2);

// output: cebe@example.com
echo $formatter->asEmail(‘cebe@example.com’);

// output: Yes
echo $formatter->asBoolean(true);
// it also handles display of null values:

// output: (not set)
echo $formatter->asDate(null);

20.Q:Yii2 Framework,what Is Latest Version of yii2?

Ans: The latest version of Yii 2 is 2.0.12, released on June 5, 2017.