Loading…

Yii 2 interview questions

By Umesh Singh
Last update: 29 Mar 2020, 20 Questions
Introduction to Yii2
Yii 2 interview questions

Most Frequently Asked Yii 2 interview questions And Answers

1. What is YII2?

It is a generic programming framework and can be used for producing all kinds of web apps using the PHP framework.Yii is based on the approach that code must be written in the most simple 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.

2. What is the directory structure of YII2?
/
	backend/
	common/
		components/
		config/
			params.php
			params-local.php *
		lib/
			Pear/
			yii/
			Zend/
		migrations/
		models/
			Comment.php
			Extension.php
			...
	console/
		commands/
			SitemapCommand.php
			...
		config/
			main.php
			main-local.php *
			params.php
			params-local.php *
		runtime/
		yiic.php *
	frontend/
		components/
		config/
			main.php
			main-local.php *
			params.php
			params-local.php *
		controllers/
			SiteController.php
		lib/
		models/	
			ContactForm.php
			SearchForm.php		
		runtime/
		views/
			layouts/
			site/
		www/
			assets/
			css/
			js/
			index.php *
	yiic
	yiic.bat
3. What are the new features acquired in YII2?

Some of the new features of Yii 2:

 

  • A new set of PHP Namespaces for encapsulating objects
  • New traits for reusing methods in various classes
  • Anonymous functions
  • SPL classes, interfaces, and late static bindings
  • Use of Array syntax [elements] instead of array [elements]
  • Code generated automatically
  • Automatic handling and logging of errors
4. What is the difference between YII & YII2?
S.no Yii 1 Yii 2
1. Server PHP 5.2 Server PHP 5.4
2. Prefix C used. Classes were global namespaces. C prefix not used in namespaces. Classes based on directory structure.
3. Uses PHP as a primary template language Equipped with PHP, TWIG, and SMARTY
5. What are the advantages of YII2?
  • Yii 2 is based on writing simpler and elegant code
  • Highly extensible
  • Developers can customize almost every piece of their code
  • High performance
  • Biggest online community
6. What are the steps to install YII2?

You can install yii2 with composer by this command
composer create-project --prefer-dist yiisoft/yii2-app-basic YOUR_DIRECTORY_NAME

7. What are the system requirements to install YII2?

Yii 2.0 needs PHP 5.4.0 or higher. However, it will run best with the latest version, PHP 7.

8. What is the purpose of “GII” in Yii2?

It is a code-generator, web-based module in Yii 2, which helps the developers in creating and generating fully customized models, forms, and CRUD for databases.

9. What do you mean by Yii Helpers?

Yii offers different classes that can simplify common coding tasks, including string and array manipulations or HTML code generation. These classes are called helper classes and are placed under the yii\helpers namespace. They are static, which means they only contain static properties and methods and you must never instantiate them.

10. What do you mean by Active Record in yii?

Active Record offers an object-oriented interface that helps the developers access and manipulate data stored in the databases. This is associated with a database table and corresponds to a row of the same table. This is one of the common Yii2 interview questions.

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

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;

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

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]);

13. How to use delete(), insert() and save() methods in YII2?
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();

 

14. How to use DateTimePicker in YII2?

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.

16. How can we get all root directory path in yii2?
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
20. How can we set a flash message in Yii2?

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

Add Review