0% found this document useful (0 votes)
243 views1 page

Zend Framework Cheat Sheet

Uploaded by

高巍
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
243 views1 page

Zend Framework Cheat Sheet

Uploaded by

高巍
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Zend Framework Cheatsheet v1.

Zend_Controller Zend_Db/Zend_Db_Table

class IndexController extends Zend_Controller_Action { class user extends Zend_Db_Table_Abstract {


public function init() { // will be called for each action
// name of the database table
$this->view->doctype('XHTML1_TRANSITIONAL'); protected $_name = 'user';
$this->view->headMeta()->appendName('robots','index,follow');
// primary key of this table
protected $_primary = 'uid';
}
public function indexAction() { // array with table names that contain the uid as a foreign key
$this->view->headMeta()->appendName('description','My description protected $_dependentTables = array('Book');
for this action');
$this->view->headMeta()- public function getFirstname($uid) {
>appendName('keywords','keywords,for,this,action'); $select = $this->select()->where('uid = ?', $uid);

// append inline styles to that action // return a single value


$this->view->headStyle()->appendStyle('p { color: #AAA; }'); return $this->getAdapter()->fetchOne($select);

// append javascript files and an inline script }


$this->view->appendFile()->('/url/to/file.js');
$this->view->appendScript('alert("Page loaded!");'); public function getByEmail($email) {
}
public function displayFirstnameAction() { $select = $this->select()->where('email = ?', $email);

// fetch user id from GET or POST // we only return one row here
$uid = $this->getRequest()->getParam('uid'); return $this->fetchRow($select);

// fetch firstname from database }


$userTable = new User();
$this->view->firstname = $userTable->getFirstname($uid); public function getPairs() {

// fetch all users from database to build a drop down


} return $this->getAdapter()->fetchPairs('SELECT uid, name FROM user
public function activateUser() { WHERE type="student" ORDER BY lastname');

$this->view->activationForm = $this->_getActivationForm(); }

// check if the request is POST (so the form is submitted) public function activate($uid) {
if ($this->getRequest()->isPost()) {
$where = $this->getAdapter()->quoteInto('uid = ?', $uid,
// validate the form values and assign error messages (if not valid) 'INTEGER');
if ($this->view->activationForm->isValid($_POST)) {
$this->update(array(
// get the filtered values of the activation form 'active' => 1,
$values = $this->view->activationForm->getValues(); 'registered_on' => new Zend_Db_Expr('NOW()')
), $where);
$userTable = new User();
$userTable->activate($values['uid']); }
}
// display another view with success message
$this->render('activationComplete');
}
else {
$this->view->activationForm->setDescription('Your account could
not be activate. Please correct the form values.');
}
}
}
}

Zend_Registry Zend_View/Zend_Layout
// get the sitewide database adapter // Layout deaktivieren
$db = Zend_Registry::get('db'); $this->getHelper('layout')->disableLayout();

// get the current language (if any is set) // View deaktivieren


$lang = Zend_Registry::get('Zend_Translate')->getLocale(); $this->getHelper('ViewRenderer')->setNoRenderer();

// get a configuration value /* Redirection


$mail = Zend_Registry::get('config')->mail->webmaster; With $this->_forward('/foo/bar/') you transfer the execution of the current
request to a different action and the URL in the browser doesn't change.

With $this->_redirect('/foo/bar/') you actually make another round trip


from the server to the client and the URL in the browser will change.

Therefore, using $this->_forward() will be a less resource intensive call,


but sometimes you may actually need to redirect to a new location e.g. in
an access denied situation. */

© www.ideveloper.de

You might also like