introduction to zend framework

25
$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me Zend Framework 1.x

Upload: matteo-magni

Post on 15-Jan-2015

1.570 views

Category:

Technology


4 download

DESCRIPTION

$incontro['pugBO'][7] = 'Introduction to Zend Framework'Introduction to Zend Framework 1.x for pugBO

TRANSCRIPT

Page 1: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Zend Framework 1.x

Page 2: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

whoami

• Matteo Magni

• @ilbonzo

• https://github.com/ilbonzo

• http://it.linkedin.com/in/matteomagni

Page 3: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Zend Framework 1.X

• Zend Framework is an open source, object oriented web application framework for PHP 5. Zend Framework is often called a 'component library', because it has many loosely coupled components that you can use more or less independently.

• http://framework.zend.com/

• BSD license

• Version 1.11

Page 4: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Installation

• Register and download source

• Add to your include path– sudo mv ~/ZendFramework­1.11.11/library/Zend/ /usr/share/php/Zend

OR

– sudo mv ~/ZendFramework­1.11.11/library/Zend/ [project]/library/Zend

• Install bin tools– sudo mv ~/ZendFramework­1.11.11 /usr/share/php/ZendFramework

– alias

zf=/usr/share/php/ZendFramework/bin/zf.sh

Page 5: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

MVC

Page 6: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Demo project for Layar

• http://layar.com/

It's mobile browser allows users to find various items based upon augmented reality technology.

• https://github.com/ilbonzo/Wade

Page 7: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Create Project

Page 8: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Config Apps

/application/configs/application.ini

Page 9: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Bootstrap

/application/Bootstrap.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap

{

    protected function _initDoctype()

    {

        $this­>bootstrap('view');

        $view = $this­>getResource('view');

        $view­>doctype('HTML5');

    }

    .......

}

Page 10: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Layouts

• $ zf enable layout • /application/configs/application.ini

– resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"

• Twitter Bootstrap http://twitter.github.com/bootstrap/

Other Layouts$this­>_helper­>layout­>setLayout('layout_other');

Page 11: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

ZF overview

• Models(db-tables,objectmodels)

• Views(helpers,scripts,layouts)

• Controllers(actions,helpers)

• Forms

Page 12: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Namespace & Route

• All classes named for their folder (e.g.Application_Model_Post in application/models/post.php)

• Every Zend module is auto loaded when needed

• Default controller/action is Index

• URLformat: /<controller>/<action>

Page 13: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Controller & Actions

$ zf create controller Point

$ zf create action view Point

$ zf create action add Point

$ zf create action update Point

$ zf create action delete Point

create file /application/controllers/PointController.phpclass PointController extends Zend_Controller_Action

{

    public function init()

    {

        /* Initialize action controller here */

    }

    public function indexAction()

   {

       // action body

   }

 ...

Page 14: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Views e Actions

• Create views/application/views/scripts/point/[action].phtml

esempio:<br /><br />

<div id="view­content">

<p>View script for controller <b>Point</b> and script/action name <b>add</b></p>

</div>

Page 15: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Database

/application/configs/application.ini

PostgreSQL DB

resources.db.adapter = PDO_PGSQL

resources.db.params.host = localhost

resources.db.params.username = postgres

resources.db.params.password = 

resources.db.params.dbname = wade

Page 16: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Zend_Db

The Zend_Db_Table solution is an implementation of the Table Data Gateway pattern. The solution also includes a class that implements the Row Data Gateway pattern.

Page 17: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

DbTable - DbTableRow

$ zf create db­table Points points$ zf create model Point

/application/models/Point.phpclass Application_Model_Point extends Zend_Db_Table_Row_Abstract

{    function __get($key)    {        if (method_exists($this, $key)) {            return $this­>$key();        }        return parent::__get($key);    }

}

Page 18: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Form - Validation

$ zf create form Point

/application/forms/Point.phpclass Application_Form_Point extends Zend_Form

{

    public function init()

    {

        $title = new Zend_Form_Element_Text('title');

        $title­>setLabel('title')

                ­>setRequired(true)

                ­>addFilter('StripTags')

                ­>addFilter('StringTrim')

                ­>addValidator('NotEmpty');

    }

}

Page 19: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Controller – Form

/application/controllers/PointController.php

        $form = new Application_Form_Point();

        $form­>submit­>setLabel('Add');

        $this­>view­>form = $form;

        if ($this­>getRequest()­>isPost()) {

            $formData = $this­>getRequest()­>getPost();

            if ($form­>isValid($formData)) {

                $title = $form­>getValue('title');

                ....

                $point = new Application_Model_DbTable_Points();

                $point­>addPoint($title, $footnote, $description, $lat, $lon, $alt);

                $this­>_helper­>redirector('index');

            } else {

                $form­>populate($formData);

            }

        }        

Page 20: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Views - Form

/application/views/scripts/point/add.phtml

<?php

$this­>title = "Add new Point";

$this­>headTitle($this­>title);

echo $this­>form ;

?>

Page 21: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Models and Table Relationships

class Application_Model_DbTable_Points extends Zend_Db_Table_Abstract

{  

    protected $_dependentTables = array('Application_Model_DbTable_Actions');

....

class Application_Model_DbTable_Actions extends Zend_Db_Table_Abstract

{

    protected $_name = 'actions';

    protected $_rowClass = 'Application_Model_Action';

    protected $_primary = 'id';

    

    protected $_referenceMap = array(

        'Application_Model_DbTable_Points' => array(

         'columns' => array('point_id'),

            'refTableClass' => 'Application_Model_DbTable_Points',

            'refColumns' => array('id')

        ),

Page 22: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Component Library

•Zend_Acl

•Zend_Amf

•Zend_Application

•Zend_Auth

•Zend_Barcode

•Zend_Cache

•Zend_Captcha

•--------

•Zend_Config

•Zend_Config_Writer

•Zend_Controller

•Zend_Currency

•Zend_Date

•Zend_Db

•----------

•ZendX_JQuery

Page 23: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Youtube

https://developers.google.com/youtube/2.0/developers_guide_php

Page 24: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

ZF & Drupal

http://drupal.org/project/zend

It's possible but Dries chose Symfony

Page 25: Introduction to Zend framework

$incontro['pugBO'][7] = 'Introduction to Zend Framework' http://magni.me

Questions?

http://blog.ilbonzo.org

http://twitter.com/ilbonzo