Javatpoint Logo
Javatpoint Logo

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:

  • Easy to Install
  • Utilizes Modern Technologies
  • Highly Extensible
  • Encourages Testing
  • Simplifies Security
  • Shorten Development Time
  • Easy to Tune for Better Performance

6) What are the main features of the Yii framework?

Yii framework has the following main features, such as:

  • Model-View-Controller (MVC) design pattern
  • Database Access Objects (DAO), Query Builder, Active Record, and DB Migration
  • Form input and validation
  • AJAX-enabled widgets
  • Yii provides a skinning and theming mechanism
  • Web Service available for Apps like android
  • Internationalization and localization translation for multilingual
  • Caching to speed up the application
  • Error handling and logging for tracking
  • It provides cross-site scripting (XSS), cross-site request forgery (CSRF) protection.
  • PHPUnit and Selenium for testing
  • Automatic code generation help us to fast development
  • Authentication and authorization
  • Friendly with third-party code
  • Extension library

7) What's new in Yii version 2.0?

The following things are newly added in Yii version 2.0:

  • The Yii 2.0 framework supports PSR-4 class autoloading, simpler namespaces, faster loading, and improved usability for developers.
  • The Yii 2.0 framework added performance and security improvements.
  • The Yii 2.0 framework added RESTful API integration.
  • The Yii 2.0 framework added improvement on URL handling and processing.
  • Now Yii 2.0 framework, Translations of core messages available in 26 languages.

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:

  • db- database connection.
  • assetManager - manage the publishing of private asset files
  • authManager - manage role-based access control
  • cache- manage to cache functionality
  • clientScript- manage javascript and CSS
  • coreMessages- provides translated core messages
  • errorHandler- manage errors handling.
  • themeManager- Manage themes
  • urlManager- URL parsing and creation functionality
  • statePersister- a mechanism for persisting global state
  • session- Session management
  • securityManager- Security Management

10) What are the server requirements to install Yii?

Yii framework requires the following things, such as:

  • PHP 5.4 or above
  • mbstring extension
  • PCRE-support

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,

  • Controllers may access $_GET, $_POST, and other PHP variables representing user requests.
  • It may create model instances and manage their life cycles. Note that the actual implementation of saving a model should be located in the model instead of the controller.
  • It should avoid containing embedded SQL statements, which are better kept in models.
  • It should avoid containing any HTML or any other presentational markup, which is better kept in views.

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,

  • Models should contain properties to represent specific data.
  • It should contain business logic (e.g., validation rules) to ensure the represented data fulfills the design requirement.
  • It may contain code for manipulating data.

15) What are Views in Yii?

Views are responsible for presenting models in the format that end users desire. In general,

  • Views should mainly contain presentational code, such as HTML and simple PHP code to traverse, format, and render data.
  • It should avoid containing code that performs explicit DB queries. Such code is better placed in models.
  • This is the controller's job. It should avoid direct access to $_GET, $_POST, or other similar variables representing the end-user request. The view should be focused on the display and layout of the data provided to it by the controller or model, but not attempting to access request variables or the database directly.
  • It may access properties and methods of controllers and models directly. However, It should be done only for presentation.

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:

  • The Yii framework is a generic Web programming framework used for developing all kinds of Web applications quickly using PHP. It is also very suitable for developing large-scale applications.
  • The primary goal of the Yii or Yii 2.0 framework is - high performance.
  • The Yii framework is a full-stack framework and ready-to-use features for both the relational and NoSQL databases.
  • The Yii framework implements the MVC (Model-View-Controller) architectural pattern.
  • The Yii framework is extremely extensible.
  • The Yii framework contains the Larger Community, and it also purchased an unlimited license to the beautiful web-based rich text editor and Redactor.

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.

  • You can define the table prefix when using Gii. In your case, you need to set it to tbl_. Then it should generate UserController instead of TblUserController.
  • The Yii Framework employs a class naming convention whereby the names of the classes directly map to the directories in which they are stored. The root-level directory of the Yii Framework is the "framework/" directory, under which all classes are stored hierarchically.
  • Class names may only contain alphanumeric characters. Numbers are permitted in class names but are discouraged. A dot (.) is only permitted in place of the path separator.

Routes that refer respectively to module, controller, and the action requested take the following format:


  • The ModuleID is optional. The ModuleID must be specified in the module's configuration property under the same name.
  • The ControllerID and ActionID should contain only English characters in lowercase, digits, underscores, dashes, and forward slashes.
  • URL Conventions: By default, Yii recognizes URLs with 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:

Yii Interview Questions
  1. Pre-initialize the application with CApplication::preinit();
  2. Set up the error handling;
  3. Register core application components;
  4. Load application configuration;
  5. Initialize the application with CApplication::init()
    • Register application behaviors;
    • Load static application components;
  6. Raise an onBeginRequest event;
  7. Process the user request.
    • Collect information about the request;
    • Create a controller;
    • Run the controller;
  8. Raise an onEndRequest event;

26) What is the difference between render, renderPartial and renderfile in the Yii framework?

Render() renderPartial() renderFile()
render() is commonly used to render a view corresponding to what a user sees as a "page" in your application.
It first renders the view you have specified and then renders the layout for the current controller action (if applicable), placing the result of the first render into the layout. It then performs output processing and finally outputs the result. i.e.) render with a theme.
renderPartial() is commonly used to render a "piece" of a page. The main difference from render() is that this method does not place the render results in a layout.
By default, it also does not perform output processing, but you can override this behavior using the $processOutputparameter. i.e.) render without a theme.
renderFile() is a low-level method that does the grunt work of rendering: it extracts the data variables in the current scope and then runs the view code.
The other two methods internally call this one, but you should practically never need to call it yourself. If you do, keep in mind that you need to pass in a file path (not a view path).

27) How can we use Ajax in Yii?

Ajax can be called in the Yii framework. The basic procedure to do so is:

  • Write the code that responds to (processes) the ajax call as an action in a controller.
  • Call the action with the (Yii-framework) appropriate url in the jQuery method.

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:

  • ArrayHelper
  • Console
  • FileHelper
  • FormatConverter
  • Html
  • HtmlPurifier
  • Imagine
  • Inflector
  • Json
  • Markdown
  • StringHelper
  • Url
  • VarDumper

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:

Yii Yii2
Yii requires PHP 5.2. Yii 2.0 requires PHP 5.4.0 or higher, which makes use of the latest features of PHP.
In Yii, prefix C was used, and the classes were in global namespaces. In Yii2, prefix C is not used in namespaces. And classes based on the directory structure.
Yii uses the On-event method, where custom event names are not allowed to use. In Yii 2.0, any name can be used for the event with a handler attached to it, and event handling can be done using J-query.
Yii uses PHP as the primary template language. Yii 2.0 is equipped with PHP and two more template engines- TWIG and SMARTY.

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.

  1. Form models
  2. Active records.

You may also like:

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA