Zend framework


Published on

zend framework is the most used/using PHP framework today.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Zend framework

  1. 1. Intro• A web application framework written in PHP5• Loosely coupled set of modules that perform variousTasks‣ Database access (Zend_DB)‣ Google Data API‟s (Zend_Gdata)• Easy to implement MVC model
  2. 2. What is MVC?• MVC stands for Model-View-Controller ‣ Code is divided into three distinct groups• Model -- Internal representation of data, interface to backend storage (i.e. database), and “business logic”• View -- Code that represents the application‟s UI• Controller -- Code that generates output to populate the view using the model
  3. 3. •Download latest zend frame work•It contains two folders “bin” and “library". Copy that to wamp/bin/php/=>newfolder say zend_frame.•Change the include_path for windows in php.ini to wamp/bin/php/zend_frame/libraryNow zend is installed…to check zend is installed or not--------------- Take cmd prompt and-> type zf show version If its displaying the correct version, Your Zend is functioning
  4. 4. Take cmd => go to your local host folder here its wamp/www/Type=> zf create project Zendy projectnameYour project is created….you can check in the wwwfolder….Also copy the folder zend in the library to this project`slibrary
  6. 6. • application/Bootstrap.php ‣ Application bootstrap code• application/configs ‣ Configuration files• application/controllers ‣ Backend controller code• application/models ‣ Code mapping from domaindata to storage data (PHP interfaceto DB for example)• application/views/scripts ‣ User interface code• application/configs/application.ini ‣ Main configuration file• application/controllers/ ‣ ErrorController.php-> Default controller called when anerror occurs ‣ IndexController.php->Default controller when nocontroller is specified
  7. 7. You can go to your project by typing localhost/zendy/public on your browser Your project frontage will be like this:
  8. 8. A controller that handles all requests for a Web site. Zend_Controller_Front implements a Front Controller pattern used in Model-View- Controller (MVC) applications. Its purpose is to initialize the request environment, route the incoming request, and then dispatch any discovered actions; it aggregates any responses and returns them when the process is complete.
  9. 9. The index.php file is the entry point to our application and is usedto create an instance of Zend_Application to initialise ourapplication and then run it. This file also defines two constants:APPLICATION_PATH and APPLICATION_ENV which define the pathto the application/ directory and the environment or mode of theapplication. The default is set to production in index.php, but youshould set it to development in the .htaccess file by adding thisline: SetEnv APPLICATION_ENV development
  10. 10. E:wampwwwzendbinapplicationviewsscriptsindex
  11. 11. Adding a New Action• When forms are submitted, there is some backendcode that processes the input‣ We will handle this in a new action within theIndexcontroller‣ We use the „zf‟ tool to create the relevant code stubs zf create action actionname controllernameEg: zf create action about index• This creates the function aboutAction() inapplication/controllers/IndexController.php class IndexController extends Zend_Controller_Action { public function addAction() { /* Initialize action controller here */ }
  12. 12. EXAMPLE FOR ZEND Page Controller Action Home page Index index Add new album Index add Edit album Index edit Delete album Index delete And the data fields are id(auto increment) ,artist and album
  13. 13. Actions zf create action add Index zf create action edit Index zf create action delete Index The URLs for each action are: http://localhost/zf-tutorial/public/ IndexController::indexAction() http://localhost/zf-tutorial/public/index/add IndexController::addAction() http://localhost/zf-tutorial/public/index/edit IndexController::editAction() http://localhost/zf-tutorial/public/index/delete IndexController::deleteAction()class IndexController extends Zend_Controller_Action { public function addAction() { /* Initialize action controller here*/ }
  14. 14. Database configurationOpen application/configs/application.ini and add the following to theend of the[production] section (i.e. above the [staging] section):resources.db.adapter = “PDO_MYSQL”resources.db.params.host = “localhost”resources.db.params.username = “root”resources.db.params.password = “”resources.db.params.dbname = “zend”
  15. 15. Create the database tableCREATE TABLE albums (id int(11) NOT NULL auto_increment,artistvarchar(100) NOT NULL,title varchar(100) NOT NULL,PRIMARY KEY(id)); Inserting test data INSERT INTO albums (artist, title) VALUES (Paolo Nutine, Sunny Side Up„), (Florence + The Machine, Lungs), (Massive Attack, Heligoland), (Andre Rieu, Forever Vienna), (Sade, Soldier of Love);
  16. 16. The MODELZend Framework provides Zend_Db_Table which implements theTable DataGateway design pattern to allow for interfacing with data in adatabase table.For this tutorial, we are going to create a model that extendsZend_Db_Table and uses Zend_Db_Table_Row. zf create db-table Albums albums <?php class Application_Model_DbTable_Albums extends Zend_Db_Table_Abstract { protected $_name = albums; } ?>
  17. 17. Layouts and viewsZend_Layout allows us to move all the common header, footer and othercode to a layout view script which then includes the specific view code forthe action being executed. Zf enable layoutZendy/application/layouts/scripts/layout.phtml To get the view script for the current action to display, we echo out the content placeholder using the layout() view helper: echo $this->layout()->content; which does the work for us.
  18. 18. We need to set the doctype for the webpage before we render any view scripts.As the action view scripts are rendered earlier and may need to know whichdoctype is in force. This is especially true for Zend_Form.To set the doctype we add another line to our application.ini, in the [production]section:resources.view.doctype = "XHTML1_STRICT“resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"The doctype() view helper will now output the correct doctype and componentslike Zend_Form willgenerate compatible HTML.
  19. 19. Styling Styling is done by making css file in ZENDY/public/css/site.css And then include that file inside the layout <?php echo $this->headLink()->prependStylesheet($this->baseUrl()./css/site.css); ?>By using headLink()‟s prependStylesheet() method, we allow for additional, more specific, CSS files to be added within the controller view scripts which will be rendered within the <head> section after site.css
  20. 20. To display all the dats in the index page…..zf-tutorial/application/controllers/IndexController.php...function indexAction(){$albums = new Application_Model_DbTable_Albums();$this->view->albums = $albums->fetchAll();}…zf-tutorial/application/views/scripts/index/index.phtml <?php foreach($this->albums as $album) : ?> <td><?php echo $this->escape($album->title);?></td> <td><?php echo $this->escape($album->artist);?></td> <a href="<?php echo $this->url(array(controller=>index, action=>edit, id=>$album->id));?>">Edit</a> <a href="<?php echo $this->url(array(controller=>index, action=>delete, id=>$album->id));?>">Delete</a> <?php endforeach; ?>
  21. 21. Adding new albums There are two bits to this part: • Display a form for user to provide details • Process the form submission and store to database zf create form AlbumThis creates the file Album.php in application/forms
  22. 22. Zend Form
  23. 23. <?phpADD action $this->title = "Add new album"; $this->headTitle($this->title) ;echo $this->form ; ?>
  24. 24. EDIT ACTION
  25. 25. zf-tutorial/application/views/scripts/index/edit.phtml<?php$this->title = "Edit album";$this->headTitle($this->title);echo $this->form ;?> DELETE ACTION
  26. 26. That‟s all………….
  27. 27. This concludes our brief look at building a simple, butfully functional, MVC application using Zend Framework.•Zend framework is loosely packed ,but more secure