Skip to main content

Yii PHP Framework Basics

yii workflow


  1. A user makes a request with the URL http://www.example.com/index.php?r=post/show&id=1
  2. The bootstrap script creates an application instance and runs it.
  3.  The application obtains the detailed user request information from an application      component named request.
  4. The application determines the requested controller and action with the help of an application component named urlManager. 
  5. The application creates an instance of the requested controller to further handle theuser request.
  6. The action reads a Post model whose ID is 1 from the database.
  7. The action renders a view named show with the Post model.
  8. The view reads and displays the attributes of the Post model.
  9. The view executes some widgets
  10. The view rendering result is embedded in a layout


Widget
Widget is an instance of the CWidget
1.
<?php $this->beginWidget('path.to.WidgetClass'); ?>
...body content that may be captured by the widget...
<?php $this->endWidget(); ?>
or
2.
<?php $this->widget('path.to.WidgetClass'); ?>

2 is used when you dont need any content in the widget body.

Application

The application object is instantiated as a singleton by the entry script. The application singleton can be accessed at any place via Yii::app().
By default, the application object is an instance of CWebApplication.
An alternative way of customizing it is to extend CWebApplication.
The configuration is an array of key-value pairs
We usually store the configuration in a separate PHP script (e.g. protected/config/main.php).
To apply the configuration, we pass the configuration file name as a parameter to the application's constructor, or to Yii::createWebApplication() in the following manner, usually in the entry script: $app=Yii::createWebApplication($configFile);

Core Application Components
Yii predefines a set of core application components to provide features common among Web applications.
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:


  1. assetManager: CAssetManager - manages the publishing of private asset files.
  2. authManager: CAuthManager - manages role-based access control (RBAC).
  3. 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.
  4. clientScript: CClientScript - manages client scripts (javascript and CSS).
  5. coreMessages: CPhpMessageSource - provides translated core messages used by the Yii framework.
  6. db: CDbConnection - provides the database connection. Note, you must configure its connectionString property in order to use this component.
  7. errorHandler: CErrorHandler - handles uncaught PHP errors and exceptions.
  8. format: CFormatter - formats data values for display purpose.
  9. messages: CPhpMessageSource - provides translated messages used by the Yii application.
  10. request: CHttpRequest - provides information related to user requests.
  11. securityManager: CSecurityManager - provides security-related services, such as hashing and encryption.
  12. session: CHttpSession - provides session-related functionality.
  13. statePersister: CStatePersister - provides the mechanism for persisting global state.
  14. urlManager: CUrlManager - provides URL parsing and creation functionality.
  15. user: CWebUser - carries identity-related information about the current user.
  16. themeManager: CThemeManager - manages themes.


 Application Life Cycle 
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()
  6. Register application behaviors;
  7. Load static application components;
  8. Raise an onBeginRequest event;
  9. Process the user request:
  10. Collect information about the request;
  11. Create a controller;
  12. Run the controller;
  13. Raise an onEndRequest event;


Controller
A controller is an instance of CController or of a class that extends CController.
An action, in its simplest form, is just a controller class method whose name starts with action.
A controller has a default action. When the user request does not specify which action to execute, the default action will be executed. By default, the default action is named as index. It can be changed.
The siteController has
actionIndex(){
$this->render('index');
//where index is an index.php file in views/site

}

and actionContact(){
......
}
and many other actions.




by default yii urls follow http://localhost/yourapp/index.php?r=controller/action

THE END
 More coming up in a few days..subscribe or follow

Note: this article is adapted from the  Yii Framework Guide

Comments

Popular posts from this blog

Ten output devices, advantages and disadvantages, inter-site back-up mechanism, Expert systems for medical diagnosis,information systems security

(i)Printer Printer enables us to produce information output on paper. It is one of the most popular computer output devices we often use to get information on paper - called hard copy. Advantage They produce high quality paper output for presentation at a speedy rate. It is also possible to share the printer among different users in a network. Disadvantage The cost for maintenance of the printer equipment as well printing ink is cumulatively high. (ii) Plotters These devices are used to produce graphical outputs on paper. They have automated pens that make line drawings on paper Advantage They can produce neat drawings on a piece of paper based on user commands. In Computer Aided Design (CAD) they are used to produce paper prototypes that aid in design of the final system. Disadvantage They are more expensive than printers. Further, the command based interface is difficult to use. (iii)Monitor It is...

Start Wamp server on windows automatically permanently

For those that have completely refused to use linux platforms for development, you might find this useful. As with all (aspiring) web developers, it’s always important to test your projects locally before putting it out there for the entire web community to see. One must-have developer tool for this purpose is WAMPServer. We’ve all wished it’s automatically up and running when we need it. These easy steps will help you automate WAMPServer to run on system start-up. For those unfamiliar with WAMPServer, it is a development package that lets you run web development projects locally. WAMP stands for Windows, Apache, MySQL, PHP/Perl/Python. It’s basically four programs packaged to work as one. WAMP basically turns any Windows PC into a localized web server. The Linux counterpart is called LAMP, obviously. Once WAMPServer is installed in your PC, you’ll be able to test your web projects before putting it into the live environment. But I always found it a hassle to manually s...

simple basic object oriented java code for employee salary calculation

import java.io.*; import java.util.Scanner; public class Employees {     Scanner scan=new Scanner(System.in);     String Fname;   int EmpID;  int DOB; double Allowance;   double Salary;     public void getDetails(){   System.out.println("Enter the first name");   Fname=scan.next();   System.out.println("Enter the ID number");   EmpID=scan.nextInt();   System.out.println("Enter the date of birth");   DOB=scan.nextInt();   System.out.println("Enter the salary");   Salary=scan.nextDouble();   Allowance=0.6*Salary;     }    public void printReport(){    System.out.println(Fname+"\t"+EmpID+"\t"+calGross()+"\t"+calPayee()+"\t"+calNetIncome());    }    public double calGross(){        return Salary + Allowance;    }    public double calPayee(){     ...