Your SlideShare is downloading. ×
0
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Zend Framework 1.9 Setup & Using Zend_Tool

9,680

Published on

Presentation for azPHP on setting up a new project using Zend_Tool. Also goes over creating basic modules, controllers, actions, models and layouts. …

Presentation for azPHP on setting up a new project using Zend_Tool. Also goes over creating basic modules, controllers, actions, models and layouts.

All code in the presentation has not necessarily been tested. Will update presentation when done.

Published in: Technology, Education
5 Comments
7 Likes
Statistics
Notes
No Downloads
Views
Total Views
9,680
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
259
Comments
5
Likes
7
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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

×