YII Framework Dept. of IT 1.INTRODUCTION YII Is Pronounced As “Yee”, and Stands for “Yes It Is!”. Unlike Other Component
Total Page:16
File Type:pdf, Size:1020Kb
YII Framework Dept. of IT 1.INTRODUCTION YII is pronounced as “yee”, and stands for “Yes It Is!”. Unlike other component based PHP frameworks, YII scores way ahead in terms of RPC performance and adopts the ‘lazy-loading’ technique that increases its efficiency manifold. Other than this critical advantage of YII over other similar Frameworks, YII is written using OOP protocols and offers extensive reusability for developing web 2.0 applications. Installation of Yii mainly involves the following two steps: • Download Yii Framework from yiiframework.com. • Unpack the Yii release ?le to a Web-accessible directory After installing Yii, you may want to verify that your server satisfies all the requirements of using Yii. You can do so by accessing the requirement checker script at the following URL in a Web browser: http://hostname/path/to/yii/requirements/index.php The minimum requirement by Yii is that your Web server supports PHP 5.1.0 or above. Yii has been tested with Apache HTTP server on Windows and Linux operating systems. It may also run on other Web servers and platforms provided PHP 5 is supported. 1.1 About YII: Yii is a high-performance PHP framework best for developing Web 2.0 applications .Yii helps Web developers build complex applications and deliver them on-time .Yii is pronounced as Yee or [ji:], and is an acroynym for "Yes It Is!". This is often the accurate, and most concise response to inquires from those new to Yii . Is it fast? ... Is it secure? ... Is it professional? ... Is it right for my next project? ... Yes, it is! Yii is a free, open-source Web application development framework written in PHP5 that promotes clean, DRY design and encourages rapid development. It works to streamline your application development and helps to ensure an extremely efficient, extensible, and maintainable end product. M.L.Engineering College 1 YII Framework Dept. of IT Being extremely performance optimized, Yii is a perfect choice for any sized project. However, it has been built with sophisticated, enterprise applications in mind. You have full control over the configuration from head-to-toe (presentation-to- persistence) to conform to your enterprise development guidelines. It comes packaged with tools to help test and debug your application, and has clear and comprehensive documentation .To learn more about what Yii brings to the table, check out the features section. 1.2 Why Use YII: 1. Its fast: Using the ‘lazy-loading’ technique, YII calls classes and objects only when needed and hence helps faster loading. It has a neat integration with AJAX and uses powerful layered caching support. 2. Its secure: Using OOP standards increases the reliability of YII. Its other security essentials such as input filtering, SQL injection and cross-site scripting prevention means a stronger framework for your PHP applications to work on 3. Its developer-friendly: Using the model-view-controller (MVC) paradigm, YII offers clean separation of business rules, logic and presentation. M.L.Engineering College 2 YII Framework Dept. of IT 2. MODEL VIEW CONTROLLER (MVC) Yii implements the model-view-controller (MVC) 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 a_ecting 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.Besides MVC, Yii also introduces a front-controller, called application, which represents the execution context of request processing. Application resolves the user request and dispatches it to an appropriate controller for further handling. The following diagram shows the static structure of a Yii application: M.L.Engineering College 3 YII Framework Dept. of IT Figure: Static structure of Yii application 3. A TYPICAL WORKFLOW M.L.Engineering College 4 YII Framework Dept. of IT The following diagram shows a typical workflow of a Yii application when it is handling a user request. Figure: A typical workflow of Yii application 1. A user makes a request with the URL http://www.example.com/index.php?r=post/ show&id=1 and the Web server handles the request by executing the bootstrap script index.php . 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 URL Manager . For this example, the controller is post M.L.Engineering College 5 YII Framework Dept. of IT which refers to the Post Controller class; and the action is show whose actual meaning is determined by the controller. 5. The application creates an instance of the requested controller to further handle the user request. The controller determines that the action show refers to a method named action Show in the controller class. It then creates and executes filters (e.g. access control, benchmarking) associated with this action. The action is executed if it is allowed by the filters. 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. 11. The action completes the view rendering and displays the result to the user. 4. CREATING AN YII APPLICATION M.L.Engineering College 6 YII Framework Dept. of IT For creating YII web application we use a powerful YII tool which can be used to automate code creation for certain tasks. For simply, we assume that YII root is the directory where YII is installed, and Web Root is the document root of our Web server. 1. Preparation: After installing the YII framework, run a simple console command “% YII Root/framework/yiicwebappWebRoot/testdrive” to generate a selection Web application built with Yii, This will create a selection Yii application under the directory WebRoot/testdrive. 2. Create The Database: While YII can virtually eliminate most respective coding tasks, you are responsible for the real creative work . This often starts with designing the whole system to be built, in terms of some database. 2A.YII generates the model classes using the built-in web-based code generator,you can turn database table definitions into model classes instantly, without writing a single line of code. The model classes will allow you to access the database tables in an object-oriented fashion. 2B.YII generates the CRUD code using the code generator, we can further generate code that implements the typical CRUD (create, read, update, delete) features for the selected database tables. The generated code is highly usable and customizable following the well-adapted MVC(Model-View-Controller) design pattern. 3. You Customize The Code To Fit Your Exact Needs: Finally we customize the code to fit our exact needs.For example,to find the password user column on the user administration page,simply cross out the ‘password’ element shown in the user admin view file. Creating An First YII Application: M.L.Engineering College 7 YII Framework Dept. of IT 1. Connecting to Database 2. Implementing CRUD Operations To get an initial experience with Yii, we describe in this section how to create our firstYii application. We will use the powerful yiic tool which can be used to automate code creation for certain tasks. For convenience, we assume that YiiRoot is the directory where Yii is installed, and WebRoot is the document root of our Web server. Run yiic on the command line as follows: % YiiRoot/framework/yiic webapp WebRoot/testdrive This will create a skeleton Yii application under the directory WebRoot/testdrive. The application has a directory structure that is is needed by most Yii applications.Without writing a single line of code, we can test drive our _rst Yii application by accessing the following URL in a Web browser: http://hostname/testdrive/index.php As we can see, the application has four pages: the homepage, the about page, the contact page and the login page. The contact page displays a contact form that users can fill into submit their inquiries to the webmaster, and the login page allows users to be authenticated before accessing privileged contents. See the following screenshots for more details. The following diagram shows the directory structure of our application. Please see conventions for detailed explanation about this structure. 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 M.L.Engineering College 8 YII Framework Dept. of IT Home page M.L.Engineering College 9 YII Framework Dept. of IT M.L.Engineering College 10 YII Framework Dept. of IT Contact page with input errors M.L.Engineering College 11 YII Framework Dept. of IT Contact page with success message Login page M.L.Engineering College 12 YII Framework Dept. of IT 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