Framework
-
Upload
nguyen-linh -
Category
Technology
-
view
628 -
download
4
description
Transcript of Framework
PHP FrameworksUsing PHP framework for development process
“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.”
• Save times• Unify Development Process• Reusable• Modularity• Scalable Software• Easy Maintenance Enterprise
Softwares
• Maintain by Zend - The PHP Company• Partners: Adobe, Google, IBM,
Microsoft• Open source• Large community• Good documents
• MVC Framework• Component base framework• Tons of components• Require PHP 5.2.x or higher
• 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)
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
• Accept HTTP Request• Routing• Call Action Controller• Response to client
• 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
<?php
/*** Index Controller*/class IndexController extends Zend_Controller_Action{/*** Index Action*/public function indexAction(){// action body}
}
• 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
<?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);
<?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);
<?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]’));
<?php
//Update$user = new Users();$data = array(‘email’ => ‘[email protected]’);
$user->update($data, ‘user_id = 1234’);
//Delete row$user->delete(‘user_id = 1234’);
<?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);
• 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
<?php
/*** Index Controller*/class IndexController extends Zend_Controller_Action{/*** Index Action*/public function indexAction(){$this->view->name = ‘Dream Team’;}
}
<?php//index.phtml
<h1><?php echo $this->escape($this->name); ?></h1>
• 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
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');}}
<?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'); }}}
• 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
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'), )));}}
• 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
• Create Module• Create Controller, Action• Create Layout, View• Create Form• Update data