0% found this document useful (0 votes)
116 views39 pages

MVC Components in Zend Framework - PHP Tutorial 4

The component structure of Zend Framework is somewhat unique; each component is designed with few dependencies on other components. This loosely coupled architecture allows developers to use components individually. We often call this a "use-at-will" design.

Uploaded by

apex_tgi
Copyright
© © All Rights Reserved
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)
116 views39 pages

MVC Components in Zend Framework - PHP Tutorial 4

The component structure of Zend Framework is somewhat unique; each component is designed with few dependencies on other components. This loosely coupled architecture allows developers to use components individually. We often call this a "use-at-will" design.

Uploaded by

apex_tgi
Copyright
© © All Rights Reserved
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/ 39

By

Apex Tg India Pvt Ltd


http://www.apextgi.in

Components

Library of Zend components

Reminder:
Zend/Db.php = Zend_Db
Zend/Db/Table.php = Zend_Db_Table

Components vs. straight PHP


Additional functionality
Zend_Session vs. straight $_SESSION
Zend_Debug::dump() vs. vardump

Thread-safety
Zend_Translate vs. gettext
Zend_Locale vs. setlocale

OO
OO Syntax
May be extended with custom functionality
ZF components reduce coding when used together

Theyve been tested and integrated for you

Components in 1.75
Zend_Acl
Zend_Amf
Zend_Auth
Zend_Cache
Zend_Captcha
Zend_Config
Zend_Config_Writer
Zend_Console_Getopt
Zend_Controller
Zend_Currency
Zend_Date
Zend_Db
Zend_Debug
Zend_Dojo
Zend_Dom
Zend_Exception
Zend_Feed
Zend_File
Zend_Filter
Zend_FilterInput
Zend_Form
Zend_Gdata
Zend_Http

Zend_Infocard
Zend_Json
Zend_Layout
Zend_Ldap
Zend_Loader
Zend_Locale
Zend_Log
Zend_Mail
Zend_Measure
Zend_Memory
Zend_Mime
Zend_OpenId
Zend_Paginator
Zend_Pdf
Zend_ProgressBar
Zend_Registry
Zend_Rest
Zend_Search_Lucene
Zend_Server_Reflection
Zend_Akismet
Zend_Amazon
Zend_Audioscrobbler
Zend_Delicious

Zend_Flickr
Zend_Nirvanix
Zend_ReCaptcha
Zend_Simpy
Zend_SlideShare
Zend_StrikeIron
Zend_Technorati
Zend_Twitter
Zend_Yahoo
Zend_Session
Zend_Soap
Zend_Test
Zend_Text
Zend_Timesync
Zend_Translate
Zend_Uri
Zend_Validate
Zend_Version
Zend_View
Zend_Wildfire
Zend_XmlRpc
Zend_XConsoleProcessUnix
Zend_XJQuery

Component categories
MVC
Formatting
Ajax
Identity, Authentication, and Authorization
Forms
Datastore
Web Services
Enterprise
Debugging, Logging, and Testing
I18n and L10n
Mail
Infrastructure

MVC components (you already know


these)
Zend_Controller
Zend_Layout
Zend_View

Formatting
Zend_Text
Zend_Paginator

Ajax
Zend_Dojo
Zend_Json

Identity, authentication, and


authorization
Zend_Acl
Zend_Auth
Zend_Infocard
Zend_OpenId
Zend_Session

Form and input validation


Zend_Captcha
Zend_Form
Zend_Validate
Zend_Filter

Database access
Zend_Db
Zend_Db_Table
Zend_Db_Profiler

Web services
Zend_Feed
Zend_Gdata
Zend_Http
Zend_Rest
Zend_Server_Reflection
Zend_Soap
Zend_Uri
Zend_XmlRpc

Zend_Service_* clients
Akismet
Amazon
Audioscrobbler
Delicious
Flickr
Nirvanix
ReCaptcha
Simpy
SlideShare
StrikeIron
Technorati
Twitter
Yahoo

Additional categories
Zend_Ldap
Zend_Search_Lucene
Zend_Pdf

Debugging, logging, and testing


Zend_Debug
Zend_Log
Zend_Test
Zend_Wildfire

I18n and L10n


Zend_Currency
Zend_Calendar
Zend_Date
Zend_Locale
Zend_Measure
Zend_TimeSync
Zend_Translate

Mail
Zend_Mail
Zend_Mime

Infrastructure
Zend_Cache
Zend_Config
Zend_Console
Zend_File
Zend_Loader
Zend_Memory
Zend_Registry
Zend_Version

Registry

Registry
Store global data without polluting the global

scope

Zend_Registry::set(key, $value);
Zend_Registry::get(key);
Useful for storing sessions, configuration data or

any data that could potentially be important on a


per-request basis

Logging

Logging

Structured, flexible logging class

Zend_Log has several backends

Stream (write to a file)


Firebug
Mail
Db

Example of logging to file system


$writer=newZend_Log_Writer_Stream('/path/to/logfile');
// Only email warning level entries and higher.
$writer->addFilter(Zend_Log::WARN);
$logger=newZend_Log($writer);
// later
$logger->info('Informationalmessage');
$logger->err(Errormessage');
// Error message

Logging: Firebug backend


Zend_Log_Writer_Firebug, Zend_Db_Profiler_Firebug
Install Firebug and FirePHP in Firefox
// in bootstrap
$logger = new Zend_Log();
$writer = new Zend_Log_Writer_Firebug();
$logger->addWriter($writer);
Zend_Registry::set('logger',$logger);
// in action controller or anywhere
$logger = Zend_Registry::get('logger');
$logger->log(Log message', Zend_Log::DEBUG);
$logger->log(Info message', Zend_Log::INFO);
$logger->log(Warn message', Zend_Log::WARN);
$logger->log(Error message', Zend_Log::ERR);

Config

Zend_Config

OO syntax for reading/writing config files

Internally its an array

Multiple backends

INI
XML

Divide sections with [ ], good for dev/prod

And hierarchichy with periods after that


db.params.username = xxxx

Many components accept Zend_Config object as well as array

Db
Forms

Config + Registry
Config.ini:
[dev]
db.adapter = PDO_MYSQL
db.params.username = admin
db.params.password = xxxx
db.params.dbname = appdb
db.params.host = 192.168.9.1
log.filename = /appdev/logs/app.log
$config = new Zend_Config_Ini(realpath(dirname(__FILE__) . '/../application/config.ini'), 'dev');
$registry = Zend_Registry::getInstance();
$registry->set('config', $config);
$db = Zend_Db::factory($config->db);
Zend_Db_Table::setDefaultAdapter($db);
$registry->set('db', $db);
// create logger; store it in registry
$logFile = $registry->get('config')->log->filename;
$writer = new Zend_Log_Writer_Stream($logFile);
$logger = new Zend_Log($writer);
$registry->set('logger', $logger);

Sessions

Sessions

Zend_Session does more than PHPs ext/session

Namespaces let multiple applications (or sections of application) run in the same
$_SESSION without risking key name collision
Used by Zend_Auth by default
Can set data to expire based on time or clicks/requests
$s = new Zend_Session_Namespace(myNamespace');
$s->a = 'apple';
$s->p = 'pear';
$s->o = 'orange';
$s->setExpirationSeconds(5, 'a'); // expire only the key "a" in 5
seconds
// expire entire namespace in 5 "hops"
$s->setExpirationHops(5);
$s->setExpirationSeconds(60);
// The "expireAll" namespace will be marked "expired" on
// the first request received after 60 seconds have elapsed,
// or in 5 hops, whichever happens first.

Authentication

Authentication
Built over an adapter that implements

Zend_Auth_Adapter_Interface

Must have an authenticate() method that returns a

Zend_Auth_Result object

Several pre-defined adapter classes can

authenticate against common data stores


Zend_Auth_Adapter_Db_Table, Digest, Http, Ldap,

Openid

Zend_Auth_Storage_Sessionuses a session

namespace of"Zend_Auth" by default

Authentication example
// created db adapter earlier
$dbAdapter=Zend_Registry::get('db');
$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
$authAdapter
->setTableName('users')
->setIdentityColumn('username')
->setCredentialColumn('password')
;
// Perform the authentication query, saving the result in $_SESSION
$result = $authAdapter->authenticate();
if($result->isValid) {
echo logged in;
} else {
echo failed;
}
// Print the result row
print_r($authAdapter->getResultRowObject());
/* Output:
my_username
Array
(
[id] => 1
[username] => my_username
[password] => my_password
[real_name] => My Real Name
)
*/

Authorization

Authorization
Classes used
Zend_Acl_Role
Zend_Acl_Resource

Three concepts

Role
Assigns particular groups of access to an individual

Resource
An object to which access is controlled
Resources can have privileges (e.g., "create", "read", "update", "delete")

Access Control List (ACL)


A hierarchical list that connects role/resource permissions

Authorization
$acl = new Zend_Acl();
$acl->addRole(new Zend_Acl_Role('guest'))
->addRole(new Zend_Acl_Role('member'))
->addRole(new Zend_Acl_Role('admin'));
$parents = array('guest', 'member', 'admin');
$acl->addRole(new Zend_Acl_Role('someUser'), $parents);
$acl->add(new Zend_Acl_Resource('someResource'));
$acl->deny('guest', 'someResource');
$acl->allow('member', 'someResource');
echo $acl->isAllowed('someUser', 'someResource') ?
'allowed' : 'denied';

Forms

Zend_Form

Flexible solution for building forms

Create in code

Put it in a model class

Or create in config file

Factory pattern lets you determine element type at runtime

Pass $form to view script, where its output

echo $form; or
echo $form->ordernum; or
echo $form->getElement(ordernum);

Decorators

Zend_Form example

class My_Form extends Zend_Form


{
$form->addElement('text', 'username', array(
'validators' => array(
'alnum',
array('regex', false, '/^[a-z]/i')
),
'required' => true,
'filters'

=> array('StringToLower'),

));
}
// in your controller
$form = new My_Form();
$this->view = $form
// in your view
cho $this->form;

Questions?
Send me your thoughts: [email protected]

See you at APEX


TG!

You might also like