SlideShare a Scribd company logo
Beginning Zend Framework Or: How I learned to stop worrying and love the framework. Part 1: Setting up
Preparing Zend Framework Download ZF1.9 svn co  http://framework.zend.com/svn/framework/standard/tags/release-1.9.1/library/Zend Add directory containing Zend to php.ini as part of the path.
Alternately, check out the framework into your pear directory.
Preparing ZF Contd. Check out bin (Zend Tool) svn co  http://framework.zend.com/svn/framework/standard/tags/release-1.9.1/bin Copy zf.php and zf.sh to a directory in your path (/usr/bin)
Rename zf.sh to zf and make it executable
Using Zend_Tool to set up your project Create a directory to house your projects mkdir projects Enter directory cd projects Create a project using the zf script zf create project  {projectname}
Add project to apache This can vary widely with OS and version, so you're mostly on your own.
Make the DocumentRoot the “public” directory within your  {projectname}  directory.
After reloading apache, you should be able to go to the site you just set up and see the default zend framework page!
Normal Application Setup Normally we would have the domain set up to point directly to the “public” directory Keeps php code from being directly accessible
NO php should go in public folder unless it’s simple and framework is overkill for it Except for index.php
Directory Structure /application application files…more in a moment /library framework files ( /library/Zend )
Extra classes/libraries used by the app /public index.php, html/js/images/etc, anything that should be directly accessible by the browser /tests unit tests… another time maybe
Application Directory Bootstrap.php Specific application – framework setup configs Application .ini file (soon) controllers Holds controller classes (gasp!) models Holds model classes (double-gasp!) views You get one guess what goes in here…
Wrong, not classes, simple php+html files.
index.php Prepares include path
Specifies application configuration
Load & run bootstrap
Create a database/table Use whatever tools you're used to CREATE TABLE `user` ( `user_id` INT NOT NULL AUTO_INCREMENT , `username` VARCHAR( 255 ) NOT NULL , `email` VARCHAR( 128 ) NOT NULL , `password` VARCHAR( 32 ) NOT NULL , `salt` VARCHAR( 32 ) NOT NULL , `api_key` VARCHAR( 32 ) NOT NULL , `api_secret` VARCHAR( 32 ) NOT NULL , `user_type` TINYINT NOT NULL , PRIMARY KEY ( `user_id` ), UNIQUE KEY `username` (`username`) )
Add database to app config Edit application/configs/application.ini # Database resources.db.adapter = "pdo_mysql" resources.db.params.host = "localhost" resources.db.params.username = "zfclass" resources.db.params.password = "zfclass" resources.db.params.dbname =  "zfclass_ {yourname} " resources.db.isDefaultTableAdapter = true
application.ini Note that application.ini has multiple sections Production, Staging, Development
Defined in .htaccess
Can be used to specify different databases for different environments
Configuring the layout What's a layout? 2-step way of providing “overall” layout to your app
View result html will be placed as “content” in layout
Layout can house dynamic links for header js, css, etc
Easily reached from controller or view.
Layout Contd. Create directory application/layouts and application/layouts/scripts
Add the following to your application.ini: #layout resources.layout.layout = "layout" resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
Module Layouts We're making an admin module
Let's load a specific admin layout for it
class Plugin_ModuleLayout extends Zend_Controller_Plugin_Abstract  { public function preDispatch(Zend_Controller_Request_Abstract $request) { $module = strtolower($request->getParam('module')); $layout = Zend_Layout::getMvcInstance(); if ($layout->getMvcEnabled()) { switch($module) { case 'default': break; default: $layout->setLayoutPath(APPLICATION_PATH . '/modules/' . $module . '/layouts/scripts'); break; } } } } application/plugins/ModuleLayout.php
Enable the plugin Edit application/Bootstrap.php public function _initLayouts() { Zend_Layout::startMvc(); $this->getPluginResource('frontcontroller') ->getFrontController() ->registerPlugin(new Plugin_ModuleLayout()); }
Application general layout applications/layouts/scripts/layout.phtml <?php echo $this->doctype() ?> <html> <head>     <?php echo $this->headTitle() ?>     <?php echo $this->headLink() ?>     <?php echo $this->headStyle() ?>     <?php echo $this->headScript() ?> </head> <body>     <?php echo $this->layout()->content ?> </body> </html>
Create a User model application/models/DbTable/User.php <?php class Model_DbTable_User extends Zend_Db_Table { protected $_name = 'user'; protected $_primary = 'user_id';
User Model Cont'd public function addUser($username, $email, $password, $user_type=1) { // generate unique id for the password salt $salt = strtolower(uniqid(rand(), true)); // and finally...one more for their initial api 'secret' key $api_secret_key = strtolower(uniqid(rand(), true)); // create a password hash to save in the database $hashed_pwd = strtolower(md5($password . $salt));
User Model... $data = array( 'username' => $username, 'email' => $email, 'password' => $hashed_pwd, 'salt' => $salt, 'user_type' => $user_type ); return $this->insert($data); }
User Model... function updateUser($id, $email, $password=null, $user_type=1) { $where = array('user_id = ?' => (int)$id); $data = array('email' => $email,'user_type'=>$user_type); if ($password !== null){ // generate unique id (again) for the password salt $salt = strtolower(uniqid(rand(), true)); $hashed_pwd = strtolower(md5($password . $salt)); $data['salt']=$salt; $data['password']=$hashed_pwd; } $this->update($data, $where); } } // End class
Autoloader Allow us to call our model class without having to include it manually In application/Bootstrap.php public function _initAutoload() { $autoloader = new Zend_Application_Module_Autoloader( array( 'namespace'=>'', 'basePath'=>APPLICATION_PATH ) ); return $autoloader; }
Database Profiling in Firebug! Still in Bootstrap.php public function _initDbprofile() { if($this->getEnvironment() == 'development') { $profiler = new Zend_Db_Profiler_Firebug('All DB  Queries'); $db = $this->getPluginResource('db'); $db = $db->getDbAdapter(); $profiler->setEnabled(true); $db->setProfiler($profiler); } }
URL Structure Defaults to Index controller & Index action /public/{controller}/{action}
Ad

Recommended

Power of Simplicity in FW/1
Power of Simplicity in FW/1
Masha Edelen
 
TurboGears2 Pluggable Applications
TurboGears2 Pluggable Applications
Alessandro Molina
 
Let ColdFusion ORM do the work for you!
Let ColdFusion ORM do the work for you!
Masha Edelen
 
Head First Zend Framework - Part 1 Project & Application
Head First Zend Framework - Part 1 Project & Application
Jace Ju
 
Laravel 로 배우는 서버사이드 #5
Laravel 로 배우는 서버사이드 #5
성일 한
 
Implementing access control with zend framework
Implementing access control with zend framework
George Mihailov
 
Django - 次の一歩 gumiStudy#3
Django - 次の一歩 gumiStudy#3
makoto tsuyuki
 
Doctrine 2
Doctrine 2
zfconfua
 
Creating fast, dynamic ACLs in Zend Framework
Creating fast, dynamic ACLs in Zend Framework
Wim Godden
 
Creating fast, dynamic ACLs in Zend Framework (Zend Webinar)
Creating fast, dynamic ACLs in Zend Framework (Zend Webinar)
Wim Godden
 
Django
Django
Ivan Widodo
 
Ant
Ant
sundar22in
 
Rails 3 overview
Rails 3 overview
Yehuda Katz
 
Hack in the Box Keynote 2006
Hack in the Box Keynote 2006
Mark Curphey
 
Php frameworks
Php frameworks
Anil Kumar Panigrahi
 
Error Reporting in ZF2: form messages, custom error pages, logging
Error Reporting in ZF2: form messages, custom error pages, logging
Steve Maraspin
 
Moving from Django Apps to Services
Moving from Django Apps to Services
Craig Kerstiens
 
Drupal 8 Services And Dependency Injection
Drupal 8 Services And Dependency Injection
Philip Norton
 
The effective use of Django ORM
The effective use of Django ORM
Yaroslav Muravskyi
 
Perl5i
Perl5i
Marcos Rebelo
 
Drupal 8 Services
Drupal 8 Services
Philip Norton
 
Getting Into Drupal 8 Configuration
Getting Into Drupal 8 Configuration
Philip Norton
 
Zend ACL Basics
Zend ACL Basics
Mindfire Solutions
 
Content Driven Zend_Acl in the Model Layer
Content Driven Zend_Acl in the Model Layer
Jeroen Keppens
 
深入淺出 MVC
深入淺出 MVC
Jace Ju
 
PhpBB meets Symfony2
PhpBB meets Symfony2
Fabien Potencier
 
OSCON Google App Engine Codelab - July 2010
OSCON Google App Engine Codelab - July 2010
ikailan
 
Symfony2 - OSIDays 2010
Symfony2 - OSIDays 2010
Fabien Potencier
 
Zend framework
Zend framework
Prem Shankar
 
Getting Started with Zend Framework
Getting Started with Zend Framework
Juan Antonio
 

More Related Content

What's hot (20)

Creating fast, dynamic ACLs in Zend Framework
Creating fast, dynamic ACLs in Zend Framework
Wim Godden
 
Creating fast, dynamic ACLs in Zend Framework (Zend Webinar)
Creating fast, dynamic ACLs in Zend Framework (Zend Webinar)
Wim Godden
 
Django
Django
Ivan Widodo
 
Ant
Ant
sundar22in
 
Rails 3 overview
Rails 3 overview
Yehuda Katz
 
Hack in the Box Keynote 2006
Hack in the Box Keynote 2006
Mark Curphey
 
Php frameworks
Php frameworks
Anil Kumar Panigrahi
 
Error Reporting in ZF2: form messages, custom error pages, logging
Error Reporting in ZF2: form messages, custom error pages, logging
Steve Maraspin
 
Moving from Django Apps to Services
Moving from Django Apps to Services
Craig Kerstiens
 
Drupal 8 Services And Dependency Injection
Drupal 8 Services And Dependency Injection
Philip Norton
 
The effective use of Django ORM
The effective use of Django ORM
Yaroslav Muravskyi
 
Perl5i
Perl5i
Marcos Rebelo
 
Drupal 8 Services
Drupal 8 Services
Philip Norton
 
Getting Into Drupal 8 Configuration
Getting Into Drupal 8 Configuration
Philip Norton
 
Zend ACL Basics
Zend ACL Basics
Mindfire Solutions
 
Content Driven Zend_Acl in the Model Layer
Content Driven Zend_Acl in the Model Layer
Jeroen Keppens
 
深入淺出 MVC
深入淺出 MVC
Jace Ju
 
PhpBB meets Symfony2
PhpBB meets Symfony2
Fabien Potencier
 
OSCON Google App Engine Codelab - July 2010
OSCON Google App Engine Codelab - July 2010
ikailan
 
Symfony2 - OSIDays 2010
Symfony2 - OSIDays 2010
Fabien Potencier
 
Creating fast, dynamic ACLs in Zend Framework
Creating fast, dynamic ACLs in Zend Framework
Wim Godden
 
Creating fast, dynamic ACLs in Zend Framework (Zend Webinar)
Creating fast, dynamic ACLs in Zend Framework (Zend Webinar)
Wim Godden
 
Rails 3 overview
Rails 3 overview
Yehuda Katz
 
Hack in the Box Keynote 2006
Hack in the Box Keynote 2006
Mark Curphey
 
Error Reporting in ZF2: form messages, custom error pages, logging
Error Reporting in ZF2: form messages, custom error pages, logging
Steve Maraspin
 
Moving from Django Apps to Services
Moving from Django Apps to Services
Craig Kerstiens
 
Drupal 8 Services And Dependency Injection
Drupal 8 Services And Dependency Injection
Philip Norton
 
The effective use of Django ORM
The effective use of Django ORM
Yaroslav Muravskyi
 
Getting Into Drupal 8 Configuration
Getting Into Drupal 8 Configuration
Philip Norton
 
Content Driven Zend_Acl in the Model Layer
Content Driven Zend_Acl in the Model Layer
Jeroen Keppens
 
深入淺出 MVC
深入淺出 MVC
Jace Ju
 
OSCON Google App Engine Codelab - July 2010
OSCON Google App Engine Codelab - July 2010
ikailan
 

Similar to Zend Framework 1.9 Setup & Using Zend_Tool (20)

Zend framework
Zend framework
Prem Shankar
 
Getting Started with Zend Framework
Getting Started with Zend Framework
Juan Antonio
 
Introduction to Zend Framework
Introduction to Zend Framework
Michelangelo van Dam
 
My Very First Zf App Part One
My Very First Zf App Part One
isaaczfoster
 
Framework
Framework
Nguyen Linh
 
Introduction to Zend Framework
Introduction to Zend Framework
Jamie Hurst
 
php-and-zend-framework-getting-started
php-and-zend-framework-getting-started
tutorialsruby
 
PHP
PHP
webhostingguy
 
php-and-zend-framework-getting-started
php-and-zend-framework-getting-started
tutorialsruby
 
php-and-zend-framework-getting-started
php-and-zend-framework-getting-started
tutorialsruby
 
php-and-zend-framework-getting-started
php-and-zend-framework-getting-started
tutorialsruby
 
Zend - Installation And Sample Project Creation
Zend - Installation And Sample Project Creation
Compare Infobase Limited
 
Zend Framework
Zend Framework
OpenSource Technologies Pvt. Ltd.
 
Zend Lab
Zend Lab
Leo Nguyen
 
Getting started-with-zend-framework
Getting started-with-zend-framework
Nilesh Bangar
 
Get Started with Zend Framework 2
Get Started with Zend Framework 2
Mindfire Solutions
 
Intro To Mvc Development In Php
Intro To Mvc Development In Php
funkatron
 
2007 Zend Con Mvc
2007 Zend Con Mvc
Pablo Morales
 
Hanoi php day 2008 - 01.pham cong dinh - how.to.build.your.own.framework
Hanoi php day 2008 - 01.pham cong dinh - how.to.build.your.own.framework
Nguyen Duc Phu
 
How to learn to build your own PHP framework
How to learn to build your own PHP framework
Dinh Pham
 
Getting Started with Zend Framework
Getting Started with Zend Framework
Juan Antonio
 
My Very First Zf App Part One
My Very First Zf App Part One
isaaczfoster
 
Introduction to Zend Framework
Introduction to Zend Framework
Jamie Hurst
 
php-and-zend-framework-getting-started
php-and-zend-framework-getting-started
tutorialsruby
 
php-and-zend-framework-getting-started
php-and-zend-framework-getting-started
tutorialsruby
 
php-and-zend-framework-getting-started
php-and-zend-framework-getting-started
tutorialsruby
 
php-and-zend-framework-getting-started
php-and-zend-framework-getting-started
tutorialsruby
 
Zend - Installation And Sample Project Creation
Zend - Installation And Sample Project Creation
Compare Infobase Limited
 
Getting started-with-zend-framework
Getting started-with-zend-framework
Nilesh Bangar
 
Get Started with Zend Framework 2
Get Started with Zend Framework 2
Mindfire Solutions
 
Intro To Mvc Development In Php
Intro To Mvc Development In Php
funkatron
 
Hanoi php day 2008 - 01.pham cong dinh - how.to.build.your.own.framework
Hanoi php day 2008 - 01.pham cong dinh - how.to.build.your.own.framework
Nguyen Duc Phu
 
How to learn to build your own PHP framework
How to learn to build your own PHP framework
Dinh Pham
 
Ad

Recently uploaded (20)

From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
Mastering AI Workflows with FME by Mark Döring
Mastering AI Workflows with FME by Mark Döring
Safe Software
 
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
janeliewang985
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
Mastering AI Workflows with FME by Mark Döring
Mastering AI Workflows with FME by Mark Döring
Safe Software
 
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
janeliewang985
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
Ad

Zend Framework 1.9 Setup & Using Zend_Tool

  • 1. Beginning Zend Framework Or: How I learned to stop worrying and love the framework. Part 1: Setting up
  • 2. Preparing Zend Framework Download ZF1.9 svn co http://framework.zend.com/svn/framework/standard/tags/release-1.9.1/library/Zend Add directory containing Zend to php.ini as part of the path.
  • 3. Alternately, check out the framework into your pear directory.
  • 4. Preparing ZF Contd. Check out bin (Zend Tool) svn co http://framework.zend.com/svn/framework/standard/tags/release-1.9.1/bin Copy zf.php and zf.sh to a directory in your path (/usr/bin)
  • 5. Rename zf.sh to zf and make it executable
  • 6. Using Zend_Tool to set up your project Create a directory to house your projects mkdir projects Enter directory cd projects Create a project using the zf script zf create project {projectname}
  • 7. Add project to apache This can vary widely with OS and version, so you're mostly on your own.
  • 8. Make the DocumentRoot the “public” directory within your {projectname} directory.
  • 9. After reloading apache, you should be able to go to the site you just set up and see the default zend framework page!
  • 10. Normal Application Setup Normally we would have the domain set up to point directly to the “public” directory Keeps php code from being directly accessible
  • 11. NO php should go in public folder unless it’s simple and framework is overkill for it Except for index.php
  • 12. Directory Structure /application application files…more in a moment /library framework files ( /library/Zend )
  • 13. Extra classes/libraries used by the app /public index.php, html/js/images/etc, anything that should be directly accessible by the browser /tests unit tests… another time maybe
  • 14. Application Directory Bootstrap.php Specific application – framework setup configs Application .ini file (soon) controllers Holds controller classes (gasp!) models Holds model classes (double-gasp!) views You get one guess what goes in here…
  • 15. Wrong, not classes, simple php+html files.
  • 18. Load & run bootstrap
  • 19. Create a database/table Use whatever tools you're used to CREATE TABLE `user` ( `user_id` INT NOT NULL AUTO_INCREMENT , `username` VARCHAR( 255 ) NOT NULL , `email` VARCHAR( 128 ) NOT NULL , `password` VARCHAR( 32 ) NOT NULL , `salt` VARCHAR( 32 ) NOT NULL , `api_key` VARCHAR( 32 ) NOT NULL , `api_secret` VARCHAR( 32 ) NOT NULL , `user_type` TINYINT NOT NULL , PRIMARY KEY ( `user_id` ), UNIQUE KEY `username` (`username`) )
  • 20. Add database to app config Edit application/configs/application.ini # Database resources.db.adapter = &quot;pdo_mysql&quot; resources.db.params.host = &quot;localhost&quot; resources.db.params.username = &quot;zfclass&quot; resources.db.params.password = &quot;zfclass&quot; resources.db.params.dbname = &quot;zfclass_ {yourname} &quot; resources.db.isDefaultTableAdapter = true
  • 21. application.ini Note that application.ini has multiple sections Production, Staging, Development
  • 23. Can be used to specify different databases for different environments
  • 24. Configuring the layout What's a layout? 2-step way of providing “overall” layout to your app
  • 25. View result html will be placed as “content” in layout
  • 26. Layout can house dynamic links for header js, css, etc
  • 27. Easily reached from controller or view.
  • 28. Layout Contd. Create directory application/layouts and application/layouts/scripts
  • 29. Add the following to your application.ini: #layout resources.layout.layout = &quot;layout&quot; resources.layout.layoutPath = APPLICATION_PATH &quot;/layouts/scripts&quot;
  • 30. Module Layouts We're making an admin module
  • 31. Let's load a specific admin layout for it
  • 32. class Plugin_ModuleLayout extends Zend_Controller_Plugin_Abstract { public function preDispatch(Zend_Controller_Request_Abstract $request) { $module = strtolower($request->getParam('module')); $layout = Zend_Layout::getMvcInstance(); if ($layout->getMvcEnabled()) { switch($module) { case 'default': break; default: $layout->setLayoutPath(APPLICATION_PATH . '/modules/' . $module . '/layouts/scripts'); break; } } } } application/plugins/ModuleLayout.php
  • 33. Enable the plugin Edit application/Bootstrap.php public function _initLayouts() { Zend_Layout::startMvc(); $this->getPluginResource('frontcontroller') ->getFrontController() ->registerPlugin(new Plugin_ModuleLayout()); }
  • 34. Application general layout applications/layouts/scripts/layout.phtml <?php echo $this->doctype() ?> <html> <head>     <?php echo $this->headTitle() ?>     <?php echo $this->headLink() ?>     <?php echo $this->headStyle() ?>     <?php echo $this->headScript() ?> </head> <body>     <?php echo $this->layout()->content ?> </body> </html>
  • 35. Create a User model application/models/DbTable/User.php <?php class Model_DbTable_User extends Zend_Db_Table { protected $_name = 'user'; protected $_primary = 'user_id';
  • 36. User Model Cont'd public function addUser($username, $email, $password, $user_type=1) { // generate unique id for the password salt $salt = strtolower(uniqid(rand(), true)); // and finally...one more for their initial api 'secret' key $api_secret_key = strtolower(uniqid(rand(), true)); // create a password hash to save in the database $hashed_pwd = strtolower(md5($password . $salt));
  • 37. User Model... $data = array( 'username' => $username, 'email' => $email, 'password' => $hashed_pwd, 'salt' => $salt, 'user_type' => $user_type ); return $this->insert($data); }
  • 38. User Model... function updateUser($id, $email, $password=null, $user_type=1) { $where = array('user_id = ?' => (int)$id); $data = array('email' => $email,'user_type'=>$user_type); if ($password !== null){ // generate unique id (again) for the password salt $salt = strtolower(uniqid(rand(), true)); $hashed_pwd = strtolower(md5($password . $salt)); $data['salt']=$salt; $data['password']=$hashed_pwd; } $this->update($data, $where); } } // End class
  • 39. Autoloader Allow us to call our model class without having to include it manually In application/Bootstrap.php public function _initAutoload() { $autoloader = new Zend_Application_Module_Autoloader( array( 'namespace'=>'', 'basePath'=>APPLICATION_PATH ) ); return $autoloader; }
  • 40. Database Profiling in Firebug! Still in Bootstrap.php public function _initDbprofile() { if($this->getEnvironment() == 'development') { $profiler = new Zend_Db_Profiler_Firebug('All DB Queries'); $db = $this->getPluginResource('db'); $db = $db->getDbAdapter(); $profiler->setEnabled(true); $db->setProfiler($profiler); } }
  • 41. URL Structure Defaults to Index controller & Index action /public/{controller}/{action}
  • 46. URL Routing You can create your own url structures using Zend_Controller_Router_* Many options Static
  • 47. Regex
  • 50. Create Admin Module Use zf tool zf create module admin Enable module in application.ini Add to [production] area
  • 51. resources.frontController.moduleDirectory = APPLICATION_PATH “/modules” Copy application/layout/scripts/layout.phtml to application/modules/admin/layouts/scripts/layout.phtml
  • 52. Create User Controller zf create controller user false admin
  • 53. One small bug in zf tool: Rename admin/controllers/UserController.php to Admin_UserController.php
  • 54. Rename class in file to Admin_UserController
  • 55. Create Action zf create action createuser user 1 admin
  • 56. Open User controller file in an editor...
  • 57. public function createuserAction() { // create form $form = new Zend_Form(); $form->setMethod('post'); $form->addElement('text','username', array( 'label' =>'User name', 'required'=>true, 'filters'=>array('StringTrim') )); $form->addElement('password','password',array( 'label'=>'Password', 'required'=>true, 'filters'=>array('StringTrim') )); $form->addElement('text','email',array( 'label'=>'Email address', 'required'=>true, 'filters'=>array('StringTrim'), 'validators'=>array('EmailAddress') )); Create a form programatically!
  • 58. $form->addElement('captcha','captcha',array( 'label'=>'Enter the letters below', 'required'=>true, 'captcha'=>array( 'captcha'=>'Figlet', 'wordLen'=>5, 'timeout'=>300 ) )); $form->addElement('submit','Save',array( 'ignore'=>true, 'label'=>'Save New User' )); // End form
  • 59. $request = $this->getRequest(); if($request->isPost()) { if($form->isValid($request->getPost())) { $data = $form->getValues(); $user = new Model_DbTable_User(); if($user->addUser( $data['username'], $data['email'], $data['password'],2)) // 2 for admin { $this->view->message = 'User created'; } else { $this->view->message = 'Something bad happened.'; } } } $this->view->form = $form; } // End function
  • 60. Create View application/modules/admin/views/scripts/user/createuser.php <?php if(isset($this->message)) { ?><h1><?=$this->message ?></h1><?php } ?> Create your new user. <?php $this->form->setAction($this->url()); echo $this->form; ?>
  • 61. Create your user! You should now be able to head to http://{yoursite}/admin/user/createuser
  • 63. Create a login action Close the user controller file in your editor
  • 64. zf create action login user 1 admin
  • 65. Open it back up and find the loginAction method
  • 66. public function loginAction() { $form = new Zend_Form(); $form->setMethod('post'); $form->addElement('text','username', array( 'label' =>'User name', 'required'=>true, 'filters'=>array('StringTrim') )); $form->addElement('password','password',array( 'label'=>'Password', 'required'=>true, 'filters'=>array('StringTrim') )); $form->addElement('submit','submit',array( 'label'=>'Login', 'ignore'=>true ));
  • 67. $request = $this->getRequest(); $data = $request->getPost(); if($request->isPost() && $form->isValid($data)) { $u = new Model_DbTable_User(); $auth = new Zend_Auth_Adapter_DbTable($u->getAdapter() ,'user','username','password', &quot;MD5(CONCAT(?,salt)) AND user_type=2&quot; ); $auth->setIdentity($data['username'])->setCredential( $data['password'] ); $mainauth = Zend_Auth::getInstance(); $result = $mainauth->authenticate($auth); if($result->isValid()) { $this->view->message = 'You are now logged in.'; } else { $m = $result->getMessages(); $this->view->message = $m[0]; } } $this->view->form = $form; }// End function
  • 68. Create a logout action Try the initial part yourself to create the action method. public function logoutAction() { $auth=Zend_Auth::getInstance(); $auth->clearIdentity(); $this->_redirect('/admin/user/login'); }
  • 69. Create a login “helper” Edit application/modules/admin/views/helpers/LoginLink.php <?php class Admin_View_Helper_LoginLink extends Zend_View_Helper_Abstract { public function loginLink() { $auth = Zend_Auth::getInstance(); $front = Zend_Controller_Front::getInstance(); if($auth->hasIdentity()) { $username = $auth->getIdentity(); return &quot;Hello, $username [<a href='&quot;.$front->getBaseUrl(). &quot;/admin/user/logout'>Logout</a>]&quot;; } else { return '[<a href=&quot;'.$front->getBaseUrl(). '/admin/user/login&quot;>Login</a>]'; } } }
  • 70. Add login helper to layout Edit application/modules/admin/layouts/scripts/layout.phtml Add before echo content: <div id= &quot;login&quot; > <?php echo $this->loginLink();?> </div> This calls the LoginLink helper we created before. Try it out: http://{yoursite}/admin/user/login
  • 71. Enough for now If we got this far at the meeting, I'm amazed.
  • 72. If not, I can continue at the next meeting.