问题 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 { }
问题 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();
问题 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'));
问题 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'));
问题 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'); }
问题 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');
问题 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
问题 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');
问题 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);
问题 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
问题 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.');
问题 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'));
问题 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'); }}
问题 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');
问题 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.
问题 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));
问题 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 } }
问题 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'); }
问题 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'));
问题 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);
问题 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 };
问题 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();
问题 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));
问题 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'); } }
问题 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))
问题 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