ZEND FRAMEWORK Advanced Training
http://kb.vteamslabs.com 1
By: Raza Mehdi
Topics Covered in Previous 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
TOPICS FOR SESSION
• Themes using Zend_Layout & Zend_View• Zend_Pagination• Managing Table Relationships using Zend_Db• Handling Security in Zend Framework.• Site Search Using Zend_Search_Lucene.• Modules in Zend Framework.
http://kb.vteamslabs.com 3
THEMES Using Zend_Layout & Zend_View
http://kb.vteamslabs.com 4
1. Layout & Views Folders
http://kb.vteamslabs.com 5
2. Changed Layout & Views Folders for Themes
http://kb.vteamslabs.com 6
3. Change Bootstrap.php to load Themes
http://kb.vteamslabs.com 7
• Add a constructor function to load theme of choice:
3. Change Bootstrap.php to load Themes (cont ..)
http://kb.vteamslabs.com 8
• Add a _initLayout function to load layout files of selected theme:
3. Change Bootstrap.php to load Themes (cont ..)
http://kb.vteamslabs.com 9
• Add a _initView function to load view files of selected theme:
4. Create Theme Configuration File
http://kb.vteamslabs.com 10
• The template.xml in each theme folder may hold theme config information as well as theme’s CSS & JS filenames. For example:
5. Create View Helper To Load Theme Config
Create a file called LoadTemplate.php in application/views/helpers folder to load
information from template.xml.
http://kb.vteamslabs.com 11
5.1 Loading Theme Config Data
http://kb.vteamslabs.com 12
5.2 Saving Configuration Data
http://kb.vteamslabs.com 13
• Next we will save the retrieved configuration data as array.
5.3 Adding Configuration Data To Layout
http://kb.vteamslabs.com 14
• Finally we will add the items to the Layout.
http://kb.vteamslabs.com 15
6. Update Layout Files
http://kb.vteamslabs.com 16
USING Zend_Paginator to paginate records
http://kb.vteamslabs.com 17
1. Update Model’s method to return Zend_Paginator object
http://kb.vteamslabs.com 18
2. Update Controller’s method which lists records
http://kb.vteamslabs.com 19
3. Update View File
http://kb.vteamslabs.com 20
4. Creating Paginator Template
http://kb.vteamslabs.com 21
• Create a new folder named partials in application/views/scripts.
• Now create a file pagination-control.phtml.• Put in the following code in the file
4.1 Adding Request Parameters to URL
http://kb.vteamslabs.com 22
4.2 Adding Previous Page Control to Paginator
http://kb.vteamslabs.com 23
4.3 Adding Total Pages List to Paginator
http://kb.vteamslabs.com 24
4.4 Adding Next Page Control to Paginator
http://kb.vteamslabs.com 25
http://kb.vteamslabs.com 26
http://kb.vteamslabs.com 27
Managing table relationships using Zend_Db
http://kb.vteamslabs.com 28
1. Defining Relationship Between Database Tables• In this case, we will take example of a simple CMS.
– Have two tables page & node.– A page can have multiple nodes. – A Page can be parent to another page.
http://kb.vteamslabs.com 29
2. Creating Models for Tables
http://kb.vteamslabs.com 30
3. Defining Relationships for Models
http://kb.vteamslabs.com 31
• Since pages table has a One-to-many Relationship with the content_nodes table, we need to do the following:
– Model_Page: Tell Zend_Db_Table that the pages table is dependent on the content_nodes table.
– Model_ContentNode: Tell Zend_Db_Table that the content_nodes table is related to the pages table, by doing the following:• Setting the related columns. • Adding reference table class (Model_Page). • Setting the referenced table columns.
4. ContentNode to Page Relationship
http://kb.vteamslabs.com 32
• Add the following code to the application/models/ContentNode.php:
5. Page to Page Relationship
http://kb.vteamslabs.com 33
• Add the following code to the application/models/Page.php:
6. Fetching Dependent Rows
http://kb.vteamslabs.com 34
7. Fetching Parent Rows
http://kb.vteamslabs.com 35
HANDLING SECURITY IN ZEND FRAMEWORK
http://kb.vteamslabs.com 36
• Security in Zend Framework is done through:– Zend_Auth solely concerned with authenticating
(and persisting) the application users.
– Zend_Acl handles resources, roles (user roles), and which roles can access which resources.
http://kb.vteamslabs.com 37
1. Managing Users• Normally we have two user types in a CMS:
– Users: These are registered users who don’t have admin privileges.
– Administrators: These are the site managers who can access any area of the CMS.
Fields for Users Table:• `id`• `username`• `password`• `first_name` • `last_name` • `role`
http://kb.vteamslabs.com 38
2. Creating User Model Class
http://kb.vteamslabs.com 39
User Authentication using Zend_Auth
http://kb.vteamslabs.com 40
LIST OF AVAILABLE ZEND_AUTH ADAPTERS• Some of the common Zend_Auth adapters are:
– Database table authentication.– Digest authentication.– LDAP authentication.– Open ID.
Here, we will use Zend_Auth_Adapter_DbTable.
http://kb.vteamslabs.com 41
SAMPLE USAGE OF Zend_Auth
http://kb.vteamslabs.com 42
CREATING THE USER LOGIN
http://kb.vteamslabs.com 43
1. Adding the Login Form to Controller
http://kb.vteamslabs.com 44
2. Adding the Login Form to View
http://kb.vteamslabs.com 45
3. Adding User Authentication
First validate the form. If valid, then get the posted data:
http://kb.vteamslabs.com 46
3. Adding User Authentication (continued …)
• Creating Zend_Auth Adapter.• Pass the username & password.• Do the authentication.
http://kb.vteamslabs.com 47
3. Adding User Authentication (continued …)
• Get the Zend_Auth authentication data.– Store the valid Zend_Auth data in storage (PHP session).
Then redirect the user to some page.– Show error message in case of invalid result.
http://kb.vteamslabs.com 48
http://kb.vteamslabs.com 49
LOGOUT FOR AUTHENTICATED USERS
http://kb.vteamslabs.com 50
http://kb.vteamslabs.com 51
Controlling Access using Zend_Acl
http://kb.vteamslabs.com 52
SAMPLE USAGE OF Zend_Acl
http://kb.vteamslabs.com 53
SECURING RESOURCES USING ZEND_ACL
http://kb.vteamslabs.com 54
1. Creating a Controller Plugin• Create a folder CMS in application/library.• Create a folder Controller in CMS folder.• Create another folder Plugin in the Controller folder.• Create a file Acl.php in the Plugin folder.
http://kb.vteamslabs.com 55
2. Creating ACL Instance, roles & resourcesCreating a Zend_Acl instance, and then adding relevant roles & resources:
http://kb.vteamslabs.com 56
3. Access Rules for Roles & Resources
http://kb.vteamslabs.com 57
4. Authenticating User Requests
http://kb.vteamslabs.com 58
http://kb.vteamslabs.com 59
5. Registering Plugin for Controller
Open the application/configs/application.ini, and add the following lines:
resources.frontController.plugins.acl = "CMS_Controller_Plugin_Acl"
http://kb.vteamslabs.com 60
6. Adding Unauthorized Access Landing Page• Add the noauth action to the
application/controller/ErrorController.php file, and the add the following code to its relevant view file:
http://kb.vteamslabs.com 61
Site Search using Zend_Search_Lucene
http://kb.vteamslabs.com 62
What Is Zend_Search_Lucene?• Zend_Search_Lucene provides a very straightforward
way to build and search indexes of content. Through this, you can do the following:
– Create Search Indexes.– Add Documents for searching content.– Create fields to add to documents for search.
http://kb.vteamslabs.com 63
IMPLEMENTING SEARCH
http://kb.vteamslabs.com 64
1. Adding the Search Controller
• Create a file SearchController.php in the applications/controllers file.
• Add the following methods to the file, and create the relevant view files in the application/views/scripts/search folder:– indexAction()– buildAction()– searchAction()
http://kb.vteamslabs.com 65
2. Securing the Search Functionality
• Add the following code to Acl.php in the application/library/CMS/Controller/Plugin folder:
http://kb.vteamslabs.com 66
3. Create the folder to store search indexes
Create a folder indexes in the application folder
http://kb.vteamslabs.com 67
4. BUILDING THE SEARCH INDEX
http://kb.vteamslabs.com 68
4.1 Create search index & fetch all records
Add the following code to buildAction method for SearchController.php file.
http://kb.vteamslabs.com 69
4.2 Create search document for every record
http://kb.vteamslabs.com 70
4.3 Optimizing the index & displaying indexing results
http://kb.vteamslabs.com 71
4.4 Updating Search index view file
http://kb.vteamslabs.com 72
5. SEARCHING THE SITE
http://kb.vteamslabs.com 73
5.1 Creating the Search Form Create a file Search.php in the application/forms folder:
http://kb.vteamslabs.com 74
5.2 Adding Search Form to LayoutAdding the following code to layout.phtml file in the application/layouts/scripts folder:
http://kb.vteamslabs.com 75
5.3 Processing a Search Request
http://kb.vteamslabs.com 76
5.3 Rendering Search Results
http://kb.vteamslabs.com 77
ADDING MODULES IN ZEND FRAMEWORK APPLICATION
http://kb.vteamslabs.com 78
1. Creating Modules• Create a new folder in the application folder named
modules.• Create a module folder name of your choice e.g.
contact.• Create subfolders in the your module folder in this
order:– application/modules/contact/controllers– application/modules/contact/models– application/modules/contact/views
• application/modules/contact/views/scripts• application/modules/contact/views/helpers
. http://kb.vteamslabs.com 79
Run this command:zf create module contact
in the terminal or command prompt in the project directory
http://kb.vteamslabs.com 80
Creating Resources in Modules
• Any custom functionality must be done in the modules’s Bootstrap.php file. This must be done with care.
• For every resource you need to add prefix [module_name] to class definition. For example:
– For Controller: [module_name]_IndexController.php– For Model: [module_name]_Model_Page.php– For Form: [module_name]_Form_User.php
http://kb.vteamslabs.com 81
Thank You
QUESTIONS
http://kb.vteamslabs.com 82