Yii Interview Questions and Answers
Freshers / Beginner level questions & answers
Ques 1. What is Yii?
Yii is a high-performance PHP framework for developing web applications.
Example:
// Yii::app()->run();
Ques 2. Explain Yii's Gii tool.
Gii is a web-based code generation tool provided by Yii that helps in quickly generating code for models, controllers, forms, and more.
Example:
http://your-app/index.php?r=gii
Ques 3. How does Yii handle form validation?
Yii uses model-based form validation. You define validation rules in the model, and Yii automatically validates input data against these rules.
Example:
// public function rules() { return array('username, password', 'required'); }
Ques 4. How to handle form submissions in Yii?
You can handle form submissions in Yii by using the CActiveForm widget and processing the form data in the controller action.
Example:
// if(isset($_POST['Model'])) { $model->attributes=$_POST['Model']; if($model->save()) { // success action } }
Intermediate / 1 to 5 years experienced level questions & answers
Ques 5. Explain MVC architecture in Yii.
Yii follows the Model-View-Controller architectural pattern where the model represents the data, the view displays the data, and the controller handles user input and updates the model and view.
Example:
// class PostController extends CController { }
Ques 6. What is Yii's ActiveRecord?
Yii's ActiveRecord is an implementation of the Active Record pattern, providing an object-oriented interface for interacting with database tables.
Example:
// $post = new Post; $post->title = 'New Post'; $post->save();
Ques 7. What is Yii's widget?
Widgets in Yii are reusable components that can be embedded in views or layouts to encapsulate complex UI functionality.
Example:
// Yii::app()->widget('application.widgets.MyWidget', array('param'=>'value'));
Ques 8. Explain Yii's asset management.
Yii provides asset management to efficiently manage and publish CSS, JavaScript, and image files, improving application performance.
Example:
// $baseUrl = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('application.assets'));
Ques 9. What is Yii's filter in controllers?
Filters in Yii allow you to perform actions before and after controller actions are executed, providing a way to modify the request or response.
Example:
// public function filters() { return array('accessControl', 'postOnly + delete'); }
Ques 10. Explain Yii's RBAC (Role-Based Access Control).
Yii's RBAC system allows you to manage access control through roles, permissions, and assignments, providing a flexible way to control user access to actions and resources.
Example:
// $auth = Yii::app()->authManager; $role = $auth->createRole('admin'); $auth->assign('admin', 'user123');
Ques 11. Explain Yii's database migration.
Yii's database migration allows you to version control and apply changes to the database schema in a structured and organized way.
Example:
// $ yii migrate/create create_user_table
Ques 12. What is Yii's extension?
Extensions in Yii are packages that provide reusable features, and they can be easily integrated into Yii applications.
Example:
// Yii::import('ext.example.MyClass');
Ques 13. Explain Yii's caching mechanism.
Yii provides support for caching to improve application performance. It includes data caching, page caching, and fragment caching.
Example:
// $dependency = new CDbCacheDependency('SELECT MAX(last_modified) FROM posts'); Yii::app()->cache->set('posts', $data, 3600, $dependency);
Ques 14. What is Yii's console application?
Yii's console application allows you to run commands from the command line, providing a way to perform tasks such as database migrations and cron jobs.
Example:
// $ yii migrate/up
Ques 15. Explain Yii's error handling.
Yii provides a robust error handling mechanism, including detailed error pages, logging, and the ability to customize error messages.
Example:
// throw new CHttpException(404, 'The requested page does not exist.');
Ques 16. What is Yii's internationalization (i18n) and localization (l10n) support?
Yii provides powerful tools for internationalization and localization, allowing you to easily translate messages and format dates, numbers, and currencies.
Example:
// Yii::t('app', 'Hello, {name}!', array('{name}' => 'John'));
Ques 17. Explain Yii's RESTful API support.
Yii supports building RESTful APIs by providing RESTful actions and making it easy to expose models and data in a RESTful manner.
Example:
// class PostController extends CController { public function actions() { return array('rest' => 'ext.yii-rest-actions.ERestActionProvider'); }}
Ques 18. What is Yii's asset bundles?
Asset bundles in Yii allow you to organize and manage assets such as CSS and JavaScript files, improving the efficiency of asset management.
Example:
// class MyAsset extends CAssetBundle { public $css = array('style.css'); } Yii::app()->clientScript->registerPackage('MyAsset');
Ques 19. Explain Yii's URL management.
Yii provides a powerful URL management system that allows you to define clean and user-friendly URLs through rules and patterns.
Ques 20. What is Yii's CActiveDataProvider?
CActiveDataProvider is a data provider in Yii that provides a set of data for widgets like grids and lists, making it easy to work with database data.
Example:
// $dataProvider = new CActiveDataProvider('Post', array('criteria' => $criteria));
Ques 21. Explain Yii's behavior.
Behaviors in Yii allow you to enhance the functionality of a component by attaching additional methods and event handlers.
Example:
// class MyBehavior extends CBehavior { public function extraMethod() { // additional method } }
Ques 22. Explain Yii's CSRF protection.
Yii includes built-in CSRF protection to prevent cross-site request forgery attacks. It can be enabled by adding the 'csrf' filter to controller actions.
Example:
// public function filters() { return array('csrf', 'accessControl'); }
Ques 23. What is Yii's asset manager?
The asset manager in Yii is responsible for managing and publishing asset files such as CSS, JavaScript, and images.
Example:
// $baseUrl = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('application.assets'));
Ques 24. How does Yii handle authentication?
Yii provides various authentication methods, including RBAC, password-based authentication, and integration with external authentication systems.
Example:
// Yii::app()->user->login($identity);
Ques 25. What is Yii's event handling?
Yii's event handling allows you to attach event handlers to components and execute custom code when specific events occur.
Example:
// $component->onEvent = function($event) { // handle event };
Ques 26. Explain Yii's DAO (Data Access Objects).
Yii's DAO provides a low-level database access layer for executing SQL queries and accessing database data without using ActiveRecord.
Example:
// $command = Yii::app()->db->createCommand($sql); $result = $command->queryAll();
Ques 27. What is Yii's CActiveRecord find() method used for?
The find() method in CActiveRecord is used to retrieve a single record based on the specified conditions.
Example:
// $post = Post::model()->find('status=:status', array(':status'=>1));
Ques 28. Explain Yii's dynamic form validation.
Dynamic form validation in Yii allows you to define validation rules in the model based on specific conditions, providing flexibility in validation logic.
Example:
// public function rules() { if ($this->scenario == 'scenario1') { return array('field1', 'required'); } }
Ques 29. How to handle user sessions in Yii?
Yii handles user sessions by using the 'session' component. You can configure session parameters in the application configuration.
Example:
// 'components' => array('session' => array('timeout' => 1440))
Ques 30. Explain Yii's migration command options.
Yii's migration command provides options like 'up', 'down', 'create', and 'history', allowing you to perform actions such as migrating up or down, creating a new migration, and viewing migration history.
Example:
// $ yii migrate/up
Most helpful rated by users: