ZEND FRAMEWORK Basics Training
http://kb.vteamslabs.com 1
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.• Development of a simple Application using Zend Framework
to showcase its various features.
2http://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 MVC design, making your code more reusable and easier to maintain.
http://kb.vteamslabs.com 3
Defining Model-View-Controller
http://kb.vteamslabs.com 4
Model
http://kb.vteamslabs.com 5
VIEW
http://kb.vteamslabs.com 6
CONTROLLER
http://kb.vteamslabs.com 7
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 8
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 9
DIFFERENCES BETWEEN ZF1 & ZF2Zend Framework 1 Zend Framework 2Command Line Tool available No Command Line Tool Available
No Namespaces Extensive Use of Namespaces
No proper modular structure. ZF1 modules are really a method of grouping discrete areas of code together, without them being inherently reusable.
Truly modular structure. You can drop in someone else's module, and with a simple line added to your application config file.
Bootstrapping resources in ZF1 is a combination of directives in the application.ini, and underscore prefixed functions in a module's Bootstrap.php.
No application level bootstrapping in ZF2. Each module is responsible for bootstrapping it's own resources in it's Module.php file by using a combination of the onBootstrap method of the module class, and the Event Manager.
http://kb.vteamslabs.com 10
DIFFERENCES BETWEEN ZF1 & ZF2 (Continued …)Zend Framework 1 Zend Framework 2Routes in ZF1 is done by adding configured Zend_Controller_Router_Route objects to the standard Zend_Controller_Router_Rewrite object during bootstrapping or using the shortcuts in application.ini.
ZF2 has no application level routing, instead, routing is added on a per-module basis. This has the benefit of allowing a module to add routes to an application on an ad-hoc basis.
ZF1 doesn't have a Service Manager as such, the closest thing is Zend_Registry which is an object store. It basically allows you to store objects somewhere that other classes will look to pull from,
ZF2 doesn’t have Zend_Registry. It's been indirectly been replaced by Zend\ServiceManager (SM), and, to a lesser extent, Zend\Di. SM allows you to manage all your services in one place. It will lazily load classes as and when you need them.
http://kb.vteamslabs.com 11
ZEND FRAMEWORK APPLICATION DEVELOPMENT
http://kb.vteamslabs.com 12
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 13
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:
zf create project PROJECT_NAME
http://kb.vteamslabs.com 14
http://kb.vteamslabs.com 15
DETAILED LOOK AT ZF PROJECT FILES
http://kb.vteamslabs.com 16
.htaccess in /public folder
http://kb.vteamslabs.com 17
Index.php in /public folder
http://kb.vteamslabs.com 18
/application/configs/application.ini
http://kb.vteamslabs.com 19
ADDING LAYOUTS USING ZEND_VIEW, ZEND_LAYOUT
http://kb.vteamslabs.com 20
Zend_View
• Zend_View class separates presentation code from the application logic and data.
– View Scripts– View Helpers
http://kb.vteamslabs.com 21
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_View to render the appropriate view script.
http://kb.vteamslabs.com 22
View helpers are simply classes. When you call a helper from within your script:
• Zend_View creates 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 camelCase convention. For example, MyHelper will run the myHelper() method.
• Zend_View then returns the response from the respective method.
http://kb.vteamslabs.com 23
Zend_LayoutZend_Layout enables 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_Layout is initiated by its startMvc method. 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 24
Views Structure to be used in ZF
http://kb.vteamslabs.com 25
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/"
ORRun 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 26
Adding Zend_View’s Placeholders
http://kb.vteamslabs.com 27
CREATING FORMS USING Zend_Form
http://kb.vteamslabs.com 28
What is Zend_Form?• Zend_Form is 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 29
Create a Form using Zend_Form• Create a folder forms in applications folder.• Create a file BugReport.php in applications/forms folder.• Create a file BugController.php in applications/controllers
folder.
Following fields are to be added:• Author• E-mail• Date• URL• Description• Priority• Status
http://kb.vteamslabs.com 30
Adding Elements to Zend_Form
http://kb.vteamslabs.com 31
Zend_Form Resource Mapping in Bootstrap
http://kb.vteamslabs.com 32
Edit the application/Bootstrap.php to add the following code:
Adding Zend_Form to View
http://kb.vteamslabs.com 33
Adding Zend_Form to View (continued …)
http://kb.vteamslabs.com 34
http://kb.vteamslabs.com 35
Managing Data USING Zend_Db
http://kb.vteamslabs.com 36
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 37
Database Configuration
http://kb.vteamslabs.com 38
application/configs/application.ini
Models Resource Mapping in Bootstrap
http://kb.vteamslabs.com 39
• Edit the _initAutoload function in application/Bootstrap.php as:
Creating bugs Model
http://kb.vteamslabs.com 40
Creating CRUD for bugs MODEL
http://kb.vteamslabs.com 41
ADDING A RECORD
http://kb.vteamslabs.com 42
1. Adding a method for data insertion in Model
http://kb.vteamslabs.com 43
2. Updating the Bug Controller’s Submit Action
http://kb.vteamslabs.com 44
3. Creating the Confirmation page
http://kb.vteamslabs.com 45
1. Create a method called confirmAction in application/controllers/BugController.php file.
2. Create a view file confirm.phtml in applications/views/scripts/bug/ folder, and add the following code:
EDITING RECORDS
http://kb.vteamslabs.com 46
1. Creating method in Model to update records
http://kb.vteamslabs.com 47
2. Creating the view file for update records
http://kb.vteamslabs.com 48
• Create a file edit.phtml in the applications/views/scripts/bug/ folder, and add the following code:
2. Creating method in Controller to update records
http://kb.vteamslabs.com 49
VIEWING ALL RECORDS
http://kb.vteamslabs.com 50
1. Creating method in Model to list all records
http://kb.vteamslabs.com 51
2. Adding a method to controller to list all records• Add a method listAction() to
applications/controllers/BugController.php file.
http://kb.vteamslabs.com 52
3. Adding a view file to list all records• Create a file list.phtml in the
applications/views/scripts/bug/ folder, and add the following code:
http://kb.vteamslabs.com 53
4. Listing all rows• Create a file table-row.phtml in the
applications/views/scripts/bug/ folder, and add the following code:
http://kb.vteamslabs.com 54
DELETING RECORDS
http://kb.vteamslabs.com 55
1. Creating method in Model to delete records
http://kb.vteamslabs.com 56
2. Adding a method to controller to delete a record• Add a method deleteAction() to
applications/controllers/BugController.php file.
http://kb.vteamslabs.com 57
TOPICS FOR NEXT SESSION
• Zend_Pagination• Managing Table Relationships using Zend_Db• Handling Security in Zend Framework.• Searching & Sharing Content.• Modules in Zend• Performance Tuning & Caching.• Templating System using Zend_Layout &
Zend_View
http://kb.vteamslabs.com 58
Thank You
QUESTIONS
http://kb.vteamslabs.com 59