600 likes | 875 Views
ZEND FRAMEWORK Basics Training. By: Raza Mehdi. Topics Covered in Session. What is Zend Framework? What is Model-View-Controller Pattern? Coding Guidelines for Zend Framework. Difference between Zend Framework versions.
E N D
ZEND FRAMEWORK Basics Training • By: Raza Mehdi http://kb.vteamslabs.com
Topics Covered in Session • What is Zend Framework? • What is Model-View-Controller Pattern? • Coding Guidelines for Zend Framework. • Difference between Zend Framework versions. • Development of a simple Application using Zend Framework to showcase its various features. http://kb.vteamslabs.com
What Is Zend Framework? • Zend Framework (ZF) is an open source, object-oriented web application development framework implemented in PHP5 and licensed under the New BSD License. • This framework is designed to eliminate the tedious process of developing a codebase for a specific system as much as possible. • This framework implements the highly-modular MVCdesign, making your code more reusable and easier to maintain. http://kb.vteamslabs.com
Defining Model-View-Controller http://kb.vteamslabs.com
Model http://kb.vteamslabs.com
VIEW http://kb.vteamslabs.com
CONTROLLER http://kb.vteamslabs.com
CODING GUIDELINES FOR ZEND FRAMEWORK • No leading or trailing spaces. • Proper namespace like Zend_ etc. For Resources like forms and models Form_ & Model_namespace is ideal. • Use camel-style lowercase instead of underscores (as in getTodaysDate()) for function names. • Use underscore in start for your variables if they are private or protected. • Declare all variables as private, protected, or public. • Use standard php tag <?php ?>. • Use spaces when concatenating text for making code more readable. • Use pass-by reference in function declaration. http://kb.vteamslabs.com
Zend Framework Versions • Zend Framework is available in two versions: • ZF 1.11 contains support for older PHP versions like 5.2 etc. • ZF2 is based on PHP version 5.3+, and is the framework’s latest release. This training session will utilize ZF 1.11 as reference in the coming slides. http://kb.vteamslabs.com
DIFFERENCES BETWEEN ZF1 & ZF2 http://kb.vteamslabs.com
DIFFERENCES BETWEEN ZF1 & ZF2 (Continued …) http://kb.vteamslabs.com
ZEND FRAMEWORK APPLICATION DEVELOPMENT http://kb.vteamslabs.com
Sample Bugs Reporting System • Installation & Configuration of Zend Framework • Capturing the data: The data is captured, validated, and filtered by Zend_Form. • Storing the data: The data to be stored in the database by using Zend_Db. http://kb.vteamslabs.com
Installation & Configuration of Zend Framework • Download latest ZF1 release from http://framework.zend.com/downloads/latest#ZF1 • Extract the folder to your desired location. • Copy the contents of bin folder to PHP installation directory. • Copy the contents of library folder to PHP’s include path. • Environment Variable PATH = “C:/xampp/php” • Open Command Line, and enter command: zfcreate project PROJECT_NAME http://kb.vteamslabs.com
DETAILED LOOK AT ZF PROJECT FILES http://kb.vteamslabs.com
.htaccess in /public folder http://kb.vteamslabs.com
Index.php in /public folder http://kb.vteamslabs.com
/application/configs/application.ini http://kb.vteamslabs.com
Adding Layouts using Zend_View, Zend_Layout http://kb.vteamslabs.com
Zend_View • Zend_Viewclass separates presentation code from the application logic and data. • View Scripts • View Helpers http://kb.vteamslabs.com
The view scripts are basically PHP files that are rendered within the scope of Zend_View. View Scripts are rendered in two stages: • First, the action controller creates a new instance of Zend_View(done using view renderer action helper), which you load with data in your action method. • Second, once the action method has been processed, the view renderer helper tells Zend_Viewto render the appropriate view script. http://kb.vteamslabs.com
View helpersare simply classes. When you call a helper from within your script: • Zend_Viewcreates a new instance of the class and then runs the method that it relates to the helper class name. • View Helper are named using the camelCaseconvention. For example, MyHelper will run the myHelper() method. • Zend_View then returns the response from the respective method. http://kb.vteamslabs.com
Zend_Layout Zend_Layoutenables you to create templates that wrap your individual view scripts. It can be used: • Standalone component: In this case, Zend_Layout allows you to name sections of your site templates and then load these from within your controller and view scripts. • MVC component: Zend_Layoutis initiated by its startMvcmethod. Normally following parameters are passed in array: • layout: layout script to render. • layoutPath: This is the path to the layout scripts. • contentKey: ‘content’ In the controllers by using: $this->_helper->layout. In the view by using the view helper: $this->layout(). http://kb.vteamslabs.com
Views Structure to be used in ZF http://kb.vteamslabs.com
Creating Layouts • Create a new folder in the application folder named layouts. • Add a subfolder to the layouts folder named scripts. • Create a file called layout.phtml. • Add the following lines to application/configs/application.ini: resources.layout.layoutPath= APPLICATION_PATH "/layouts/scripts/" OR Run this command zf enable layout in the terminal or command prompt in the project directory. You must create css, images sub-folders in the public folder. http://kb.vteamslabs.com
Adding Zend_View’sPlaceholders http://kb.vteamslabs.com
CREATING FORMS USING Zend_Form http://kb.vteamslabs.com
What is Zend_Form? • Zend_Formis a wrapper for XHTML forms, wrapped in <form /> tag. It is constructed & rendered using its native init() & render() methods respectively. • The Zend_Form object serves as a container for Zend_Form_Element objects which contain all of the standard form & form elements attributes. It also supports several non-standard form controls, such as Dojo widgets. • Zend_Form’s XHTML output is handled by decorators. Decorators are snippets of code that render the appropriate markup dynamically, and can be used up-to requirements for any element object. • Custom form elements can be created. http://kb.vteamslabs.com
Create a Form using Zend_Form • Create a folder forms in applications folder. • Create a file BugReport.phpin applications/forms folder. • Create a file BugController.phpin applications/controllers folder. Following fields are to be added: • Author • E-mail • Date • URL • Description • Priority • Status http://kb.vteamslabs.com
Adding Elements to Zend_Form http://kb.vteamslabs.com
Zend_Form Resource Mapping in Bootstrap Edit the application/Bootstrap.phpto add the following code: http://kb.vteamslabs.com
Adding Zend_Form to View http://kb.vteamslabs.com
Adding Zend_Form to View (continued …) http://kb.vteamslabs.com
Managing Data USING Zend_Db http://kb.vteamslabs.com
What is Zend_Db? Zend_Db is an object-oriented interface to SQL database systems. Zend Framework uses adapters to connect to database servers. These adapters provide a standardized interface to a range of commercially available database systems, including the following: • IBM DB2 • MySQL • Microsoft SQL Server • Oracle • PostgreSQL • SQLite http://kb.vteamslabs.com
Database Configuration application/configs/application.ini http://kb.vteamslabs.com
Models Resource Mapping in Bootstrap • Edit the _initAutoload function in application/Bootstrap.php as: http://kb.vteamslabs.com
Creating bugs Model http://kb.vteamslabs.com
Creating CRUD for bugs MODEL http://kb.vteamslabs.com
ADDING A RECORD http://kb.vteamslabs.com
1. Adding a method for data insertion in Model http://kb.vteamslabs.com
2. Updating the Bug Controller’s Submit Action http://kb.vteamslabs.com
3. Creating the Confirmation page • Create a method called confirmActionin application/controllers/BugController.phpfile. • Create a view file confirm.phtml in applications/views/scripts/bug/ folder, and add the following code: http://kb.vteamslabs.com
EDITING RECORDS http://kb.vteamslabs.com
1. Creating method in Model to update records http://kb.vteamslabs.com
2. Creating the view file for update records • Create a file edit.phtml in the applications/views/scripts/bug/ folder, and add the following code: http://kb.vteamslabs.com
2. Creating method in Controller to update records http://kb.vteamslabs.com
VIEWING ALL RECORDS http://kb.vteamslabs.com