Yii Interview Questions
The following are the most popular and useful Yii framework interview questions and answers for fresher and experienced candidates. These questions are created specifically to familiarize you with the types of questions you might encounter during your interview. According to our experiences, good interviewers rarely plan to ask any specific topic during the interview. Instead, questioning usually begins with a basic understanding of the subject, and based on your responses, further discussion happened.
1) What is Yii?
Yii is an open-source, high-performance, component-based, and object-oriented PHP web application framework. Yii follows the MVC (Model-View-Controller) architectural pattern. This PHP framework is best for Web 2.0 development.
Yii enables maximum reusability in programming and significantly accelerates the process of web application development.
2) What are Yii Versions?
The Yii framework currently has two major versions, one is 1.1, and another one is 2.0. Version 1.0 is old, and version 2.0 is a complete rewrite of Yii 1.0
3) Why is Yii so fast?
Yii is so much faster because it is using the lazy loading technique extensively. For example, it does not include a class file until the class is used for the first time. And it does not create an object until the object is accessed for the first time. Other frameworks suffer from the performance hit because they enable functionality (e.g., DB connection, user session) no matter if it is used or not during a request.
4) What is Yii best for?
Yii is a generic Web programming framework that can be used for developing virtually any type of Web application. Because it is lightweight and equipped with sophisticated caching mechanisms, it is especially suited to high-traffic applications, such as portals, forums, content management systems (CMS), e-commerce systems, etc.
5) Why Use Yii 2.0 Framework?
The Yii 2.0 Framework helps to create modern web applications quickly and make sure they perform well. Yii 2.0 framework is:
6) What are the main features of the Yii framework?
Yii framework has the following main features, such as:
7) What's new in Yii version 2.0?
The following things are newly added in Yii version 2.0:
8) What are Components in Yii?
Components are an independent set of code written to perform a specific task in controllers. Yii applications are built upon components which are objects written to a specification.
A component is an instance of a CComponent or its derived class. Using a component mainly involves accessing its properties and raising/handling its events. The base class CComponent specifies how to define properties and events.
9) What are the core components of Yii?
Here are the following core components of Yii, such as:
10) What are the server requirements to install Yii?
Yii framework requires the following things, such as:
11) How to install the Yii framework?
You can install Yii2 by running these commands via composer:
12) What is the directory structure of the Yii framework?
13) What is Controller in the Yii framework?
Controllers are the glue that binds models, views, and other components together into a runnable application. Controllers are responsible for dealing directly with end-user requests. Therefore,
14) What is Model in Yii?
Models represent the underlying data structure of a Web application. Models are often shared among different sub-applications of a Web application. For example, a LoginForm model may be used by both the front end and the back end of an application. A New model may be used by the console commands, Web APIs, and the front or back end of an application. Therefore,
15) What are Views in Yii?
Views are responsible for presenting models in the format that end users desire. In general,
16) What are Yii Helpers?
Helpers are static classes in Yii that simplify common coding tasks, such as string or array manipulations, HTML code generation, etc. In Yii, all helpers are kept under the yii\helpers namespace.
You use a helper class in Yii by directly calling one of its static methods, as the following:
17) What is Formatter in Yii?
Formatter is a Yii application component that is used format view data in readable format for users. By default, the formatter is implemented by yii\i18n\Formatter, which provides a set of methods to format data as date/time, numbers, currencies, and other commonly used formats. You can use the formatter as the following,
18) What is Active Record (AR) in Yii?
Active Record provides an object-oriented interface for accessing and manipulating data stored in databases. An Active Record class is associated with a database table, an Active Record instance corresponds to a row of that table, and an attribute of an Active Record instance represents the value of a particular column in that row.
Instead of writing raw SQL statements, you would access Active Record attributes and call Active Record methods to access and manipulate the data stored in database tables.
19) What is Gii in Yii2?
Gii is a web-based code-generator module provided by the Yii framework. It helps to create and generate fully customized forms, models, CRUD for databases, and more.
We can enable Gii by configuring it in the modules property of the application. Depending upon how we created our application. You may find the following code is already provided in the config/web.php configuration file:
20) What are the benefits of Yii over other frameworks?
Yii has the following benefits over other frameworks of PHP:
21) What is the first file that loaded when you run an application using Yii?
index.php is the first file that loaded when we run an application using the Yii application. It will create a new object of new yii\web\Application and start the application.
22) What is the first function that is loaded from a Controller?
index: is the first function that gets loaded from a controller.
23) What is the naming convention in Yii Framework?
In the latest version of Yii Framework uses a simple standard to set names.
Routes that refer respectively to module, controller, and the action requested take the following format:
The r refers to the route.
24) What is CModel class in Yii?
Yii CModel is the base class that provides the common features needed by data model objects. CModel defines the basic framework for data models that need to be validated. All Models in Yii extend CModel class.
25) Explain request Life Cycle in Yii2 framework?
The Request Life Cycle in the Yii2 framework explains in the following stages:
26) What is the difference between render, renderPartial and renderfile in the Yii framework?
27) How can we use Ajax in Yii?
Ajax can be called in the Yii framework. The basic procedure to do so is:
28) What is meant habtm?
Habtm means HasAndBelongsToMany. It is a kind of association defined in models for retrieving related data across different entities.
29) What is a Yiibase?
YiiBase is a helper class that serves functionalities of a common framework. It would be best if you did not use YiiBase directly. Instead, have to use its child class where we can customize the methods of YiiBase.
30) How to generate CRUD code?
After creating a model class file, we shall generate the code that implements the CRUD operations for user data. We choose the Crud Generator in Gii. In the model class field, we enter 'User'. In the Controller ID field, we enter 'user' in lower case. Then press the preview button followed by the Generate button. Now we are done with the CRUD code generation.
31) How to configure the Yii application with a database?
In the default application of Yii, open the main.php file that exists in protected/config/main.php and search for a parameter named db, there you can add host-name, database name, username, and password of your database server.
32) How can you create and work with the different modules in Yii?
To create a module, access the gii url, and there you can create a module. After creating a module, you need to add it to the main configuration file of your Yii application.
33) What are the Core Helper Classes in the Yii framework?
The following core helper classes are:
34) How to customizing Helper Classes in the Yii framework?
To customize a core helper class, we should create a new class extending from the helper base class. For example,
35) How to set or change the default controller in Yii?
You can set or change the default controller in Yii by using the default yii2 advanced template that redirects to URL:
1) Add below code on backend main/config.php user component:
2) Yii2 advanced template default controller is controllers/siteController. You can change it to your module or controller name by putting the below configuration in main/config.php:
36) What is CFormModel in Yii framework?
In Yii, CFormModel represents a data model that collects HTML form inputs. The data collected by CFormModel are stored in memory only instead of a database. Therefore to collect user inputs, you may extend CFormModel and define the attributes whose values are collected from user inputs.
37) How to get and set session in Yii?
To set session variables, you will need to apply a global PHP $_SESSION variable. Whereas to access the data stored in a session, you can do the following:
38) How to convert debug mode to production mode on the Yii project?
Just define YII_DEBUG as false on the entry script (root/index.php).
39) What is CActiveRecord in the Yii framework?
Active Record (AR) is a design pattern used to abstract database access in an object-oriented fashion. Each AR object is an instance of CActiveRecord or a subclass of that class, representing a single row in a database table.
The fields in the row are represented as properties of the AR object. All AR details are found in Active Record.
40) How to get the current url in Yii?
Yii::app()->request->getUrl() method is used to get current url in Yii framework.
41) How to set Home page Url in Yii?
You can use the below code to set the Home Page Url:
42) How many types of table relations are supported by Yii?
Yii supports almost every type of table relations that are used all across its platform.
43) What is the use of the renderFile() function in Yii?
In Yii, the renderFile() function is usually used in response to AJAX Web requests. It renders a view specified in terms of a view file path or alias.
44) How to get current controller name?
45) How to get current action name?
46) Which ORM does Yii2 supports?
Active Record provides an object-oriented interface for accessing and manipulating data stored in databases.
47) What is another controller view in Yii?
By using renderPartial() method, You can call the view of another controller. You can reuse view files. You can call function in view and controller files. For example,
48) What is the difference between Yii & Yii2?
The difference between Yii & Yii2 is explained as:
49) What is a filter on the Yii framework?
The filter is a piece of code that is configured to be executed before or after a controller action executes.
An action can have multiple filters. The filters are executed in the order that they appear in the filter list. A filter can prevent the execution of the action and the rest of the unexecuted filters.
A filter can be defined as a controller class method. The method name must begin with a filter.
50) What are the types of models available in the Yii framework?
Yii implements two kinds of models. And they both extend from the same base class, CModel.