Thursday 26 April 2018

Yii Framework Interview Questions

1. What is Yii ?

Yii is a high-performance, component-based PHP framework for developing large-scale Web applications rapidly. It enables maximum reusability in Web programming and can significantly accelerate your Web application development process.

2. What are the requirements need for use Yii framework?

you need a Web server that supports PHP 5.1.0.
You also have knowledge on object-oriented programming (OOP).

3. What is Yii best for?

Yii is a generic Web programming framework that can be used for developing virtually any type of Web application. Because it is light-weight and equipped with sophisticated caching mechanisms, it is especially suited to high-traffic applications, such as portals, forums, content management systems (CMS), e-commerce systems, etc.

4. How to install Yii framework ?

Installation of Yii mainly involves the following two steps:
a. Download Yii Framework from yiiframework.com.
b. Unpack the Yii release file to a Web-accessible directory.

5. What command used to create new Yii project ?

% YiiRoot/framework/yiic webapp WebRoot/testdrive
6. Explain structure of Yii Project?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
testdrive/
  index.php              Web application entry script file
  index-test.php         entry script file for the functional tests
  assets/                containing published resource files
  css/                   containing CSS files
  images/                containing image files
  themes/                containing application themes
  protected/             containing protected application files
     yiic              yiic command line script for Unix/Linux
     yiic.bat            yiic command line script for Windows
     yiic.php            yiic command line PHP script
     commands/           containing customized 'yiic' commands
        shell/        containing customized 'yiic shell' commands
     components/         containing reusable user components
        Controller.php   the base class for all controller classes
        UserIdentity.php the 'UserIdentity' class used for authentication
     config/             containing configuration files
        console.php      the console application configuration
        main.php        the Web application configuration
        test.php        the configuration for the functional tests
     controllers/        containing controller class files
        SiteController.php  the default controller class
     data/              containing the sample database
        schema.mysql.sql the DB schema for the sample MySQL database
        schema.sqlite.sql   the DB schema for the sample SQLite database
        testdrive.db     the sample SQLite database file
     extensions/         containing third-party extensions
     messages/           containing translated messages
     models/             containing model class files
        LoginForm.php    the form model for 'login' action
        ContactForm.php the form model for 'contact' action
     runtime/            containing temporarily generated files
     tests/              containing test scripts
     views/              containing controller view and layout files
        layouts/        containing layout view files
           main.php the base layout shared by all pages
           column1.php the layout for pages using a single column
           column2.php the layout for pages using two columns
        site/        containing view files for the 'site' controller
           pages/ containing "static" pages
              about.php the view for the "about" page
           contact.php the view for 'contact' action
           error.php the view for 'error' action (displaying external errors)
           index.php the view for 'index' action
           login.php the view for 'login' action

7. How to connect database on Yii Project?

To use a database, we need to tell the application how to connect to it. This is done in the application configuration fileWebRoot/testdrive/protected/config/main.php, highlighted as follows,
1
2
3
4
5
6
7
8
9
10
return array(
   ......
   'components'=>array(
       ......
       'db'=>array(
           'connectionString'=>'sqlite:protected/data/testdrive.db',
       ),
   ),
   ......
);

8. How to configure gii web based code generator tool on Yii Project?

In order to use Gii, we first need to edit the file WebRoot/testdrive/protected/config/main.php, which is known as the application configuration file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
return array(
   ......
   'import'=>array(
       'application.models.*',
       'application.components.*',
   ),
 
   'modules'=>array(
       'gii'=>array(
           'class'=>'system.gii.GiiModule',
           'password'=>'pick up a password here',
       ),
   ),
);

9. What is MVC ? Why need to use MVC?

MVC (Model – View- Controller ) is a design pattern., which is widely adopted in Web programming.
MVC aims to separate business logic from user interface considerations, so that developers can more easily change each part without affecting the other. In MVC, the model represents the information (the data) and the business rules; the view contains elements of the user interface such as text, form inputs; and the controller manages the communication between the model and the view.

10. How to change debug mode to production mode on yii project ?

Just define YII_DEBUG is false on entry script (root/index.php).
1
2
3
4
5
6
7
// remove the following line when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',false);
// include Yii bootstrap file
require_once('path/to/yii/framework/yii.php');
// create application instance and run
$configFile='path/to/config/file.php';
Yii::createWebApplication($configFile)->run();

11. How to set default controller on Yii Project ?

We can set on configuration file (protected/config/main.php).
1
2
3
4
array(
   'name'=>'Yii Framework',
   'defaultController'=>'site',
)

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;

More about Yii Framework:

No comments:

Post a Comment