820 likes | 1.07k Views
ZEND FRAMEWORK Advanced Training. 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.
E N D
ZEND FRAMEWORK Advanced Training • By: Raza Mehdi http://kb.vteamslabs.com
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. http://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
THEMES Using Zend_Layout & Zend_View http://kb.vteamslabs.com
1. Layout & Views Folders http://kb.vteamslabs.com
2. Changed Layout & Views Folders for Themes http://kb.vteamslabs.com
3. Change Bootstrap.php to load Themes • Add a constructor function to load theme of choice: http://kb.vteamslabs.com
3. Change Bootstrap.php to load Themes (cont ..) • Add a _initLayout function to load layout files of selected theme: http://kb.vteamslabs.com
3. Change Bootstrap.php to load Themes (cont ..) • Add a _initView function to load view files of selected theme: http://kb.vteamslabs.com
4. Create Theme Configuration File • The template.xml in each theme folder may hold theme config information as well as theme’s CSS & JS filenames. For example: http://kb.vteamslabs.com
5. Create View Helper To Load Theme Config Create a file called LoadTemplate.phpin application/views/helpers folder to load information from template.xml. http://kb.vteamslabs.com
5.1 Loading Theme Config Data http://kb.vteamslabs.com
5.2 Saving Configuration Data • Next we will save the retrieved configuration data as array. http://kb.vteamslabs.com
5.3 Adding Configuration Data To Layout • Finally we will add the items to the Layout. http://kb.vteamslabs.com
6. Update Layout Files http://kb.vteamslabs.com
USING Zend_Paginator to paginate records http://kb.vteamslabs.com
1. Update Model’s method to return Zend_Paginator object http://kb.vteamslabs.com
2. Update Controller’s method which lists records http://kb.vteamslabs.com
3. Update View File http://kb.vteamslabs.com
4. Creating Paginator Template • 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 http://kb.vteamslabs.com
4.1 Adding Request Parameters to URL http://kb.vteamslabs.com
4.2 Adding Previous Page Control to Paginator http://kb.vteamslabs.com
4.3 Adding Total Pages List to Paginator http://kb.vteamslabs.com
4.4 Adding Next Page Control to Paginator http://kb.vteamslabs.com
Managing table relationships using Zend_Db http://kb.vteamslabs.com
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
2. Creating Models for Tables http://kb.vteamslabs.com
3. Defining Relationships for Models • Since pages table has a One-to-many Relationship with the content_nodestable, 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. http://kb.vteamslabs.com
4. ContentNode to Page Relationship • Add the following code to the application/models/ContentNode.php: http://kb.vteamslabs.com
5. Page to Page Relationship • Add the following code to the application/models/Page.php: http://kb.vteamslabs.com
6. Fetching Dependent Rows http://kb.vteamslabs.com
7. Fetching Parent Rows http://kb.vteamslabs.com
HANDLING SECURITY IN ZEND FRAMEWORK http://kb.vteamslabs.com
Security in Zend Framework is done through: • Zend_Authsolely 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
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
2. Creating User Model Class http://kb.vteamslabs.com
User Authentication using Zend_Auth http://kb.vteamslabs.com
LIST OF AVAILABLE ZEND_AUTH ADAPTERS • Some of the common Zend_Authadapters are: • Database table authentication. • Digest authentication. • LDAP authentication. • Open ID. Here, we will use Zend_Auth_Adapter_DbTable. http://kb.vteamslabs.com
SAMPLE USAGE OF Zend_Auth http://kb.vteamslabs.com
Creating the User Login http://kb.vteamslabs.com
1. Adding the Login Form to Controller http://kb.vteamslabs.com
2. Adding the Login Form to View http://kb.vteamslabs.com
3. Adding User Authentication First validate the form. If valid, then get the posted data: http://kb.vteamslabs.com
3. Adding User Authentication (continued …) • Creating Zend_Auth Adapter. • Pass the username & password. • Do the authentication. http://kb.vteamslabs.com
3. Adding User Authentication (continued …) • Get the Zend_Authauthentication 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
Logout for authenticated users http://kb.vteamslabs.com