Yii Interview Questions and Answers – Part 2

11. How to set default controller on Yii Project ?
We can set on configuration file (protected/config/main.php).

    'name'=>'Yii Framework',

12. How to access application components on Yii Project ?
To access an application component, use Yii::app()->ComponentID, where ComponentID refers to the ID of the component (e.g. Yii::app()->cache).

13. What are the core application components available on Yii?
Yii predefines a set of core application components to provide features common among Web applications. For example, the request component is used to collect information about a user request and provide information such as the requested URL and cookies. By configuring the properties of these core components, we can change the default behavior of nearly every aspect of Yii.
Here is a list the core components that are pre-declared by CWebApplication:
• assetManager: CAssetManager – manages the publishing of private asset files.
• authManager: CAuthManager – manages role-based access control (RBAC).
• cache: CCache – provides data caching functionality. Note, you must specify the actual class (e.g.CMemCache, CDbCache). Otherwise, null will be returned when you access this component.
• clientScript: CClientScript – manages client scripts (javascript and CSS).
• coreMessages: CPhpMessageSource – provides translated core messages used by the Yii framework.
• db: CDbConnection – provides the database connection. Note, you must configure its connectionStringproperty in order to use this component.
• errorHandler: CErrorHandler – handles uncaught PHP errors and exceptions.
• format: CFormatter – formats data values for display purpose.
• messages: CPhpMessageSource – provides translated messages used by the Yii application.
• request: CHttpRequest – provides information related to user requests.
• securityManager: CSecurityManager – provides security-related services, such as hashing and encryption.
• session: CHttpSession – provides session-related functionality.
• statePersister: CStatePersister – provides the mechanism for persisting global state.
• urlManager: CUrlManager – provides URL parsing and creation functionality.
• user: CWebUser – carries identity-related information about the current user.
• themeManager: CThemeManager – manages themes.

14. Application Life Cycle of Yii framework ?
When handling a user request, an application will undergo the following life cycle:
1. Pre-initialize the application with CApplication::preinit();
2. Set up the class autoloader and error handling;
3. Register core application components;
4. Load application configuration;
5. Initialize the application with CApplication::init()
• Register application behaviors;
• Load static application components;
6. Raise an onBeginRequest event;
7. Process the user request:
• Collect information about the request;
• Create a controller;
• Run the controller;
8. Raise an onEndRequest event;

15. How to get current controller id in Yii ?
$controllerid = Yii::app()->controller->id;

16. How to get current action id in Yii ?
$actionid = Yii::app()->controller->action->id;

17. What is filter on Yii framework ?
Filter is a piece of code that is configured to be executed before and/or after a controller action executes. For example, an access control filter may be executed to ensure that the user is authenticated before executing the requested action; a performance filter may be used to measure the time spent executing the action.
An action can have multiple filters. The filters are executed in the order that they appear in the filter list. A filter can prevent the execution of the action and the rest of the unexecuted filters.
A filter can be defined as a controller class method. The method name must begin with filter. For example, a method named filterAccessControl defines a filter named accessControl. The filter method must have the right signature:

public function filterAccessControl($filterChain)
    // call $filterChain->run() to continue filter and action execution

18. What are the type of models available in Yii framework ?
Yii implements two kinds of models: Form models and active records. They both extend from the same base class,CModel.

19. What is CFormModel in Yii framework ?
A form model is an instance of CFormModel. Form models are used to store data collected from user input. Such data is often collected, used and then discarded. For example, on a login page, we can use a form model to represent the username and password information that is provided by an end user. For more details, please refer to Working with Forms.

20. What is CActiveRecord in Yii framework ?
Active Record (AR) is a design pattern used to abstract database access in an object-oriented fashion. Each AR object is an instance of CActiveRecord or of a subclass of that class, representing a single row in a database table. The fields in the row are represented as properties of the AR object. Details about AR can be found in Active Record.

One Response to “Yii Interview Questions and Answers – Part 2”

  1. nik says:

    Useful questions and answers. Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Get Alert

    Enter your email address:

  • Categories

  • Twitter

Subscribe Now!
Sign-up for our email newsletter and get free job alerts, current affairs and GK updates regularly.

Subscribe Here

Popup Dialog Box Powered By : XYZScripts.com
  • RSS
  • Facebook
  • Google+
  • Twitter