Yii 2 interview questions and Answers

Last updated on Mar 17, 2022
  • Share
Yii 2 interview questions

Yii 2.0 is a component-based and high-performance PHP framework for developing modern-looking and robust web apps. It is a generic programming framework and can be used for producing all kinds of web apps using the PHP framework. We will discuss a few of the most frequently asked Yii2 interview questions further here for your acknowledgment.

Similar to other PHP frameworks, Yii also implements the Model-View-Controller (MVC) architecture. Yii is based on the approach that code must be written in the most straightforward way possible. It is a full-stack framework that offers proven features, such as query builders, multi-tier caching support; RESTful API development support; etc.

Due to component-based architecture, this framework is appropriate for developing large-scale comprehensive apps such as online forums, e-commerce websites, portals, and content management systems (CMS). A vast variety of Yii2 interview questions are also available on our website.

Yii 2.0 is a rewrite of Yii 1.0 and adopts the latest features and protocols, such as Composer, namespaces, PSR, traits, etc.

 

Most Frequently Asked Yii 2 interview questions

Here in this article, we will be listing frequently asked Yii 2 interview questions and Answers with the belief that they will be helpful for you to gain higher marks. Also, to let you know that this article has been written under the guidance of industry professionals and covered all the current competencies.

Q11. How to get current action & controller name in YII2?
Answer

Developers can get the controller name in Yii2 throigh this method -

//Controller Name
echo Yii::$app->controller->id;

We can also get current action through this method:
echo Yii::$app->controller->action->id;

Q12. How to use find() and findAll () in YII2?
Answer

findAll(): It gives us a list of active record models that match the specified conditions.

Admin::findAll(['type' => 'client', 'status' => 1]);

find(): It returns a single record using the primary key.

Admin::find(['id' => 1]);

Q13. How to use delete(), insert() and save() methods in YII2?
Answer
1. delete()

$connection->createCommand()->delete('tbl_user', 'status = 1') ->execute();

2. save()

$model = new Admin;
$model->name = 'YII2';
$model->email = '[email protected]';
$model->save(); // this is equivalent to $model->insert();

3. insert()

$connection->createCommand()->insert('tbl_user',
[
'name' => 'yii',
'status' => 1,
])
->execute();

 

Q14. How to use DateTimePicker in YII2?
Answer

DateTimePicker is a widget in the Yii2 wrapper for Bootstrap DateTimePicker plugin. This plugin is specially styled for Yii 2.0 and is similar to DatePicker widget except that it adds time functionality. This widget also allows degradation to an HTML text input if your browser is not supporting JQuery.

Q15. How to pass parameters from controller to layout in YII2?
Answer
Q16. How can we get all root directory path in yii2?
Answer
Yii2 Basic App
  • @app: It uses to get the application root directory
  • @vendor: It uses to get vendor directory on your root app install directory
  • @runtime: It uses to get application files runtime storage folder
  • @web: It uses to get application base URL path
  • @webroot: It uses to get application web root
  • @tests: It uses to get console tests directory
Yii2 Advanced App
  • @app: It uses to get application root directory frontend or backend
  • @vendor: It uses to get vendor directory on root app install directory
  • @runtime: It uses to get application files runtime storage folder
  • @web: It uses to get application base URL
  • @webroot: It uses to get application web root path
  • @tests: It uses to get console tests directory
  • @common: It uses to get Alias of common root folder on root app install directory
  • @frontend: It uses to get Alias of frontend root folder on root app install directory
  • @backend: It uses to get Alias of backend root folder on root app install directory
  • @console: It uses to get Alias of console root folder on root app install directory
Q17. Explain the difference between homeUrl and baseUrl in Yii2?
Answer
Q18. How we can change the url for Yii::app()->homeUrl in Yii2?
Answer
Q19. How does a search() in SearchModel work in Yii2?
Answer
Q20. How can we set a flash message in Yii2?
Answer

A flash message in Yii 2 is used to keep a message in session through requests of the same user. By default, a flash message is removed from the session after it has been shown to the user. You can set the Flash messages through setFlash() Method

Development History

Unlike other frameworks, Yii is not created by a single individual. Its creation and development is the result of continuous support and feedback by a determined core team, as well as a vast online community of professionals.

In October 2014, The first stable release of Yii 2.0 was done. You may be asked this information in your Yii2 basic interview questions.

Latest Version

The latest version is 2.0.16, which was released in January 30, 2019. You must read in-depth about this if you are preparing for Yii2 interview questions for experienced developers.

Reviewed and verified by Best Interview Question
Best Interview Question

With our 10+ experience in PHP, MySQL, React, Python & more our technical consulting firm has received the privilege of working with top projects, 100 and still counting. Our team of 25+ is skilled in...