Fatskills
Practice. Master. Repeat.
Study Guide: All The Useful YII Interview Questions & Answers
Source: https://www.fatskills.com/php-programming/chapter/all-the-useful-yii-interview-questions-answers

All The Useful YII Interview Questions & Answers

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~7 min read

Q 1. What is YII?
Yii is a component-based, high-performance PHP framework for developing extensive Web applications quickly. Yii enables maximum reusability in programming and significantly accelerates the process of web application development.

Q 2. How to compare yii with other frameworks? Explain

Symfony    Vs Laravel  Vs    YII
Allow developers to create scalable and robust applications    - Full-stack PHP framework    - Fast and high-performance app development framework
APIs enable easy integration with third-party apps    - Supports Object Relationship Mapping    Utilizes - Composer dependency manager to handle installations and dependencies
Compatible with other front-end frameworks  -     Robust and reliable for creating web apps.    - Fastest PHP framework

Q 3. What is the latest version of YII?
YII 2.0

Q 4. Who developed YII?
Yii is not developed by a single individual. A strong team of core developers and an online community of professionals has been contributing to this framework's development.

Q 5. What are the advantages of YII Framework?
Shortened Development Time: Yii offers quite a few tools to automate many of the repetitive tasks in projects.
Security
Caching
Easy Configuration
Huge Community Support

Q 6. What are the directory structure of Yii?
components : contains components (e.g. helpers, widgets) that are only used by this application. 
config : contains the configuration used by the application. 
controllers : contains controller classes. 
lib : contains third-party libraries that are only used by this application.

Q 7. How to install Yii Framework?
Step 1: Assigning PHP engine location path in system variable. Go to “My Computer” ->Right click and select "Properties" ...
Step 2: Create project folder under "www" ...
Step 3: Download Yii framework and put into www directory and install the framework for bid project.

Q 8. How to connect with database in Yii?
In any Yii application, you need to open the main.php file in the protected/config/main.php folder. Now search for the "DB" parameter. In this parameter, you can add the database name, hostname, username, and password for your database server.

Q 9. What is the naming convention in Yii framework? Explain.
Table prefix is defined using Gii by setting to tbl_. Once you set it, UserController will get generated instead of TblUserController.

The Yii Framework includes a class naming convention in which the names of classes map to the directories where they are stored. For your information, 'framework/' directory is the root level directory of Yii that stores all classes hierarchically. Also, in Yii, the class names may contain only alphanumeric characters. Though numbers are allowed but are discouraged. You can use Dot (.) just in place of path separators.

Q 10. What is components in Yii? Explain.
In Yii, a component is an independent code written for a particular task which can be used by summoning controllers. An example of elements is an email component.

Q 11. What is helper in Yii? Why is it used?
The Yii Framework provides many classes that simplify common coding tasks like array manipulation, code generation, etc. These are called Helper classes. These can be found under yii\helpers. These classes are static, which means they merely contain static methods and should never be instantiated.

Q 12. How to make layout in Yii?

Create a Layout
Step 1 − Inside the views/layouts directory, create a file called newlayout. ...
Step 2 − To apply this layout to the SiteController, add the $layout property to the SiteController class.
Step 3 − Now if you go to the web browser at any view of the SiteController, you will see that the layout has changed.

Q 13. How to include a view file in another view file Yii?
You can include them like

$this->render('controlerName/viewName', array('data'=>$data));

Q 14. How we can remove index.php from URL in Yii?

How to remove index. php from URL in Yii2
RewriteEngine on.
RewriteCond %{REQUEST_FILENAME} !- f.
RewriteCond %{REQUEST_FILENAME} !- d.
RewriteRule . index. php.

Q 15. What is the difference between render() and renderpartial() in Yii?

render() Vs renderPartial()

1.    Used for presenting views that correspond to what users see a web page. -    Used for rendering a portion of a page

2.    Places the render result into the layout.  -     Does not place the results of rendering in the layout.

3.    Performs output processing and outputs the result. -     Does not perform output processing

Q 16. In Yii, How to get ip address?
To get the IP address of a user within the Yii Framework, the following code can be used:
echo Yii::app()->request->userHostAddress;
You can call this code from anywhere within your Yii application.

Q 17. What is gii? Why it is used?
Gii is one of the most powerful web-based, code-generator modules in the Yii Framework. It helps the developers in creating fully-customized forms and models for databases.

Q 18. How we can call layouts in controller file in Yii?
When the user is looking at site/login, the SiteController‘s actionLogin() method will be called. That method will render the views/site/login.php View page, pulling that file’s contents into the main layout file at that echo $content location. That’s what’s going on behind the scenes.

So here, then, is the first key concept: if you want to change the general look of your Web site, edit the layout file (views/layouts/main.php). If you were to take your HTML mockup for your site, drop in the echo $content; line at the right place, and save it as views/layout/main.php, you will have created a custom look for your Web app.

Q 19. How we can get current controller & action name in Yii?
You can get the current controller name by:
//Controller Name
echo Yii::$app->controller->id;

You can get current action name by:
echo Yii::$app->controller->action->id;

Q 20. What are the server requirements to install Yii2?
To install the Yii Framework, your server requires PHP 5.4 or higher version, mbstring extension, and PCRE-support

Q 21. List some database related query functions in Yii.

Using query builder usually involves two steps:

Build a yii\db\Query object to represent different parts (e.g. SELECT, FROM) of a SELECT SQL statement.
Execute a query method (e.g. all()) of yii\db\Query to retrieve data from the database.
The following code shows a typical way of using query builder:

$rows = (new \yii\db\Query())
    ->select(['id', 'email'])
    ->from('user')
    ->where(['last_name' => 'Smith'])
    ->limit(10)
    ->all();

Q 22. How we can set default controller in Yii?
You can set the default controller file through (protected/config/main.php).
array(
   'name'=>'Yii Framework',
   'defaultController'=>'site',
)

Q 23. How to use form validations in Yii?
Given a model populated with user inputs, you can validate the inputs by calling the yii\base\Model::validate() method. The method will return a boolean value indicating whether the validation succeeded or not. If not, you may get the error messages from the yii\base\Model::$errors property.

Q 24. How to use session in Yii?

Create Session

Yii::app()->session['name'] = "umesh singh";
Get value from session
$name = Yii::app()->session['name'];
Unset session like
unset(Yii::app()->session['name']);
Remove all session
Yii::app()->session->clear();
Remove session from server
Yii::app()->session->destroy();

Q 25. What is widgets in Yii? How we can use it?
Widgets are instances of CWidget or their child class. This component is primarily for presentational purposes and is embedded in the view script to help generate a complex UI.

Q 26. How to use asset Bundles in Yii?
namespace appassets;
use yiiwebAssetBundle;
class DemoAsset extends AssetBundle {
public $basePath = '@webroot';
public $baseUrl = '@web';
public $js = ['js/demo.js'];
}

Q 27. How to enable the pretty URL format in Yii2?
Create an .htaccess file and add this code.

RewriteEngine on

# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php

2. Now Modify common/config/main-local.php

'urlManager' => [
  'class' => 'yii\web\UrlManager',
  'showScriptName' => false,   
  'enablePrettyUrl' => true,
  'rules' => array(
     '<controller:\w+>/<id:\d+>' => '<controller>/view',
     '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
     '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
  ),
],