Zend framework

31
Zend Framework

description

zend framework is the most used/using PHP framework today. - Regards, Prem

Transcript of Zend framework

Page 1: Zend framework

Zend Framework

Page 2: Zend framework

• 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

Intro

Page 3: Zend framework

• 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

What is MVC?

Page 4: Zend framework

Installing Zend….•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/library

Now 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

Page 5: Zend framework

Starting with project….

Take cmd => go to your local host folderhere its wamp/www/

Type=> zf create project Zendy

Your project is created….you can check in the www folder….Also copy the folder zend in the library to this project`s library

projectname

Page 6: Zend framework

‣ <path>/application• Core application code

‣ <path>/library• Auxillary code

‣ <path>/publicCode that is directly accessible to

the web server (index.php)

‣ <path>/tests

• Directory for test code

ZEND PROJECT SKELTON

Page 7: Zend framework

• application/Bootstrap.php‣ Application bootstrap code

• application/configs‣ Configuration files

• application/controllers‣ Backend controller code

• application/models‣ Code mapping from domain

data to storage data (PHP interface to DB

for example)• application/views/scripts

‣ User interface code• application/configs/application.ini

‣ Main configuration file• application/controllers/

‣ ErrorController.php-> Default controller called when an error occurs

‣ IndexController.php->Default controller when no controller is specified

Page 8: Zend framework

Working with ProjectYou can go to your project by typing localhost/zendy/public on your browser

Your project frontage will be like this:

Page 9: Zend framework

front controller pattern

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.

Page 10: Zend framework

The index.php file is the entry point to our application and is used to create an instance of Zend_Application to initialise our application and then run it. This file also defines two constants:APPLICATION_PATH and APPLICATION_ENV which define the path to the application/ directory and the environment or mode of the application. The default is set to production in index.php, but youshould set it to development in the .htaccess file by adding this line: SetEnv APPLICATION_ENV development

Page 11: Zend framework

E:\wamp\www\zend\bin\application\views\scripts\index

Page 12: Zend framework

Adding a New Action• When forms are submitted, there is some backend code that processes the input‣ We will handle this in a new action within the Indexcontroller‣ 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 */ }

Page 13: Zend framework

EXAMPLE FOR ZEND

Page Controller Action

Home page Index indexAdd new album Index addEdit album Index editDelete album Index delete

And the data fields are id(auto increment) ,artist and album

Page 14: Zend framework

zf create action add Indexzf create action edit Indexzf create action delete Index

Actions

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*/ }

Page 15: Zend framework

Database configurationOpen application/configs/application.ini and add the following to the end 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”

Page 16: Zend framework

Create the database table

CREATE TABLE albums (id int(11) NOT NULL auto_increment,artist varchar(100) NOT NULL,title varchar(100) NOT NULL,PRIMARY KEY (id));

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');

Inserting test data

Page 17: Zend framework

The MODEL

Zend Framework provides Zend_Db_Table which implements the Table DataGateway design pattern to allow for interfacing with data in a database table.

For this tutorial, we are going to create a model that extends Zend_Db_Table and uses Zend_Db_Table_Row.

zf create db-table Albums albums

<?phpclass Application_Model_DbTable_Albums extends Zend_Db_Table_Abstract{

protected $_name = 'albums';}?>

Page 18: Zend framework
Page 19: Zend framework
Page 20: Zend framework

Zend_Layout allows us to move all the common header, footer and other code to a layout view script which then includes the specific view code for the action being executed.

Zf enable layout

Zendy/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.

Layouts and views

Page 21: Zend framework

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 which doctype 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 components like Zend_Form willgenerate compatible HTML.

Page 22: Zend framework

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

Page 23: Zend framework

zf-tutorial/application/controllers/IndexController.php...function indexAction(){$albums = new Application_Model_DbTable_Albums();$this->view->albums = $albums->fetchAll();}…

To display all the dats in the index page…..

<?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; ?>

zf-tutorial/application/views/scripts/index/index.phtml

Page 24: Zend framework
Page 25: Zend framework

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

Page 26: Zend framework

Zend Form

Page 27: Zend framework

ADD action

<?php$this->title = "Add new album";$this->headTitle($this->title);echo $this->form ;?>

Page 28: Zend framework

EDIT ACTION

Page 29: Zend framework

zf-tutorial/application/views/scripts/index/edit.phtml

<?php$this->title = "Edit album";$this->headTitle($this->title);echo $this->form ;?>

DELETE ACTION

Page 30: Zend framework

That’s all………….

Page 31: Zend framework

conclusion

This concludes our brief look at building a simple, but fully functional, MVC application using Zend Framework.

•Zend framework is loosely packed ,but more secure