Framework

28
PHP Frameworks Using PHP framework for development process

description

 

Transcript of Framework

Page 1: Framework

PHP FrameworksUsing PHP framework for development process

Page 2: Framework

“A software framework, in computer programming, is an abstraction in which common code providing generic functionality can be selectively overridden or specialized by user code providing specific functionality. Frameworks are a special case of software libraries in that they are reusable abstractions of code wrapped in a well-defined Application programming interface (API), yet they contain some key distinguishing features that separate them from normal libraries.”

Page 3: Framework

• Save times• Unify Development Process• Reusable• Modularity• Scalable Software• Easy Maintenance Enterprise

Softwares

Page 4: Framework
Page 5: Framework

• Maintain by Zend - The PHP Company• Partners: Adobe, Google, IBM,

Microsoft• Open source• Large community• Good documents

Page 6: Framework

• MVC Framework• Component base framework• Tons of components• Require PHP 5.2.x or higher

Page 7: Framework
Page 8: Framework

• Controllero Accept Requesto Call Modelo Pass model data to View

• Modelo Process business logico Return result to Controller

• Viewo Receive result from

Controllero Display datao Call model (optional)

Page 9: Framework

Front Controller

ActionController Component

Library

Data

Internationalization

PDF Database JSON

Date Translate Locale

Webservices

SOAP Amazon Google

Core

Log Cache ACL

Plugins

ViewEditDel

HTTP Request

Page 10: Framework

• Accept HTTP Request• Routing• Call Action Controller• Response to client

Page 11: Framework

• Find the target action base on request URL• Standard router:

o http://example/news Module = news

o http://example/foo In case of “foo” module does not exists: Module= Default Module,

Controller = fooo http://example/blog/archive

Module = Blog, Controller = archiveo http://example/blog/archive/list

Module = Blog, Controller = Archive, Action = Listo http://example/blog/archive/list/sort/alpha/dir/desc

Module = Blog, Controller = Archive, Action = List Params: sort = alpha, dir=desc

Page 12: Framework

<?php

/*** Index Controller*/class IndexController extends Zend_Controller_Action{/*** Index Action*/public function indexAction(){// action body}

}

Page 13: Framework

• Zend_Db_Adaptero IBM DB2, MySQL, SQL Server, Oracle, PostgreSQL, SQLite,

Firebird• Zend_Db_Statement• Zend_Db_Profiler• Zend_Db_Select• Zend_Db_Table• Zend_Db_Table_Row• Zend_Db_Table_Rowset• Zend_Db_Table Relationships• Zend_Db_Table_Definition

Page 14: Framework

<?php

$params = array('host' => '127.0.0.1','username' => 'webuser','password' => ’*******','dbname' => 'test',);$db = Zend_Db::factory('Pdo_Mysql', $params);

$sql = ‘SELECT * FROM users’;$rows = $db->fetchAll($sql);

Page 15: Framework

<?php

$select = $db->select()                 ->from(‘users’, array(‘username’, ‘email’))                      ->where(‘username = ?’, $username);//Same as:// SELECT username, email FROM users// WHERE username = ‘…’

$user = $db->fetchRow($select);

Page 16: Framework

<?phpclass Users extends Zend_Db_Table_Abstract{protected $_name = 'users';protected $_primaryKey = 'user_id';}

//Insert$user = new Users();$user->insert(array(‘username’ => ‘cusc’‘name’ => ‘CUSC Software’,‘email’ => ‘[email protected]’));

Page 17: Framework

<?php

//Update$user = new Users();$data = array(‘email’ => ‘[email protected]’);

$user->update($data, ‘user_id = 1234’);

//Delete row$user->delete(‘user_id = 1234’);

Page 18: Framework

<?php

//Find rows by primary key$user = new Users();$rows = $user->find(1234);

//Find by conditions$select = $user->select()                         ->where(“email LIKE ‘%?’”, ‘musical.vn’);$rows = $user->fetchAll($select);

Page 19: Framework

• Scripts (templates)o PHP-based script templates to present datao Should contain only display logic, not business logico Default naming: “actionname.phtml”

• Helperso Classes and methods that provide reusable view functionalityo Examples of built in view helpers: escape(), formText(),

partial(), partialLoop(), headTitle()o Write your own, too

• Layouto Define application/page layouto Using Two Step View pattern

• Placeholders

Page 20: Framework

<?php

/*** Index Controller*/class IndexController extends Zend_Controller_Action{/*** Index Action*/public function indexAction(){$this->view->name = ‘Dream Team’;}

}

Page 21: Framework

<?php//index.phtml

<h1><?php echo $this->escape($this->name); ?></h1>

Page 22: Framework

• Flexible solution for building forms• Create by PHP code, put it in model class• Or create in configuration file• Validation support• Auto restore input values when error occurs.• Using decorator for output form elements

Page 23: Framework

class Admin_Form_Account extends Zend_Form{public function init() {parent::init();

$this->setAction('/admin/account/createPost');

$this->addElement($this->createElement('text', 'username', array('required' => true,'label'       => 'Username',)));

$this->addElement($this->createElement('text', 'name', array(          'required' => true,          'label'       => 'Name',)));

$this->addElement($this->createElement('text', 'email', array(          'required' => true,          'label' => 'Email',)));

$this->addElement($this->createElement('password', 'password', array(          'required' => true,          'label' => 'Lastname',)));

$this->addElement('submit', 'Submit');}}

Page 24: Framework

<?php

class Admin_Account_Controller extends Zend_Controller_Action{public function formAction(){          $form = new Admin_Form_Account();          $this->view->form = $form;}

public function createPostAction(){          $form = new Admin_Form_Account();

          if (!$form->isValid($this->getRequest()->getPost())) {                   $this->view->form = $form;                   $this->render(’form');          } else {                   $this->_forward(’success');          }}}

Page 25: Framework

• Alnum• Alpha• Barcode• Between• CreditCard• Ccnum• Date• Db_RecordExists• Digits• EmailAddress• Float• GreaterThan• Hex

• Hostname• Iban• Identical• InArray• Int• Ip• Isbn• LessThan• NotEmpty• PostCode• Regex• StringLength

Page 26: Framework

class Admin_Form_Account extends Zend_Form{public function init() {       parent::init();       $this->addElement($this->createElement('text', 'email', array(               'required'   => true,               'label'         => 'Email',               'validators' => array('emailaddress'),         )));}}

Page 27: Framework

• Main website: http://framework.zend.com• Quick Start:

http://framework.zend.com/manual/en/learning.quickstart.intro.html

• Manual: http://framework.zend.com/manual/manual

Page 28: Framework

• Create Module• Create Controller, Action• Create Layout, View• Create Form• Update data