zend framework basics training

59
ZEND FRAMEWORK Basics Training http://kb.vteamslabs.com 1 By: Raza Mehdi

Upload: ovidio

Post on 12-Feb-2016

86 views

Category:

Documents


1 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ZEND FRAMEWORK  Basics Training

ZEND FRAMEWORK Basics Training

http://kb.vteamslabs.com 1

By: Raza Mehdi

Page 2: ZEND FRAMEWORK  Basics Training

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

Page 3: ZEND FRAMEWORK  Basics Training

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

Page 4: ZEND FRAMEWORK  Basics Training

Defining Model-View-Controller

http://kb.vteamslabs.com 4

Page 5: ZEND FRAMEWORK  Basics Training

Model

http://kb.vteamslabs.com 5

Page 6: ZEND FRAMEWORK  Basics Training

VIEW

http://kb.vteamslabs.com 6

Page 7: ZEND FRAMEWORK  Basics Training

CONTROLLER

http://kb.vteamslabs.com 7

Page 8: ZEND FRAMEWORK  Basics Training

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

Page 9: ZEND FRAMEWORK  Basics Training

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

Page 10: ZEND FRAMEWORK  Basics Training

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

Page 11: ZEND FRAMEWORK  Basics Training

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

Page 12: ZEND FRAMEWORK  Basics Training

ZEND FRAMEWORK APPLICATION DEVELOPMENT

http://kb.vteamslabs.com 12

Page 13: ZEND FRAMEWORK  Basics Training

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

Page 14: ZEND FRAMEWORK  Basics Training

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

Page 15: ZEND FRAMEWORK  Basics Training

http://kb.vteamslabs.com 15

Page 16: ZEND FRAMEWORK  Basics Training

DETAILED LOOK AT ZF PROJECT FILES

http://kb.vteamslabs.com 16

Page 17: ZEND FRAMEWORK  Basics Training

.htaccess in /public folder

http://kb.vteamslabs.com 17

Page 18: ZEND FRAMEWORK  Basics Training

Index.php in /public folder

http://kb.vteamslabs.com 18

Page 19: ZEND FRAMEWORK  Basics Training

/application/configs/application.ini

http://kb.vteamslabs.com 19

Page 20: ZEND FRAMEWORK  Basics Training

ADDING LAYOUTS USING ZEND_VIEW, ZEND_LAYOUT

http://kb.vteamslabs.com 20

Page 21: ZEND FRAMEWORK  Basics Training

Zend_View

• Zend_View class separates presentation code from the application logic and data.

– View Scripts– View Helpers

http://kb.vteamslabs.com 21

Page 22: ZEND FRAMEWORK  Basics Training

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

Page 23: ZEND FRAMEWORK  Basics Training

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

Page 24: ZEND FRAMEWORK  Basics Training

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

Page 25: ZEND FRAMEWORK  Basics Training

Views Structure to be used in ZF

http://kb.vteamslabs.com 25

Page 26: ZEND FRAMEWORK  Basics Training

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

Page 27: ZEND FRAMEWORK  Basics Training

Adding Zend_View’s Placeholders

http://kb.vteamslabs.com 27

Page 28: ZEND FRAMEWORK  Basics Training

CREATING FORMS USING Zend_Form

http://kb.vteamslabs.com 28

Page 29: ZEND FRAMEWORK  Basics Training

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

Page 30: ZEND FRAMEWORK  Basics Training

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

Page 31: ZEND FRAMEWORK  Basics Training

Adding Elements to Zend_Form

http://kb.vteamslabs.com 31

Page 32: ZEND FRAMEWORK  Basics Training

Zend_Form Resource Mapping in Bootstrap

http://kb.vteamslabs.com 32

Edit the application/Bootstrap.php to add the following code:

Page 33: ZEND FRAMEWORK  Basics Training

Adding Zend_Form to View

http://kb.vteamslabs.com 33

Page 34: ZEND FRAMEWORK  Basics Training

Adding Zend_Form to View (continued …)

http://kb.vteamslabs.com 34

Page 35: ZEND FRAMEWORK  Basics Training

http://kb.vteamslabs.com 35

Page 36: ZEND FRAMEWORK  Basics Training

Managing Data USING Zend_Db

http://kb.vteamslabs.com 36

Page 37: ZEND FRAMEWORK  Basics Training

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

Page 38: ZEND FRAMEWORK  Basics Training

Database Configuration

http://kb.vteamslabs.com 38

application/configs/application.ini

Page 39: ZEND FRAMEWORK  Basics Training

Models Resource Mapping in Bootstrap

http://kb.vteamslabs.com 39

• Edit the _initAutoload function in application/Bootstrap.php as:

Page 40: ZEND FRAMEWORK  Basics Training

Creating bugs Model

http://kb.vteamslabs.com 40

Page 41: ZEND FRAMEWORK  Basics Training

Creating CRUD for bugs MODEL

http://kb.vteamslabs.com 41

Page 42: ZEND FRAMEWORK  Basics Training

ADDING A RECORD

http://kb.vteamslabs.com 42

Page 43: ZEND FRAMEWORK  Basics Training

1. Adding a method for data insertion in Model

http://kb.vteamslabs.com 43

Page 44: ZEND FRAMEWORK  Basics Training

2. Updating the Bug Controller’s Submit Action

http://kb.vteamslabs.com 44

Page 45: ZEND FRAMEWORK  Basics Training

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:

Page 46: ZEND FRAMEWORK  Basics Training

EDITING RECORDS

http://kb.vteamslabs.com 46

Page 47: ZEND FRAMEWORK  Basics Training

1. Creating method in Model to update records

http://kb.vteamslabs.com 47

Page 48: ZEND FRAMEWORK  Basics Training

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:

Page 49: ZEND FRAMEWORK  Basics Training

2. Creating method in Controller to update records

http://kb.vteamslabs.com 49

Page 50: ZEND FRAMEWORK  Basics Training

VIEWING ALL RECORDS

http://kb.vteamslabs.com 50

Page 51: ZEND FRAMEWORK  Basics Training

1. Creating method in Model to list all records

http://kb.vteamslabs.com 51

Page 52: ZEND FRAMEWORK  Basics Training

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

Page 53: ZEND FRAMEWORK  Basics Training

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

Page 54: ZEND FRAMEWORK  Basics Training

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

Page 55: ZEND FRAMEWORK  Basics Training

DELETING RECORDS

http://kb.vteamslabs.com 55

Page 56: ZEND FRAMEWORK  Basics Training

1. Creating method in Model to delete records

http://kb.vteamslabs.com 56

Page 57: ZEND FRAMEWORK  Basics Training

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

Page 58: ZEND FRAMEWORK  Basics Training

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

Page 59: ZEND FRAMEWORK  Basics Training

Thank You

QUESTIONS

http://kb.vteamslabs.com 59