Yii Framework Interview Questions

This section covers frequently asked Yii framework interview questions.

1. What is Yii?

Yii is a high-performance, open-source PHP framework for building web applications. It follows the Model-View-Controller (MVC) design pattern and emphasizes code reusability.

2. Yii Versions.

Yii has two major versions: 1.x and 2.x. Yii 2.x is a significant rewrite of the 1.x version.

3. Why is Yii Fast?

Yii uses lazy loading—loading classes and creating objects only when needed. This minimizes overhead and improves performance compared to frameworks that eagerly load resources.

4. What is Yii Best For?

Yii is suitable for various web applications, particularly those requiring high performance and scalability (like portals, forums, and e-commerce sites).

5. Reasons to Use Yii 2.0.

  • Easy installation
  • Uses modern technologies
  • Highly extensible
  • Strong testing support
  • Built-in security features
  • Faster development
  • Easy performance tuning

6. Main Features of Yii.

  • MVC architecture
  • Database access tools (DAO, Query Builder, Active Record)
  • Form handling and validation
  • AJAX support
  • Theming and skinning
  • Internationalization and localization
  • Caching
  • Error handling and logging
  • Security (XSS, CSRF protection)
  • Testing support (PHPUnit, Selenium)
  • Code generation
  • Authentication and authorization

7. What's New in Yii 2.0?

  • PSR-4 autoloading
  • Improved namespaces
  • Performance enhancements
  • RESTful API support
  • Better URL management
  • Multilingual support

8. Components in Yii.

Components are independent objects that provide specific functionalities within a Yii application. They are reusable across controllers.

9. Core Components of Yii.

(This section would list core Yii components: db, assetManager, authManager, cache, clientScript, etc.)

10. Server Requirements for Yii.

  • PHP 5.4 or higher
  • mbstring extension
  • PCRE (Perl Compatible Regular Expressions) support

11. Installing Yii Framework.

(This section would provide commands for installing Yii using Composer.)

12. Yii Framework Directory Structure.

(This would show a visual representation or description of the typical directory structure of a Yii application.)

13. Controllers in Yii.

Controllers handle user requests, interact with models, and select views. They should be focused on managing application flow and data and shouldn't contain presentation logic (HTML) or database access code (better left for models).

14. Models in Yii.

Models represent data structures and business logic. They often contain data validation rules and methods for interacting with the database (using Active Record).

15. Views in Yii.

Views are responsible for displaying data to the user. They contain presentation logic (HTML, templating code) and should not directly access data or request variables.

16. Yii Helpers.

Helpers provide reusable utility functions (string manipulation, HTML generation, etc.).

17. Formatter in Yii.

The formatter component formats data for display (dates, numbers, currencies, etc.).

18. Active Record (AR) in Yii.

Active Record provides an object-oriented way to interact with database tables.

19. Gii (Code Generator).

Gii is a tool for generating code (models, controllers, forms, etc.).

20. Benefits of Yii Over Other Frameworks.

(This section would list advantages of using Yii, such as high performance, rapid development, a robust ecosystem, and a large community.)

21. First File Loaded in Yii Application.

index.php is the entry point of a Yii application. It instantiates the application and starts the request processing cycle.

index.php

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';

$config = require __DIR__ . '/../config/web.php';

(new yii\web\Application($config))->run();

22. First Function Loaded in a Controller.

The `indexAction()` method is typically the first function executed when a user accesses a controller.

23. Naming Conventions in Yii.

Yii's naming conventions map class names to their file locations and follow these guidelines:

  • Class names are typically camel case.
  • Numbers are permitted in class names but are discouraged.
  • Routes follow the pattern: ModuleID/ControllerID/ActionID.
  • URLs typically follow this format: http://hostname/index.php?r=ControllerID/ActionID (though this can be customized with URL management).

24. CModel Class.

CModel is a base class for data models in Yii. It provides basic model functionality (like data validation).

25. Request Lifecycle in Yii 2.

  1. Application pre-initialization.
  2. Error handling setup.
  3. Core component registration.
  4. Configuration loading.
  5. Application initialization.
  6. Behavior registration.
  7. Static component loading.
  8. onBeginRequest event.
  9. Request processing.
  10. Controller creation and execution.
  11. onEndRequest event.

26. render(), renderPartial(), and renderFile().

Method Description
render() Renders a view and applies the layout.
renderPartial() Renders a view without applying the layout.
renderFile() Low-level view rendering; rarely used directly.

27. Using AJAX in Yii.

(This section explains how to handle AJAX requests in Yii controllers and use AJAX for communication between client and server.)

28. HABTM (Has And Belongs To Many) Association.

HABTM is a type of database relationship (many-to-many) used to model cases where multiple records in one table can be related to multiple records in another table.

29. YiiBase Class.

YiiBase is a base class providing core framework functionality; typically not used directly but extended by other classes.

30. Generating CRUD Code with Gii.

(This section explains how to use Gii, a code generation tool in Yii, to create CRUD (Create, Read, Update, Delete) operations for your database tables.)

31. Configuring Database Connection.

(This would detail how to configure a database connection in a Yii application's configuration file, usually `config/web.php` or `config/db.php`.)

32. Creating and Managing Modules.

(This would detail how to create and configure modules using Gii and the application configuration file.)

33. Core Helper Classes in Yii.

(This section lists and briefly explains common Yii helper classes—`ArrayHelper`, `Html`, `Url`, etc.)

34. Customizing Helper Classes.

Extend a core helper class to add custom functionality.

35. Setting the Default Controller.

(This section would explain how to change the default controller in Yii, showing the modifications to be made in the configuration file.)

36. CFormModel.

CFormModel is used for creating data models that collect data from HTML forms; the data is stored in memory and is not persistently saved to a database.

37. Getting and Setting Sessions.

(This section shows PHP code examples for working with session variables.)

38. Switching Between Debug and Production Modes.

Set the `YII_DEBUG` constant in your entry script (`index.php`).

39. CActiveRecord.

CActiveRecord is the base class for Active Record models in Yii. It provides methods for accessing and manipulating database data.

40. Getting the Current URL.

PHP

Yii::app()->request->getUrl();

41. Setting the Home Page URL.

(This section explains how to configure the home page URL in Yii's application configuration file.)

42. Types of Table Relations in Yii.

(This section would list various database relationship types supported by Yii.)

43. renderFile() Function.

renderFile() renders a view file directly, bypassing the layout.

44. Getting Current Controller and Action Names.

PHP

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

// Action name
echo Yii::$app->controller->action->id;

45. ORM Support in Yii 2.

Yii 2 uses Active Record as its Object-Relational Mapper (ORM).

46. Rendering Another Controller's View.

(This would show how to render a view from a different controller using the `renderPartial()` method.)

47. Yii 1.x vs. Yii 2.x.

Version PHP Version
Yii 1.x 5.2+
Yii 2.x 5.4+

48. Yii vs. Yii 2.

Feature Yii 1.x Yii 2.x
Class Naming Uses 'C' prefix; global namespaces No 'C' prefix; namespaces based on directory structure
Event Handling Predefined event names Custom event names
Templating PHP PHP, Twig, Smarty

49. Filters in the Yii Framework.

Filters are pieces of code executed before or after a controller action. They can modify requests or responses or even prevent action execution.

50. Types of Yii Models.

  • Form models: Used for collecting and validating data from HTML forms.
  • Active Record models: Provide an object-oriented interface for database access.