Beruflich Dokumente
Kultur Dokumente
Manual en Español
Traducción al 38.7% - Actualizado el 14 de Noviembre del 2009 - Revisión Nro.471
Colaboradores
Pedro Boda
Kattia Ninahuanca
Javier Martinez
Benjamín Gonzales
Actualmente presentamos a los colaboradores más activos en la portada del manual. Esto no
quiere decir que no existan más colaboradores que también trabajan en el manual o que hayan
ayudado de manera activa en un pasado. Existen también muchas personas que ayudan con sus
observaciones a los cuales le estamos infinitamente agradecidos. Si deseas unirte al grupo de traductores
puedes enviarnos un mail a zendframeworkspanish@googlegroups.com solicitando tu inclusión
Compilado por
Benjamín Gonzales
(http://www.codigolinea.com)
iii
Guía de Referencia del Programador
Zend_Application_Resource_Navigation ............................................................................... 53
Zend_Application_Resource_Router ..................................................................................... 54
Zend_Application_Resource_Session .................................................................................... 54
Zend_Application_Resource_View ....................................................................................... 55
5. Zend_Auth ................................................................................................................................... 57
Introducción ............................................................................................................................. 57
Adaptadores ..................................................................................................................... 57
Resultados ....................................................................................................................... 58
Persistencia de Identidad .................................................................................................... 59
Uso ................................................................................................................................ 61
Tabla de base de datos de autenticación ........................................................................................ 63
Introducción ..................................................................................................................... 63
Advanced Usage: Manteniendo el resultado del Objeto DbTable ................................................ 65
Ejemplo de Uso Avanzado ................................................................................................. 66
Autenticación "Digest" ............................................................................................................... 67
Introducción ..................................................................................................................... 67
Detalles Específicos .......................................................................................................... 68
Identidad ......................................................................................................................... 68
Adaptador de Autenticación HTTP .............................................................................................. 68
Introducción ..................................................................................................................... 68
Descripción del diseño ....................................................................................................... 69
Opciones de Configuración ................................................................................................. 69
Resolvers ......................................................................................................................... 70
Uso Básico ...................................................................................................................... 70
LDAP Authentication ................................................................................................................ 71
Introduction ..................................................................................................................... 71
Usage ............................................................................................................................. 72
The API .......................................................................................................................... 73
Server Options ................................................................................................................. 75
Collecting Debugging Messages .......................................................................................... 77
Common Options for Specific Servers .................................................................................. 78
Autenticación con Open ID ....................................................................................................... 79
Introducción ..................................................................................................................... 79
Características .................................................................................................................. 80
6. Zend_Cache ................................................................................................................................. 83
Introducción ............................................................................................................................. 83
The Theory of Caching .............................................................................................................. 85
The Zend_Cache Factory Method ........................................................................................ 85
Tagging Records ............................................................................................................... 86
Cleaning the Cache ........................................................................................................... 86
Zend_Cache Frontends ............................................................................................................... 87
Zend_Cache_Core ............................................................................................................. 87
Zend_Cache_Frontend_Output ............................................................................................ 90
Zend_Cache_Frontend_Function .......................................................................................... 91
Zend_Cache_Frontend_Class .............................................................................................. 91
Zend_Cache_Frontend_File ................................................................................................ 93
Zend_Cache_Frontend_Page ............................................................................................... 94
Zend_Cache Backends ............................................................................................................... 98
Zend_Cache_Backend_File ................................................................................................. 98
Zend_Cache_Backend_Sqlite .............................................................................................. 99
Zend_Cache_Backend_Memcached .................................................................................... 100
Zend_Cache_Backend_Apc ............................................................................................... 101
Zend_Cache_Backend_Xcache .......................................................................................... 101
Zend_Cache_Backend_ZendPlatform .................................................................................. 101
iv
Guía de Referencia del Programador
v
Guía de Referencia del Programador
vi
Guía de Referencia del Programador
vii
Guía de Referencia del Programador
viii
Guía de Referencia del Programador
ix
Guía de Referencia del Programador
x
Guía de Referencia del Programador
xi
Guía de Referencia del Programador
xii
Guía de Referencia del Programador
xiii
Guía de Referencia del Programador
xiv
Guía de Referencia del Programador
xv
Guía de Referencia del Programador
xvi
Guía de Referencia del Programador
xvii
Guía de Referencia del Programador
xviii
Guía de Referencia del Programador
xix
Guía de Referencia del Programador
xx
Guía de Referencia del Programador
xxi
Guía de Referencia del Programador
xxii
Guía de Referencia del Programador
xxiii
Guía de Referencia del Programador
xxiv
Guía de Referencia del Programador
xxv
Guía de Referencia del Programador
xxvi
Guía de Referencia del Programador
xxvii
Guía de Referencia del Programador
xxviii
Guía de Referencia del Programador
xxix
Guía de Referencia del Programador
xxx
Guía de Referencia del Programador
xxxi
Lista de tablas
2.1. Controles de Acceso para un CMS de ejemplo .................................................................................. 5
4.1. Zend_Application options ............................................................................................................. 39
4.2. Métodos de Zend_Application ...................................................................................................... 40
4.3. Zend_Application_Bootstrap_Bootstrapper Interface ......................................................................... 42
4.4. Zend_Application_Bootstrap_ResourceBootstrapper Interface ............................................................. 43
4.5. Métodos de Zend_Application_Bootstrap_BootstrapAbstract ............................................................... 44
4.6. Zend_Application_Resource_Resource Interface ............................................................................... 47
4.7. Zend_Application_Resource_ResourceAbstract Methods .................................................................... 47
5.1. Opciones de Configuración ........................................................................................................... 69
5.2. Server Options ........................................................................................................................... 75
5.3. Debugging Messages ................................................................................................................... 77
5.4. Options for Active Directory ........................................................................................................ 78
5.5. Options for OpenLDAP ............................................................................................................... 79
6.1. Core Frontend Options ................................................................................................................ 87
6.2. Function Frontend Options ........................................................................................................... 91
6.3. Class Frontend Options ................................................................................................................ 92
6.4. File Frontend Options .................................................................................................................. 93
6.5. Page Frontend Options ................................................................................................................ 94
6.6. File Backend Options .................................................................................................................. 98
6.7. Sqlite Backend Options ............................................................................................................... 99
6.8. Memcached Backend Options ..................................................................................................... 100
6.9. Xcache Backend Options ............................................................................................................ 101
6.10. TwoLevels Backend Options ..................................................................................................... 102
9.1. Parámetros del constructor Zend_Config_Ini .................................................................................. 128
13.1. Constants for the selecting the currency description ....................................................................... 240
13.2. Constants for the selecting the position of the currency description ................................................... 240
14.1. Date Parts .............................................................................................................................. 252
14.2. Basic Operations ..................................................................................................................... 253
14.3. Date Comparison Methods ........................................................................................................ 254
14.4. Date Output Methods ............................................................................................................... 255
14.5. Date Output Methods ............................................................................................................... 256
14.6. Miscellaneous Methods ............................................................................................................ 257
14.7. Operations Involving Zend_Date::HOUR ..................................................................................... 259
14.8. Day Constants ........................................................................................................................ 259
14.9. Week Constants ...................................................................................................................... 260
14.10. Month Constants ................................................................................................................... 260
14.11. Year Constants ...................................................................................................................... 260
14.12. Time Constants ..................................................................................................................... 261
14.13. Timezone Constants ............................................................................................................... 261
14.14. Date Format Constants (formats include timezone) ...................................................................... 261
14.15. Date and Time Formats (format varies by locale) ........................................................................ 262
14.16. Constants for ISO 8601 Date Output ......................................................................................... 263
14.17. Constants for PHP Date Output ................................................................................................ 266
14.18. Types of Supported Horizons for Sunset and Sunrise .................................................................... 270
15.1. Metadata Fields Returned by describeTable() ............................................................................... 289
15.2. Constantes usedas por getPart() y reset() ..................................................................................... 318
20.1. Feed Level API Methods .......................................................................................................... 441
20.2. Extended Feed Level API Methods ............................................................................................. 442
20.3. Entry Level API Methods ......................................................................................................... 443
20.4. Extended Entry Level API Methods ............................................................................................ 444
20.5. Core Extensions (pre-registered) ................................................................................................ 445
xxxii
Guía de Referencia del Programador
xxxiii
Guía de Referencia del Programador
xxxiv
Lista de Ejemplos
2.1. Herencia Múlltiple entre Roles ....................................................................................................... 4
3.1. Servidor AMF básico .................................................................................................................. 13
3.2. Agregar cabeceras de mensaje a la respuesta de AMF ....................................................................... 17
4.1. Muestra de la Configuracion de Recursos del Adaptador DB (Base de Datos) ........................................ 50
4.2. Ejemplo Front Controller resource configuration .............................................................................. 51
4.3. Recuperar el Front Controller de su arranque (bootstrap) .................................................................... 51
4.4. Sample Layout configuration ........................................................................................................ 51
4.5. Configurando Módulos ................................................................................................................ 52
4.6. Recuperando el bootstrap de un módulo específico ........................................................................... 53
4.7. Sample Navigation resource configuration ....................................................................................... 53
4.8. Sample Router Resource configuration ........................................................................................... 54
4.9. Configuración de recursos de la Sesión Ejemplo .............................................................................. 54
4.10. Ejemplo de configuración del recurso Vista ................................................................................... 55
5.1. Modifying the Session Namespace ................................................................................................. 59
5.2. Usando una Clase de Almacenamiento Personalizada ........................................................................ 60
5.3. Uso Básico ................................................................................................................................ 63
6.1. Obtener un frontend con Zend_Cache::factory() .............................................................................. 83
6.2. Almacenando en caché un resultado de consulta a una base de datos .................................................... 83
6.3. El almacenamiento en caché de salida con la interfaz de salida Zend_Cache ......................................... 84
8.1. Generando clases PHP ............................................................................................................... 111
8.2. Generando clases PHP con propiedades de clase ............................................................................. 112
8.3. Generando clases PHP con métodos de clase ................................................................................. 113
8.4. Generando archivos PHP ............................................................................................................ 115
8.5. Sembrando la generación de código para un archivo PHP via reflection ............................................... 117
8.6. Sembrando la generación de clases PHP via reflection ..................................................................... 117
8.7. Sembrando la generación de métodos PHP via reflection .................................................................. 118
9.1. Usando Zend_Config Per Se ....................................................................................................... 125
9.2. Usando Zend_Config con un Archivo de Configuración PHP ............................................................ 126
9.3. Utilizando Zend_Config_Ini ........................................................................................................ 128
9.4. Usando Zend_Config_Xml ......................................................................................................... 129
9.5. Usando atributos de etiqueta en Zend_Config_Xml ......................................................................... 130
10.1. Using Zend_Config_Writer ....................................................................................................... 133
10.2. Modifying an existing config ..................................................................................................... 134
11.1. Using the Short Syntax ............................................................................................................ 136
11.2. Using the Long Syntax ............................................................................................................. 136
11.3. Catching Getopt Exceptions ...................................................................................................... 137
11.4. Using getOption() .................................................................................................................... 138
11.5. Using __get() and __isset() Magic Methods ................................................................................. 138
11.6. Using getRemainingArgs() ........................................................................................................ 138
11.7. Using addRules() ..................................................................................................................... 139
11.8. Using setHelp() ....................................................................................................................... 139
11.9. Using setAliases() .................................................................................................................... 140
11.10. Using addArguments() and setArguments() ................................................................................ 140
11.11. Using setOption() .................................................................................................................. 141
11.12. Using setOptions() ................................................................................................................. 141
12.1. Manejando Acciones No Existentes ............................................................................................ 185
12.2. Agregando una Tarea Usando Nombres de Acción, Controllador y Módulo ........................................ 189
12.3. Agregando una Tarea al Objeto Solicitud (Request) ....................................................................... 190
12.4. AutoCompletion con Dojo Usando Zend MVC ............................................................................. 192
12.5. Permitiendo a las Acciones Responder a Requerimientos Ajax ......................................................... 201
12.6. Estableciendo Opciones ............................................................................................................ 205
xxxv
Guía de Referencia del Programador
xxxvi
Guía de Referencia del Programador
xxxvii
Guía de Referencia del Programador
xxxviii
Guía de Referencia del Programador
xxxix
Guía de Referencia del Programador
xl
Guía de Referencia del Programador
xli
Guía de Referencia del Programador
xlii
Guía de Referencia del Programador
xliii
Guía de Referencia del Programador
xliv
Guía de Referencia del Programador
xlv
Guía de Referencia del Programador
xlvi
Guía de Referencia del Programador
xlvii
Guía de Referencia del Programador
xlviii
Guía de Referencia del Programador
xlix
Guía de Referencia del Programador
l
Guía de Referencia del Programador
B.8. Changes for the hash validator from 1.6 to 1.7 ............................................................................. 1388
B.9. Changes for the imagesize validator from 1.6 to 1.7 ...................................................................... 1388
B.10. Changes for the size validator from 1.6 to 1.7 ............................................................................ 1389
B.11. How to change isLocale() from 1.6 to 1.7 .................................................................................. 1389
B.12. How to change getDefault() from 1.6 to 1.7 ............................................................................... 1390
B.13. Setting languages without getting notices ................................................................................... 1391
B.14. How to change your file validators from 1.6.1 to 1.6.2 ................................................................. 1392
F.1. Ejemplo: Optimized include_path ............................................................................................... 1430
li
Capítulo 1. Introducción a Zend
Framework
Descripción general
Zend Framework (ZF) es un framework de código abierto para desarrollar aplicaciones web y servicios web con PHP5.
ZF es una implementación que usa código 100% orientado a objetos. La estructura de los componentes de ZF es algo
único; cada componente está construido con una baja dependencia de otros componentes. Esta arquitectura débilmente
acoplada permite a los desarrolladores utilizar los componentes por separado. A menudo se refiere a este tipo de diseño
como "use-at-will" (uso a voluntad).
Aunque se pueden utilizar de forma individual, los componentes de la biblioteca estándar de Zend Framework
conforman un potente y extensible framework de aplicaciones web al combinarse. ZF ofrece un gran rendimiento y
una robusta implementación MVC, una abstración de base de datos fácil de usar, y un componente de formularios que
implementa la prestación de formularios HTML, validación y filtado para que los desarrolladores puedan consolidar
todas las operaciones usando de una manera sencilla la interfaz orientada a objetos. Otros componentes, como
Zend_Auth y Zend_Acl, proveen autentificación de usuarios y autorización diferentes a las tiendas de certificados
comunes (REVISAR ESTO). También existen componentes que implementan bibliotecas de cliente para acceder de
forma sencilla a los web services más populares. Cualesquiera que sean las necesidades de su solicitud, usted tiene todas
las posibilidades de encontrar un componente de Zend Framework que se pueda utilizar para reducir drásticamente el
tiempo de desarrollo, con una base completamente sólida.
El principal patrocinador del proyecto Zend Framework es Zend Technologies [http://www.zend.com], pero muchas
empresas han contribuido con componentes o características importantes para el marco. Empresas como Google,
Microsoft y StrikeIron se han asociado con Zend para proporcionar interfaces de servicios web y otras tecnologías que
desean poner a disposición de los desarrolladores de Zend Framework.
Zend Framework no podría haber proporcionado y apoyado todas estas características sin la ayuda de la vibrante
comunidad de ZF. Los miembros de la comunidad, incluidos los contribuyentes, están disponibles en las , listas de
correo [http://framework.zend.com/archives], canales de IRC [http://www.zftalk.com], y en otros foros. Cualquier
duda que tenga acerca de ZF, la comunidad está siempre disponible para responder.
Instalación
Zend Framework requiere por lo menos PHP 5.1.4 o superior, aunque Zend recomienda encarecidamente la versión
5.2.3 o superior, porque hay parches de seguridad y mejoras en el rendimiento entre estas dos versiones. Por favor,
consulte el anexo sobre los requisitos del sistema. para obtener más información.
La instalación del Zend Framework es muy simple. Una vez que haya descargado y descomprimido el framework,
deberá añadir la carpeta /library de la distribución al principio de su "include path". También puede mover la
carpeta "library" a cualquier otra posición (compartida o no) de su sistema de archivos.
• Download the latest nightly snapshot. [http://framework.zend.com/download/snapshot] For those who would brave
the cutting edge, the nightly snapshots represent the latest progress of Zend Framework development. Snapshots are
bundled with documentation either in English only or in all available languages. If you anticipate working with the
latest Zend Framework developments, consider using a Subversion (SVN) client.
• Using a Subversion [http://subversion.tigris.org] (SVN) client. Zend Framework is open source software, and the
Subversion repository used for its development is publicly available. Consider using SVN to get Zend Framework
1
Introducción a Zend Framework
if you already use SVN for your application development, want to contribute back to the framework, or need to
upgrade your framework version more often than releases occur.
The URL for the trunk of Zend Framework’s SVN repository is: http://framework.zend.com/svn/framework/
standard/trunk [http://framework.zend.com/svn/framework/standard/trunk]
Una vez que tenga disponible una copia de Zend Framework, su aplicación necesita poder acceder a
las clases del framework. Aunque hay diferentes maneras de lograr esto [http://www.php.net/manual/en/
configuration.changes.php], su include_path [http://www.php.net/manual/en/ini.core.php#ini.include-path] de
PHP necesita contener una ruta a la librería de Zend Framework.
Ya que los componentes de Zend Framework están débilmente conectados, tiene la opción de usar cualquier
combinación de ellos en sus aplicaciones. Los siguientes capítulos presentan una referencia exhaustiva de Zend
Framework, componente a componente.
2
Capítulo 2. Zend_Acl
Introducción
Zend_Acl provee la implementación de un sistema simple y flexible de Listas de Control de Acceso (ACL, por sus
siglas en inglés) para la administración de privilegios. En general, una aplicación puede utilizar las ACL para controlar
el acceso a ciertos objetos protegidos, que son requeridos por otros objetos.
En términos generales, Los roles solicitan acceso a los recursos . Por ejemplo, si una persona solicita acceso a un
automóvil, entonces la persona se convierte en el rol solicitante, y el automóvil en el recurso, puesto que el acceso al
automóvil puede no estar disponible a cualquiera.
A través de la especificación y uso de Listas de Control de Acceso (ACL), una aplicación puede controlar cómo los
objetos solicitantes (roles) han obtenido acceso a objetos protegidos (recursos).
Zend_Acl provee un estructura de árbol a la cual pueden ser agregados múltiples recursos (o "Áreas con Controles
de Acceso").Ya que los recursos son almacenados en esta estructura de árbol, estos pueden ser organizados desde
lo general (hacia la raíz del árbol) a lo específico (hacia las ramas del árbol). Consultas sobre un recurso específico
buscarán automáticamente, en la jerarquía del recurso, reglas asignadas a recursos anteriores a los que el recurso
actual haga referencia, permitiendo la herencia simple de reglas. Por ejemplo, si una regla por defecto se aplica a cada
edificio en una ciudad, uno simplemente podría asignar la regla a la ciudad, en lugar de asignar la misma regla a cada
edificio. Algunos edificios pueden necesitar excepciones a la regla, sin embargo, y esto es fácil de hacer en Zend_Acl
asignando esta excepción a cada edificio que necesite una excepción a la regla. Un recurso sólo puede heredar de un
recurso padre, aunque este recurso padre puede tener a la vez su propio recurso padre, y así; sucesivamente.
En Zend_Acl, un Rol puede heredar de otro o más roles. Esto es para soportar herencia de reglas entre roles. Por
ejemplo, un Rol de usuario, como "sally", puede estar bajo uno o más roles padre, como "editor" y "administrador".
El desarrollador puede asignar reglas a "editor" y "administrador" por separado, y "sally" puede heredar tales reglas
de ambos, sin tener que asignar reglas directamente a "sally".
3
Zend_Acl
Dado que la habilidad de herencia desde múltiples roles es muy útil, múltiples herencias también introduce cierto grado
de complejidad. El siguiente ejemplo ilustra la condición de ambiguedad y como Zend_Acl soluciona esto.
require_once 'Zend/Acl.php';
$acl = new Zend_Acl();
require_once 'Zend/Acl/Role.php';
$acl->addRole(new Zend_Acl_Role('invitado'))
->addRole(new Zend_Acl_Role('miembro'))
->addRole(new Zend_Acl_Role('admin'));
require_once 'Zend/Acl/Resource.php';
$acl->add(new Zend_Acl_Resource('unRecurso'));
$acl->deny('invitado', 'unRecurso');
$acl->allow('miembro', 'unRecurso');
Ya que no hay reglas específicamente definidas para el rol "unUsuario" y "unRecurso", Zend_Acl debe buscar por
reglas que puedan estar definidas para roles "unUsuario" hereda. Primero, el rol "admin" es visitado, y no hay regla
de acceso definida para éste. Luego, el rol "miembro" es visitado, y Zend_Acl encuentra que aquí hay una regla
especificando que "miembro" tiene permiso para acceder a "unRecurso".
Así, Zend_Acl va a seguir examinando las reglas definidas para otros roles padre, sin embargo, encontraría que
"invitado" tiene el acceso denegado a "unRecurso". Este hecho introduce una ambigüedad debido a que ahora
"unUsuario" está tanto denegado como permitido para acceder a "unRecurso", por la razón de tener un conflicto de
reglas heredadas de diferentes roles padre.
Zend_Acl resuelve esta ambigüedad completando la consulta cuando encuentra la primera regla que es directamente
aplicable a la consulta. En este caso, dado que el rol "miembro" es examinado antes que el rol "invitado", el código
de ejemplo mostraría "permitido".
Nota
Cuando se especifican múltiples padres para un Rol, se debe tener en cuenta que el último padre listado es el
primero en ser buscado por reglas aplicables para una solicitud de autorización.
4
Zend_Acl
varias escalas de grupos sobre una amplia variedad de áreas. Para crear un nuevo objeto ACL, iniciamos la ACL sin
parámetros:
require_once 'Zend/Acl.php';
Nota
Hasta que un desarrollador especifique una regla"permitido", Zend_Acl deniega el acceso a cada privilegio
sobre cada recurso para cada rol.
Registrando Roles
El Sistema de Administración de Contenido (CMS) casi siempre necesita una jerarquía de permisos para determinar
la capacidad de identificación de sus usuarios. Puede haber un grupo de 'Invitados' para permitir acceso limitado
para demostraciones, un grupo de 'Personal' para la mayoría de usuarios del CMS quienes realizan la mayor parte de
operaciones del día a día, un grupo 'Editores' para las responsabilidades de publicación, revisión, archivo y eliminación
de contenido, y finalmente un grupo 'Administradores' cuyas tareas pueden incluir todas las de los otros grupos y
también el mantenimiento de la información delicada, manejo de usuarios, configuración de los datos básicos y su
respaldo/exportación. Este grupo de permisos pueden ser representados en un registro de roles, permitiendo a cada
grupo heredar los privilegios de los grupos 'padre', al igual que proporcionando distintos privilegios solo para su grupo
individual. Los permisos pueden ser expresados como:
Para este ejemplo, se usa Zend_Acl_Role , pero cualquier objeto que implemente
Zend_Acl_Role_Interface es admisible. Estos grupos pueden ser agregados al registro de roles de la siguiente
manera:
require_once 'Zend/Acl.php';
5
Zend_Acl
Consecuentemente, podemos definir un grupo razonablemente complejo de reglas con un mínimo de código. Para
aplicar estos permisos básicos como están definidos arriba:
require_once 'Zend/Acl.php';
require_once 'Zend/Acl/Role.php';
// Personal hereda el privilegio de ver de invitado, pero también necesita privilegios adic
$acl->allow('personal', null, array('editar', 'enviar', 'revisar'));
El valor NULL en las llamadas de allow() es usado para indicar que las reglas de permiso se aplican a todos los
recursos.
6
Zend_Acl
Consultando la ACL
Ahora tenemos una ACL flexible que puede ser usada para determinar qué solicitantes tienen permisos para realizar
funciones a través de la aplicación web. Ejecutar consultas es la forma más simple de usar el método isAllowed() :
echo $acl->isAllowed('administrador') ?
"permitido" : "denegado";
// permitido porque administrador tiene permitidos todos los privilegios
Para el CMS del ejemplo se ha determinado que, si bien el grupo 'staff' cubre las necesidades de la gran mayoría de
usuarios, hay una necesidad de un nuevo grupo 'marketing' que requiere el acceso al boletín de noticias y las últimas
noticias en el CMS. El grupo es bastante auto suficiente y tendrá la capacidad de publicar y de archivar los boletines
de noticias y las últimas noticias.
Primero revisamos el registro del rol para reflejar estos cambios. Hemos determinado que el grupo 'marketing' tiene
los mismos permisos básicos que 'staff', así que definimos 'marketing' de tal manera que herede los permisos de 'staff':
7
Zend_Acl
A continuación, la nota que por encima de los controles de acceso se refieren a recursos específicos (por ejemplo,
"boletín informativo", "últimas noticias", "anuncio de noticias"). Ahora añadimos estos recursos:
// news
$acl->addResource(new Zend_Acl_Resource('news'));
// Últimas Noticias
$acl->addResource(new Zend_Acl_Resource('latest'), 'news');
// anuncio de noticias
$acl->addResource(new Zend_Acl_Resource('announcement'), 'news');
Entonces es simplemente una cuestión de la definición de estas normas más específicas en ámbitos de la ACL:
//
Marketing debe ser capaz de archivar y publicar boletines informativos y
// las últimas noticias
$acl->allow('marketing',
array('newsletter', 'latest'),
array('publish', 'archive'));
8
Zend_Acl
// denegado
Los privilegios pueden ser modificados de manera incremental como se ha indicado anteriormente, pero un valor NULL
para los privilegios anula tales cambios incrementales:
9
Zend_Acl
$acl->allow('marketing', 'latest');
Uso Avanzado
Almacenamiento Permanente de los Datos ACL
Zend_Acl fue diseñado de tal manera que no requiere ninguna tecnología particular como bases de datos o un servidor
de cache para el almacenamiento de datos ACL. Al poseer una implementación completamente construida en PHP, es
posible construir herramientas de administración personalizadas sobre Zend_Acl con relativa facilidad y flexibilidad.
En muchas situaciones se requiere alguna forma de mantenimiento interactivo de una ACL, y Zend_Acl provee
métodos para configurar, y consultar, los controles de acceso de una aplicación.
El almacenamiento de los datos ACL es una tarea que se delega al desarrollador, puesto que la utilización variará
extensamente en distintas situaciones. Dado que Zend_Acl es serializable, los objetos ACL pueden serializarse con
la función serialize() [http://php.net/serialize] de PHP, y los resultados pueden ser almacenados donde sea que
el desarrollador lo desee, en un archivo, base de datos, o mecanismo de cache
Zend_Acl provee soporte para reglas condicionales con Zend_Acl_Assert_Interface . Con el fin de utilizar
la regla de aserción de la interfaz, un desarrollador escribe una clase que implemente el método assert() de la
interfaz:
10
Zend_Acl
{
// ...
}
}
Una vez la clase de aserción esta disponible, el desarrollador puede suministrar una instancia de la clase de aserción
cuando asigna reglas condicionales. Una regla que es creada con una aserción sólo se aplica cuando el método de la
aserción devuelve TRUE.
El código anterior crea una regla condicional que permite el acceso a todos los privilegios sobre todo, por todo el
mundo, excepto cuando la IP de quien hace la petición está en la "lista negra". Si una petición viene desde una IP que
no está considerada "limpia", entonces la regla no se aplica. Dado que la regla se aplica a todos los roles, todos los
recursos, y todos los privilegios, una IP "no limpia" daría lugar a una negación de acceso. Éste es un caso especial,
sin embargo, y debería ser entendido que en todos los otros casos (por ejemplo, cuando un rol específico, recurso, o
privilegio está especificado por la regla), una aserción fallida provoca que la regla no se aplique, y otras reglas deberían
ser usadas para determinar si el acceso está permitido o denegado.
El método assert() de un objeto aserción es pasado a la ACL, regla, recurso, y privilegio para el cual una consulta
de autorización (por ejemplo, isAllowed() ) se aplica, con el fin de proporcionar un contexto para que la clase de
aserción determine sus condiciones cuando fuera necesario.
11
12
Capítulo 3. Zend_Amf
Introducción
Zend_Amf provee apoyo para el Formato de Mensajes de ActionScript Action Message Format [http://
en.wikipedia.org/wiki/Action_Message_Format] (AMF) de Adobe, que permite la comunicación entre Adobe Flash
Player [http://en.wikipedia.org/wiki/Adobe_Flash_Player] y PHP. Específicamente, proporciona una aplicación para
un servidor gateway que tramita las solicitudes enviadas desde Flash Player al servidor, mapeando estos requerimientos
al objeto y a sus métodos de clase, como así también a llamadas arbitrarias de comunicación.
Zend_Amf_Server
Zend_Amf_Server proporciona un servidor al estilo RPC para tramitar solicitudes hechas desde Adobe Flash Player
utilizando el protocolo AMF. Al igual que todas las clases de servidor, Zend Framework sigue la API de SoapServer,
proporcionando una interfaz para crear servidores fácil de recordar.
Asumamos que ha creado la clase Foo con una variedad de métodos públicos. Usando el siguiente código, puede
crear un servidor AMF:
Alternativamente, en su lugar puede elegir agregar una función simple como llamada de retorno:
También puede combinar y examinar la identidad de varias clases y funciones. Al hacerlo, sugerimos darle un espacio
de nombres a cada una para garantizar que no ocurran colisiones entre nombres de métodos; puede hacerse simplemente
pasando una segunda cadena de argumentos para cualquier addFunction() o setClass():
13
Zend_Amf
El Zend_Amf_Server también permite cargar servicios dinámicamente, en función de una ruta de directorio ya
suministrada. Puede añadir al servidor tantos directorios como desee. El orden en que se añadan los directorios al
servidor será el orden en que se realizarán las búsquedas LIFO en los directorios para coincidir con la clase. El método
addDirectory() realiza la acción de añadir directorios.
$servidor->addDirectory(dirname(__FILE__) .'/../services/');
$servidor->addDirectory(dirname(__FILE__) .'/../package/');
Cuando se llama a servicios remotos, los nombres de los directorios que contengan las fuentes pueden tener los
delimitadores guión bajo ("_") y el punto ("."). Cuando se utilize un guión bajo ("_") tanto en PEAR como en Zend
Framework, se respetarán los nombres de clases de acuerdo a las convenciones de nomenclatura. Esto significa que si
usted llama al servicio com_Foo_Bar el servidor buscará el archivo Bar.php en cada una de las rutas incluidas en
com/Foo/Bar.php. Si se usa la notación punto para su servicio remoto como com.Foo.Bar cada ruta incluida
deberá tener com/Foo/Bar.php agregado al final para autocargar Bar.php
Todos las solicitudes AMF enviadas al script serán manejadas por el servidor, y este devolverá una respuesta AMF.
/**
* @param string $nombre
* @param string $saludo
* @return string
*/
function holaMundo($ombre, $saludo = 'Hola')
{
return $saludo . ', ' . $nombre;
}
// Clase agregada
class Mundo
{
/**
* @param string $nombre
* @param string $saludo
* @return string
*/
public function hola($nombre, $saludo = 'Hola')
{
return $saludo . ', ' . $nombre;
}
}
14
Zend_Amf
Por ejemplo, digamos que ya ha creado su servidor y lo ha puesto en el fichero server.php en el directorio raíz
(root) de su aplicación, por lo tanto la URI es http://example.com/server.php. En este caso, usted debería
modificar su fichero service-config.xml poniendo este valor como atributo al punto final del canal uri.
Si nunca ha creado un fichero service-config.xml puede hacerlo abriendo su proyecto en la ventana del
navegador. Haga clic derecho sobre el nombre del proyecto y seleccione 'properties' (propiedades). En el cuadro de
diálogo 'properties' del proyecto ir al menú ‘Flex Build Path' (Crear ruta Flex), luego en la pestaña ‘Library path’ (ruta
de biblioteca) asegúrese de que el fichero 'rpc.swc' sea añadido a su ruta de proyectos y pulse Ok (Aceptar) para
cerrar la ventana.
También necesitará indicarle al compilador que debe usar service-config.xml para encontrar el punto final
de RemoteObject. Para hacerlo, abra de nuevo el panel de propiedades de su proyecto haciendo clic en el botón
derecho sobre el proyecto en la carpeta del navegador y seleccione 'properties' (propiedades). Ahora seleccione ‘Flex
Compiler' (Compilador Flex) y añada la cadena: -services "services-config.xml". Presione 'Apply' (Aplicar) y luego
en OK para volver a actualizar la opción. Lo que acaba de hacer es decirle al compilador Flex que busque en el fichero
services-config.xml aquellas variables que se usarán en tiempo de ejecución por la clase RemotingObject.
Ahora, para conectarnos a nuestros métodos remotos debemos indicarle a Flex qué fichero de configuración de
servicios utilizar. Por esta razón creamos un nuevo fichero 'services-config.xml' en la carpeta src del
proyecto Flex. Con click derecho sobre el proyecto y seleccionando 'new'(nuevo) 'File' (fichero), se abrirá una nueva
ventana. Seleccione la carpeta del proyecto y luego nombre el archivo 'services-config.xml' y presione
'finish' (finalizar).
Flex ha creado y abierto el nuevo fichero services-config.xml. Utilice el siguiente texto de ejemplo para
su fichero services-config.xml. Asegúrese de actualizar su punto final para que concuerde con el servidor.
Asegúrese también de guardar el fichero.
15
Zend_Amf
</channel-definition>
</channels>
</services-config>
Hay dos puntos clave en el ejemplo. En primer lugar, pero último en el listado, creamos un canal AMF, y especificamos
el punto final como la URL a nuestro Zend_Amf_Server:
<channel-definition id="zend-endpoint"
<endpoint uri="http://example.com/server.php"
class="flex.messaging.endpoints.AMFEndpoint"/>
</channel-definition>
Advierta que a este canal le hemos dado un identificador, "zend-endpoint". El ejemplo crea un servicio cuyo destino
hace referencia a este canal, asignándole también un ID, en este caso es "zend".
Dentro de nuestros ficheros Flex MXML, necesitamos vincular un RemoteObject al servicio. En MXML, esto podría
hacerse así:
<mx:RemoteObject id="myservice"
fault="faultHandler(event)"
showBusyCursor="true"
destination="zend">
Aquí, hemos definido un nuevo objeto remoto identificado por "myservice" vinculado destino de servicio "zend" que
hemos definido en el fichero services-config.xml. Entonces invocamos sus métodos en nuestro ActionScript
simplemente llamando a "myservice.<method>". . A modo de ejemplo:
myservice.hello("Wade");
myservice.world.hello("Wade");
Para más información sobre como invocar a Flex RemoteObject visite el sitio de ayuda de Adobe Flex 3 en:http://
livedocs.adobe.com/flex/3/html/help.html?content=data_access_4.html.
Manejo de errores
Por defecto, todas las excepciones producidas en sus clases o funciones adjuntas serán capturados y devueltas como
mensajes de error de AMF (AMF ErrorMessages). Sin embargo, el contenido de estos objetos de mensajes de error
variará dependiendo de si el servidor está o no en modo "producción" (el estado por defecto).
Cuando se está en modo de producción, únicamente el código de excepción será devuelto. Si desactiva el modo de
producción, algo que debe hacerse sólo para probar -- serán devueltos más detalles de la excepción: el mensaje de
excepción (error), línea y backtrace serán adjuntados.
$server->setProduction(false);
16
Zend_Amf
$server->setProduction(true);
Un área en la que se debe tener especialmente mucho cuidado son los errores propios de PHP. Cuando la directiva
INI display_errors está habilitada, los errores de PHP de cualquier nivel del reporte actual serán pasados directamente
a la salida, y potencialmente se podrían hacer estragos con las respuestas de AMF. Para prevenir estos problemas,
sugerimos deshabilitar la directiva display_errors cuando se está en modo de producción.
Respuestas de AMF
En ocasiones es posible que quiera manipular ligeramente el objeto respuesta, es bastante usual querer devolver algunas
cebeceras de mensajes adicionales. Puede hacerlo mediante el método del servidor handle() que devuelve el objeto
respuesta.
$respuesta = $servidor->handle();
$respuesta->addAmfHeader(new Zend_Amf_Value_MessageHeader('foo', true, 'bar'))
echo $respuesta;
Objetos tipados
Similarmente a SOAP, AMF permite pasar objetos entre cliente y servidor. Esto le da una gran flexibilidad y coherencia
a ambos entornos.
• Primero, usted puede crear uniones explícitas a nivel del servidor, utilizando el método setClassMap(). El
primer argumento es el nombre de la clase de ActionScript, el segundo es el nombre de la clase PHP que lo mapea:
• Segundo, en su clase PHP puede ajustar la propiedad como pública mediante $_explicitType, con el valor
representativo de la clase ActionScript que mapear:
class Contact
{
17
Zend_Amf
• Tercero, en un sentido similar, puede definir como público el método getASClassName() dentro de su clase.
Este método debe devolver la clase ActionScript apropiada:
class Contact
{
public function getASClassName()
{
return 'ContactVO';
}
}
Aunque hemos creado ContactVO en el servidor, ahora tenemos que hacer su clase correspondiente en AS3 para que
el servidor pueda mapear el objeto.
Haga clic derecho sobre la carpeta src del proyecto Flex y seleccione New -> ActionScript File. Nombre el fichero
como ContactVO y pulse 'finish' (finalizar) para verlo. Copie el siguiente código en el fichero para terminar de crear
la clase.
package
{
[Bindable]
[RemoteClass(alias="ContactVO")]
public class ContactVO
{
public var id:int;
public var firstname:String;
public var lastname:String;
public var email:String;
public var mobile:String;
public function ProductVO():void {
}
}
}
La clase es sintácticamente equivalente a la de PHP del mismo nombre. Los nombres de variables son exactamente los
mismos y necesitan estar en el mismo contenedor para trabajar correctamente. Hay dos meta tags AS3 únicos en esta
clase. El primero es vinculable y dispara un evento cuando es actualizada. El segundo es el tag RemoteClass y define
que esta clase puede tener mapeado un objeto remoto con un nombre de alias, en este caso ContactVO Es obligatorio
que en esta etiqueta(tag), el valor que se estableció es la clase PHP sea estrictamente equivalente.
[Bindable]
private var myContact:ContactVO;
El siguiente resultado del evento debido a la llamada de servicio, se incorporó instantáneamente a ContactVO de Flex.
Cualquier cosa que esté ligada a myContact será actualizada con los datos retornados por ContactVO.
18
Zend_Amf
Recursos
Zend_Amf provides tools for mapping resource types returned by service classes into data consumable by
ActionScript.
In order to handle specific resource type, the user needs to create a plugin class named after the resource name, with
words capitalized and spaces removed (so, resource type "mysql result" becomes MysqlResult), with some prefix, e.g.
My_MysqlResult. This class should implement one method, parse(), receiving one argument - the resource -
and returning the value that should be sent to ActionScript. The class should be located in the file named after the last
component of the name, e.g. MysqlResult.php.
The directory containing the resource handling plugins should be registered with Zend_Amf type loader:
Zend_Amf_Parse_TypeLoader::addResourceDirectory(
"My",
"application/library/resources/My"
));
For detailed discussion of loading plugins, please see the plugin loader section.
Default directory for Zend_Amf resources is registered automatically and currently contains handlers for "mysql
result" and "stream" resources.
Trying to return unknown resource type (i.e., one for which no handler plugin exists) will result in an exception.
Abra Flash CS y cree un nuevo fichero Flash (ActionScript 3). Nombre al documento como ZendEjemplo.fla y
guárdelo en una carpeta que utilizará para este ejemplo. Cree una nuevo fichero AS3 en el mismo directorio y llámelo
19
Zend_Amf
Main.as. Abra ambos ficheros con su editor. Ahora vamos a conectar las dos ficheros a través de la clase documento.
Seleccione ZendEjemplo y haga clic en el escenario. Desde el panel del escenario cambie la propiedad de la clase
Document a Main. Esto vincula al fichero Main.as con la interfaz de usuario enZendEjemplo.fla Cuando ejecute
el fichero ZendEjemplo de Flash se ejecutará ahora la clase Main.as El paso siguiente será añadir ActionScript para
hacer una lamada AMF.
Ahora vamos a hacer una clase Main(principal) para que podamos enviar los datos al servidor y mostrar el resultado.
Copie el código siguiente en su fichero Main.as y luego vamos a recorrer el código para describir cuál es el papel
de cada elemento.
package {
import flash.display.MovieClip;
import flash.events.*;
import flash.net.NetConnection;
import flash.net.Responder;
Primero tenemos que importar dos bibliotecas de ActionScript que realizan la mayor parte del trabajo. La primera es
NetConnection que actúa como un tubo bidireccional entre el cliente y el servidor. La segunda es un objeto Responder
que maneja los valores de retorno desde el servidor, y que están relacionados con el éxito o el fracaso de la llamada.
import flash.net.NetConnection;
import flash.net.Responder;
En la clase necesitaremos tres variables para representar a NetConnection, Responder, y la URL del gateway a nuestra
instalación Zend_Amf_Server.
20
Zend_Amf
La función onComplete se ejecuta tan pronto como la construcción ha concluido, enviando los datos al servidor.
Necesitamos añadir una línea más que hace una llamada a la función Zend_Amf_Server Mundo->hola.
Cuando creamos la variable responder hemos definido las funciones onResult y onFault para manejar la respuesta
proveniente del servidor. Hemos añadido la función OnResult para el resultado exitoso desde el servidor. Cada vez
que se ejecuta apropiadamente el manejo de conexión con el servidor, el manejador de eventos llama esta función.
La función onFault, se llama si hubo una respuesta nula desde el servidor. Esto ocurre cuando hay un error en el
servidor, la URL al servidor es inválida, el servicio remoto o método no existe o cualquier otra cuestión relacionada
con la conexión.
La inclusión de ActionScript para realizar la conexión remota ha finalizado. Al ejecutar el fichero ZendEjemplo, se
establece una conexión con Zend_Amf. En resumen, se han añadido las variables requeridas para abrir una conexión
con el servidor remoto, se han definido qué métodos se deben utilizar cuando su aplicación recibe una respuesta desde
el servidor, y finalmente se han mostrado los datos de salida devueltos a través de trace().
Authentication
Zend_Amf_Server allows you to specify authentication and authorization hooks to control access to the services.
It is using the infrastructure provided by Zend_Auth and Zend_Acl components.
In order to define authentication, the user provides authentication adapter extening Zend_Amf_Auth_Abstract
abstract class. The adapter should implement the authenticate() method just like regular authentication adapter.
The adapter should use properties _username and _password from the parent Zend_Amf_Auth_Abstract class
in order to authenticate. These values are set by the server using setCredentials() method before call to
authenticate() if the credentials are received in the AMF request headers.
The identity returned by the adapter should be an object containing property role for the ACL access control to work.
21
Zend_Amf
If the authentication result is not successful, the request is not proceseed further and failure message is returned with
the reasons for failure taken from the result.
$server->setAuth(new My_Amf_Auth());
If the ACL object is set, and the class being called defines initAcl() method, this method will be called with the
ACL object as an argument. The class then can create additional ACL rules and return TRUE, or return FALSE if no
access control is required for this class.
After ACL have been set up, the server will check if access is allowed with role set by the authentication, resource
being the class name (or NULL for function calls) and privilege being the function name. If no authentication was
provided, then if the anonymous role was defined, it will be used, otherwise the access will be denied.
22
Capítulo 4. Zend_Application
Introducción
Zend_Application facilita la inicialización de nuestras aplicaciones proporcionando recursos reutilizables,
comunes y asociados con el módulo de la clase de arranque y control de dependencias. También se ocupa de establecer
el entorno de PHP e introduce la autocarga (autoloading) por defecto.
Si se planea utilizar Zend_Tool para crear su proyecto, continúe leyendo. Si va añadir Zend_Application a
un proyecto existente, debe ir aquí.
Usando Zend_Tool
La forma más rápida para comenzar a utilizar Zend_Application es usar Zend_Tool para generar su proyecto.
Esto también creará su clase y archivo Bootstrap.
newproject
|-- application
| |-- Bootstrap.php
| |-- configs
| | `-- application.ini
| |-- controllers
| | |-- ErrorController.php
| | `-- IndexController.php
| |-- models
| `-- views
| |-- helpers
| `-- scripts
| |-- error
| | `-- error.phtml
| `-- index
| `-- index.phtml
23
Zend_Application
|-- library
|-- public
| `-- index.php
`-- tests
|-- application
| `-- bootstrap.php
|-- library
| `-- bootstrap.php
`-- phpunit.xml
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
Todos las opciones en este archivo de configuración son para su uso con Zend_Application y su bootstrap.
<?php
// Define el path al directorio de la aplicación
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH',
realpath(dirname(__FILE__) . '/../application'));
24
Zend_Application
|| define('APPLICATION_ENV',
(getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
: 'production'));
/** Zend_Application */
require_once 'Zend/Application.php';
Para continuar con el inicio rápido, por favor vaya a la sección de Recursos.
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
}
Ahora, debe crear su configuración. Para este tutorial, usaremos una configuración de estilo INI; puede, por supuesto,
usar un archivo de configuración XML o PHP. Cree el archivo application/configs/application.ini,
proporcionando el siguiente contenido.
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
25
Zend_Application
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
Ahora, modifiquemos su script de entrada (gateway), public/index.php. Si el archivo no existe, cree uno; de lo
contrario, sustitúyalo por el siguiente contenido:
<?php
// Define la ruta al directorio de la aplicación
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH',
realpath(dirname(__FILE__) . '/../application'));
/** Zend_Application */
require_once 'Zend/Application.php';
Se puede observar que el valor constante del entorno de la aplicación busca una variable de entorno
"APPLICATION_ENV". Recomendamos este ajuste en el entorno de su servidor web. En Apache, puede también
configurarlo en la definición de su vhost, o en su archivo .htaccess. Recomendamos el siguiente contenido para
su archivo public/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
26
Zend_Application
En este punto, está listo para comenzar a usar las ventajas de Zend_Application.
En esta sección, veremos como añadir dos recursos a su aplicación. En primer lugar, estableceremos los layouts, y
luego vamos a personalizar sus objetos de la vista.
Uno de los recursos estándar provistos con Zend_Application es el recurso "layout". Este recurso espera que
definas valores de configuración, los que entonces utilizará para configurar su instancia Zend_Layout.
Para usarlo, todo lo que tenemos que hacer es actualizar el archivo de configuración.
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
; ADD THE FOLLOWING LINES
resources.layout.layout = "layout"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
27
Zend_Application
<head>
<?php echo $this->headTitle() ?>
<?php echo $this->headLink() ?>
<?php echo $this->headStyle() ?>
<?php echo $this->headScript() ?>
</head>
<body>
<?php echo $this->layout()->content ?>
</body>
</html>
Ahora, vamos añadir una vista de recurso personalizada. Cuando se inicializa la vista, queremos establecer el DocType
HTML y un valor predeterminado del título para utilizarlo en la cabecera de HTML. Esto puede llevarse a cabo
editando su clase Bootstrap para agregar un método.
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{ protected function _initView()
{
// Inicializar la vista
$view = new Zend_View();
$view->doctype('XHTML1_STRICT');
$view->headTitle('My First Zend Framework Application');
// Añadir al ViewRenderer
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer'
);
$viewRenderer->setView($view);
return $view;
}
}
Este método se ejecuta automáticamente cuando arranca la aplicación, y se asegurará que su vista sea inicializada
según las necesidades de su aplicación.
Teoría de Operación
Obtener una aplicación MVC configurada y lista para funcionar requiere de un porcentaje cada vez mayor de código
que disponga de más características, tales como: Establecer la base de datos, configurar la vista y los ayudantes(helpers)
de vistas, configurar los layouts, registro de plugins, registro de ayudantes de acción (action helpers), y mucho más.
28
Zend_Application
Además, a menudo deseará reutilizar el mismo código para arrancar sus pruebas, un cronjob, o un servicio en linea de
comandos. Si bien es posible incluir simplemente su script bootstrap, a menudo hay inicializaciones que son específicas
del entorno, puede que no necesite el MVC para un cronjob, o simplemente la capa de DB para un servicio script.
Zend_Application pretende hacer esto más fácil y promover la reutilización mediante el encapsulamiento del
bootstraping en paradigmas de OOP.
• Zend_Application_Resource provee una interfaz para recursos estandar de bootstrap que pueden ser
cargados por demanda mediante una instancia bootstrap, así como implementaciones de varios recursos por defecto.
• El entorno actual
Las opciones de bootstrap incluyen la ruta hacia el archivo que contiene la clase bootstrap y opcionalmente:
Las opciones puden ser una array, un objeto Zend_Config, o la ruta a un archivo de configuración.
Bootstrapping
La segunda área de responsabilidad de Zend_Application es ejecutar la solicitud del bootstrap. Los bootstraps
necesitan mínimamente implementar Zend_Application_Bootstrap_Bootstrapper, la que define la
siguiente API:
interface Zend_Application_Bootstrap_Bootstrapper
29
Zend_Application
{
public function __construct($application);
public function setOptions(array $options);
public function getApplication();
public function getEnvironment();
public function getClassResources();
public function getClassResourceNames();
public function bootstrap($resource = null);
public function run();
}
Esta API permite aceptar al bootstrap en el entorno y la configuración desde el objeto de la aplicación, informa la
responsabilidad de los recursos para los recursos bootstraping, luego hace el bootstrap y ejecuta la aplicación.
Además de esta funcionalidad, hay muchas otras áreas de incumbencia con las cuales debe familiarizarse.
Métodos Recursos
La implementación de Zend_Application_Bootstrap_BootstrapAbstract proporciona una simple
convención para definir métodos de recursos de clase. Cualquier método protegido cuyo nombre comience con un
prefijo _init será considerado un método de recurso.
Para arrancar un único método de recurso, utilizar el método bootstrap(), y pasarle el nombre del recurso. El
nombre será el nombre de método menos el prefijo _init.
Para arrancar varios métodos de recursos, pasar un array de nombres. Para bootstrap de todos los métodos de recursos,
no pasar nada.
30
Zend_Application
$bootstrap->bootstrap('foo');
$bootstrap->bootstrap(array('foo', 'bar));
Para arrancar todos los métodos de recursos, llame a bootstrap() sin argumentos:
$bootstrap->bootstrap();
Si su bootstrap debe ser capaz de utilizar recursos de plugins, necesitará implementar una interfaz adicional,
Zend_Application_Bootstrap_ResourceBootstrapper. Esta interfaz define una API para localizar,
registrar, y cargar recursos de plugins:
interface Zend_Application_Bootstrap_ResourceBootstrapper
{
public function registerPluginResource($resource, $options = null);
public function unregisterPluginResource($resource);
public function hasPluginResource($resource);
public function getPluginResource($resource);
public function getPluginResources();
public function getPluginResourceNames();
public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader);
public function getPluginLoader();
}
Básicamente los recursos de plugins ofrecen la posibilidad de crear recursos incializadores que puede ser re-utilizados
entre aplicaciones. Esto le permite mantener su actual bootstrap relativamente limpio, e introducir nuevos recursos sin
necesidad de tocar su propio arranque (bootstrap).
Zend_Application_Bootstrap_BootstrapAbstract (y
Zend_Application_Bootstrap_Bootstrap por extensión) implementan esta interfaz, que le permite utilizar
recursos de plugins.
Para utilizar recursos de plugins, debe especificarlos en las opciones que pasó al objeto aplicación y/o bootstrap.
Estas opciones pueden provenir de un archivo de configuración, o ser pasadas manualmente. Las opciones deberán
ser pares de clave/opción, representando con la clave el nombre del recurso. El nombre de recurso será el segmento
siguiente al prefijo de clase. Por ejemplo, los recursos que vienen con Zend Framework tienen el prefijo de clase
"Zend_Application_Resource_"; cualquier cosa que le siga después debe ser el nombre del recurso. Como por ejemplo,
31
Zend_Application
Esto indica que el recurso "Front Controller", debería ser utilizado, con las opciones especificadas.
Si usted comienza a escribir su propio recurso de plugin, o utilizar recursos de plugin de terceras partes, necesitará
decirle a su bootstrap donde encontrarlos. Internamente, el bootstrap utiliza Zend_Loader_PluginLoader, de
manera tal que sólo necesitará indicar el prefijo de la clase común como pares de path.
Supongamos por ejemplo, que usted tiene recursos de plugins personalizados en APPLICATION_PATH/
resources/ y que ellos comparten el prefijo de clase común My_Resource. Entonces, debería pasar esa
información al objeto aplicación de la siguiente manera:
Ahora usted está habilitado para utilizar los recursos de ese directorio.
Tal como los métodos de recursos, utilice el método bootstrap() para ejecutar recursos de plugins. También tal como
con los métodos de recursos, puede especificar bien un único recurso de plugin, múltiples plugins (vía un array), o
todos los plugins. Además, los puede combinar para ejecutar métodos de recursos.
// Ejecute uno:
$bootstrap->bootstrap('FrontController');
// Ejecute varios:
$bootstrap->bootstrap(array('FrontController', 'Foo'));
Registro de Recursos
Muchos, si no todos, sus métodos de recursos o plugins inicializarán objetos y, en muchos casos, estos objetos serán
necesarios en otros lugares de su aplicación. ¿Cómo se puede acceder a ellos?
32
Zend_Application
Para máxima flexibilidad, este registro es mencionado internamente como un "contenedor"; el único requisito es que
sea un objeto. Los recursos son luego registrados como propiedades nombrados después del nombre del recurso. Por
defecto, una instancia de Zend_Registry es utilizada, pero también puede especificar cualquier otro objeto que
desee. Los métodos setContainer() y getContainer() pueden ser utilizados para manipular el contenedor en
si mismo. getResource($resource) puede ser utilizado para recuperar un recurso determinado del contenedor,
y hasResource($resource) para verificar si el recurso ha sido efectivamente registrado.
return $view;
}
}
// Via el contenedor:
$container = $bootstrap->getContainer();
if (isset($container->view)) {
$view = $container->view;
}
Tenga en cuenta que el registro y el contenedor no es global. Esto significa que usted necesita acceso al bootstrap a fin
de recuperar recursos. Zend_Application_Bootstrap_Bootstrap proporciona cierta comodidad para ello:
durante las ejecución de run() se registra a sí mismo como el "Front Controller" en el parámetro del "bootstrap",
que permite buscarlo desde el router, despachador, plugins, y los contoladores de acción.
Como ejemplo, si quiere tener acceso a los recursos de la vista desde dentro de su controlador de acción, podría hacer
lo siguiente:
33
Zend_Application
Al mismo tiempo, algunos recursos puede depender de otros que están en ejecución. Para resolver estas dos cuestiones,
Zend_Application_Bootstrap_BootstrapAbstract proporciona un mecanismo simple pero eficaz para
la localización de dependencias.
Como se señaló anteriormente, todos los recursos -- ya sean métodos o plugins -- son arrancados llamando a
bootstrap($resource), dende $resource es el nombre de un recurso, un array de recursos, o si se dejó vacío,
indica que deberían ejecutarse todos los recursos.
Si un recurso depende de otro recurso, debe llamar a bootstrap() dentro de su código para garantizar que ese
recurso ha sido ejecutado. Las llamadas subsiguientes a él, serán ignoradas.
return $request;
}
}
Plugins de Recursos
Como se señaló anteriormente, una buena forma de crear recursos de bootstrap re-utilizables y a traspasar mucha de
su codificación a clases discretas es utilizar plugins de recursos. Si bien Zend Framework se entrega con una serie
de plugins de recursos, la intención es que los desarrolladores deberían escribir los suyos para encapsular sus propias
necesidades de inicialización.
Los recursos plugins solo necesitan implemetar Zend_Application_Resource_Resource, o más simple aún,
extenderse Zend_Application_Resource_ResourceAbstract. La interfaz básica es simplemente esto:
34
Zend_Application
interface Zend_Application_Resource_Resource
{
public function __construct($options = null);
public function setBootstrap(
Zend_Application_Bootstrap_Bootstrapper $bootstrap
);
public function getBootstrap();
public function setOptions(array $options);
public function getOptions();
public function init();
}
La interfaz define simplemente que un recurso plugin debe aceptar opciones para el constructor, tiene mecanismos
de establecer y recuperar opciones, mecanismos de establecer y recuperar el objeto bootstrap, y un método de
inicialización.
Como ejemplo, supongamos que tiene una vista común de inicialización que utiliza en sus aplicaciones. Usted tiene
un doctype común, CSS y JavaScript, y quiere se capaz de pasar desde un documento base el título via configuración.
Un recurso plugin tal podría ser como este:
$viewRenderer =
Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer'
);
$viewRenderer->setView($view);
35
Zend_Application
$this->_view = $view;
}
return $this->_view;
}
}
Minetrtas usted haya registrado el path del prefijo para este recurso de plugin, puede usarlo en su aplicación. Mejor
aún, ya que usa el cargador de plugin, usted está pasando por encima del recurso de plugin de la "View" que viene con
Zend Framework, se está asegurando así que usa el suyo en lugar del original.
Ejemplos
La propia clase Bootstrap suelen ser bastante mínima; a menudo, será simplemente un talón vacío ampliando la clase
base bootstrap:
; APPLICATION_PATH/configs/application.ini
[production]
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
[testing : production]
[development : production]
Sin embargo, si debiera ser necesaria un inicialización personalizada, usted tiene dos opciones. En primer lugar,
usted puede escribir métodos prefijados con _init para especificar códigos distintos de arranque. Estos métodos
pueden ser llamados por bootstrap(), y también pueden ser llamados como si fueran métodos públicos:
bootstrap<resource>(). Deben aceptar opcionalmente un array de opciones.
Si su método recurso devuelve un valor, será almacenado en un contenedor en el bootstrap. Esto puede ser útil
cuando diferentes recursos necesitan interactuar (como un recurso inyectándose a sí mismo en otro). Luego, el método
getResource() puede ser utilizado para recuperar esos valores.
El siguiente ejemplo muestra un recurso de método para inicializar el objeto solicitado. Hace uso del segimiento de
la dependencia (que depende del recurso Front Controller), obteniendo un recurso desde el bootstrap y devolver el
valor para almacenar en el bootstrap.
36
Zend_Application
$this->bootstrap('FrontController');
$front = $this->getResource('FrontController');
Nótese en este ejemplo la llamada a bootstrap(); esto asegura que el front controller ha sido inicializado antes de
llamar a este método. Esa llamada puede desencadenar tanto un recurso u otro método de la clase.
La otra opción es usar plugins de recursos, estos son objetos que realizan inicializaciones específicas, y pueden ser
especificados:
class My_Bootstrap_Resource_View
extends Zend_Application_Resource_ResourceAbstract
{
public function init()
{
$view = new Zend_View($this->getOptions());
Zend_Dojo::enableView($view);
$view->doctype('XHTML1_STRICT');
$view->headTitle()->setSeparator(' - ')->append('My Site');
$view->headMeta()->appendHttpEquiv('Content-Type',
'text/html; charset=utf-8');
$view->dojo()->setDjConfigOption('parseOnLoad', true)
->setLocalPath('/js/dojo/dojo.js')
->registerModulePath('../spindle', 'spindle')
->addStylesheetModule('spindle.themes.spindle')
->requireModule('spindle.main')
->disable();
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer'
37
Zend_Application
);
$viewRenderer->setView($view);
return $view;
}
}
Para decirle al bootstrap que utilice éste, se tendría que proporcionar ya sea el nombre de la clase del plugin del recurso,
o una combinación del del prefijo de la ruta de carga del plugin y el nombre corto del plugin del recurso (por ejemplo,
"view"):
Los recursos que son plugins puede llamar a otros recursos e inicializadores accediendo al bootstrap padre:
class My_Bootstrap_Resource_Layout
extends Zend_Application_Resource_ResourceAbstract
{
public function init()
{
// ensure view is initialized...
$this->getBootstrap()->bootstrap('view');
// ...
}
}
38
Zend_Application
En lugar de utilizar el método bootstrap() para llamar a los métodos internos o recursos, también puede usar
sobrecarga:
Funcionalidad Básica
Aquí encontrará documentación del tipo API acerca de todos los componentes básicos de Zend_Application.
Zend_Application
Zend_Application proporciona la funcionalidad básica del componente, y el punto de entrada a su aplicación
Zend Framework. Su propósito es doble: para configurar el medio ambiente PHP (incluyendo autocarga), y ejecutar
su aplicación bootstrap.
Option names
Please note that option names are case insensitive.
Típicamente, pasará toda la configuración al constructor Zend_Application, pero también puede configurar
totalmente el objeto utilizando sus propios métodos. En esta referencia se ilustran ambos casos de uso.
39
Zend_Application
• $options>: opcional.
El argumento puede ser
uno de los siguientes
valores:
• Zend_Config:
instancia del objeto de
configuración.
getEnvironment() string N/A Recuperar el string del
medio ambiente que se pasó
al constructor.
getAutoloader() N/A
Zend_Loader_Autoloader Recuperar la instancia de
Zend_Loader_Autoloader
registrados durante la
instanciación.
setOptions(array Zend_Application • $options: requerido. Todas las opciones se
$options) Un array de opciones de almacenan internamente, y
aplicación. llamando al método varias
veces las opciones se
fusionarán. Las opciones
concordantes con los
diversos métodos setter se
40
Zend_Application
41
Zend_Application
• $class: opcional. Si
$path: es un string,
entonces $class puede
ser especificado, y debe
ser el string nombre
de clase de la clase
contenida en el archivo
representado por path.
getBootstrap() null | N/A Recuperar la instancia del
Zend_Application_Bootstrap_Bootstrapperbootstrap registrado.
bootstrap() void N/A Llamar al método
bootstrap() para cargar
la aplicación.
run() void N/A Llamar al método run()
del bootstrap para despachar
la aplicación.
Zend_Application_Bootstrap_Bootstrapper
Zend_Application_Bootstrap_Bootstrapper es la interfaz base que deben implementar todas clases
bootstrap. Las funcionalidades de base están encaminadan a la configuración, identificación de los recursos,
bootstraping (ya sea de los recursos individuales o de toda la aplicación), y de despachar la aplicación.
42
Zend_Application
Zend_Application_Bootstrap_ResourceBootstrapper
Zend_Application_Bootstrap_ResourceBootstrapper es una interfaz para usar cuando una clase
de arranque cargará recursos externos -- por ejemplo, uno o más recursos no se definirán directamente en la
clase, sino más bien via plugins. Debe ser utilizado en conjunción con Zend_Application_Bootstrap_Bootstrapper;
Zend_Application_Bootstrap_BootstrapAbstract que implementan esta funcionalidad.
• $options: opcional.
Un array o un
objeto Zend_Config
para pasar al recurso en
instanciación.
unregisterPluginResource($resource) • $resource: requerido. Eliminar un recurso de
Zend_Application_Bootstrap_ResourceBootstrapper
Nombre de un recurso plugin de la clase.
para des-registrar desde la
clase.
43
Zend_Application
Zend_Application_Bootstrap_BootstrapAbstract
Zend_Application_Bootstrap_BootstrapAbstract es una clase abstracta que proporciona la base
funcional de un arranque (bootstrap) común. Implementa a ambos Zend_Application_Bootstrap_Bootstrapper y a
Zend_Application_Bootstrap_ResourceBootstrapper.
44
Zend_Application
45
Zend_Application
46
Zend_Application
Zend_Application_Bootstrap_Bootstrap
Zend_Application_Bootstrap_Bootstrap es una implementación concreta de
Zend_Application_Bootstrap_BootstrapAbstract. Su principal característica es que registra el recurso Front Controller,
y que el método run() primero comprueba esté definido un módulo por defecto y luego despacha el front controller.
En muchos casos, usted quisiera extender esta clase por sus necesidades de bootstrapping, o simplemente utilizar esta
clase y proporcionar una lista de los plugins de recursos a utilizar.
Zend_Application_Resource_Resource
Zend_Application_Resource_Resource es una interfaz para recursos de plugin utilizados con clases
bootstrap implementando Zend_Application_Bootstrap_ResourceBootstrapper. Se espera que los
recursos de plugins permitan la configuración, estar enterados del bootstrap, y aplicar un patrón de estrategia para la
inicialización de recursos.
Zend_Application_Resource_ResourceAbstract
Zend_Application_Resource_ResourceAbstract es una clase abstracta implementando
Zend_Application_Resource_Resource, y es un buen punto de partida para crear sus propios recursos de plugin
personalizados.
Nota: esta clase abstracta no implementa el método init(); esto se deja para la definición de extensiones concretas
de la clase.
47
Zend_Application
Resource Names
When registering plugin resources, one issue that arises is how you should refer to them from the parent bootstrap
class. There are three different mechanisms that may be used, depending on how you have configured the bootstrap
and its plugin resources.
First, if your plugins are defined within a defined prefix path, you may refer to them simply by their
"short name" -- i.e., the portion of the class name following the class prefix. As an example, the
class "Zend_Application_Resource_View" may be referenced as simply "View", as the prefix path
"Zend_Application_Resource" is already registered. You may register them using the full class name or the
short name:
In each case, you can then bootstrap the resource and retrieve it later using the short name:
$bootstrap->bootstrap('view');
$view = $bootstrap->getResource('view');
Second, if no matching plugin path is defined, you may still pass a resource by the full class name. In this case, you
can reference it using the resource's full class name:
48
Zend_Application
$bootstrap->bootstrap('My_Resource_View');
$view = $bootstrap->getResource('My_Resource_View');
This brings us to the third option. You can specify an explicit name that a given resource class will register as. This
can be done by adding a public $_explicitType property to the resource plugin class, with a string value; that
value will then be used whenever you wish to reference the plugin resource via the bootstrap. As an example, let's
define our own view class:
$bootstrap->bootstrap('My_View');
$view = $bootstrap->getResource('My_View');
Using these various naming approaches, you can override existing resources, add your own, mix multiple resources
to achieve complex initialization, and more.
Zend_Application_Resource_Db
Zend_Application_Resource_Db inicializará un adaptador Zend_Db basado en las opciones que se le pasen.
Por defecto, también establece el adaptador por defecto para usarlo con Zend_Db_Table.
• params: array asociativo de parámetros de configuración para utilizar al recuperar la instancia del adaptador.
49
Zend_Application
• isDefaultTableAdapter: Indica si establecer o no este adaptador como el adaptador de tablas por defecto.
[production]
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "webuser"
resources.db.params.password = "XXXXXXX"
resources.db.params.dbname = "test"
resources.db.isDefaultTableAdapter = true
Como con cualquier recurso de plugin, se puede recuperar el recurso de plugin de DB desde su archivo de
arranque:
$resource = $bootstrap->getPluginResource('db');
Una vez que tiene el recurso objeto, puede recuperar el adaptador de DB usando el método
getDbAdapter():
$db = $resource->getDbAdapter();
Zend_Application_Resource_Frontcontroller
Probablemente el recurso más común que se carga con Zend_Application será el recurso Front Controller ,
que proporciona la habilidad para configurar Zend_Controller_Front. Este recurso ofrece la posibilidad de
establecer parámetros arbitrarios del Front Controller, especificar plugins para inicializar, y mucho más.
Una vez inicializado, el recurso asigna la propiedad del $frontController del bootstrap a la instancia
Zend_Controller_Front.
Las claves de configuración disponibles incluyen las siguientes, sin importar si son mayúsculas ó minúsculas:
• controllerDirectory:: ya sea un valor de string especificando un único directorio controlador, o un array de pares
de directorio módulo/controlador.
50
Zend_Application
• plugins: array con los nombres de los plugins de las clases de los Front Controllers. El recurso instanciará a cada
clase (sin argumentos al constructor) y luego registra la instancia con el Front Controller.
Si se ingresa una clave no reconocida, ésta será registrada como un parámetro de Front Controller pasándolo a
setParam().
[production]
resources.frontController.controllerDirectory = APPLICATION_PATH . "/controllers"
resources.frontController.moduleControllerDirectoryName = "actions"
resources.frontController.moduleController = APPLICATION_PATH . "/modules"
resources.frontController.defaultControllerName = "site"
resources.frontController.defaultAction = "home"
resources.frontController.defaultModule = "static"
resources.frontController.baseUrl = "/subdir"
resources.frontController.plugins.foo = "My_Plugin_Foo"
resources.frontController.plugins.bar = "My_Plugin_Bar"
resources.frontController.env = APPLICATION_ENV
$bootstrap->bootstrap('frontController');
$front = $bootstrap->frontController;
Zend_Application_Resource_Layout
Zend_Application_Resource_Layout can be used to configure Zend_Layout. Configuration options are
per the Zend_Layout options.
resources.layout.layout = "NameOfDefaultLayout"
resources.layout.Path = "/path/to/layouts"
51
Zend_Application
Zend_Application_Resource_Modules
Zend_Application_Resource_Modules se utiliza para inicializar sus módulos de aplicación. Si su módulo
tiene un archivo Bootstrap.php en su raíz, y contiene una clase llamada Module_Bootstrap (donde "Module"
es el nombre del módulo), entonces usará esa clase para arrancar el módulo.
Ya que los módulos no reciben ningún parámetro por defecto, para poder activarlos via configuración, será necesario
crearlo como un array vacío. Seguiendo el estilo de configuración
<acronim>INI</acronim>
será similar a:
resources.modules[] =
<resources>
<modules>
<!-- Placeholder to ensure an array is created -->
<placeholder />
</modules>
</resources>
$options = array(
'resources' => array(
'modules' => array(),
),
);
Por ejemplo, supongamos que su aplicación tiene un módulo "noticias". Los siguientes son ejemplos de INI y XMLque
muestran la configuración de recursos en ese módulo.
[production]
news.resources.db.adapter = "pdo_mysql"
52
Zend_Application
news.resources.db.params.host = "localhost"
news.resources.db.params.username = "webuser"
news.resources.db.params.password = "XXXXXXX"
news.resources.db.params.dbname = "news"
news.resources.layout.layout = "news"
<?xml version="1.0"?>
<config>
<production>
<news>
<resources>
<db>
<adapter>pdo_mysql</adapter>
<params>
<host>localhost</host>
<username>webuser</username>
<password>XXXXXXX</password>
<dbname>news</dbname>
</params>
<isDefaultAdapter>true</isDefaultAdapter>
</db>
</resources>
</news>
</production>
</config>
$resource = $bootstrap->getPluginResource('modules');
$moduleBootstraps = $resource->getExecutedBootstraps();
$newsBootstrap = $moduleBootstraps['news'];
Zend_Application_Resource_Navigation
Zend_Application_Resource_Navigation can be used to configure a Zend_Navigation instance.
Configuration options are per the Zend_Navigation options.
Once done configuring the navigation instance, it assigns the instance to Zend_View_Helper_Navigation by
default -- from which you may retrieve it later.
53
Zend_Application
Zend_Application_Resource_Router
Zend_Application_Resource_Router can be used to configure the router as it is registered with the Front
Controller. Configuration options are per the Zend_Controller_Router_Route options.
resources.router.routes.route_id.route = "/login"
resources.router.routes.route_id.defaults.module = "user"
resources.router.routes.route_id.defaults.controller = "login"
resources.router.routes.route_id.defaults.action = "index"
For more information on the Chain Name Separator, please see its section.
Zend_Application_Resource_Session
Zend_Application_Resource_Session le permite configurar Zend_Session y opcionalmente inicializar
una sesión SaveHandler.
Para establecer un manejador de sesiones, simplemente pasar al recurso la clave opcional saveHandler (case
insensible). El valor de esta opción puede ser uno de los siguientes:
• Array: un array con las claves "class" y, opcionalmente, "options", indicando la clase que implementa
Zend_Session_SaveHandler_Interface que debería ser instanciada y una variedad de opciones para
proporcionar a su constructor.
Pasando cualquier otra opción de claves será pasado a Zend_Session::setOptions() para configurar
Zend_Session.
54
Zend_Application
Zend_Application_Resource_View
Zend_Application_Resource_View puede ser utilizada para configurar una instancia Zend_View. Las
opciones de configuración son por las opciones de Zend_View.
resources.view.encoding = "UTF-8"
resources.view.basePath = APPLICATION_PATH "/views/scripts"
55
56
Capítulo 5. Zend_Auth
Introducción
Zend_Auth provee una API para autenticación e incluye adaptadores concretos de autenticación para escenarios de
casos de uso común.
Zend_Auth es concerniente sólo con autenticación y no con autorización . Autenticación es vagamente definido
como: determinar si una entidad realmente es lo que pretende ser (o sea, identificación), basandose en un grupo de
credenciales. Autorización, el proceso de decidir si se permite a una entidad: acceso a, o el realizar operaciones en,
otras entidades esta fuera del alcance de Zend_Auth . Para más información sobre autorización y control de acceso
con Zend Framework, por favor vea Zend_Acl .
Nota
La clase Zend_Auth implementa el patrón Singleton - sólo una instancia de la clase está disponible - a través
de su método estático getInstance() . Esto significa que usar el operador new y la keyword clone no
va a funcionar con la clase Zend_Auth : use Zend_Auth::getInstance() en su lugar.
Adaptadores
Un adaptador Zend_Auth es usado para autenticar en contra de un tipo particular de servicio de autenticación,
como LDAP, RDBMS, o almacenamiento basado en ficheros. Diferentes adaptadores pueden tener opciones y
compotamientos muy diferentes, pero algunas cosas básicas son comunes entre los adaptadores de autenticación. Por
ejemplo, aceptar credenciales de autenticación (incluyendo una identidad supuesta), realizar consultas ante el servicio
de autenticación, y regresar resultados, son comunes para los adaptadores Zend_Auth .
El siguente ejemplo es un adaptador de autenticación que requiere que un nombre de usuario y contraseña sean
especificados para la autenticación. Otros detalles, como la forma de realizar peticiones al servicio de autenticación,
han sido omitídos por brevedad:
/**
* Realiza un intento de autenticación
57
Zend_Auth
*
* @throws Zend_Auth_Adapter_Exception Si la autenticación no puede
* ser realizada
* @return Zend_Auth_Result
*/
public function authenticate()
{
// ...
}
}
Resultados
Los adaptadores Zend_Auth regresan una instancia de Zend_Auth_Result con authenticate() para
representar el resultado de un intento de autenticación. Los adaptadores llenan el objeto Zend_Auth_Result en
cuanto se construye, así que los siguientes cuatro métodos proveen un grupo básico de operaciones "frente al usuario"
que son comunes a los resultados de adaptadores Zend_Auth:
• getCode() - regresa una constante identificadora Zend_Auth_Result para determinar el tipo de fallo en la
autenticación o si ha sido exitosa. Este puede ser usado en situaciones cuando el desarrollador desea distinguir entre
varios tipos de resultados de autenticación. Esto permite a los desarrolladores, por ejemplo, mantener estadísticas
detalladas de los resultados de autenticación. Otro uso de esta característica es: proporcionar al usuario mensajes
específicos detallados por razones de usabilidad, aunque los desarrolladores son exhortados a considerar el riesgo
de proporcionar tales detalles a los usuarios, en vez de un mensaje general de fallo en la autenticación. Para más
información, vea las siguientes notas:
El desarrollador podría desear ramificar basado en el tipo de resultado de la autenticación a fin de realizar operaciones
mas específicas. Algunas operaciones que los desarrolladores podrían encontrar útiles son: bloquear cuentas despues
de varios intentos fallidos de ingresar una contraseña, marcar una dirección IP despues de que ha intentado muchas
identidades no existentes, y porporcionar al usuario mensajes especificos resultados de la autenticación. Los siguientes
codigos de resultado están disponibles:
Zend_Auth_Result::SUCCESS
Zend_Auth_Result::FAILURE
Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND
Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS
Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID
Zend_Auth_Result::FAILURE_UNCATEGORIZED
El siguiente ejemplo ilustra como un desarrollador podría ramificar basado en el código resultado:
58
Zend_Auth
switch ($result->getCode()) {
case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND:
/** realiza algo para identidad inexistente **/
break;
case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:
/** realiza algo para credencial invalida **/
break;
case Zend_Auth_Result::SUCCESS:
/** realiza algo para autenticación exitosa **/
break;
default:
/** realiza algo para otras fallas **/
break;
}
Persistencia de Identidad
Autenticar una petición que incluye credenciales de autenticación es util por sí mismo, pero también es importante el
soportar mantener la identidad autenticada sin tener que presentar las credenciales de autenticación con cada petición.
HTTP es un protocolo sin estado, sin embargo, se han desarrollado técnicas como las cookies y sesiones a fin de
facilitar mantener el estado a través de multiples peticiones en aplicaciones web del lado del servidor.
Nota
Si el automático almacenamiento persistente de la identidad no es apropiado para un caso en particular,
entonces los desarrolladores podrían dejar de usar la clase Zend_Auth al mismo tiempo, utilizando en su
lugar una clase adaptadora directamente.
59
Zend_Auth
/**
* @todo Set up the auth adapter, $authAdapter
*/
/**
* Regresa el contenido del almacenamiento
*
* El comportamiento es indefinido cuando el almacenamiento esta vacio
*
* @arroja Zend_Auth_Storage_Exception Si leer contenido de
* almacenamiento es imposible
* @regresa mixto
*/
public function read()
{
/**
* @por hacer implementación
60
Zend_Auth
*/
}
/**
* Escribe $contents al almacenamiento
*
* @parametros mezclado $contents
* @arroja Zend_Auth_Storage_Exception Si escribir $contents al
* almacenamiento es imposible
* @regresa boleano
*/
public function write($contents)
{
/**
* @por hacer implementación
*/
}
/**
* limpia contenidos del almacenamiento
*
* @arroja Zend_Auth_Storage_Exception Si limpiar contenidos del
* almacenamiento es imposible
* @regresa void
*/
public function clear()
{
/**
* @por hacer implementación
*/
}
}
A fin de poder usar esta clase de almacenamiento personalizada, Zend_Auth::setStorage() es invocada antes
de intentar una petición de autenticación:
/**
* @por hacer Configurar el adaptador de autenticación, $authAdapter
*/
Uso
Hay dos formas provistas de usar adaptadores Zend_Auth:
61
Zend_Auth
El siguiente ejemplo ilustra como usar el adaptador :Zend_Auth : indirectamente, a través del uso de la clase
Zend_Auth :
if (!$result->isValid()) {
// Fautenticación fallida: imprime el por que
foreach ($result->getMessages() as $message) {
echo "$message\n";
}
} else {
// Autenticación exitosa, la identidad ($username) es almacenada
// en la sesión
// $result->getIdentity() === $auth->getIdentity()
// $result->getIdentity() === $username
}
Una vez que la autenticación ha sido intentada en una petición, como en el ejemplo anterior, es fácil verificar si existe
una identidad autenticada exitosamente:
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
// Existe la identidad; obtenla
$identity = $auth->getIdentity();
}
Para remover una identidad del almacenamiento persistente, simplemente usa el metodo clearIdentity()
method. Comunmente esto sería usado para implementar una operación "cerrar sesión" en la aplicación:
Zend_Auth::getInstance()->clearIdentity();
Cuando el uso automático de almacenamiento persistente es inapropiado para un caso en particular, el desarrollador
podría simplemente omitir el uso de la clase Zend_Auth , usando una clase adaptadora directamente. El uso
directo de una clase adaptadora implica configurar y preparar un objeto adaptador y despues llamar a su metodo
authenticate() . Los detalles específicos del adaptador son discutidos en la documentación de cada adaptador.
El siguiente ejemplo utiliza directamente MyAuthAdapter :
if (!$result->isValid()) {
// Autenticación fallida, imprime el porque
62
Zend_Auth
• tableName: Nombre de tabla de la base de datos que contiene las credenciales de autenticación, y contra la cual se
realiza la búsqueda de autenticación en la base de datos.
• identityColumn: Nombre de la columna de la tabla de la base de datos utilizada para representar la identidad. La
columna identidad debe contar con valores únicos, tales como un apellido ó una dirección de e-mail.
• credentialColumn: Nombre de la columna de la tabla de la base de datos utilizada para representar la credencial.
Conforme a un sistema de identidad simple y autenticación de contraseña, el valor de la credencial corresponde con
la contraseña. Véase también la opción credentialTreatment .
• credentialTreatment: En muchos casos, contraseñas y otros datos son encriptados, mezclados, codificados,
ocultados, desplazados o tratados de otra manera a través de alguna función o algoritmo. Al especificar una cadena
de tratamiento parametrizada con este método, tal como 'MD5(?)' o 'PASSWORD(?)', un desarrollador podría
aplicar sentencias arbitrarias SQL sobre los datos credenciales de entrada. Ya que estas funciones son específicas de
los RDBMS, debemos consultar el manual de la base de datos para comprobar la disponibilidad de tales funciones
para su sistema de base de datos.
El siguiente código crea un adaptador para una base de datos en memoria , un esquema simple de la tabla, e inserta
una fila contra la que se pueda realizar una consulta de autenticación posterior. Este ejemplo requiere que la extensión
PDO SQLite esté disponible.
63
Zend_Auth
// Construir una consulta para insertar una fila para que se pueda realizar la autenticació
$sqlInsert = "INSERT INTO users (username, password, real_name) "
. "VALUES ('my_username', 'my_password', 'My Real Name')";
Con la conexión de la base de datos y los datos de la tabla disponibles, podemos crear un instancia de
Zend_Auth_Adapter_DbTable. Los valores de las opciones de configuración pueden ser pasados al constructor
o pasados como parámetros a los métodos setter después de ser instanciados.
$authAdapter
->setTableName('users')
->setIdentityColumn('username')
->setCredentialColumn('password')
;
En este punto, el adaptador de la instancia de autenticación está listo para aceptar consultas de autenticación. Con el
fin de elaborar una consulta de autenticación, los valores de entrada de la credencial son pasados por el adaptador ates
de llamar al método authenticate():
// Imprimir la identidad
echo $result->getIdentity() . "\n\n";
64
Zend_Auth
/* Salida:
my_username
Array
(
[id] => 1
[username] => my_username
[password] => my_password
[real_name] => My Real Name
)
Ya que la fila de la tabla contiene el valor de la credencial, es importante proteger los valores contra accesos no
deseados.
if ($result->isValid()) {
// Almacena la identidad como un objedo dónde solo username y
// real_name han sido devueltos
$storage = $this->_auth->getStorage();
$storage->write($adapter->getResultRowObject(array(
'username',
'real_name',
)));
/* ... */
} else {
/* ... */
65
Zend_Auth
Con esa excepción fuera de lo común, Zend_Auth_Adapter_DbTable ha construido mecanismos que pueden
ser aprovechados para realizar controles adicionales en la autenticación a la vez que se resuelven algunos problemas
comunes de los usuarios.
Otra idea puede ser la aplicación de un mecanismo de "salting". "Salting" es un término que se refiere a una técnica
que puede mejorar altamente la seguridad de su aplicación. Se basa en la idea de concatenar una cadena aleatoria a
cada contraseña para evitar un ataque de fuerza bruta sobre la base de datos usando los valores hash de un diccionario
pre-calculado.
Por lo tanto, tenemos que modificar nuestra tabla para almacenar nuestra cadena mezclada:
Aquí hay una forma sencilla de generar una cadena mezclada por cada usuario en el momento del registro:
66
Zend_Auth
'password',
"MD5(CONCAT('"
. Zend_Registry::get('staticSalt')
. "', ?, password_salt))"
);
Nota
Puede mejorar aún más la seguridad mediante el uso de un valor 'salt' estático fuertemente codificado en su
aplicación. En el caso de que su base de datos se vea comprometida (por ejemplo, por un ataque de inyección
SQL), su servidor web está intacto y sus datos son inutilizable para el atacante.
Un ejemplo de una situación en la que uno podría querer utilizar el método getDbSelect() sería comprobar el
estado de un usuario, en otras palabras, ver si la cuenta del usuario está habilitada.
Autenticación "Digest"
Introducción
La Autenticación "Digest" [http://en.wikipedia.org/wiki/Digest_access_authentication] es un método de la
autenticación HTTP que mejora la Autenticación Básica [http://en.wikipedia.org/wiki/Basic_authentication_scheme]
proporcionando una manera de autenticar sin tener que transmitir la contraseña de manera clara a través de la red.
Este adaptador permite la autentificación contra archivos de texto que contengan líneas que tengan los elementos
básicos de la autenticación "Digest":
• Hash MD5 del username, realm y password, separados por dos puntos
67
Zend_Auth
Los elementos anteriores están separados por dos puntos, como en el ejemplo siguiente (en el que la contraseña es
"somePassword"):
someUser:Some Realm:fde17b91c3a510ecbaf7dbd37f59d4f8
Detalles Específicos
El adaptador de autenticación "Digest", Zend_Auth_Adapter_Digest, requiere varios parámetros de entrada:
• filename - Nombre del archivo contra el que se realiza la autenticación de las consultas
Identidad
El adaptador de autenticación "Digest" devuelve un objeto Zend_Auth_Result, que ha sido rellenado con la
identidad como un array que tenga claves realm y username. Los respectivos valores del array asociados con esas
claves correspondes con los valores fijados andes de llamar a authenticate().
$result = $adapter->authenticate();
$identity = $result->getIdentity();
print_r($identity);
/*
Array
(
[realm] => Some Realm
[username] => someUser
)
*/
68
Zend_Auth
Características Principales:
• Establece retos en todos los proyectos soportados, por lo que el cliente puede responder con cualquier proyecto
que soporte.
• Incluye soporte para la autenticación contra archivos de texto y proporciona una interfaz para autenticar contra otras
fuentes, tales como bases de datos.
• Seguimiento "nonce", que permitiría un gran apoyo, y un aumento de la protección de repetidos ataques.
Opciones de Configuración
La clase Zend_Auth_Adapter_Http requiere un array configurado que pasará a su constructor. Hay varias
opciones de configuración disponibles, y algunas son obligatorias:
69
Zend_Auth
Nota
La implementación actual del nonce_timeout tiene algunos efectos colaterales interesantes. Este ajuste es
supuesto para determinar la vida util válida para un determinado "nonce", o de manera efectiva el tiempo que
una información de autenticación del cliente es aceptada. Actualmente, si se establece en 3600 (por ejemplo),
hará que el adaptador indique al cliente las nuevas credenciales cada hora, a la hora en punto.
Resolvers
El trabajo del "Resolver" es tener un username y un realm, y devolver algún valor de tipo credencial. La autenticación
básica espera recibir la versión codificada en Base64 de la contraseña del usuario. La autenticación "Digest" espera
recibir un hash del username del usuario, un realm, y su contraseña (separados por coma). Actualmente, sólo se admite
el algoritmo de hash MD5.
Archivo Resolver
El archivo "resolver" es una clase muy simple. Tiene una única propiedad que especifique un nombre de archivo, que
también puede ser pasado al constructor. Su método resolve() recorre el archivo de texto, buscando una linea con
el correspondiente username y realm. El formato del archivo de texto es similar a los archivos htpasswd de Apache:
<username>:<realm>:<credentials>\n
Cada linea consta de tres campos -username, realm, y credenciales - cada uno separados por dos puntos. El campo
credenciales es opaco al archivo "resolver"; simplemente devuelve el valor tal como és al llamador. Por lo tanto, este
formato de archivo sirve tanto de autenticación básica como "Digest". En la autenticación básica, el campo credenciales
debe ser escrito en texto claro. En la autenticación "Digest", debería ser en hash MD5 descrito anteriormente.
$path = 'files/passwd.txt';
$resolver = new Zend_Auth_Adapter_Http_Resolver_File($path);
$path = 'files/passwd.txt';
$resolver = new Zend_Auth_Adapter_Http_Resolver_File();
$resolver->setFile($path);
Uso Básico
En primer lugar, establecemos un array con los valores de configuración obligatorios:
70
Zend_Auth
$config = array(
'accept_schemes' => 'basic digest',
'realm' => 'My Web Site',
'digest_domains' => '/members_only /my_account',
'nonce_timeout' => 3600,
);
Este array hará que el adaptador acepte la autenticación básica o "Digest", y requerirá un acceso autenticado a todas
las áreas del sitio en /members_only y /my_account. El valor realm es normalmente mostrado por el navegador
en el cuadro de dialogo contraseña. El nonce_timeout, por supuesto, se comporta como se ha descrito anteriormente.
Ya que estamos soportando tanto la autenticación básica como la "Digest", necesitamos dos objetos diferentes resolver.
Tenga en cuenta que esto podría ser facilmente dos clases diferentes:
$adapter->setBasicResolver($basicResolver);
$adapter->setDigestResolver($digestResolver);
Por último, realizamos la autenticación. El adaptador necesita una referencia a ambos objetos solicitud y respuesta
para hacer su trabajo:
$adapter->setRequest($request);
$adapter->setResponse($response);
$result = $adapter->authenticate();
if (!$result->isValid()) {
// Bad userame/password, or canceled password prompt
}
LDAP Authentication
Introduction
Zend_Auth_Adapter_Ldap supports web application authentication with LDAP services. Its features include
username and domain name canonicalization, multi-domain authentication, and failover capabilities. It has been tested
to work with Microsoft Active Directory [http://www.microsoft.com/windowsserver2003/technologies/directory/
activedirectory/] and OpenLDAP [http://www.openldap.org/], but it should also work with other LDAP service
providers.
71
Zend_Auth
This documentation includes a guide on using Zend_Auth_Adapter_Ldap, an exploration of its API, an outline
of the various available options, diagnostic information for troubleshooting authentication problems, and example
options for both Active Directory and OpenLDAP servers.
Usage
To incorporate Zend_Auth_Adapter_Ldap authentication into your application quickly, even if you're not using
Zend_Controller, the meat of your code should look something like the following:
$username = $this->_request->getParam('username');
$password = $this->_request->getParam('password');
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($adapter);
if ($log_path) {
$messages = $result->getMessages();
Of course, the logging code is optional, but it is highly recommended that you use a logger.
Zend_Auth_Adapter_Ldap will record just about every bit of information anyone could want in $messages
(more below), which is a nice feature in itself for something that has a history of being notoriously difficult to debug.
The Zend_Config_Ini code is used above to load the adapter options. It is also optional. A regular array would
work equally well. The following is an example application/config/config.ini file that has options for
two separate servers. With multiple sets of server options the adapter will try each, in order, until the credentials are
successfully authenticated. The names of the servers (e.g., 'server1' and 'server2') are largely arbitrary. For details
regarding the options array, see the Server Options section below. Note that Zend_Config_Ini requires that any
values with "equals" characters (=) will need to be quoted (like the DNs shown below).
72
Zend_Auth
[production]
ldap.log_path = /tmp/ldap.log
The above configuration will instruct Zend_Auth_Adapter_Ldap to attempt to authenticate users with the
OpenLDAP server s0.foo.net first. If the authentication fails for any reason, the AD server dc1.w.net will
be tried.
With servers in different domains, this configuration illustrates multi-domain authentication. You can also have
multiple servers in the same domain to provide redundancy.
Note that in this case, even though OpenLDAP has no need for the short NetBIOS style domain name used by Windows,
we provide it here for name canonicalization purposes (described in the Username Canonicalization section below).
The API
The Zend_Auth_Adapter_Ldap constructor accepts three parameters.
The $options parameter is required and must be an array containing one or more sets of options. Note that it is
an array of arrays of Zend_Ldap options. Even if you will be using only one LDAP server, the options must still
be within another array.
Below is print_r() [http://php.net/print_r] output of an example options parameter containing two sets of server
options for LDAP servers s0.foo.net and dc1.w.net (the same options as the above INI representation):
Array
(
[server2] => Array
(
[host] => dc1.w.net
[useStartTls] => 1
[accountDomainName] => w.net
[accountDomainNameShort] => W
[accountCanonicalForm] => 3
[baseDn] => CN=Users,DC=w,DC=net
73
Zend_Auth
The information provided in each set of options above is different mainly because AD does not require a username be
in DN form when binding (see the bindRequiresDn option in the Server Options section below), which means we can
omit a number of options associated with retrieving the DN for a username being authenticated.
The names of servers (e.g. 'server1' and 'server2' shown above) are largely arbitrary, but for the sake of using
Zend_Config, the identifiers should be present (as opposed to being numeric indexes) and should not contain any
special characters used by the associated file formats (e.g. the '.' INI property separator, '&' for XML entity references,
etc).
With multiple sets of server options, the adapter can authenticate users in multiple domains and provide failover so
that if one server is not available, another will be queried.
The username and password parameters of the Zend_Auth_Adapter_Ldap constructor represent the credentials
being authenticated (i.e., the credentials supplied by the user through your HTML login form). Alternatively, they may
also be set with the setUsername() and setPassword() methods.
74
Zend_Auth
Server Options
Each set of server options in the context of Zend_Auth_Adapter_Ldap consists of the following options, which
are passed, largely unmodified, to Zend_Ldap::setOptions():
75
Zend_Auth
Name Description
should be, as retrieving the DN requires an extra round trip
to the server). Otherwise, this option must be set to TRUE
(e.g. for OpenLDAP). This option also controls the default
acountFilterFormat used when searching for accounts. See
the accountFilterFormat option.
baseDn The DN under which all accounts being authenticated
are located. This option is required. if you are
uncertain about the correct baseDn value, it should be
sufficient to derive it from the user's DNS domain
using DC= components. For example, if the user's
principal name is alice@foo.net, a baseDn of
DC=foo,DC=net should work. A more precise location
(e.g., OU=Sales,DC=foo,DC=net) will be more efficient,
however.
accountCanonicalForm A value of 2, 3 or 4 indicating the form to
which account names should be canonicalized after
successful authentication. Values are as follows: 2
for traditional username style names (e.g., alice), 3
for backslash-style names (e.g., FOO\alice) or 4
for principal style usernames (e.g., alice@foo.net).
The default value is 4 (e.g., alice@foo.net). For
example, with a value of 3, the identity returned
by Zend_Auth_Result::getIdentity() (and
Zend_Auth::getIdentity(), if Zend_Auth
was used) will always be FOO\alice, regardless
of what form Alice supplied, whether it
be alice, alice@foo.net, FOO\alice, FoO
\aLicE, foo.net\alice, etc. See the Account
Name Canonicalization section in the Zend_Ldap
documentation for details. Note that when using multiple
sets of server options it is recommended, but not required,
that the same accountCanonicalForm be used with all
server options so that the resulting usernames are always
canonicalized to the same form (e.g., if you canonicalize
to EXAMPLE\username with an AD server but to
username@example.com with an OpenLDAP server,
that may be awkward for the application's high-level
logic).
accountDomainName The FQDN domain name for which the target LDAP
server is an authority (e.g., example.com). This option
is used to canonicalize names so that the username
supplied by the user can be converted as necessary
for binding. It is also used to determine if the server
is an authority for the supplied username (e.g., if
accountDomainName is foo.net and the user supplies
bob@bar.net, the server will not be queried, and a
failure will result). This option is not required, but if
it is not supplied, usernames in principal name form
(e.g., alice@foo.net) are not supported. It is strongly
recommended that you supply this option, as there are
76
Zend_Auth
Name Description
many use-cases that require generating the principal name
form.
accountDomainNameShort The 'short' domain for which the target LDAP server
is an authority (e.g., FOO). Note that there is a
1:1 mapping between the accountDomainName and
accountDomainNameShort. This option should be used
to specify the NetBIOS domain name for Windows
networks, but may also be used by non-AD servers (e.g.,
for consistency when multiple sets of server options with
the backslash style accountCanonicalForm). This option
is not required but if it is not supplied, usernames in
backslash form (e.g., FOO\alice) are not supported.
accountFilterFormat The LDAP search filter used to search for accounts.
This string is a printf() [http://php.net/printf]-
style expression that must contain one '%s' to
accomodate the username. The default value is
'(&(objectClass=user)(sAMAccountName=%s))', unless
bindRequiresDn is set to TRUE, in which case the
default is '(&(objectClass=posixAccount)(uid=%s))'. For
example, if for some reason you wanted to use
bindRequiresDn = true with AD you would need
to set accountFilterFormat = '(&(objectClass=user)
(sAMAccountName=%s))'.
optReferrals If set to TRUE, this option indicates to the LDAP client that
referrals should be followed. The default value is FALSE.
Nota
If you enable useStartTls = TRUE or useSsl = TRUE you may find that the LDAP client generates an error
claiming that it cannot validate the server's certificate. Assuming the PHP LDAP extension is ultimately
linked to the OpenLDAP client libraries, to resolve this issue you can set "TLS_REQCERT never" in the
OpenLDAP client ldap.conf (and restart the web server) to indicate to the OpenLDAP client library that
you trust the server. Alternatively, if you are concerned that the server could be spoofed, you can export the
LDAP server's root certificate and put it on the web server so that the OpenLDAP client can validate the
server's identity.
77
Zend_Auth
In practice, index 0 should be displayed to the user (e.g., using the FlashMessenger helper), index 1 should be logged
and, if debugging information is being collected, indexes 2 and higher could be logged as well (although the final
message always includes the string from index 1).
Nota
Technically there should be no danger of accidental cross-domain authentication with the current
Zend_Auth_Adapter_Ldap implementation, since server domains are explicitly checked, but this may
not be true of a future implementation that discovers the domain at runtime, or if an alternative adapter is
used (e.g., Kerberos). In general, account name ambiguity is known to be the source of security issues, so
always try to use qualified account names.
78
Zend_Auth
El OpenID solo es un URI que apunta a un sitio con información del usuari, así como información especiales que
describe que servidor usar y que información (identidad) se debe enviar. Puedes leer más información acerca de OpenID
en el sitio oficial de OpenId [http://www.openid.net/].
79
Zend_Auth
Nota
Zend_OpenId aprovecha las GMP extension [http://php.net/gmp], cuando estén disponibles.
Considere la posibilidad de usar GMP extension para un mejor rendimiento cuando use
Zend_Auth_Adapter_OpenId.
Características
Como es el caso de todos los adaptadores Zend_Auth, la clase Zend_Auth_Adapter_OpenId implementa
Zend_Auth_Adapter_Interface, el cual define un metodo authenticate(). Este método realiza la
autenticación en sí, pero el objeto debe estar configurado antes de ser llamado. La configuracion del adaptador requiere
la creacion de un OpenID y otras opciones de Zend_OpenId específicos.
<?php
$status = "";
$auth = Zend_Auth::getInstance();
if ((isset($_POST['openid_action']) &&
$_POST['openid_action'] == "login" &&
!empty($_POST['openid_identifier'])) ||
isset($_GET['openid_mode']) ||
isset($_POST['openid_mode'])) {
$result = $auth->authenticate(
new Zend_Auth_Adapter_OpenId(@$_POST['openid_identifier']));
if ($result->isValid()) {
$status = "You are logged in as "
. $auth->getIdentity()
. "<br>\n";
} else {
$auth->clearIdentity();
foreach ($result->getMessages() as $message) {
$status .= "$message<br>\n";
}
}
} else if ($auth->hasIdentity()) {
if (isset($_POST['openid_action']) &&
$_POST['openid_action'] == "logout") {
80
Zend_Auth
$auth->clearIdentity();
} else {
$status = "You are logged in as "
. $auth->getIdentity()
. "<br>\n";
}
}
?>
<html><body>
<?php echo htmlspecialchars($status);?>
<form method="post"><fieldset>
<legend>OpenID Login</legend>
<input type="text" name="openid_identifier" value="">
<input type="submit" name="openid_action" value="login">
<input type="submit" name="openid_action" value="logout">
</fieldset></form></body></html>
Puede personalizar el proceso de autenticación OpenID de varias formas. Por ejemplo, recibir la redirección
del servidor de OpenID en una página aparte, especificando la "raíz" del sitio web y utilizar un
Zend_OpenId_Consumer_Storage o un Zend_Controller_Response. Usted también puede utilizar el
simple registro de extensiones para recuperar información sobre el usuario desde el servidor de OpenID. Todas estas
posibilidades se describen con más detalle en el capítulo Zend_OpenId_Consume.
81
82
Capítulo 6. Zend_Cache
Introducción
Zend_Cache provee una forma genérica para cualquier caché de datos.
El almacenamiento en caché en Zend Framework se opera por interfaces, mientras que los registros de caché son
almacenados a través de adapatadores del backend ( Archivo , Sqlite , Memcache ...) mediante un sistema flexible de
documentos de identidad y etiquetas. Utilizando éstas, es fácil en el futuro eliminar determinados tipos de registro.
(Ejemplo: "eliminar todos los registros caché de determinada etiqueta").
El módulo principal (Zend_Cache_Core) es genérico, flexible y configurable. Aun para sus necesidades específicas
existen frontends de caché que extienden Zend_Cache_Core a conveniencia: Output , File , Function y Class .
$frontendOptions = array(
'lifetime' => 7200, // tiempo de vida de caché de 2 horas
'automatic_serialization' => true
);
$backendOptions = array(
'cache_dir' => './tmp/' // Carpeta donde alojar los archivos de caché
);
83
Zend_Cache
$cache->save($result, 'myresult');
} else {
print_r($result);
Dentro, los datos de salida, como siempre – todas las salidas serán almacenadas en caché cuando se ordene la ejecución
del método end() . En la siguiente ejecución, toda la sección se saltará a favor de la búsqueda de datos del caché
(tanto tiempo como el registro del caché sea válido).
$frontendOptions = array(
'lifetime' => 30, // tiempo de vida de caché de 30 segundos
'automatic_serialization' => false // éste es el valor por defecto
);
$cache = Zend_Cache::factory('Output',
'File',
$frontendOptions,
$backendOptions);
Note que delineamos el resultado de time() dos veces; esto es algo dinámico para los propósitos de la demostración.
Trate de ejecutarlo y entonces regenérelo muchas veces; notará que el primer número no cambia mientras que el
segundo cambia a medida que pasa el tiempo. Esto es porque el primer número esta delineado en la sección caché
84
Zend_Cache
y esta guardado en medio de otras salidas. Después de medio minuto (habremos establecido el tiempo de vida de 30
segundos) los números deben acoplarse nuevamente porque el registro caché ha expirado -- sólo para ser almacenado
en caché nuevamente. Deberá probarlo en su visualizador o consola.
Nota
Cuando usamos Zend_Cache, ponemos atención a la importación del identificador caché (pasado a
save() y start() ). Éste deberá ser único para cada recurso que se almacene en caché, de otra manera
los registros almacenados en caché que no se vinculan podrían borrarse unos a otros, o peor aún, mostrarse
uno en lugar del otro.
Nota
The conditional execution design of your generating code is not necessary in some frontends (Function, for
an example) when the whole logic is implemented inside the frontend.
Nota
'Cache hit' is a term for a condition when a cache record is found, is valid and is 'fresh' (in other words hasn't
expired yet). 'Cache miss' is everything else. When a cache miss happens, you must generate your data (as
you would normally do) and have it cached. When you have a cache hit, on the other hand, the backend
automatically fetches the record from cache transparently.
85
Zend_Cache
$frontendOptions,
$backendOptions);
In the following examples we will assume that the $cache variable holds a valid, instantiated frontend as shown and
that you understand how to pass parameters to your chosen backends.
Nota
Always use Zend_Cache::factory() to get frontend instances. Instantiating frontends and backends
yourself will not work as expected.
Tagging Records
Tags are a way to categorize cache records. When you save a cache with the save() method, you can set an array of
tags to apply for this record. Then you will be able to clean all cache records tagged with a given tag (or tags):
Nota
note than the save() method accepts an optional fourth argument: $specificLifetime (if != FALSE,
it sets a specific lifetime for this particular cache record)
$cache->remove('idToRemove');
To remove or invalidate several cache ids in one operation, you can use the clean() method. For example to remove
all cache records :
If you want to remove cache entries matching the tags 'tagA' and 'tagC':
$cache->clean(
Zend_Cache::CLEANING_MODE_MATCHING_TAG,
array('tagA', 'tagC')
);
If you want to remove cache entries not matching the tags 'tagA' or 'tagC':
$cache->clean(
Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG,
array('tagA', 'tagC')
);
86
Zend_Cache
If you want to remove cache entries matching the tags 'tagA' or 'tagC':
$cache->clean(
Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG,
array('tagA', 'tagC')
);
Zend_Cache Frontends
Zend_Cache_Core
Introduction
Zend_Cache_Core is a special frontend because it is the core of the module. It is a generic cache frontend and is
extended by other classes.
Nota
All frontends inherit from Zend_Cache_Core so that its methods and options (described below) would
also be available in other frontends, therefore they won't be documented there.
Available options
These options are passed to the factory method as demonstrated in previous examples.
87
Zend_Cache
Ejemplos
An example is given in the manual at the very beginning.
If you store only strings into cache (because with "automatic_serialization" option, it's possible to store some booleans),
you can use a more compact construction like:
if (!($data = $cache->load($id))) {
// cache miss
$data = '';
for ($i = 0; $i < 10000; $i++) {
$data = $data . $i;
88
Zend_Cache
$cache->save($data);
If you want to cache multiple blocks or data instances, the idea is the same:
// block 1
if (!($data = $cache->load($id1))) {
// cache missed
$data = '';
for ($i=0;$i<10000;$i++) {
$data = $data . $i;
}
$cache->save($data);
}
echo($data);
// block 2
if (!($data = $cache->load($id2))) {
// cache missed
$data = '';
for ($i=0;$i<10000;$i++) {
$data = $data . '!';
}
$cache->save($data);
}
echo($data);
If you want to cache special values (boolean with "automatic_serialization" option) or empty strings you can't use the
compact construction given above. You have to test formally the cache record.
// cache missed
89
Zend_Cache
$cache->save($data);
// [...]
// cache missed
$cache->save($data);
} else {
// cache hit
$data = $cache->load($id);
Zend_Cache_Frontend_Output
Introduction
Zend_Cache_Frontend_Output is an output-capturing frontend. It utilizes output buffering in PHP to capture
everything between its start() and end() methods.
Available Options
This frontend doesn't have any specific options other than those of Zend_Cache_Core.
Ejemplos
An example is given in the manual at the very beginning. Here it is with minor changes:
90
Zend_Cache
Using this form it is fairly easy to set up output caching in your already working project with little or no code
refactoring.
Zend_Cache_Frontend_Function
Introduction
Zend_Cache_Frontend_Function caches the results of function calls. It has a single main method named
call() which takes a function name and parameters for the call in an array.
Available Options
Tabla 6.2. Function Frontend Options
Option Data Type Default Value Description
cache_by_default Boolean TRUE if TRUE, function calls will
be cached by default
cached_functions Array function names which will
always be cached
non_cached_functions Array function names which must
never be cached
Ejemplos
Using the call() function is the same as using call_user_func_array() in PHP:
$cache->call('veryExpensiveFunc', $params);
// $params is an array
// For example to call veryExpensiveFunc(1, 'foo', 'bar') with
// caching, you can use
// $cache->call('veryExpensiveFunc', array(1, 'foo', 'bar'))
Zend_Cache_Frontend_Function is smart enough to cache both the return value of the function and its internal
output.
Nota
You can pass any built in or user defined function with the exception of array(), echo(), empty(),
eval(), exit(), isset(), list(), print() and unset().
Zend_Cache_Frontend_Class
Introduction
Zend_Cache_Frontend_Class is different from Zend_Cache_Frontend_Function because it allows
caching of object and static method calls.
91
Zend_Cache
Available Options
Tabla 6.3. Class Frontend Options
Option Data Type Default Value Description
cached_entity (required) Mixed if set to a class name, we
will cache an abstract class
and will use only static calls;
if set to an object, we will
cache this object methods
cache_by_default Boolean TRUE if TRUE, calls will be cached
by default
cached_methods Array method names which will
always be cached
non_cached_methods Array method names which must
never be cached
Ejemplos
For example, to cache static calls :
class Test {
// Static method
public static function foobar($param1, $param2) {
echo "foobar_output($param1, $param2)";
return "foobar_return($param1, $param2)";
}
// [...]
$frontendOptions = array(
'cached_entity' => 'Test' // The name of the class
);
// [...]
class Test {
92
Zend_Cache
// [...]
$frontendOptions = array(
'cached_entity' => new Test() // An instance of the class
);
// [...]
Zend_Cache_Frontend_File
Introduction
Zend_Cache_Frontend_File is a frontend driven by the modification time of a "master file". It's really
interesting for examples in configuration or templates issues. It's also possible to use multiple master files.
For instance, you have an XML configuration file which is parsed by a function which returns a "config object" (like
with Zend_Config). With Zend_Cache_Frontend_File, you can store the "config object" into cache (to
avoid the parsing of the XML config file at each time) but with a sort of strong dependency on the "master file". So,
if the XML config file is modified, the cache is immediately invalidated.
Available Options
Tabla 6.4. File Frontend Options
Option Data Type Default Value Description
master_file (deprecated) String '' the complete path and name
of the master file
master_files Array array() an array of complete path of
master files
master_files_mode String Zend_Cache_Frontend_File::MODE_OR
Zend_Cache_Frontend_File::MODE_A
or
Zend_Cache_Frontend_File::MODE_O
; if MODE_AND, then all
master files have to be
touched to get a cache
invalidation if MODE_OR,
then a single touched master
file is enough to get a cache
invalidation
ignore_missing_master_files Boolean FALSE if TRUE, missing master
files are ignored silently (an
exception is raised else)
Ejemplos
Use of this frontend is the same than of Zend_Cache_Core. There is no need of a specific example - the only thing
to do is to define the master_file when using the factory.
93
Zend_Cache
Zend_Cache_Frontend_Page
Introduction
Zend_Cache_Frontend_Page is like Zend_Cache_Frontend_Output but designed for a complete page.
It's impossible to use Zend_Cache_Frontend_Page for caching only a single block.
On the other hand, the "cache id" is calculated automatically with $_SERVER['REQUEST_URI'] and (depending
on options) $_GET, $_POST, $_SESSION, $_COOKIE, $_FILES. More over, you have only one method to call
(start()) because the end() call is fully automatic when the page is ended.
For the moment, it's not implemented but we plan to add a HTTP conditional system to save bandwidth (the system
will send a HTTP 304 Not Modified if the cache is hit and if the browser has already the good version).
Available Options
Tabla 6.5. Page Frontend Options
Option Data Type Default Value Description
http_conditional Boolean FALSE use the http_conditional
system (not implemented
for the moment)
debug_header Boolean FALSE if TRUE, a debug text is
added before each cached
pages
default_options Array array(...see an associative array of
below...) default options:
• (boolean, TRUE by
default) cache: cache is
on if TRUE
• (boolean, FALSE by
default)
cache_with_get_variables:
if TRUE, cache is still on
even if there are some
variables in $_GET array
• (boolean, FALSE by
default)
cache_with_post_variables:
if TRUE, cache is still on
even if there are some
variables in $_POST
array
• (boolean, FALSE by
default)
cache_with_session_variables:
if TRUE, cache is still on
even if there are some
variables in $_SESSION
array
94
Zend_Cache
• (boolean, FALSE by
default)
cache_with_cookie_variables:
if TRUE, cache is still on
even if there are some
variables in $_COOKIE
array
• (boolean, TRUE by
default)
make_id_with_get_variables:
if TRUE, the cache id
will be dependent of the
content of the $_GET
array
• (boolean, TRUE by
default)
make_id_with_post_variables:
if TRUE, the cache id
will be dependent of the
content of the $_POST
array
• (boolean, TRUE by
default)
make_id_with_session_variables:
if TRUE, the cache
id will be dependent
of the content of the
$_SESSION array
• (boolean, TRUE by
default)
make_id_with_files_variables:
if TRUE, the cache id
will be dependent of the
content of the $_FILES
array
• (boolean, TRUE by
default)
make_id_with_cookie_variables:
if TRUE, the cache
id will be dependent
95
Zend_Cache
• (array, array() by
default) tags: tags for the
cache record
Ejemplos
Use of Zend_Cache_Frontend_Page is really trivial:
$cache->start();
// if the cache is hit, the result is sent to the browser
// and the script stop here
a more complex example which shows a way to get a centralized cache management in a bootstrap file (for using with
Zend_Controller for example)
96
Zend_Cache
/*
* You should avoid putting too many lines before the cache section.
* For example, for optimal performances, "require_once" or
* "Zend_Loader::loadClass" should be after the cache section.
*/
$frontendOptions = array(
'lifetime' => 7200,
'debug_header' => true, // for debugging
'regexps' => array(
// cache the whole IndexController
'^/$' => array('cache' => true),
$backendOptions = array(
'cache_dir' => '/tmp/'
);
$cache->start();
// if the cache is hit, the result is sent to the browser and the
// script stop here
97
Zend_Cache
$cache->start();
// [...]
if ($someTest) {
$cache->cancel();
// [...]
}
// [...]
Zend_Cache Backends
There are two kinds of backends: standard ones and extended ones. Of course, extended backends offer more features.
Zend_Cache_Backend_File
This (extended) backends stores cache records into files (in a choosen directory).
98
Zend_Cache
Zend_Cache_Backend_Sqlite
This (extended) backends stores cache records into a SQLite database.
99
Zend_Cache
Zend_Cache_Backend_Memcached
This (extended) backends stores cache records into a memcached server. memcached [http://www.danga.com/
memcached/] is a high-performance, distributed memory object caching system. To use this backend, you need a
memcached daemon and the memcache PECL extension [http://pecl.php.net/package/memcache].
Be careful : with this backend, "tags" are not supported for the moment as the "doNotTestCacheValidity=true"
argument.
100
Zend_Cache
Zend_Cache_Backend_Apc
This (extended) backends stores cache records in shared memory through the APC [http://pecl.php.net/package/APC]
(Alternative PHP Cache) extension (which is of course need for using this backend).
Be careful : with this backend, "tags" are not supported for the moment as the "doNotTestCacheValidity=true"
argument.
Zend_Cache_Backend_Xcache
This backends stores cache records in shared memory through the XCache [http://xcache.lighttpd.net/] extension
(which is of course need for using this backend).
Be careful : with this backend, "tags" are not supported for the moment as the "doNotTestCacheValidity=true"
argument.
Zend_Cache_Backend_ZendPlatform
This backend uses content caching API of the Zend Platform [http://www.zend.com/en/products/platform/] product.
Naturally, to use this backend you need to have Zend Platform installed.
This backend supports tags, but does not support CLEANING_MODE_NOT_MATCHING_TAG cleaning mode.
Specify this backend using a word separator -- '-', '.', ' ', or '_' -- between the words 'Zend' and 'Platform' when using
the Zend_Cache::factory() method:
Zend_Cache_Backend_TwoLevels
This (extend) backend is an hybrid one. It stores cache records in two other backends : a fast one (but limited) like
Apc, Memcache... and a "slow" one like File, Sqlite...
This backend will use the priority parameter (given at the frontend level when storing a record) and the remaining
space in the fast backend to optimize the usage of these two backends.
101
Zend_Cache
Specify this backend using a word separator -- '-', '.', ' ', or '_' -- between the words 'Two' and 'Levels' when using the
Zend_Cache::factory() method:
102
Zend_Cache
Zend_Cache_Backend_ZendServer_Disk and
Zend_Cache_Backend_ZendServer_ShMem
These backends store cache records using Zend Server [http://www.zend.com/en/products/server/downloads-all?
zfs=zf_download] caching functionality.
Be careful: with these backends, "tags" are not supported for the moment as the "doNotTestCacheValidity=true"
argument.
These backend work only withing Zend Server environment for pages requested through HTTP or HTTPS and don't
work for command line script execution
Specify this backend using parameter customBackendNaming as TRUE when using the Zend_Cache::factory()
method:
103
104
Capítulo 7. Zend_Captcha
Introducción
CAPTCHA [http://en.wikipedia.org/wiki/Captcha] es el acrónimo de "Completely Automated Public Turing test to
tell Computers and Humans Apart" (Prueba de Turing pública y automática para diferenciar a máquinas y humanos).
Es usado como un desafío-respuesta para asegurar que la información individual suministrada viene de un humano y
no de un proceso automatizado. Típicamente, un captcha es usado con envío de formularios donde no es necesario que
el usuario se haya autenticado, pero se desea prevenir el envío de spam.
Los Captchas pueden presentarse en multitud de formas, incluyendo preguntas lógicas, caracteres trastocados o
presentar múltiples imágenes y preguntar cómo se relacionan. Zend_Form intenta proveer una amalgama de backends
que pueden ser utilizados por separado o en conjunción con Zend_Form .
Captcha Operation
All CAPTCHA adapter implement Zend_Captcha_Adapter, which looks like the following:
The name setter and getter are used to specify and retrieve the CAPTCHA identifier. getDecorator() can be
used to specify a Zend_Form decorator either by name or returning an actual decorator object. The most interesting
methods are generate() and render(). generate() is used to create the CAPTCHA token. This process
typically will store the token in the session so that you may compare against it in subsequent requests. render()
is used to render the information that represents the CAPTCHA, be it an image, a figlet, a logic problem, or some
other CAPTCHA.
105
Zend_Captcha
// Originating request:
$captcha = new Zend_Captcha_Figlet(array(
'name' => 'foo',
'wordLen' => 6,
'timeout' => 300,
));
$id = $captcha->generate();
echo "<form method=\"post\" action=\"\">";
echo $captcha->render($view);
echo "</form>";
// On subsequent request:
// Assume captcha setup as before, the value of $_POST['foo']
// would be key/value array: id => captcha ID, input => captcha value
if ($captcha->isValid($_POST['foo'], $_POST)) {
// Validated!
}
CAPTCHA Adapters
The following adapters are shipped with Zend Framework by default.
Zend_Captcha_Word
Zend_Captcha_Word is an abstract adapter that serves as the base class for most other CAPTCHA adapters.
It provides mutators for specifying word length, session TTL, the session namespace object to use, and the
session namespace class to use for persistence if you do not wish to use Zend_Session_Namespace.
Zend_Captcha_Word encapsulates validation logic.
By default, the word length is 8 characters, the session timeout is 5 minutes, and Zend_Session_Namespace is
used for persistence (using the namespace "Zend_Form_Captcha_<captcha ID>").
• setWordLen($length) and getWordLen() allow you to specify the length of the generated "word" in
characters, and to retrieve the current value.
• setTimeout($ttl) and getTimeout() allow you to specify the time-to-live of the session token, and to
retrieve the current value. $ttl should be specified in seconds.
• getWord() allows you to retrieve the generated word to use with the CAPTCHA. It will generate the word for
you if none has been generated yet.
106
Zend_Captcha
All word CAPTCHAs allow you to pass an array of options to the constructor, or, alternately, pass them to
setOptions(). You can also pass a Zend_Config object to setConfig(). By default, the wordLen, timeout,
and sessionClass keys may all be used. Each concrete implementation may define additional keys or utilize the options
in other ways.
Nota
Zend_Captcha_Word is an abstract class and may not be instantiated directly.
Zend_Captcha_Dumb
The Zend_Captch_Dumb adapter is mostly self-descriptive. It provides a random string that must be typed in
reverse to validate. As such, it's not a good CAPTCHA solution and should only be used for testing. It extends
Zend_Captcha_Word.
Zend_Captcha_Figlet
The Zend_Captcha_Figlet adapter utilizes Zend_Text_Figlet to present a figlet to the user.
Options passed to the constructor will also be passed to the Zend_Text_Figlet object. See the
Zend_Text_Figlet documentation for details on what configuration options are available.
Zend_Captcha_Image
The Zend_Captcha_Image adapter takes the generated word and renders it as an image, performing various
skewing permutations to make it difficult to automatically decipher. It requires the GD extension [http://php.net/gd]
compiled with TrueType or Freetype support. Currently, the Zend_Captcha_Image adapter can only generate
PNG images.
• setGcFreq($gcFreq) and getGcFreg() allow you to specify how frequently garbage collection should run.
Garbage collection will run every 1/$gcFreq calls. The default is 100.
• setFont($font) and getFont() allow you to specify the font you will use. $font should be a fully qualified
path to the font file. This value is required; the CAPTCHA will throw an exception during generation if the font
file has not been specified.
• setFontSize($fsize) and getFontSize() allow you to specify the font size in pixels for generating the
CAPTCHA. The default is 24px.
• setHeight($height) and getHeight() allow you to specify the height in pixels of the generated
CAPTCHA image. The default is 50px.
• setWidth($width) and getWidth() allow you to specify the width in pixels of the generated CAPTCHA
image. The default is 200px.
• setImgDir($imgDir) and getImgDir() allow you to specify the directory for storing CAPTCHA images.
The default is "./images/captcha/", relative to the bootstrap script.
107
Zend_Captcha
• setImgUrl($imgUrl) and getImgUrl() allow you to specify the relative path to a CAPTCHA image to use
for HTML markup. The default is "/images/captcha/".
• setSuffix($suffix) and getSuffix() allow you to specify the filename suffix for the CAPTCHA image.
The default is ".png". Note: changing this value will not change the type of the generated image.
All of the above options may be passed to the constructor by simply removing the 'set' method prefix and casting the
initial letter to lowercase: "suffix", "height", "imgUrl", etc.
Zend_Captcha_ReCaptcha
The Zend_Captcha_ReCaptcha adapter uses Zend_Service_ReCaptcha to generate and validate CAPTCHAs. It
exposes the following methods:
• setPrivKey($key) and getPrivKey() allow you to specify the private key to use for the ReCaptcha service.
This must be specified during construction, although it may be overridden at any point.
• setPubKey($key) and getPubKey() allow you to specify the public key to use with the ReCaptcha service.
This must be specified during construction, although it may be overridden at any point.
• setService(Zend_Service_ReCaptcha $service) and getService() allow you to set and get the
ReCaptcha service object.
108
Capítulo 8. Zend_CodeGenerator
Introducción
Zend_CodeGenerator ofrece facilidades para generar código arbitrario usando una interfaz orientada a objetos,
tanto para crear código nuevo como para actualizar código existente. Mientras que la implementación actual se limita
a generar código PHP, usted fácilmente puede extender la clase base a fin de proveer generación de código para otras
tareas como: JavaScript, archivos de configuración, apache vhost, etc.
Teoría de Operación
En el caso de uso más típico, simplemente instanciará una clase de generación de código y podrá pasarle tanto la
configuración adecuada o configurarla después de la instanciación. Para generar el código, simplemente haga "echo"
del objeto o llame a su método generate().
// o grabarlo a un archivo:
file_put_contents('World.php', $file->generate());
109
Zend_CodeGenerator
<?php
class World
{
Otro caso de uso común es actualizar el código actual -- por ejemplo, para añadir un método a una clase. En
ese caso, primero debe inspeccionar el código actual utilizando reflexión, y entonces añadir su nuevo método.
Zend_CodeGenerator lo hace trivialmente simple, aprovechando Zend_Reflection.
Como ejemplo, digamos que hemos grabado lo anterior al archivo "World.php", y que ya está incluído. Podríamos
entonces hacer lo siguiente:
$class = Zend_CodeGenerator_Php_Class::fromReflection(
new Zend_Reflection_Class('World')
);
<?php
class World
{
110
Zend_CodeGenerator
Ejemplos de Zend_CodeGenerator
Ejemplo 8.1. Generando clases PHP
El siguiente ejemplo genera una clase vacía con una clase de nivel DocBlock.
/**
* Sample generated class
*
* This is a class generated with Zend_CodeGenerator.
*
* @version $Rev:$
* @license New BSD
*
*/
class Foo
{
111
Zend_CodeGenerator
/**
* Sample generated class
*
* This is a class generated with Zend_CodeGenerator.
*
112
Zend_CodeGenerator
* @version $Rev:$
* @license New BSD
*
*/
class Foo
{
113
Zend_CodeGenerator
echo $foo->generate();
/**
* Sample generated class
*
* This is a class generated with Zend_CodeGenerator.
*
* @version $Rev:$
* @license New BSD
*/
114
Zend_CodeGenerator
class Foo
{
/**
* Set the bar property
*
* @param string bar
* @return string
*/
public function setBar($bar)
{
$this->_bar = $bar;
return $this;
}
/**
* Retrieve the bar property
*
* @return string|null
*/
public function getBar()
{
return $this->_bar;
}
En el ejemplo siguiente, asumiremos que ha definido $foo como una de las definiciones de clase del ejemplo anterior.
115
Zend_CodeGenerator
),
),
)),
'body' => 'define(\'APPLICATION_ENV\', \'testing\');',
));
Llamando a generate() generará el código -- pero no lo grabará en un archivo. Usted mismo deberá capturar y
grabar los contenidos en un archivo. Por ejemplo:
$code = $file->generate();
file_put_contents('Foo.php', $code);
<?php
/**
* Foo class file
*
* @license New BSD
*/
/**
* Sample generated class
*
* This is a class generated with Zend_CodeGenerator.
*
* @version $Rev:$
* @license New BSD
*/
class Foo
{
/**
* Set the bar property
*
* @param string bar
* @return string
*/
public function setBar($bar)
{
$this->_bar = $bar;
return $this;
}
116
Zend_CodeGenerator
/**
* Retrieve the bar property
*
* @return string|null
*/
public function getBar()
{
return $this->_bar;
}
define('APPLICATION_ENV', 'testing');
Ejemplo 8.5. Sembrando la generación de código para un archivo PHP via reflection
You can add PHP code to an existing PHP file using the code generator. To do so, you need to first do reflection on
it. The static method fromReflectedFileName() allows you to do this.
$generator = Zend_CodeGenerator_Php_File::fromReflectedFileName($path);
$body = $generator->getBody();
$body .= "\n\$foo->bar();";
file_put_contents($path, $generator->generate());
$generator = Zend_CodeGenerator_Php_Class::fromReflection(
new Zend_Reflection_Class($class)
);
$generator->setMethod(array(
'name' => 'setBaz',
'parameters' => array(
array('name' => 'baz'),
),
'body' => '$this->_baz = $baz;' . "\n" . 'return $this;',
'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
'shortDescription' => 'Set the baz property',
'tags' => array(
new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
'paramName' => 'baz',
'datatype' => 'string'
)),
new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
'datatype' => 'string',
)),
117
Zend_CodeGenerator
),
)),
));
$code = $generator->generate();
Referencias de Zend_CodeGenerator
Clases Abstractas e Interfaces
Zend_CodeGenerator_Abstract
La clase base desde la cual heredan todos las clases CodeGenerator proporciona la funcionalidad mínima necesaria.
Su API es la siguiente:
El constructor primero llama a _init() (que se deja vacía para implementar extenciones a clases concretas), se pasa
entonces el parámetro $options a setOptions(), y finalmente se llama a _prepare() (nuevamente, a ser
implementada por extensión de una clase)
Al igual que la mayoría de las clases en Zend Framework, setOptions() compara una opción clave con setters
existentes en la clase, y pasa el valor de ese método si lo encuentra.
setSourceContent() y getSourceContent() están destinados ya sea para fijar el valor por defecto del
contenido para el código a ser generado, o para sustituir dicho contenido una vez que se completen todas las tareas
de generación.
Zend_CodeGenerator_Php_Abstract
Zend_CodeGenerator_Php_Abstract extiende Zend_CodeGenerator_Abstract, y añade algunas
propiedades para localizar su contenido si es que ha cambiado, así como el nivel de identación que debe aparecer antes
del contenido generado. Su API es la siguiente:
118
Zend_CodeGenerator
Zend_CodeGenerator_Php_Member_Abstract
Zend_CodeGenerator_Php_Member_Abstract es una clase base para generar los miembros de clase --
propiedades y métodos -- y brinda accesos y mutadores para establecer visibilidad; ya sea el miembro abstracto o no,
estático o definitivo; y el nombre del miembro. Su API es la siguiente:
Zend_CodeGenerator_Php_Class
Zend_CodeGenerator_Php_Class Está destinado a generar clases PHP. La funcionalidad básica sólo genera
la clase PHP en si misma, así como opcionalmente el PHP DocBlock. Las clases pueden implementarse o heredarse
de otras clases, y pueden ser marcadas como abstractas. Utilizando otras clases generadoras de código, también puede
agregar constantes de clase, propiedades y métodos.
119
Zend_CodeGenerator
El método setProperty() acepta un array de información que puede ser utilizada para
generar una instancia Zend_CodeGenerator_Php_Property -- o simplemente una instancia de
Zend_CodeGenerator_Php_Property. Análogamente, setMethod() acepta o un array de información para
generar una instancia de Zend_CodeGenerator_Php_Method o una instancia concreta de esa clase.
Zend_CodeGenerator_Php_Docblock
Zend_CodeGenerator_Php_Docblock puede ser utilizada para generar PHP docblocks arbitrarios, incluidas
todas las características estándar de docblock: descripciones cortas y largas y además los tags de anotaciones.
Los tags de anotación pueden establecerse utilizando los métodos setTag() y setTags();
cada una de estas toman o un array describiendo el tag que puede ser pasado al constructor
Zend_CodeGenerator_Php_Docblock_Tag, o una instancia de esa clase.
120
Zend_CodeGenerator
Zend_CodeGenerator_Php_Docblock_Tag
Zend_CodeGenerator_Php_Docblock_Tag está destinado a crear tags de anotaciones arbitrarias para su
inclusión en PHP docblocks. Se espera que los tags (etiquetas) contengan un nombre (la porción que sigue
inmediatamente después del símbolo '@') y una descripción (todo lo que sigue después del nombre del tag).
class Zend_CodeGenerator_Php_Docblock_Tag
extends Zend_CodeGenerator_Php_Abstract
{
public static function fromReflection(
Zend_Reflection_Docblock_Tag $reflectionTag
)
public function setName($name)
public function getName()
public function setDescription($description)
public function getDescription()
public function generate()
}
Zend_CodeGenerator_Php_DocBlock_Tag_Param
Zend_CodeGenerator_Php_DocBlock_Tag_Param es una versión especializada de
Zend_CodeGenerator_Php_DocBlock_Tag, y representa un parámetro del método. El nombre del tag es
por lo tanto ("param"), pero debido al formato de este tag de anotación, es necesaria información adicional a fin de
generarla: el nombre del parámetro y el tipo de datos que representa.
class Zend_CodeGenerator_Php_Docblock_Tag_Param
extends Zend_CodeGenerator_Php_Docblock_Tag
{
public static function fromReflection(
Zend_Reflection_Docblock_Tag $reflectionTagParam
)
public function setDatatype($datatype)
public function getDatatype()
public function setParamName($paramName)
public function getParamName()
public function generate()
}
121
Zend_CodeGenerator
Zend_CodeGenerator_Php_DocBlock_Tag_Return
Al igual la variante del tag docblock, Zend_CodeGenerator_Php_Docblock_Tab_Return es una variante
de un tag de anotación para representar el valor de retorno del método. En este caso, el nombre del tag de anotación
es conocido ("return"), pero requiere un tipo de retorno.
class Zend_CodeGenerator_Php_Docblock_Tag_Param
extends Zend_CodeGenerator_Php_Docblock_Tag
{
public static function fromReflection(
Zend_Reflection_Docblock_Tag $reflectionTagReturn
)
public function setDatatype($datatype)
public function getDatatype()
public function generate()
}
Zend_CodeGenerator_Php_File
Zend_CodeGenerator_Php_File se utiliza para generar el contenido íntegro de un archivo que contiene código
PHP. El archivo puede contener clases o código PHP arbitrario, así como un archivo de nivel docblock si así lo desea.
Cuando se agregan clases al archivo, necesitará pasar o un array de información para pasar al constructor
Zend_CodeGenerator_Php_Class, o una instancia de esa clase. De manera similar, con docblocks, usted
tendrá que pasar información para que lo consuma el constructor Zend_CodeGenerator_Php_Docblock o una
instancia de la clase.
122
Zend_CodeGenerator
Zend_CodeGenerator_Php_Member_Container
Zend_CodeGenerator_Php_Member_Container es usado internamente por
Zend_CodeGenerator_Php_Class para seguir la pista de los los miembros de la clase -- a propiedades y
métodos por igual. Estos están indexados por nombre, utilizando las instancias concretas de los miembros como
valores.
Zend_CodeGenerator_Php_Method
Zend_CodeGenerator_Php_Method describe un método de clase, y puede generar tanto el código y el docblock
para el método. La visibilidad y condición estática, abstracta, o se puede indicar como final, por su clase padre,
Zend_CodeGenerator_Php_Member_Abstract. Finalmente, pueden especificarse los parámetros y valor de
retorno para el método.
Pueden establecerse los parámetros usando setParameter() o setParameters(). En cada caso, un parámetro
debe ser un array de información para pasar al constructor Zend_CodeGenerator_Php_Parameter o una
instancia de esa clase.
class Zend_CodeGenerator_Php_Method
extends Zend_CodeGenerator_Php_Member_Abstract
{
public static function fromReflection(
Zend_Reflection_Method $reflectionMethod
)
public function setDocblock(Zend_CodeGenerator_Php_Docblock $docblock)
public function getDocblock()
public function setFinal($isFinal)
public function setParameters(Array $parameters)
public function setParameter($parameter)
public function getParameters()
public function setBody($body)
public function getBody()
public function generate()
}
Zend_CodeGenerator_Php_Parameter
Zend_CodeGenerator_Php_Parameter puede ser utilizada para especificar parámetros del método. Cada
parámetro puede tener una posición (si no están especificados, se usarán en el orden que estén registrados en el método),
son oblogatorios un valor por defecto, un tipo de datos y un nombre de parámetro.
123
Zend_CodeGenerator
Zend_CodeGenerator_Php_Property
Zend_CodeGenerator_Php_Property describe una propiedad de clase, que puede ser tanto una
constante o una variable. En cada caso, la propiedad puede tener un valor predeterminado asociado con
ella. Además, la visibilidad de las propiedades de la variable puede ser establecida por la clase padre,
Zend_CodeGenerator_Php_Member_Abstract.
class Zend_CodeGenerator_Php_Property
extends Zend_CodeGenerator_Php_Member_Abstract
{
public static function fromReflection(
Zend_Reflection_Property $reflectionProperty
)
public function setConst($const)
public function isConst()
public function setDefaultValue($defaultValue)
public function getDefaultValue()
public function generate()
}
124
Capítulo 9. Zend_Config
Introducción
Zend_Config está diseñado para simplificar el acceso y el uso de datos de configuración dentro de aplicaciones.
Provee una interfaz de usuario basada en propiedades de objetos anidadas para acceder a datos de configuración
dentro del código de la aplicación. Los datos de configuración pueden venir de multitud de medios que soporten
almacenamiento de datos de forma jerárquica. Actualmente Zend_Config provee adaptadores para datos de
configuración que están almacenados en archivos de texto con Zend_Config_Ini y Zend_Config_Xml .
Como se ilustra en el ejemplo de arriba, Zend_Config provee una sintáxis de propiedades de objetos anidados para
acceder a datos de configuración pasados a su constructor.
Junto al acceso a valores de datos orientado a objetos, Zend_Config también tiene el método get() que devolverá
el valor por defecto suministrado si el elemento de datos no existe. Por ejemplo:
125
Zend_Config
// config.php
return array(
'webhost' => 'www.example.com',
'database' => array(
'adapter' => 'pdo_mysql',
'params' => array(
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'dbname' => 'mydatabase'
)
)
);
// Lectura de la configuración
$config = new Zend_Config(require 'config.php');
Aspectos Teóricos
Los datos de configuración se hacen accesibles al constructor Zend_Config a través de un array asociativo, que
puede ser multidimensional, para permitir organizar los datos desde lo general a lo específico. Las clases de adaptador
concretas permiten construir una tabla asociativa para el constructor de Zend_Config a partir de un sistema de
almacenamiento de datos de configuración. Algunos scripts de usuario pueden proveer esos arrays directamente al
constructor Zend_Config, sin usar una clase adaptador, lo cual puede ser apropiado en ciertas ocasiones.
Cada valor del array de datos de configuración se convierte en una propiedad del objeto Zend_Config. La clave es
usada como el nombre de la propiedad. Si un valor es un array por sí solo, entonces la propiedad de objeto resultante es
creada como un nuevo objeto Zend_Config, cargado con los datos del array. Esto ocurre recursivamente, de forma
que una jerarquía de datos de configuración puede ser creada con cualquier número de niveles.
Zend_Config implementa las interfaces Countable e Iterator para facilitar el aceso sencillo a los datos de
configuración. Así, uno puede usar la función count() [http://php.net/count] y constructores PHP como foreach
[http://php.net/foreach] sobre objetos Zend_Config.
Por defecto, los datos de configuración permitidos a través de Zend_Config son de sólo lectura, y una
asignación (e.g., $config->database->host = 'example.com') provoca que se lance una excepción. Este
comportamiento por defecto puede ser sobrescrito a través del constructor, sin embargo, para permitir la modificación
de valores de datos. Además, cuando las modificaciones están permitidas, Zend_Config soporta el borrado de
elementos (unset) (i.e. unset($config->database->host);). El método readOnly() puede ser usado para
determinar si las modificaciones a un objeto Zend_Config están permitidas y el método setReadOnly() puede
ser usado para evitar cualquier modificación posterior a un objeto Zend_Config que fue creado con permiso de
modificaciones.
126
Zend_Config
Nota
Es importante no confundir tales modificaciones en memoria con guardar los datos de configuración a un
medio de almacenamiento específico. Las herramientas para crear y modificar datos de configuración para
distintos medios de almacenamiento están fuera del alcance de Zend_Config. Existen soluciones third-
party de código abierto con el propósito de crear y modificar datos de configuración de distintos medios de
almacenamiento.
Las clases del adaptador heredan de la clase Zend_Config debido a que utilizan su funcionalidad.
La familia de clases Zend_Config permite organizar en secciones los datos de configuración. Los objetos de
adaptador Zend_Config pueden ser cargados con una sola sección especificada, múltiples secciones especificadas,
o todas las secciones (si no se especifica ninguna).
Las clases del adaptador Zend_Config soportan un modelo de herencia única que permite que los datos de
configuración hereden de una sección de datos de configuración a otra. Esto es provisto con el fin de reducir o eliminar
la necesidad de duplicar datos de configuración por distintos motivos. Una sección heredada puede también sobrescribir
los valores que hereda de su sección padre. Al igual que la herencia de clases PHP, una sección puede heredar de
una sección padre, la cual puede heredar de una sección abuela, etc..., pero la herencia múltiple (i.e., la sección C
heredando directamente de las secciones padre A y B) no está permitida.
Si tiene dos objetos Zend_Config, puede combinarlos en un único objeto usando la función merge(). Por ejemplo,
dados $config y $localConfig, puede fusionar datos de $localConfig a $config usando $config-
>merge($localConfig);. Los ítemes en $localConfig sobrescribirán cualquier item con el mismo nombre
en $config.
Nota
El objeto Zend_Config que está ejecutando el merge debe haber sido construido para permitir
modificaciones, pasando true como el segundo parámetro del constructor. El método setReadOnly()
puede entonces ser usado para evitar cualquier modificación posterior después de que el merge se haya
completado.
Zend_Config_Ini
Zend_Config_Ini permite a los desarrolladores almacenar datos de configuración en un formato de datos INI
familiar, y leer de ellos en la aplicación usando una sintáxis de propiedades de objetos anidados. El formato INI se
especializa en proveer tanto la habilidad de mantener una jerarquía de claves de datos (data keys) de configuración
como la de mantener una jerarquía entre secciones de datos de configuración. Las jerarquías de datos de configuración
son provistas separando las claves mediante el carácter punto ( . ). Una sección puede extender o heredar de otra
sección indicando el nombre de la sección seguido de dos puntos ( : ) y el nombre de la sección desde la cual se
quieren heredar los datos.
parse_ini_file
Zend_Config_Ini utiliza la función parse_ini_file() [http://php.net/parse_ini_file] de PHP.
Por favor, revise esta documentación para observar sus comportamientos específicos, que se propagan a
Zend_Config_Ini , tales como la forma en que los valores especiales: true , false , yes , no , y
NULL son manejados.
Separador de clave
Por defecto, el carácter separador de clave es el punto ( . ). Puede ser reemplazado, no obstante,cambiando
la clave de $options llamada 'nestSeparator' al construir el objeto Zend_Config_Ini . Por
ejemplo:
127
Zend_Config
$options['nestSeparator'] = ':';
$config = new Zend_Config_Ini('/path/to/config.ini',
'pruebas',
$options);
Ahora, asuma que el desarrollador de aplicaciones necesita los datos de configuración de la etapa de pruebas del
archivo INI. Resulta fácil cargar estos datos especificando el archivo INI en la sección de la etapa de pruebas:
Nota
Tabla 9.1. Parámetros del constructor Zend_Config_Ini
Parámetros Notas
$filename El archivo INI que se va a cargar.
$section La [sección] contenida en el archivo ini que se va a
cargar. Fijar este parámetro a null cargará todas las
secciones. Alternativamente, se puede introducir un
array de nombres de sección para cargar multiples
secciones.
128
Zend_Config
Parámetros Notas
$options = false Array de opciones. Las siguientes claves están
aceptadas:
Zend_Config_Xml
Zend_Config_Xml permite a los desarrolladores almacenar datos de configuración en un formato sencillo XML y
leerlos a través de una sintáxis de propiedades de objetos anidados. El elemento raíz del archivo XML es irrelevante
y puede ser nombrado arbitrariamente. El primer nivel de elementos XML corresponde con las secciones de datos de
configuración. El formato XML admite organización jerárquica a través del anidamiento de elementos XML bajo los
elementos a nivel de sección. El contenido de un elemento XML a nivel de hoja corresponde al valor de un dato de
configuración. La herencia de sección está permitida por un atributo XML especial llamado extends, y el valor de
este atributo se corresponde con la sección de la cual los datos son heredados por la sección extendida..
Tipo devuelto
Los datos de configuración que se leen en Zend_Config_Xml son siempre devueltos como strings. La
conversión de datos de string a otros tipos se deja en manos de los desarrolladores para que se ajuste a sus
necesidades particulares.
<?xml version="1.0"?>
<configdata>
<production>
<webhost>www.example.com</webhost>
<database>
<adapter>pdo_mysql</adapter>
<params>
<host>db.example.com</host>
<username>dbuser</username>
<password>secret</password>
<dbname>dbname</dbname>
</params>
</database>
</production>
<staging extends="production">
<database>
129
Zend_Config
<params>
<host>dev.example.com</host>
<username>devuser</username>
<password>devsecret</password>
</params>
</database>
</staging>
</configdata>
Ahora, asuma que el desarrollador de aplicaciones necesita los datos de configuración de la fase de pruebas del archivo
XML. Es una tarea sencilla cargar estos datos, especificando el archivo XML y la sección de pruebas:
<?xml version="1.0"?>
<configdata>
<production webhost="www.example.com">
<database adapter="pdo_mysql">
<params host="db.example.com" username="dbuser" password="secret" dbname="dbnam
</database>
</production>
<staging extends="production">
<database>
<params host="dev.example.com" username="devuser" password="devsecret"/>
</database>
</staging>
</configdata>
La otra forma no reduce la configuración, sino que permite mantenerla de forma más fácil dado que no es necesario
escribir el nombre de la etiqueta dos veces. Simplemente, cree una etiqueta vacía con el valor en el atributo value:
<?xml version="1.0"?>
<configdata>
<production>
<webhost>www.example.com</webhost>
<database>
<adapter value="pdo_mysql"/>
<params>
<host value="db.example.com"/>
<username value="dbuser"/>
<password value="secret"/>
<dbname value="dbname"/>
130
Zend_Config
</params>
</database>
</production>
<staging extends="production">
<database>
<params>
<host value="dev.example.com"/>
<username value="devuser"/>
<password value="devsecret"/>
</params>
</database>
</staging>
</configdata>
XML strings
Zend_Config_Xml is able to load an XML string directly, such as that retrieved from a database. The
string is passed as the first parameter to the constructor and must start with the characters '<?xml':
$string = <<<EOT
<?xml version="1.0"?>
<config>
<production>
<db>
<adapter value="pdo_mysql"/>
<params>
<host value="db.example.com"/>
</params>
</db>
</production>
<staging extends="production">
<db>
<params>
<host value="dev.example.com"/>
</params>
</db>
</staging>
</config>
EOT;
131
132
Capítulo 10. Zend_Config_Writer
Zend_Config_Writer
Zend_Config_Writer> gives you the ability to write config files out of Zend_Config objects. It works
with an adapter-less system and thus is very easy to use. By default Zend_Config_Writer> ships with three
adapters, which all work the same. You instantiate a writer with specific options, which can be filename and
config. Then you call the write() method of the writer and the config file is created. You can also give
$filename and $config directly to the write() method. Currently the following writers are shipped with
Zend_Config_Writer>:
• Zend_Config_Writer_Array
• Zend_Config_Writer_Ini
• Zend_Config_Writer_Xml
When modifying or creating a Zend_Config object, there are some things to know. To create or modify a value,
you simply say set the parameter of the Zend_Config object via the parameter accessor (->). To create a section in
the root or to create a branch, you just create a new array ($config->branch = array();). To define which
section extends another one, you call the setExtend() method on the root Zend_Config object.
$config->setExtend('staging', 'production');
$config->production->db = array();
$config->production->db->hostname = 'localhost';
$config->production->db->username = 'production';
$config->staging->db = array();
$config->staging->db->username = 'staging';
// b)
$writer = new Zend_Config_Writer_Xml();
133
Zend_Config_Writer
$writer->setConfig($config)
->setFilename('config.xml')
->write();
// c)
$writer = new Zend_Config_Writer_Xml();
$writer->write('config.xml', $config);
This will create an XML config file with the sections production and staging, where staging extends production.
// Load all sections from an existing config file, while skipping the extends.
$config = new Zend_Config_Ini('config.ini',
null,
array('skipExtends' => true,
'allowModifications' => true));
// Modify a value
$config->production->hostname = 'foobar';
134
Capítulo 11. Zend_Console_Getopt
Introduction
The Zend_Console_Getopt class helps command-line applications to parse their options and arguments.
Users may specify command-line arguments when they execute your application. These arguments have meaning to
the application, to change the behavior in some way, or choose resources, or specify parameters. Many options have
developed customary meaning, for example --verbose enables extra output from many applications. Other options may
have a meaning that is different for each application. For example, -c enables different features in grep, ls, and tar.
Below are a few definitions of terms. Common usage of the terms varies, but this documentation will use the definitions
below.
• "argument": a string that occurs on the command-line following the name of the command. Arguments may be
options or else may appear without an option, to name resources on which the command operates.
• "option": an argument that signifies that the command should change its default behavior in some way.
• "flag": the first part of an option, identifies the purpose of the option. A flag is preceded conventionally by one or
two dashes (- or --). A single dash precedes a single-character flag or a cluster of single-character flags. A double-
dash precedes a multi-character flag. Long flags cannot be clustered.
• "parameter": the secondary part of an option; a data value that may accompany a flag, if it is applicable to the
given option. For example, many commands accept a --verbose option, but typically this option has no parameter.
However, an option like --user almost always requires a following parameter.
A parameter may be given as a separate argument following a flag argument, or as part of the same argument string,
separated from the flag by an equals symbol (=). The latter form is supported only by long flags. For example, -u
username, --user username, and --user=username are forms supported by Zend_Console_Getopt.
• "cluster": multiple single-character flags combined in a single string argument and preceded by a single dash. For
example, "ls -1str" uses a cluster of four short flags. This command is equivalent to "ls -1 -s -t -r". Only single-
character flags can be clustered. You cannot make a cluster of long flags.
For example, in mysql --user=root mydatabase, mysql is a command, --user=root is an option, --user is a flag, root
is a parameter to the option, and mydatabase is an argument but not an option by our definition.
Zend_Console_Getopt provides an interface to declare which flags are valid for your application, output an error
and usage message if they use an invalid flag, and report to your application code which flags the user specified.
135
Zend_Console_Getopt
The constructor takes two more arguments, both of which are optional. The second argument may contain the
command-line arguments. This defaults to $_SERVER['argv'].
The third argument of the constructor may contain an configuration options to customize the behavior of
Zend_Console_Getopt. See Adding Configuration for reference on the options available.
The example above shows using Zend_Console_Getopt to declare that options may be given as -a, -b, or -p.
The latter flag requires a parameter.
The short syntax is limited to flags of a single character. Aliases, parameter types, and help strings are not supported
in the short syntax.
The key of each element of the associative array is a string with a format that names the flag, with any aliases, separated
by the pipe symbol ("|"). Following this series of flag aliases, if the option requires a parameter, is an equals symbol
("=") with a letter that stands for the type of the parameter:
If the parameter is optional, use a dash ("-") instead of the equals symbol.
The value of each element in the associative array is a help string to describe to a user how to use your program.
136
Zend_Console_Getopt
In the example declaration above, there are three options. --apple and -a are aliases for each other, and the option takes
no parameter. --banana and -b are aliases for each other, and the option takes a mandatory integer parameter. Finally,
--pear and -p are aliases for each other, and the option may take an optional string parameter.
The parsing of the data is deferred until the first query you make against the Zend_Console_Getopt object to find
out if an option was given, the object performs its parsing. This allows you to use several method calls to configure
the options, arguments, help strings, and configuration options before parsing takes place.
try {
$opts = new Zend_Console_Getopt('abp:');
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
}
• Option parameter is of the wrong type. E.g. a non-numeric string when an integer was required.
137
Zend_Console_Getopt
Alternatively, you can use the magic __get() function to retrieve the value of an option as if it were a class member
variable. The __isset() magic method is also implemented.
If your options are declared with aliases, you may use any of the aliases for an option in the methods above.
Reporting Options
There are several methods to report the full set of options given by the user on the current command-line.
• As a string: use the toString() method. The options are returned as a space-separated string of flag=value pairs.
The value of an option that does not have a parameter is the literal string "TRUE".
• As an array: use the toArray() method. The options are returned in a simple integer-indexed array of strings,
the flag strings followed by parameter strings, if any.
In all of the above dumping methods, the flag string is the first string in the corresponding list of aliases. For example,
if the option aliases were declared like verbose|v, then the first string, verbose, is used as the canonical name of the
option. The name of the option flag does not include any preceding dashes.
Zend_Console_Getopt supports the GNU convention that an argument consisting of a double-dash signifies the
end of options. Any arguments following this signifier must be treated as non-option arguments. This is useful if you
might have a non-option argument that begins with a dash. For example: "rm -- -filename-with-dash".
138
Zend_Console_Getopt
Configuring Zend_Console_Getopt
Adding Option Rules
You can add more option rules in addition to those you specified in the Zend_Console_Getopt constructor, using
the addRules() method. The argument to addRules() is the same as the first argument to the class constructor.
It is either a string in the format of the short syntax options specification, or else an associative array in the format of
a long syntax options specification. See Declaring Getopt Rules for details on the syntax for specifying options.
The example above shows adding the --verbose option with an alias of -v to a set of options defined in the call
to the constructor. Notice that you can mix short format options and long format options in the same instance of
Zend_Console_Getopt.
If you declared options with aliases, you can use any of the aliases as the key of the associative array.
The setHelp() method is the only way to define help strings if you declared the options using the short syntax.
An alias may be declared only once. If you try to redefine an alias, a Zend_Console_Getopt_Exception is
thrown.
139
Zend_Console_Getopt
In the example above, after declaring these aliases, -a, --apple and --apfel are aliases for each other. Also -p and --
pear are aliases for each other.
The setAliases() method is the only way to define aliases if you declared the options using the short syntax.
Adding Configuration
The third parameter to the Zend_Console_Getopt constructor is an array of configuration options that affect
the behavior of the object instance returned. You can also specify configuration options using the setOptions()
method, or you can set an individual option using the setOption() method.
The currently supported options have const definitions in the class. The options, their const identifiers (with literal
values in parentheses) are listed below:
140
Zend_Console_Getopt
The two arguments to the setOption() method are a configuration option name and an option value.
The argument to the setOptions() method is an associative array. The keys of this array are the configuration
option names, and the values are configuration values. This is also the array format used in the class constructor. The
configuration values you specify are merged with the current configuration; you don't have to list all options.
141
142
Capítulo 12. Zend_Controller
Inicio rápido a Zend_Controller
Introducción
Zend_Controller es el corazón del sistema de MVC de Zend Framework MVC. MVC son las siglas de Modelo-
Vista-Controlador [http://en.wikipedia.org/wiki/Model-view-controller] y es un patrón de diseño con el objetivo de
separar la lógica de la aplicación de la lógica de visualización. Zend_Controller_Front implementa el patrón
Front Controller (Controlador Frontal) [http://www.martinfowler.com/eaaCatalog/frontController.html] en el cual
todas las transacciones HTTP (requests) son interceptadas por el controlador frontal y enviado a una Acción particular
de un Controlador según la URL pedida.
El sistema Zend_Controller fue construido con la extensibilidad en mente, ya sea heredando las clases existentes,
escribiendo nuevas clases que implementan varias interfaces o clases abstractas que forman la base de la familia de
clases del controlador, o escribiendo plugins o helpers de las acciones para aumentar o manipular la funcionalidad
del sistema.
Quick Start
Si necesita información más detallada, mire las secciones siguientes. Si solamente quiere inicializar y ejecutar una
aplicación rápidamente, siga leyendo.
application/
controllers/
IndexController.php
models/
views/
scripts/
index/
index.phtml
helpers/
filters/
html/
.htaccess
index.php
143
Zend_Controller
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
La regla de arriba redirigirá las peticiones a recuros existentes (enlaces simbólicos existentes, archivos no vacíos, o
directorios no vacíos) en consecuencia, y todas las otras peticiones al front controller.
Nota
Las reglas de arriba pertenecen a Apache. Para ejemplos de reglas de rewrite para otros servidores web, mire
la documentación de router .
Zend_Controller_Front::run('/path/to/app/controllers');
Esto instanciará y hará un dispatch del front controller, que redigirá las peticiones a los action controllers.
El dispatcher de Zend_Controller toma entonces el valor del controlador y lo apunta a una clase. Por defecto,
pone en mayúsculas la primera letra del nombre de controlador y agrega la palabra Controller . De esta forma, en
nuestro ejemplo de arriba, el controlador roadmap es dirigido a la clase RoadmapController .
De la misma forma, el valor de action es dirigido a un método de la clase controladora. Por defecto,
el valor se pasa a minúsculas, y la palabra Action es añadida. De esta forma, en nuestro ejemplo de
arriba, la acción components se convierte en componentsAction , y el método final llamado es
RoadmapController::componentsAction() .
Continuando, creemos ahora un action controller y un método de acción por defecto. Como se ha dicho antes, el
controlador por defecto y la acción llamada son ambos index . Abra el archivo application/controllers/
IndexController.php , e introduzca lo siguiente:
/** Zend_Controller_Action */
class IndexController extends Zend_Controller_Action
{
public function indexAction()
144
Zend_Controller
{
}
}
Por defecto, el action helper ViewRenderer está activado. Esto significa que simplemente definiendo un action method
y un view script correspondiente, tendrá su contenido generado inmediatamente. Por defecto, Zend_View es usado
como la capa Vista en el patrón MVC. El ViewRenderer hace algo de magia, y usa el nombre de controlador (e.g.,
index ) y el nombre de acción actual (e.g., index ) para determinar qué plantilla traer. Por defecto, las plantillas
terminan con la extensión .phtml , lo que significa que en el ejemplo de arriba, la plantilla index/index.phtml
será generada. Adicionalmente, el ViewRenderer asume automáticamente que la carpeta views al mismo nivel
que la carpeta controller será la carpeta raíz de la vista, y que el script de vista actual estará en la subcarpeta views/
scripts/. De esta forma, la plantilla generada será encontrada en application/views/scripts/index/
index.phtml .
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Mi primera aplicación Zend Framework</title>
</head>
<body>
<h1>>¡Hola, Mundo!</h1>
</body>
</html>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
145
Zend_Controller
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Error</title>
</head>
<body>
<h1>Ocurrió un error</h1>
<p>Ocurrió un error; Por favor, inténtelo de nuevo más tarde.</p>
</body>
</html>
¡Vea el sitio!
Con su primer controlador y vista, ya puede arrancar su navegador y acceder a su sitio. Asumiendo que example.com
es su dominio, cualquiera de las siguientes URLs le llevará a la página que acaba de crear:
• http://example.com/
• http://example.com/index
• http://example.com/index/index
Ya está listo para empezar a crear más métodos de controladores y acciones. ¡Felicidades!
El siguiente diagrama muestra el flujo de trabajo, y la narrativa que le sigue describe en detalle las interacciones:
146
Zend_Controller
El flujo de procesos de Zend_Controller está implementado por varios componentes. Si bien no es necesario
entender los cimientos de todos estos componentes para utilizar el sistema, tener un conocimiento práctico del proceso
es de mucha utilidad.
147
Zend_Controller
por el servidor y es responsable en última instancia de la delegación de las solicitudes a los ActionControllers
(Zend_Controller_Action).
El router por defecto, Zend_Controller_Router_Rewrite, toma el punto final de una URI como
se especificó en Zend_Controller_Request_Http y la descompone en un controlador, una acción y
parámetros, basándose en la información de la ruta del url. Como ejemplo, la URL http://localhost/foo/
bar/key/value se decodificará para usar el controlador foo, la acción bar y especificar un parámetro key
con el valor de value.
Zend_Controller_Router_Rewrite también puede ser utilizado para igualar las rutas arbitrarios; para más
información, ver documentación del router.
El proceso actual de dispatching(despacho) consta de instanciar la clase del controlador y llamar al método acción
en esa clase. A diferencia del routing, que ocurre sólo una vez, el dispatching(despacho) ocurre en un bucle. Si el
estado del objeto que que envía la solicita es reseteado en cualquier punto, el bucle se repetirá, llamando a cualquier
acción que esté actualmente establecida en la solicitud del objeto. La primera vez el bucle termina con la solicitud
del objeto, el estado de lo enviado se establece a (booleano true), que terminará el procesamiento.
Si desea que su clase controlador o el nombre del método de la acción tenga múltiples MixedCasedWords
o camelCasedWords, para separar las palabras en la url necesitará hacerlo con un '-' o '.' (aunque puede
configurar el carácter utilizado).
148
Zend_Controller
• Zend_Controller_Action es el componente base del controlador de acción. Cada controlador es una sola
clase que extiende la clase Zend_Controller_Action y debe contener uno o más métodos de acción.
El Front Controller
Introducción
Zend_Controller_Front implementa un Front Controller pattern [http://www.martinfowler.com/eaaCatalog/
frontController.html] usado en aplicaciones Model-View-Controller (MVC) [http://en.wikipedia.org/wiki/Model-
view-controller]. Su propósito es inicializar el entorno de la solicitud, rutear la solicitud entrante, y luego hacer un
envío de cualquier de las acciones descubiertas; le agrega las respuestas y las regresa cuando se completa el proceso.
Zend_Controller_Front registra un plugin broker consigo mismo, permitiendo que diversos eventos que
dispara sean observados por plugins. En muchos casos, esto da el desarrollador la oportunidad de adaptar el proceso
de dispatch al sitio sin la necesidad de ampliar el Front Controller para añadir funcionalidad.
Como mínimo, el front controller necesita una o más paths a directorios que contengan action controllers a fin de
hacer su trabajo. Una variedad de métodos también pueden ser invocados para seguir adaptando el medio ambiente
del front controller y ese a sus helper classes.
149
Zend_Controller
Métodos Básicos
El front controller tiene varios accessors para establecer su medio ambiente. Sin embargo, hay tres métodos básicos
clave para la funcionalidad del front controller:
getInstance()
getInstance() se utiliza para recuperar una instancia del front controller. Como el front controller implementa
un patrón Singleton, este también es el único medio posible para instanciar un objeto front controller.
$front = Zend_Controller_Front::getInstance();
setControllerDirectory() y addControllerDirectory
setControllerDirectory() se usa para decirle a el dispatcher dónde buscar para los archivos de clase action
controller. Acepta bien un único path o un array asociativo de pares módulo/path.
Nota
Si usa addControllerDirectory() sin un nombre de módulo, este establecerá el directorio default
para el módulo -- sobreescribiéndolo si ya existe.
addModuleDirectory() y getModuleDirectory()
Uno de los aspectos del front controller es que puede definir una estructura modular de directorio para crear
componentes standalone; estos son llamados "módulos".
Cada módulo debe estar en su propio directorio y ser un espejo de la estructura del directorio del módulo por defecto
-- es decir, que debería tener como mínimo un subdirectorio de "controladores", y típicamente un subdirectorio de
"views" y otros subdirectorios de aplicaciones.
150
Zend_Controller
addModuleDirectory() permite pasar el nombre de un directorio que contiene uno o más directorios de módulos.
A continuación lo analiza y los añade como directorios de controladores al front controller.
Después, si quiere determinar el path a un determinado módulo o al módulo actual, puede llamar a
getModuleDirectory(), opcionalmente puede pasar un nombre de módulo para conseguir el directorio de ese
módulo específico.
dispatch()
dispatch(Zend_Controller_Request_Abstract $request = null,
Zend_Controller_Response_Abstract $response = null) hace el trabajo pesado del front controller.
Puede opcionalmente tomar un request object y/o un response object, permitiendo al desarrollador pasar objetos
peronalizados para cada uno.
Si no se pasa ningun objeto solicitud o respuesta, dispatch() comprobará por objetos previamente registrados y
utilizará esos o instanciará versiones por defecto a utilizar en su proceso (en ambos casos, el sabor de HTTP será
utilizado por defecto).
Similarmente, dispatch() comprueba los objetos registrados router y dispatcher , instanciando las versiones por
defecto de cada uno si ninguno de ellos se encuentra.
• Routing
• Dispatching
• Response
El routing se lleva a cabo exactamente una vez, utilizando los valores del objeto solicitud cuando se llama a
dispatch(). El dispatching se lleva a cabo en un bucle; una solicitud puede indicar, bien múltiples acciones de
dispatch, o el controlador o un plugin pueden restablecer el objeto solicitud para forzar medidas adicionales para
dispatch. Cuando todo está hecho, el front controller devuelve una respuesta.
run()
Zend_Controller_Front::run($path) es un método estático que toma simplemente un path a un directorio
que contiene controladores. Obtiene una instancia del front controller (via getInstance(), registra el path provisto via
setControllerDirectory(), y finalmente dispatches.
Básicamente, run() es un método conveniente que pueden utilizarse para setups de sitios que no requieran la
personalización del medio ambiente del front controller.
• resetInstance() puede ser utilizada para borrar todos los settings actuales. Su objetivo principal es para
testing, pero también puede ser utilizada para instancias donde desee encadenar múltiples front controllers.
151
Zend_Controller
• (set|get)Request() le permite especificar la clase u objeto el request a usar durante el proceso de dispatch
y recuperar el objeto actual. Al setear el objeto solicitud, puede pasarlo en un nombre de clase de solicitud, en cuyo
caso el método va a cargar el archivo clase y lo instanciará.
• (set|get)Router() le permite especificar la clase u objeto el router a usar durante el proceso de dispatch y
recuperar el objeto actual. Al setear el objeto router, puede pasarlo en un nombre de clase de router, en cuyo caso
el método va a cargar el archivo clase y lo instanciará.
Al recuperar el objeto router, en primer lugar comprueba para ver si hay alguno presente, y si no, instancia al router
por defecto(reescribe el router).
• (set|get)BaseUrl() le permite especificar la URL base de la cual tirar cuando se rutean peticiones y recuperar
el valor actual. El valor se provee al objeto solicitud justo antes de rutear.
Al recuperar el objeto dispatch, en primer lugar comprueba para ver si hay alguno presente, y si no, instancia al
dispatcher por defecto.
• (set|get)Response() le permite especificar la clase u objeto response a usar durante el proceso de dispatch
y recuperar el objeto actual. Al setear el objeto response, puede pasarlo en un nombre de clase de response, en cuyo
caso el método va a cargar el archivo clase y lo instanciará.
• unregisterPlugin($plugin) le permite desregistrar plugin objects. $plugin puede ser tanto un objeto
plugin o un string que denota la clase de plugin a desregistrar.
• returnResponse($flag) se usa para decirle al front controller cuando regresar la respuesta (true) desde
dispatch(), o si la respuesta debe ser emitida automáticamente (false). Por defecto, la respuesta es
automáticamente emitida (llamando a Zend_Controller_Response_Abstract::sendResponse());
activando returnResponse()) se anulará este comportamiento.
Las razones para regresar la respuesta incluyen un deseo de comprobar las excepciones antes de emitir la respuesta,
necesidad de hacer un log de diversos aspectos de la respuesta (tales como cabeceras), etc.
152
Zend_Controller
-- objetos y variables -- con el front controller, a ser devueltos en cualquier momento en la cadena de dispatch. Estos
valores se transmiten al router, al dispatcher, y a los action controllers. Los métodos incluyen:
• setParam($name, $value) permite establecer un único parámetro de $name con el valor $value.
• setParams(array $params) permite configurar varios parámetros a la vez usando un array asociativo.
• getParam($name) permite recuperar un único parámetro a la vez, utilizando como identificador a $name.
• clearParams() permite borrar un único parámetro (pasando un string identificador), parámetros con múltiples
nombres (pasando un array de strings identificadores), o el stack de parámetros completo (pasando nada).
Hay varios parámetros pre-definidos que puede ser seteados para tener usos específicos en la cadena de dispatch:
• useDefaultControllerAlways se usa para indicar a el dispatcher que utilice el controlador por defecto en el
módulo por defecto de cualquier solicitud que no sea dispatchable (es decir, el módulo, el controlador y/o la acción
no existen). Por defecto, está en off.
Ver the section called “Excepciones MVC que Usted Pueda Encontrar” para información más detallada sobre el
uso de este setting.
• disableOutputBuffering se usa para indicarle a el dispatcher que no debe utilizar output buffering para
capturar la salida generada por los controladores de acción. Por defecto, el dispatcher captura cualquier salida y la
añade al contenido del cuerpo del objeto respuesta.
• noViewRenderer se usa para deshabilitar el ViewRenderer. Poniendo este parámetro a true, lo deshabilita.
• noErrorHandler se usa para deshabilitar el Error Handler plugin. Poniendo este parámetro a true, lo deshabilita.
return self::$_instance;
}
}
Típicamente, no necesitará una subclase del front controller a menos que necesite añadir nuevas funcionalidades (por
ejemplo, un plugin autoloader, o una forma de especificar los paths de los action helpers). Algunos de los puntos
donde puede querer modificar el comportamiento puede incluir modificar cómo son almacenados los directorios de
controladores , o qué router predeterminado o dispatcher se utiliza.
153
Zend_Controller
• El nombre de la acción que llamar dentro del controlador es accedido por getActionName() y
setActionName().
• Los parámetros accesibles por la acción son un array asociativo de pares clave/valor que son recuperados
por getParams() y configurados con setParams(), o configurados individualmente por getParam() y
setParam().
Basado en el tipo de solicitud, puede haber más métodos disponibles. La solicitud por defecto usada,
Zend_Controller_Request_Http, por ejemplo, tiene métodos para recuperar la URI de la solicitud, ruta de
la información, parámetros $_GET y $_POST, etc.
El objeto request es pasado al controlador front, o si no es provisto, es instanciado al principio del proceso dispatcher,
antes de que ocurra el enrutamiento. Es pasado a través de todos los objetos en la cadena del dispatcher.
Adicionalmente, la solicitud objeto es particularmente útil en pruebas. El desarrolador puede cambiar el entorno de la
solicitud, incluyendo módulos, controladores, acciones, parámetros, URI, etc, y pasar la solicitud objeto al controlador
front para probar el flujo de la aplicación. Cuando se vincula con el objeto respuesta , es posible elaborar y precisar
una unidad de pruebas de aplicaciones MVC.
Solicitud HTTP
Solicitud de acceso a datos
Zend_Controller_Request_Http encapsula el acceso a relevantes valores tal como el nombre de la llave y
el valor para el controlador y variables de aación enrutamiento y todos los parámetros adicionales analizados desde
el URI. Adiccionalmente permite el acceso a valores contenidos en las superglobales como miembros públicos y
administra la actual base URL y la solicitud URI. los valores Superglobales no pueden ser determinados en una solicitud
objeto, en vez usar los métodos setParam/getParam para determinar o recuperar los parámetros del usuario.
Datos Superglobales
Cuando se accede a datos Superglobales a través Zend_Controller_Request_Http como
propiedades de miembros públicos, es necesario mantener en mente que el nombre de la propiedad
(supergloabl array key) corresponda a una supergloabl en un específico orden de precedencia:1. GET, 2.
POST, 3. COOKIE, 4. SERVER, 5. ENV.
Las supergloables específicas pueden ser accedidas usando un método público como una alternativa. Por ejemplo, el
valor original de $_POST['user'] puede ser accedido llamando a getPost('user') en la solicitud objeto.
Esto incluye getQuery() para recuperar elementos $_GET, y getHeader() para recuperar la solicitud de los
encabezadores (headers).
154
Zend_Controller
Esto es primordialmente útil para aceptar el contenido cuando se desarrolla una aplicación MVC simple.
Usted puede determinar parámetros de usuario en la solicitud objeto usando setParam() y recuperar los mismos
despues usando getParam(). El router hace uso de esta funcionalidad para determinar parámetros correspondientes
en la solicitud URI a la solicitud objeto.
Si se desea sacar solo desde parámetros se configura a través de setParam(), use getUserParam().
Además, a partir de 1.5.0, puede bloquear el parámetro que se buscará en las fuentes.
setParamSources() le permite especificar un array vacío o un array con uno o más de los valores '_GET'
o '_POST', indicando que fuente de parámetro se permite (por defecto, ambos son permitidos); si se desea
restringir el acceso a solamente '_GET' especificar setParamSources (array('_GET')).
Apache Quirks
Si está usando Apache 404 handler para pasar If you are using Apache's 404 handler to pass incoming requests
to the front controller, or using a PT flag with rewrite rules, $_SERVER['REDIRECT_URL'] contains
the URI you need, not $_SERVER['REQUEST_URI']. If you are using such a setup and getting invalid
routing, you should use the Zend_Controller_Request_Apache404 class instead of the default Http
class for your request object:
This class extends the Zend_Controller_Request_Http class and simply modifies the autodiscovery
of the request URI. It can be used as a drop-in replacement.
For example, if you keep your index.php in a webserver subdirectory named /projects/myapp/index.php,
base URL (rewrite base) should be set to /projects/myapp. This string will then be stripped from the beginning
of the path before calculating any route matches. This frees one from the necessity of prepending it to any of
your routes. A route of 'user/:username' will match URIs like http://localhost/projects/myapp/
user/martel and http://example.com/user/martel.
155
Zend_Controller
Should base URL be detected incorrectly you can override it with your own base path with the
help of the setBaseUrl() method of either the Zend_Controller_Request_Http class, or the
Zend_Controller_Front class. The easiest method is to set it in Zend_Controller_Front, which will
proxy it into the request object. Ejemplo usage to set a custom base URL:
/**
* Dispatch Request with custom base URL with Zend_Controller_Front.
*/
$router = new Zend_Controller_Router_Rewrite();
$controller = Zend_Controller_Front::getInstance();
$controller->setControllerDirectory('./application/controllers')
->setRouter($router)
->setBaseUrl('/projects/myapp'); // set the base url!
$response = $controller->dispatch();
• isGet()
• isPost()
• isPut()
• isDelete()
• isHead()
• isOptions()
The primary use case for these is for creating RESTful MVC architectures.
Currently, this header is known to be passed by default with the following JS libraries:
• Yahoo! UI Library
• jQuery
• MochiKit
156
Zend_Controller
Most AJAX libraries allow you to send custom HTTP request headers; if your library does not send this header, simply
add it as a request header to ensure the isXmlHttpRequest() method works for you.
/**
* @param string $value
* @return self
*/
public function setControllerName($value);
/**
* @return string
*/
public function getActionName();
/**
* @param string $value
* @return self
*/
public function setActionName($value);
/**
* @return string
*/
public function getControllerKey();
/**
* @param string $key
* @return self
*/
public function setControllerKey($key);
/**
* @return string
*/
public function getActionKey();
/**
* @param string $key
* @return self
*/
public function setActionKey($key);
157
Zend_Controller
/**
* @param string $key
* @return mixed
*/
public function getParam($key);
/**
* @param string $key
* @param mixed $value
* @return self
*/
public function setParam($key, $value);
/**
* @return array
*/
public function getParams();
/**
* @param array $array
* @return self
*/
public function setParams(array $array);
/**
* @param boolean $flag
* @return self
*/
public function setDispatched($flag = true);
/**
* @return boolean
*/
public function isDispatched();
}
La solicitud objeto es un contenedor para entorno de la solicitud. La cadena del controlador sólo necesita saber cómo
establecer y recuperar el controlador, la acción, los parámetros opcionales, y el estado del despachador. Por defecto,
la solicitud buscará sus propios parámetros mediante el controlador o las llaves de la acción con el fin de determinar
el controlador y la acción.
Para ampliar esta clase, o uno de sus derivados, cuando se necesita la clase solicitud que interactue con un entorno
específico con el fin de recuperar los datos para su uso en las tareas antes descritas. Los ejemplos incluyen el entorno
HTTP , un entorno CLI, o un entorno de PHP-GTK.
El Router Standard
Introducción
Zend_Controller_Router_Rewrite Es el router standard del Framework. Routing es el proceso de tomar la
parte final de una URI (la parte de la URI que viene después de la URL base) y la descomposición en parámetros para
determinar qué módulo, qué controlador y acción de ese controlador debe recibir la solicitud. Estos valores del módulo,
158
Zend_Controller
Zend_Controller_Router_Rewrite está diseñado para permitir que una funcionalidad tipo mod_rewrite se
pueda usar en estructuras PHP puras. Se basa muy vagamente en el routing de Ruby on Rails (RoR) y no requiere
ningún conocimiento previo de reescritura de la URL del webserver. Está diseñado para trabajar con solo una regla
mod_rewrite de Apache (one of):
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php
o (preferido):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
El router rewrite también puede utilizarse con el IIS webserver (versions <= 7.0) si Isapi_Rewrite [http://
www.isapirewrite.com] se ha instalado como una extensión Isapi con la siguiente regla de reescribir:
IIS Isapi_Rewrite
Cuando se usa IIS, $_SERVER['REQUEST_URI'] puede no existir, o establecerlo como un
string vacío. En este caso, Zend_Controller_Request_Http intentará usar el valor de
$_SERVER['HTTP_X_REWRITE_URL'] establecido por la extensión Isapi_Rewrite.
IIS 7.0 introduce un módulo nativo de reescribir la URL, y puede ser configurado como sigue:
159
Zend_Controller
<para>
Si está usando Lighttpd, la siguiente regla de reescritura es válida:
</para>
<programlisting language="lighttpd"><![CDATA[
url.rewrite-once = (
".*\?(.*)$" => "/index.php?$1",
".*\.(js|ico|gif|jpg|png|css|html)$" => "$0",
"" => "/index.php"
)
Usando un Router
Para utilizar adecuadamente el router de reescritura debe instanciarlo, agregar algunas rutas definidas por el usuario y
luego inyectarlo en el controlador. El siguiente código ilustra el procedimiento:
// Crear un router
$router->addRoute('user',
new Zend_Controller_Router_Route('user/:username'));
El Rewrite Router viene con seis tipos básicos de rutas (uno de los cuales es especial):
160
Zend_Controller
Las rutas pueden ser utilizadas numerosas veces para crear una cadena o un esquema de aplicación de ruteo definido por
el usuario. Puede usar cualquier número de rutas en cualquier configuración, con la excepción de la ruta del Módulo,
la cual debe ser utilizada una vez y probablemente como la ruta más genérica (es decir, por defecto). Cada ruta se
describe en mayor detalle más adelante.
El primer parámetro a addRoute es el nombre de la ruta. Se utiliza como un manejador para sacar las rutas del router
(por ejemplo, con fines de generación de URL). El segundo parámetro es la ruta misma.
Nota
El uso más común del nombre de ruta es por medio del ayudante de url Zend_View:
<a href=
"<?php echo $this->url(array('username' => 'martel'), 'user') ?>">Martel</a>
El routing es un simple proceso de iteración a través de todas las rutas provistas y la equiparación de sus definiciones
con la petición actual de URI. Cuando se encuentra una concordancia, se devuelven valores de variables desde la
instancia Route y se inyecta en el objeto Zend_Controller_Request para su posterior utilización en el dispatcher
así también como en los controladores creados por el usuario. En caso de no encontrar ninguna concordancia, se
comprobará la siguiente ruta en la cadena.
Si necesita determinar en qué ruta se encontró una concordancia, puede usar el método getCurrentRouteName(),
que devolverá el identificador usado cuando registró la ruta con el router. Si quiere el objeto de la ruta actual, puede
usar getCurrentRoute().
Matching Inverso
Las rutas están equiparadas en orden inverso para asegurarse que las rutas más genéricas se definan primero.
Valores Retornados
Los valores retornados del routing provienen de parámetros URL o de rutas definidas por
defecto por el usuario. Estas variables son accesibles posteriormente a través de los métodos
Zend_Controller_Request::getParam() o Zend_Controller_Action::_getParam().
Hay tres variables que pueden utilizarse en las rutas - 'module', 'controller' y 'action'. Estas variables especiales
son utilizados por Zend_Controller_Dispatcher para encontrar un controlador y una acción para hacer el
dispatch.
Variables Especiales
Los nombres de estas variables especiales pueden ser diferentes si elige alterar los valores por
defecto en Zend_Controller_Request_Http mediante los métodos setControllerKey y
setActionKey.
161
Zend_Controller
elemento del path, permitiendo URIs de la forma module/controller/action. Por último, también coincidrá
con cualquier parámetro adicional agregado a la URI por defecto - controller/action/var1/value1/
var2/value2.
// Asumiendo lo siguiente:
$ctrl->setControllerDirectory(
array(
'default' => '/path/to/default/controllers',
'news' => '/path/to/news/controllers',
'blog' => '/path/to/blog/controllers'
)
);
Módulo únicamente:
http://example/news
module == news
Módulo + controlador:
http://example/blog/archive
module == blog
controller == archive
Si no quiere esta ruta en particular en su esquema por defecto de routing, podrá anularla creando su
propia ruta por 'defecto' (es decir, almacenar bajo el nombre de 'default') o eliminarla por completo usando
removeDefaultRoutes():
162
Zend_Controller
Si la URL base se detecta incorrectamente se la puede anular con su propio path de base usando
Zend_Controller_Request_Http y llamando al método setBaseUrl() (ver the section called “Base Url
and Subdirectories”):
$request->setBaseUrl('/~user/application-root/');
Parámetros Globales
Puede establecer los parámetros globales en un router que se proporcionan automáticamente a una ruta cuando
se ensamblasn mediante setGlobalParam. Si se establece un parámetro global pero también se lo entrega
directamente al método de ensamblaje, el parámetro del usuario sobreescribe al parámetro global. Puede establecer
un parámetro global esta forma:
$router->setGlobalParam('lang', 'en');
Tipos de Route
Zend_Controller_Router_Route
Zend_Controller_Router_Route es la ruta standard del framework. Combina la facilidad de uso con la
flexibilidad para la definición de rutas. Cada ruta consiste fundamentalmente en el mapeo de la URL (de partes estáticas
y dinámicas (variables)) y puede ser iniciada con valores predeterminados así como con requisitos variables.
Imaginemos que nuestra aplicación ficticia necesitará algunas páginas informativas sobre los autores del contenido.
Queremos ser capaces de apuntar nuestro navegador web a http://domain.com/author/martel para ver la
información sobre este muchacho "martel". La ruta para esa funcionalidad podría parecerse a:
$router->addRoute('user', $route);
163
Zend_Controller
Uso de Caracteres
La implementación actual le permite utilizar cualquier carácter (salvo una barra) como un identificador
de variable, pero se recomienda encarecidamente que se utilicen sólo caracteres que sean válidos para
identificadores de variables PHP. En implementaciones futuras se podría alterar este comportamiento,
resultando en probables fallos escondidos en su código.
Este ejemplo de ruta debería ser coincidente cuando apunta su navegador a http://domain.com/author/
martel, en cuyo caso todas sus variables se inyectan al objeto Zend_Controller_Request y quedando
accesibles en ProfileController. Las variables devueltas por este ejemplo pueden ser representadas como el
siguiente array de pares clave/valor:
$values = array(
'username' => 'martel',
'controller' => 'profile',
'action' => 'userinfo'
);
$username = $this->_getParam('username');
}
La definición de ruta puede contener uno o más caracteres especiales - un comodín - representado por el símbolo '*'.
Se utiliza para reunir parámetros al igual que el valor de ruta por defecto del Módulo (var => pares de valores definidos
en la URI). La siguiente ruta imita más o menos el comportamiento de la ruta del Módulo:
164
Zend_Controller
$router->addRoute('archive', $route);
Este ejemplo resultará en inyectar una variable año al objeto solicitud. Ya que no hay información de enrutamiento
presente (no se define ningún controlador ni parámetros de acción), la solicitud será enviada al controlador y al método
de acción por defecto (que a la vez ambos están definidos en Zend_Controller_Dispatcher_Abstract).
Para hacerlos más utilizables, tiene que proporcionar un controlador válido y una acción válida como la ruta por defecto:
Con una ruta definida como la de arriba, el router comparará solo cuando la variable año contenga datos numéricos,
eg. http://domain.com/archive/2345. Una URL como http://example.com/archive/test no
se comparará y en su lugar el control se pasará a la próxima ruta en la cadena.
Segmentos Traducidos
El standard de ruta brinda apoyo a la traducción de segmentos. Para utilizar esta característica, tiene que definir por lo
menos un traductor (una instancia de Zend_Translate) mediante una de las siguientes formas:
165
Zend_Controller
Por defecto, se utilizará el "locale" especificado en la instancia Zend_Translate. Para anularlo, debe setearlo
(como una instancia de Zend_Locale o un string local) de una de las siguientes maneras:
Los segmentos traducidos se dividen en dos partes. Los segmentos fijos están precedidos por un único signo @, y serán
traducidos al "locale" actual para el ensamblaje y se revierten al ID del mensaje cuando se acepte nuevamente. Los
segmentos dinámicos tienen el prefijo :@. Para el ensamblaje, el parámetro dado será traducido y se insertará en la
posición del parámetro. Cuando se acepte, el parámetro traducido de la URL volverá al ID del mensaje nuevamente.
La siguiente es la forma más sencilla para preparar el itinerario normal para el uso de la traducción del segmento:
// Prepare el traductor
$translator = new Zend_Translate('array', array(), 'en');
$translator->addTranslation(array('archive' => 'archiv',
'year' => 'jahr',
'month' => 'monat',
'index' => 'uebersicht'),
'de');
// Crear la ruta
$route = new Zend_Controller_Router_Route(
'@archive',
array(
'controller' => 'archive',
'action' => 'index'
)
);
$router->addRoute('archive', $route);
166
Zend_Controller
Puede usar segmentos dinámicos para crear veriones traducidas como del tipo módulo-ruta:
// Crear la ruta
$route = new Zend_Controller_Router_Route(
':@controller/:@action/*',
array(
'controller' => 'index',
'action' => 'index'
)
);
$router->addRoute('archive', $route);
// Crear la ruta
$route = new Zend_Controller_Router_Route(
'@archive/:@mode/:value',
array(
'mode' => 'year'
'value' => 2005,
'controller' => 'archive',
'action' => 'show'
),
array('mode' => '(month|year)'
'value' => '\d+')
);
$router->addRoute('archive', $route);
Zend_Controller_Router_Route_Static
Los ejemplos sobre todo usan rutas dinámicas -- rutas que contienen patrones contra los cuales comparar. A veces, sin
embargo, una ruta en particular pareciera estar seteada en piedra, y ejecutar el motor de expresiones regulares sería
excesivo. La respuesta a esta situación es utilizar rutas estáticas:
167
Zend_Controller
);
$router->addRoute('login', $route);
Como regla general, siempre proporcione cada uno de los siguientes valores por defecto:
• controller
• action
$front->setParam('useDefaultControllerAlways', true);
Sin embargo, esto es considerado un rodeo; siempre es mejor definir explícitamente valores correctos o sanos
por defecto.
Zend_Controller_Router_Route_Regex
Además de los tipos de ruta estáticos y por defecto, también está disponible el tipo de ruta Expresión Regular. Esta
ruta ofrece más potencia y flexibilidad que los otros, pero a costa de un ligero aumento en la complejidad. Al mismo
tiempo, debería ser más rápido que la standard Route.
Al igual que la standard Route, esta ruta tiene que ser inicializada con una definición de ruta y algunos valores
predeterminados. Vamos a crear un archivo ruta como un ejemplo, similar al previamente definido, sólo que esta vez
usaremos la ruta Regex:
Cada sub-patrón regex definido será inyectado al objeto solicitud. Con nuestro ejemplo anterior, después de un
matching exitoso http://domain.com/archive/2006, el valor resultante del array puede verse como:
$values = array(
168
Zend_Controller
1 => '2006',
'controller' => 'archive',
'action' => 'show'
);
Nota
Las barras de comienzo y final están recortadas de la URL en el Router antes de una concordancia. Como
resultado, coincidendo con la URL http://domain.com/foo/bar/, involucraría al regex de foo/
bar, y no a /foo/bar.
Nota
Las anclas de comienzo y fin de línea ('^' y '$', respectivamente) son automáticamente antepuestas y
pospuestas a todas las expresiones. Así, no debe usar éstas en sus expresiones regulares, y debe coincidir
con el string completo.
Nota
Esta clase de ruta usa el carácter # como un delimitador. Esto significa que necesitará caracteres hash ('#') para
escapar pero no barras ('/') en sus definiciones de ruta. Dado que el carácter '#' (llamado ancla) es raramente
pasado al webserver, será muy rara la necesidad de utilizar ese carácter en su regex.
Nota
Tenga en cuenta que la clave es un entero (1) en lugar de un string ('1').
Sin embargo, esta ruta no funciona exactamente igual que su contraparte standard route dado que el valor por defecto
para 'year' todavía no se ha establecido. Y lo que puede ser no tan evidente es que tendremos un problema con una
barra final incluso si declaramos por defecto el año y hacemos opcional al sub-patrón. La solución es hacer que toda
la parte del año sea opcional junto con la barra pero capturar solo la parte numérica:
Ahora, ocupemósnos del problema que probablemente haya notado. Utilizar claves basadas en enteros para los
parámetros no es una solución fácilmente manejable y puede ser potencialmente problemática a largo plazo. Y aquí es
169
Zend_Controller
donde entra el tercer parámetro. Este parámetro es un array asociativo que representa un mapa de sub-patrones regex
a nombres de clave de parámetros. Trabajemos en nuestro ejemplo más fácil:
$values = array(
'year' => '2006',
'controller' => 'archive',
'action' => 'show'
);
El mapa puede ser definido en cualquier dirección para hacer que funcione en cualquier ambiente. Las claves pueden
contener nombres de variables o índices de sub-patrones:
// O
Nota
Las claves de los sub-patrones deben respresentarse por enteros.
Observe que el índice numérico en los valores del Request ahora han desaparecido y en su lugar se muestra una variable
nombrada. Por supuesto que puede mezclar variables nombradas y numéricas si lo desea:
170
Zend_Controller
Lo que resultará en una mezcla de valores disponibles en la solicitud. Como ejemplo, la URL http://
domain.com/archive/2006/page/10 resultará con los siguientes valores:
$values = array(
'year' => '2006',
2 => 10,
'controller' => 'archive',
'action' => 'show'
);
Dado que los patrones regex no pueden invertirse fácilmente, tendrá que preparar una URL inversa si desea usar un
ayudante de URL o incluso un método de ensamble de esta clase. Este path inverso está representado por un string
parseable por sprintf() y se define como el cuarto parámetro del constructor:
Todo esto es algo que ya fue posible de hacer por medio de un objeto de ruta estandard, por lo tanto podría
preguntarese: ¿cuál es la ventaja de utilizar la ruta Regex?. Principalmente, le permite describir cualquier tipo de
URL sin restricción alguna. Imagínese que tiene un blog y desea crear URLs como: http://domain.com/blog/
archive/01-Using_the_Regex_Router.html, y que tiene que descomponer el último elemento del path
01-Using_the_Regex_Router.html, en un ID de artículo y en el título/descripción del artículo; esto no es
posible con el standard route. Con la ruta Regex, puede hacer algo como la siguiente solución:
Como puede ver, esto añade una enorme cantidad de flexibilidad por encima del standard route.
Zend_Controller_Router_Route_Hostname
Zend_Controller_Router_Route_Hostname es la ruta del framework en el servidor. Funciona similarmente
a la standard route, pero funciona con el nombre del host de la URL llamada, en lugar del path.
Vamos a usar el ejemplo de la standard route y ver cómo se vería con un nombre basado en host. En lugar de llamar al
usuario mediante un path, quisiéramos que un usuario pueda llamar a http://martel.users.example.com
para ver la información acerca del usuario "martel".
171
Zend_Controller
$router->addRoute('user', $hostnameRoute->chain($plainPathRoute);
Las rutas del nombre del host pueden, pero nunca deben ser utilizadas así. La razón detrás de esto es que la ruta
del nombre del host solamente, concordaría con cualquier path. Entonces, lo que tiene que hacer es encadenar una
ruta del path a la ruta del nombre del host. Esto se hace como en el ejemplo llamando a $hostnameRoute-
>chain($pathRoute);. Haciendo esto, $hostnameRoute no se modifica, pero devuelve una nueva ruta
(Zend_Controller_Router_Route_Chain), que luego puede ser entregada al router.
Zend_Controller_Router_Route_Chain
Zend_Controller_Router_Route_Chain es una ruta que permite encadenar juntas a múltiples rutas. Esto le
permite encadenar hostname/rutas y rutas de paths, o múltiples paths de rutas por ejemplo. El encadenamiento puede
hacerse programáticamente o dentro de un archivo de configuración.
Prioridad de Parámetros
Cuando se encadenan en conjunto varias rutas, los parámetros de la ruta exterior tienen mayor prioridad que
los parámetros de la ruta interior. Así, si define un controlador en el exterior y otro en la ruta interior, será
seleccionado el controlador de la ruta exterior.
Cuando el encadenamiento se realiza prográmaticamente, hay dos maneras de archivarlo. La primera consiste en crear
una nueva instancia Zend_Controller_Router_Route_Chain y entones llamar al método chain varias
veces con todas las rutas que deberían encadenarse juntas. La otra forma es tomar la primera ruta, por ejemplo, la
ruta del nombre del host, y llamar al método chain con la ruta que debería ser anexada a ella. Esto no modificará la
ruta del nombre del host, pero devolverá una nueva instancia de Zend_Controller_Router_Route_Chain,
teniendo entonces a ambas rutas encadenadas juntas:
172
Zend_Controller
Cuando las rutas se encadenan juntas, su separador por defecto es una barra ('/'). Pueden haber casos cuando quiera
tener un separador diferente:
<routes>
<www type="Zend_Controller_Router_Route_Hostname">
<route>www.example.com</route>
<chains>
<language type="Zend_Controller_Router_Route">
<route>:language</route>
<reqs language="[a-z]{2}">
<chains>
<index type="Zend_Controller_Router_Route_Static">
<route></route>
<defaults module="default" controller="index" action="index" />
</index>
<imprint type="Zend_Controller_Router_Route_Static">
<route>imprint</route>
<defaults module="default" controller="index" action="index" />
</imprint>
</chains>
</language>
</chains>
</www>
<users type="Zend_Controller_Router_Route_Hostname">
<route>users.example.com</route>
<chains>
<profile type="Zend_Controller_Router_Route">
<route>:username</route>
<defaults module="users" controller="profile" action="index" />
</profile>
</chains>
</users>
<misc type="Zend_Controller_Router_Route_Static">
<route>misc</route>
</misc>
173
Zend_Controller
</routes>
La manera alternativa de crear una ruta encadenada es a través del parámetro chain, que sólo puede utilizarse
directamente con el tipo cadena-ruta, y también trabaja en el nivel raíz:
<routes>
<www type="Zend_Controller_Router_Route_Chain">
<route>www.example.com</route>
</www>
<language type="Zend_Controller_Router_Route">
<route>:language</route>
<reqs language="[a-z]{2}">
</language>
<index type="Zend_Controller_Router_Route_Static">
<route></route>
<defaults module="default" controller="index" action="index" />
</index>
<imprint type="Zend_Controller_Router_Route_Static">
<route>imprint</route>
<defaults module="default" controller="index" action="index" />
</imprint>
<www-index type="Zend_Controller_Router_Route_Chain">
<chain>www, language, index</chain>
</www-index>
<www-imprint type="Zend_Controller_Router_Route_Chain">
<chain>www, language, imprint</chain>
</www-imprint>
</routes>
También puede darle el parámetro a chain como un array en vez de separ las rutas con comas:
<routes>
<www-index type="Zend_Controller_Router_Route_Chain">
<chain>www</chain>
<chain>language</chain>
<chain>index</chain>
</www-index>
<www-imprint type="Zend_Controller_Router_Route_Chain">
<chain>www</chain>
<chain>language</chain>
<chain>imprint</chain>
</www-imprint>
</routes>
174
Zend_Controller
[production]
routes.archive.route = "archive/:year/*"
routes.archive.defaults.controller = archive
routes.archive.defaults.action = show
routes.archive.defaults.year = 2000
routes.archive.reqs.year = "\d+"
routes.news.type = "Zend_Controller_Router_Route_Static"
routes.news.route = "news"
routes.news.defaults.controller = "news"
routes.news.defaults.action = "list"
routes.archive.type = "Zend_Controller_Router_Route_Regex"
routes.archive.route = "archive/(\d+)"
routes.archive.defaults.controller = "archive"
routes.archive.defaults.action = "show"
routes.archive.map.1 = "year"
; O: routes.archive.map.year = 1
Entonces el archivo INI puede ser leído por un objeto Zend_Config como sigue:
En el ejemplo de arriba, le decimos el router que utilice la sección 'routes' del archivo INI para utilizarlo
en sus rutas. Cada clave de primer nivel en esa sección será utilizada para definir un nombre de ruta; el
ejemplo anterior define las rutas 'archive' y 'news'. Entonces cada ruta requiere, como mínimo, una entrada a
la 'ruta' y una o más entradas por 'default'; opcionalmente puede proporcionarse una o más 'reqs' (abreviación
de 'required'). Dicho todo esto, estos corresponden a los tres argumentos que se le suministran al objeto
Zend_Controller_Router_Route_Interface. Puede utilizarse una clave opcional 'type' para especificar
el tipo de clase de ruta a utilizar en esa ruta en particular; por defecto, usa Zend_Controller_Router_Route.
En el ejemplo de arriba, la ruta 'news' está definida para usar Zend_Controller_Router_Route_Static.
Dicho esto, en algún momento puede encontrarse a si mismo deseando usar un paradigma diferente de routing. La
intefaz Zend_Controller_Router_Interface proporciona la información mínima necesaria para crear un
router, y consiste en un único método.
interface Zend_Controller_Router_Interface
{
/**
* @param Zend_Controller_Request_Abstract $request
* @throws Zend_Controller_Router_Exception
* @return Zend_Controller_Request_Abstract
*/
175
Zend_Controller
El routing sólo ocurre una vez: cuando la petición es recibida por primera vez en el sistema. El propósito del router es
determinar el controlador, la acción, y los parámetros opcionales sobre la base del medio ambiente de la solicitud, y
luego ajustarlos en la solicitud. El objeto solicitud se pasa entonces al dispatcher. Si no es posible trazar una ruta hacia
un dispatch token, el router no debe hacer nada con el objeto solicitud.
El Despachador
Introducción
Despachar es el proceso de tomar el objeto solicitud, Zend_Controller_Request_Abstract, extraer el
nombre del módulo, el nombre del controlador, el nombre de la acción, y los parámetros opcionales contenido en
él, y luego instanciar un controlador y llamar una acción de ese controlador. Si no se encuentra algún módulo,
controlador o acción, se usarán los valores por defecto para ellos. Zend_Controller_Dispatcher_Standard
especifica index para cada uno de los controladores y acciones por defecto y default para el valor por defecto
del módulo, pero permite al desarrollador cambiar los valores por defecto para cada uno usando los métodos
setDefaultController(), setDefaultAction(), y setDefaultModule() respectivamente.
// En su front controller:
$front->setParam('prefixDefaultModule', true);
// En su despachador:
$dispatcher->setParam('prefixDefaultModule', true);
Esto le permite re-determinar un módulo existente para ser el módulo por defecto para una solicitud.
El proceso de despachar tiene lugar en un bucle en el front controller. Antes de llevarse a cabo el despacho, el front
controller rutea la solicitud para encontrar valores especificados por el usuario para el módulo, controlador, acción, y
los parámetros opcionales. A continuación entra en un loop de despacho, despachando la solicitud.
Al comienzo de cada iteración, establece un flag en el objeto solicitud indicando que la acción se ha despachado. Si
una acción o un plugin pre/postDispatch resetea ese flag, el loop de despacho continuará e intentará despachar la nueva
solicitud. Cambiando el controlador y/o la acción en la solicitud y reseteando el flag despachado, el desarrollador
puede definir una cadena de peticiones a realizar.
El método del controlador de acción que controla ese despacho es _forward(); llamar a este método para cualquiera
de los pre/postDispatch() o métodos de acción, proporcionando un controlador de acciónes, módulo y, opcionalmente
cualquier parámetro adicional que desee enviar a la nueva acción:
176
Zend_Controller
Subclaseando el Despachador
Zend_Controller_Front llamará en primer lugar al router para determinar la primera acción en la solicitud. A
continuación se entra en un loop de despacho, el cual llama al despachador para despachar la acción.
El despachador necesita de una variedad de datos a fin de hacer su trabajo - necesita saber cómo formatear los nombres
del controlador y de la acción, dónde mirar para los archivos de clase del controlador, cuándo el nombre de un
controlador provisto es válido o no, y una API para determinar si una determinada solicitud es incluso despachable
basado en la otra información disponible.
interface Zend_Controller_Dispatcher_Interface
{
/**
* Formatea un string dentro del nombre de clase del controlador.
*
* @param string $unformatted
* @return string
*/
public function formatControllerName($unformatted);
/**
* Formatea un string dentro de un nombre de método de acción.
*
* @param string $unformatted
* @return string
*/
public function formatActionName($unformatted);
/**
* Determina si la solicitud es despachable
*
* @param Zend_Controller_Request_Abstract $request
* @return boolean
177
Zend_Controller
*/
public function isDispatchable(
Zend_Controller_Request_Abstract $request
);
/**
* Establece un parámetro de usuario (via front controller, o para uso local)
*
* @param string $name
* @param mixed $value
* @return Zend_Controller_Dispatcher_Interface
*/
public function setParam($name, $value);
/**
* Establece un array de parámetros de usuario
*
* @param array $params
* @return Zend_Controller_Dispatcher_Interface
*/
public function setParams(array $params);
/**
* Recupera un único parámetro de usuario
*
* @param string $name
* @return mixed
*/
public function getParam($name);
/**
* Recupera todos los parámetros de usuario
*
* @return array
*/
public function getParams();
/**
* Limpia el stack de parámetros de usuario, o un único parámetro de usuario
*
* @param null|string|array single key or array of keys for
* params to clear
* @return Zend_Controller_Dispatcher_Interface
*/
public function clearParams($name = null);
/**
* Establece el objeto respuesta a usar, si hubiera alguno
*
* @param Zend_Controller_Response_Abstract|null $response
* @return void
*/
public function setResponse(
Zend_Controller_Response_Abstract $response = null
178
Zend_Controller
);
/**
* Recupera el objeto respuesta, si hubiera alguno
*
* @return Zend_Controller_Response_Abstract|null
*/
public function getResponse();
/**
* Agrega un directorio de controladoes al stack de directorios de controladores
*
* @param string $path
* @param string $args
* @return Zend_Controller_Dispatcher_Interface
*/
public function addControllerDirectory($path, $args = null);
/**
* Establece el directorio (o directorios) donde se almacenan los archivos
* de controladoes
*
* @param string|array $dir
* @return Zend_Controller_Dispatcher_Interface
*/
public function setControllerDirectory($path);
/**
* Regresa el directorio(s) actualmente establecido para el lookup de los
* archivos de controladores
*
* @return array
*/
public function getControllerDirectory();
/**
* Despacha una solicitud a una acción de (módulo/)controlador.
*
* @param Zend_Controller_Request_Abstract $request
* @param Zend_Controller_Response_Abstract $response
* @return Zend_Controller_Request_Abstract|boolean
*/
public function dispatch(
Zend_Controller_Request_Abstract $request,
Zend_Controller_Response_Abstract $response
);
/**
* Si un módulo dado es válido o no
*
* @param string $module
* @return boolean
*/
public function isValidModule($module);
179
Zend_Controller
/**
* Recuperar el nombre por defecto del módulo
*
* @return string
*/
public function getDefaultModule();
/**
* Recuperar el nombre por defecto del controlador
*
* @return string
*/
public function getDefaultControllerName();
/**
* Recuperar la acción por defecto
*
* @return string
*/
public function getDefaultAction();
}
Las posibles razones para subclasear al despachador incluye un deseo de utilizar un esquema diferente para nombrar las
clases o métodos en sus controladores de acción, o el deseo de utilizar otro paradigma de despacho como ser despachar
los archivos de acción bajo directorios de controladores (en lugar de despacharlos a los métodos de clase).
Controladores de Acción
Introducción
Zend_Controller_Action es una clase abstracta que puede utilizar para implementar controladores de acción
(Action Controllers) para usar con el Front Controller al crear un un sitio basado en el patrón Modelo-Vista-Controlador
(MVC).
Para usar Zend_Controller_Action, necesitará hacerla una subclase en sus clases actuales de controladores de
acción (o hacerla una subclase para crear su propia clase base de acción de controladores). La operación más elemental
es hacerla una subclase, y crear métodos de acción que corresponden a las diversas acciones que desee que el contralor
maneje para su sitio. El manejo del ruteo y envío de Zend_Controller descubrirá por sí mismo cualquier método
que termine en 'Action' en su clase, como posibles acciones del controlador.
180
Zend_Controller
La clase de arriba FooController (el controlador foo) define dos acciones, bar y baz.
Se pueden lograr muchas cosas más, tales como personalizar la inicialización de acciones, las acciones a llamar por
defecto no deberían especificar ninguna acción (o una acción inválida), ganchos de pre y post despacho, y una variedad
de métodos ayudantes. Este capítulo sirve como panorama de la funcionalidad del controlador de acciones.
// Globalmente:
$this->_helper->removeHelper('viewRenderer');
También puede simplemente desactivarse para una prestación individual ajustando el flag noRender de
ViewRenderer:
181
Zend_Controller
Las principales razones para desactivar ViewRenderer son si usted simplemente no necesita una objeto
"view" o si no está mostrándolos via view scripts (por ejemplo, cuando se utiliza un controlador de acción
para alimentar a los protocolos de un servicio web como SOAP, XML-RPC, o REST). En muchos casos,
nunca necesitará desactivar a ViewRenderer globalmente, sólo selectivamente dentro de los distintos
controladores o acciones.
Inicialización de Objectos
Si bien siempre puede anular el contolador de acción del constructor, no lo recomendamos.
Zend_Controller_Action::__construct() realiza algunas tareas importantes, tales como registrar los
objetos de solicitud y respuesta, así como los argumentos de cualquier invocación personalizada pasados desde
el front controller. Si debe anular el constructor, asegúrese de llamar a parent::__construct($request,
$response, $invokeArgs).
La manera más apropiada de personalizar la instanciación es utilizar el método init(), el cual es llamado como la
última tarea de __construct(). Por ejemplo, si se quiere conectar a una base de datos en la instanciación:
Accessors (Accededores)
Con el objeto, se registran una serie de objetos y variables, y cada uno tiene métodos de acceso.
• Objecto Requerimiento: getRequest() puede ser utilizado para recuperar el objeto solicitud utilizado para llamar
a la acción.
• Objecto Respuesta: getResponse() puede ser utilizado para recuperar el objeto respuesta agregando la respuesta
final. Algunas llamadas típicas podrían ser:
$this->getResponse()->setHeader('Content-Type', 'text/xml');
$this->getResponse()->appendBody($content);
• Argumentos de Invocación: el front controller puede empujar parámetros al router, al despachador, y al controlador
de acción. Para recuperarlos, use getInvokeArg($key); por otra parte, se puede traer toda la lista utilizando
getInvokeArgs().
182
Zend_Controller
• Parámetros de Requerimientos: La objeto solicitud agrega parámetros de solicitud, como cualquiera de los
parámetros _GET o _POST, o parámetros del usuario especificados en la información del path de la URL. Para
recuperarlos, use _getParam($key) o _getAllParams(). También se pueden establecer parámetros de
solicitud usando _setParam(); lo que es útil cuando se reenvían a acciones adicionales.
Para probar si un parámetro existe o no (muy útil para bifurcaciones lógicas), use _hasParam($key).
Nota
_getParam() puede tomar opcionalmente un segundo argumento que contiene un valor por defecto
a utilizar si el parámetro no está establecido o está vacío. Usándolo elimina la necesidad de llamar
previamente a _hasParam() para recuperar un valor:
// En lugar de:
if ($this->_hasParam('id') {
$id = $this->_getParam('id');
} else {
$id = 1;
}
Integración de Vistas
Integración de la Vista por Defecto via ViewRenderer
El contenido de esta sección sólo es válida cuando usted tiene explícitamente deshabilitado a ViewRenderer.
De lo contrario, puede saltarse esta sección.
Zend_Controller_Action proporciona un mecanismo rudimentario y flexible para ver la integración. Hay dos
métodos para lograrlo, initView() y render(); el anterior método $view carga la propiedad pública, y este
último muestra una vista en base a la acción requerida actual, utilizando la jerarquía del directorio para determinar
el path del script.
Inicialización de la Vista
initView() inicializa el objeto vista. render() llama a initView() con el fin de recuperar el objeto vista,
pero puede ser iniciada en cualquier momento; por defecto introduce información a la propiedad de $view con un
objeto Zend_View, pero se puede usar cualquier clase que implemente Zend_View_Interface. Si $view ya
ha sido inicializada, simplemente devuelve esa propiedad.
applicationOrModule/
controllers/
IndexController.php
views/
scripts/
index/
index.phtml
helpers/
183
Zend_Controller
filters/
En otras palabras, los scripts de vista se supone están en el subdirectorio views/scripts/, y en el subdirectorio
views se supone que contiene funcionalidades hermanas (ayudantes, filtros). Al determinar el nombre y el path del
script, el directorio views/scripts/ será utilizado como el path base, con directorios nombrados después que los
controladores individuales proporcionen una jerarquía a los scripts de vista.
render() suministra un script de vista. Si no se pasan argumentos, se supone que el script requerido es
[controller]/[action].phtml (donde .phtml es el valor de la propiedad $viewSuffix). Pasándole
un valor a $action suministrará esa plantilla en al subdirectorio [controller]. Para anular el subdirectorio
[controller] ponga un valor verdadero en $noController. Por último, las plantillas son suministradas en el
objeto respuesta; si desea suministrar a un determinado named segment en el objeto respuesta, pase un valor a $name.
Nota
Dado que el controlador y los nombres de acción pueden contener caracteres delimitadores como '_', '.', y '-',
render() los normaliza a '-' para determinar el nombre del script. Internamente, utiliza los delimitadores de
palabra y de path del despachador para hacer esta normalización. Así, una solicitud a /foo.bar/baz-bat
suministrará el script foo-bar/baz-bat.phtml. Si su método de acción contiene camelCasing, recuerde
que esto se traducirá en palabras separadas por '-' al determinar el nombre del archivo del script de vista.
Algunos ejemplos:
// Suministra my/bar.phtml
$this->render('bar');
// Suministra baz.phtml
$this->render('baz', null, true);
184
Zend_Controller
Métodos Utilitarios
Además de los accesadores y de los métodos de integración de vistas, Zend_Controller_Action tiene varios
métodos utilitarios para realizar tareas comunes dentro de sus métodos de acción (o de pre-/post-despacho).
• _redirect($url, array $options = array()): redireccionar a otro lugar. Este método toma una
URL y un conjunto de opciones. Por defecto, realiza una redirección HTTP 302.
• exit: ya sea para salir inmediatamente o no. Si así lo solicita, limpiamente cerrará cualquier sesión abierta y
realizará la redirección.
• prependBase: ya sea anteponiendo o no la base URL registrada con el objeto solicitud a la URL provista.
• code: qué código HTTP utilizar en la redirección. Por defecto, se utiliza un HTTP 302; se puede utilizar cualquier
código entre 301 y 306.
Además de crear una funcionalidad útil para su aplicaciones web, también puede encontrar que está repitiendo
demasiado los mismos setups o métodos utilitarios en sus diferentes controladores; si así fuera, creando una clase base
común del controlador que extienda Zend_Controller_Action puede resolver esta redundacia.
185
Zend_Controller
Usted debe anular esta funcionalidad si desea realizar otras operaciones. Por ejemplo, si desea mostrar un mensaje de
error, usted podría escribir algo como esto:
Otra posibilidad es que puede querer avanzar a un controlador de página por defecto:
Además de sobrecargar __call(), cada uno de los métodos gancho de inicialización, utilidad, accesador, vista, y
despacho mencionados anteriormente en este capítulo pueden ser anulados a fin de personalizar sus controladores.
Como ejemplo, si está almacenando su objeto vista en un registro, quizás desee modificar su método initView()
con código parecido al siguiente:
186
Zend_Controller
{
public function initView()
{
if (null === $this->view) {
if (Zend_Registry::isRegistered('view')) {
$this->view = Zend_Registry::get('view');
} else {
$this->view = new Zend_View();
$this->view->setBasePath(dirname(__FILE__) . '/../views');
}
}
return $this->view;
}
}
Es de esperar, que de la información en este capítulo, usted puede ver la flexibilidad de este componente en particular
y cómo puede darle forma a su aplicaciones o a las necesidades de su sitio web.
Action Helpers
Introduction
Action Helpers allow developers to inject runtime and/or on-demand functionality into any Action Controllers that
extend Zend_Controller_Action. Action Helpers aim to minimize the necessity to extend the abstract Action
Controller in order to inject common Action Controller functionality.
There are a number of ways to use Action Helpers. Action Helpers employ the use of a brokerage system, similar
to the types of brokerage you see in Zend_View_Helper, and that of Zend_Controller_Plugin. Action Helpers (like
Zend_View_Helper) may be loaded and called on demand, or they may be instantiated at request time (bootstrap)
or action controller creation time (init()). To understand this more fully, please see the usage section below.
Helper Initialization
A helper can be initialized in several different ways, based on your needs as well as the functionality of that helper.
The helper broker is stored as the $_helper member of Zend_Controller_Action; use the broker to retrieve
or call on helpers. Some methods for doing so include:
• Explicitly using getHelper(). Simply pass it a name, and a helper object is returned:
$flashMessenger = $this->_helper->getHelper('FlashMessenger');
$flashMessenger->addMessage('We did something in the last request');
• Use the helper broker's __get() functionality and retrieve the helper as if it were a member property of the broker:
$flashMessenger = $this->_helper->FlashMessenger;
$flashMessenger->addMessage('We did something in the last request');
• Finally, most action helpers implement the method direct() which will call a specific, default method in the
helper. In the example of the FlashMessenger, it calls addMessage():
187
Zend_Controller
Nota
All of the above examples are functionally equivalent.
You may also instantiate helpers explicitly. You may wish to do this if using the helper outside of an action controller,
or if you wish to pass a helper to the helper broker for use by any action. Instantiation is as per any other PHP class.
Zend_Controller_Action_HelperBroker::addHelper($helper);
Of course, instantiating and passing helpers to the broker is a bit time and resource intensive, so two methods exists
to automate things slightly: addPrefix() and addPath().
• addPrefix() takes a class prefix and uses it to determine a path where helper classes have been defined. It
assumes the prefix follows Zend Framework class naming conventions.
• addPath() takes a directory as its first argument and a class prefix as the second argument (defaulting
to 'Zend_Controller_Action_Helper'). This allows you to map your own class prefixes to specific
directories.
Since these methods are static, they may be called at any point in the controller chain in order to dynamically add
helpers as needed.
Internally, the helper broker uses a PluginLoader instance to maintain paths. You can retrieve the PluginLoader
using the static method getPluginLoader(), or, alternately, inject a custom PluginLoader instance using
setPluginLoader().
To determine if a helper exists in the helper broker, use hasHelper($name), where $name is the short name of
the helper (minus the prefix):
There are also two static methods for retrieving helpers from the helper broker: getExistingHelper() and
getStaticHelper(). getExistingHelper() will retrieve a helper only if it has previously been invoked
188
Zend_Controller
by or explicitly registered with the helper broker; it will throw an exception if not. getStaticHelper() does the
same as getExistingHelper(), but will attempt to instantiate the helper if has not yet been registered with the
helper stack. getStaticHelper() is a good choice for retrieving helpers which you wish to configure.
Both methods take a single argument, $name, which is the short name of the helper (minus the prefix).
// Or, simply retrieve it, not worrying about whether or not it was
// previously registered:
$redirector =
Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
}
Finally, to delete a registered helper from the broker, use removeHelper($name), where $name is the short name
of the helper (minus the prefix):
ActionStack
El ayudante ActionStack le permite empujar requerimientos al ActionStack plugin del front controller, el cual le
ayuda efectivamente a crear una cola de acciones a ejecutar durante la solicitud. El ayudante le permite añadir acciones
ya sea especificando nuevos objetos solicitud o conjuntos acción/controlador/módulo.
Ejemplo 12.2. Agregando una Tarea Usando Nombres de Acción, Controllador y Módulo
A menudo, es más sencillo simplemente especificar la acción, el controlador y el módulo (y parámetros opcionales de
requerimientos), tal como cuando llama a Zend_Controller_Action::_forward():
189
Zend_Controller
{
public function barAction()
{
// Agregar dos acciones a la pila (stack)
// Y llamar a /foo/baz/bar/baz
// (FooController::bazAction() con el requrimiento var bar == baz)
$this->_helper->actionStack('baz',
'foo',
'default',
array('bar' => 'baz'));
AutoComplete
Muchas bibliotecas de Javascript con AJAX ofrecen funcionalidad para proporcionar autocompletado según la cual
un selectlist de resultados potencialmente concordantes se visualiza a medida que el usuario tipea. El ayudante
AutoComplete pretende simplificar el retorno de respuestas aceptables a esos métodos.
Dado que no todas la bibliotecas JS implementan el autocompletado de la misma manera, el ayudante AutoComplete
ofrece algunas funcionalidades abstractas de base necesarias para muchas bibliotecas, e implementaciones concretas
190
Zend_Controller
para distintas bibliotecas. Los tipos de datos de retorno son generalmente o bien arrays de strings JSON, array de
arrays JSON (donde cada miembro del array está en un array asociativo de metadatos utilizado para crear la selectlist),
o HTML.
// O explicitamente:
$response = $this->_helper->autoCompleteDojo
->sendAutoCompletion($data);
• Envía la respuesta.
• disableLayouts() puede ser utilizada para desactivar esquemas y a ViewRenderer. Típicamente, esto se llama
dentro de prepareAutoCompletion().
191
Zend_Controller
En Zend Framework, puede pasar un simple array indexado al ayudante AutoCompleteDojo, y este regresará una
adecuada respuesta JSON para su uso como almacenamiento:
En primer lugar, veamos el Javascript necesario. Dojo ofrece un marco completo para crear Javascript OOP, tal como
lo hace Zend Framework para PHP. Parte de eso es la capacidad de crear pseudo-namespaces utilizando la jerarquía
de directorios. Crearemos un directorio 'custom' en el mismo nivel en el cual el directorio de Dojo forma parte de
la distribución Dojo. Dentro del directorio, crearemos un archivo Javascript, TestNameReadStore.js, con el siguiente
contenido:
dojo.provide("custom.TestNameReadStore");
dojo.declare("custom.TestNameReadStore", dojox.data.QueryReadStore, {
fetch:function (request) {
request.serverQuery = { test:request.query.name };
return this.inherited("fetch", arguments);
}
});
Esta clase es simplemente una extensión del propio QueryReadStore de Dojo, que es de por sí misma una clase
abstracta. Simplemente definimos un método por el cual realizamos la solicitud, y se le asigna al elemento 'test'.
192
Zend_Controller
/**
* Página final
*/
public function indexAction()
{
$this->view->form = $this->getForm();
}
$matches = array();
foreach ($this->getData() as $datum) {
if (0 === strpos($datum, $match)) {
$matches[] = $datum;
}
}
$this->_helper->autoCompleteDojo($matches);
193
Zend_Controller
}
}
En nuestro autocompleteAction() hacemos una serie de cosas. En primer lugar, esperamos a asegurarnos de
que tengamos una petición post, y que existe un parámetro 'form' establecido al valor 'ajax'; esto es simplemente para
ayudar a reducir preguntas espúreas a la acción. A continuación, vamos a comprobar el parámetro 'test', y compararlo
contra nuestros datos. (Yo deliberadamente dejé de lado la implementación de getData() aquí -- podría ser cualquier
tipo de fuente de datos). Por último, enviamos nuestros aciertos a nuestro ayudante AutoCompletion.
Ahora que tenemos todas las piezas en el backend, veamos lo que necesitamos para entregar en nuestro script de vista
para la página final. En primer lugar, necesitamos configurar nuestro data store, luego hacer nuestro formulario, y
finalmente garantizar que las biblotecas Dojo apropiadas -- incluyendo que nuestro data store personalizado -- estén
cargadas. Veamos el script de vista, el cual comenta los pasos:
<?php // establecemos CSS relacionado con Dojo a cargar en HTML head: ?>
<?php $this->headStyle()->captureStart() ?>
@import "<?php echo $this->baseUrl()
?>/javascript/dijit/themes/tundra/tundra.css";
@import "<?php echo $this->baseUrl() ?>/javascript/dojo/resources/dojo.css";
<?php $this->headStyle()->captureEnd() ?>
<?php // setup de javascript para cargar en HTML head, incluyendo todas las
// bibliotecas Dojo requeridas: ?>
<?php $this->headScript()
->setAllowArbitraryAttributes(true)
->appendFile($this->baseUrl() . '/javascript/dojo/dojo.js',
'text/javascript',
array('djConfig' => 'parseOnLoad: true'))
->captureStart() ?>
djConfig.usePlainJson=true;
dojo.registerModulePath("custom","../custom");
dojo.require("dojo.parser");
dojo.require("dojox.data.QueryReadStore");
dojo.require("dijit.form.ComboBox");
dojo.require("custom.TestNameReadStore");
<?php $this->headScript()->captureEnd()
Note que las llamadas a los ayudantes de vista como headStyle y headScript; son ubicadores, que podemos suministrar
a la sección 'head' del HTML de nuestro script de vista.
Ahora tenemos todas las piezas para que el AutoCompletion de Dojo pueda trabajar.
194
Zend_Controller
El ayudante para utilizar con esta biblioteca es 'AutoCompleteScriptaculous'. Simplemente proporcionarle un array de
datos, y el ayudante creará una respuesta HTML compatible con Ajax.Autocompleter.
Para habilitar alguno, usted debe proporcionar indicios en su controlador de qué acciones pueden responder a que
contextos. Si una solicitud entrante indica un contexto válido para la acción determinada, entonces el ayudante:
• Establecer un sufijo de vista alternativo, requiriendo de manera efectiva un script de vista separado para el contexto.
• Opcionalmente, llama a llamadas de retorno especifícas para configurar el contexto y/o realizar post-procesamiento.
/**
* Lista nuevos items
*/
public function listAction()
{
}
/**
* Vista del nuevo item
*/
public function viewAction()
{
}
}
Digamos que queremos que listAction() también esté disponible en un formato XML. En lugar de crear una
acción diferente, podemos indicarle que puede devolver una respuesta XML:
195
Zend_Controller
$contextSwitch = $this->_helper->getHelper('contextSwitch');
$contextSwitch->addActionContext('list', 'xml')
->initContext();
}
// ...
}
• Cambiar el sufijo de vista de 'xml.phtml' (o, si usa un sufifo de vista alternativo, 'xml.[su sufijo]').
Ahora, necesitará crear un nuevo script de vista, 'news/list.xml.phtml', que creará y mostrará a XML.
Para determinar si una solicitud debe iniciar un cambio de contexto, el ayudante comprueba si hay un token en el
objeto solicitud. Por defecto, busca el parámetro 'format', aunque esto puede ser configurado. Esto significa que, en
muchos casos, para provocar un cambio de contexto, puede agregar un parámetro 'format' a su solicitud:
• Via parámetro URL: /news/list/format/xml (recordar que el valor por defecto del esquema de ruteo permite
pares arbitrarios de clave/valor tras la acción).
ContextSwitch le permite especificar contextos arbitrarios, incluso qué sufijo cambiará (si hay alguno), cualquier
cabecera de respuesta que deba ser enviada, y callbacks arbitrarios para la inicialización y posterior procesamiento.
• JSON. El contexto JSON establece la cabecera de respuesta 'Content-Type' a 'application/json', y el sufijo del script
de vista a 'json.phtml'.
Sin embargo, por defecto, no es necesario un script de vista. Simplemente serializará todas las variables de la vista,
y emitirá la respuesta JSON inmediatamente.
$this->_helper->contextSwitch()->setAutoJsonSerialization(false);
• XML. El contexto XML establece la cabecera de respuesta 'Content-Type' a 'text/xml', y el sufijo del script de vista
a 'xml.phtml'. Necesitará crear un script de vista nuevo para el contexto.
La forma más fácil para añadir un nuevo contexto es a través del método addContext(). Este método tiene dos
argumentos, el nombre del contexto, y un array de especificación. La especificación debería incluir uno o más de los
siguientes:
• suffix: el sufijo a anteponer al sufijo de la vista por defecto tal como está registrado en ViewRenderer.
• headers: un array de pares cabecera/valor que desea enviar como parte de la respuesta.
196
Zend_Controller
• callbacks: un array que contiene una o más de las claves 'init' o 'post', apuntando a callbacks PHP válidos que pueden
utilizarse para inicializar el contexto y posterior procesamiento.
La inicialización de callbacks ocurre cuando el contexto es detectado por ContextSwitch. Usted puede usarlo
para ejecutar una lógica arbitraria. Por ejemplo, el contexto JSON utiliza un callback para desactivar a ViewRenderer
cuando está activada la serialización auto-JSON.
El post procesamiento ocurre durante la rutina de la acción postDispatch(), y puede ser utilizada para ejecutar
una lógica arbitraria. Como ejemplo, el contexto JSON utiliza un callback para determinar si la serialización auto-
JSON está activada; si así fuera, serializa las variables de la vista a JSON y envía la respuesta, pero si no, re-habilita
a ViewRenderer.
• addContext($context, array $spec): agrega un nuevo contexto. Lanza una excepción si el contexto
ya existe.
• addContexts(array $contexts): añade muchos contextos de una vez. El array $contexts debería ser
un array de pares contexto/especificación. Si alguno de los contextos ya existe, arrojará una excepción.
• setContexts(array $contexts): añade nuevos contextos y sobreescribe los existentes. Usa la misma
especificación que addContexts().
• removeContext($context): elimina un único contexto por su nombre. Devuelve true si tiene éxito, false si
el contexto no fue encontrado.
El método principal para añadir relaciones acción/contexto es addActionContext(). Se esperan dos argumentos,
la acción a la que el contexto se añade, y el nombre de un contexto o un array de contextos. Como ejemplo, considere
la siguiente clase controlador:
197
Zend_Controller
Supongamos que queremos añadir un contexto XML a la acción 'list', y contextos XML y JSON a la acción 'comments'.
Podríamos hacerlo en el método init():
Los siguientes métodos pueden ser utilizados para construir los mapeos del contexto:
• addActionContext($action, $context): marca uno o más contextos como disponibles para una acción.
Si ya existen los mapeos, simplemente se añade a esos mapeos. $context puede ser un único contexto, o un array
de contextos.
Un valor de true para el contexto marcará todos los contextos como disponibles para la acción.
Un valor vacío de $contexto desactivará todos los contextos para la acción determinada.
• setActionContext($action, $context): marca uno o más contextos como disponibles para una acción.
Si el mapeo ya existe, se reemplaza con los especificados. $context puede ser un único contexto, o un array de
contextos.
198
Zend_Controller
• getActionContexts($action = null): devuelve o todos los contextos para una acción determinada, o
todos los pares de acción/contexto.
En algunos casos, puede querer forzar el contexto utilizado; por ejemplo, puede que sólo quiera permitir el contexto
XML si la conmutación de contexto está activada. Puede hacerlo pasando el contexto a initContext():
$contextSwitch->initContext('xml');
Funcionalidad Adicional
Se pueden utilizar una variedad de métodos para alterar el comportamiento del ayudante ContextSwitch. Estos
incluyen:
$contextSwitch->setAutoJsonSerialization(false);
$contextSwitch->initContext();
199
Zend_Controller
Pasando un valor vacío para el sufijo hará que sólo el sufijo ViewRenderer será utilizado.
Cada contexto pueden tener múltiples cabeceras; addHeader() agrega cabeceras adicionales al stack de cabecera
del contexto.
• getHeader($context, $header): recuperar el valor de una cabecera para un determinado contexto. Retorna
null si no existe.
• getCallbacks($context): recupera todos los callbacks para un determinado contexto. Devuelve un array
de pares disparor/callback.
• setAutoDisableLayout($flag): Por defecto, los esquemas están deshabilitados cuando sucede una
conmutación de contexto; esto es porque normalmente los esquemas sólo serán utilizados para devolver
respuestas normales, y no tienen sentido en otros contextos. Sin embargo, si desea usar esquemas (tal vez
puede tener un diseño para el nuevo contexto), puede cambiar este comportamiento pasando un valor falso a
setAutoDisableLayout(). Usted debería hacer esto antes de llamar a initContext().
200
Zend_Controller
• getCurrentContext() Puede ser utilizado para determinar qué contexto fue detectado, si hay alguno. Este
retorna null si no hubo conmutación de contexto, o si initContext() fue llamado antes de ser invocado.
Funcionalidad de AjaxContext
El ayudante AjaxContext extiende ContextSwitch, así que toda de la funcionalidad listada para
ContextSwitch está disponible. Hay algunas diferencias fundamentales, sin embargo.
En primer lugar, el controlador de acción utiliza una propiedad diferente para determinar contextos, $ajaxable.
Esto es, que puede tener diferentes contextos utilizados para AJAX versus peticiones normales HTTP. Los diversos
métodos *ActionContext*() de AjaxContext le escribirán a esta propiedad.
En segundo lugar, sólo se disparará si se produjo un XmlHttpRequest, según lo determinado por la solicitud del método
del objeto isXmlHttpRequest(). Así, si se pasa el parámetro de contexto ('format') en la solicitud, pero la solicitud
no fue hecha como un XmlHttpRequest, no se disparará ninguna conmutación de contexto.
En tercer lugar, AjaxContext agrega un contexto adicional, HTML. En este contexto, se establece el sufijo a
'ajax.phtml' para diferenciar el contexto de una solicitud normal. No se devuelven cabeceras adicionales.
201
Zend_Controller
{
// Procesar un nuevo comentario
// Devolver los resultados como JSON; simplemente asignar los
// resultados como variables de la vista, y se devolverán como JSON.
}
}
En el lado del cliente, su biblioteca AJAX simplemente pedirá los parámetros finales '/comment/view', '/comment/
form', y '/comment/process', y pasar el parámetro 'format': '/comment/view/format/html', '/comment/form/format/html',
'/comment/process/format/json'. (O puede pasar el parámetro via string de consulta: e.g., "?format=json").
FlashMessenger
Introducción
El ayudante FlashMessenger le permite pasar mensajes que el usuario puede querer ver en la próxima
solicitud. Para lograrlo, FlashMessenger usa Zend_Session_Namespace para almacenar los mensajes
para las futuras o próxima solicitud de recuperación. Es una buena idea si planea utilizar Zend_Session o
Zend_Session_Namespace, que inicializa con Zend_Session::start() en su archivo bootstrap. (Para
más detalles de su uso vea la documentación en Zend_Session).
202
Zend_Controller
JSON
Las respuestas JSON se están convirtiendo en la respuesta de elección cuando se trata de requerimientos AJAX que
esperan recibir respuestas datasets; en el lado del cliente, JSON puede ser inmediatamente parseado y ejecutado
rápidamente.
• Opcionalmente, un array de opciones que pasar como segundo argumento a Zend_Json::encode(). Este array
de opciones permite habilitar layouts y codificación utilizando Zend_Json_Expr.
• Por defecto, devuelve inmediatamente la respuesta, sin esperar a la acción para finalizar la ejecución.
El uso es simple: o bien llamarlo como un método del ayudante, o llamar a uno de los métodos encodeJson() o
sendJson():
// o...
$this->_helper->json->sendJson($data);
// o recuperar json:
$json = $this->_helper->json->encodeJson($data);
}
}
203
Zend_Controller
$this->_helper->json($data, true);
Opcionalmente, puede pasar un array como el segundo parámetro. Este array puede contener una variedad
de opciones, incluida la opción keepLayouts:
Si desea hacer esto, debe pasar un array como segundo argumento. Esto también le permite
combinar otras opciones, como la opción keepLayouts. Todas esas opciones se pasan luego a
Zend_Json::encode().
$this->_helper->json($data, array(
'enableJsonExprFinder' => true,
'keepLayouts' => true,
));
Redirector
Introducción
El ayudante Redirector le permite utilizar un objeto de redireccionamiento para cumplir con necesidades de
su aplicación para redireccionar a una nueva URL. Ofrece numerosas ventajas sobre el método _redirect(),
tales como poder preconfigurar un comportamiento para todo el sitio en el objeto redirector o usando el
construido en gotoSimple($action, $controller, $module, $params), interfaz similar a la de
Zend_Controller_Action::_forward().
El Redirector tiene un número de métodos que pueden utilizarse para afectar el comportamiento al redireccionar:
• setCode() puede ser utilizado para establecer el código de respuesta HTTP que utilizar durante la redirección.
• setExit() puede usarse para forzar un exit() tras una redirección. Por defecto es verdadero (true).
• setGotoSimple() puede ser utilizada para establecer la URL que usar por defecto si no se ha pasado ninguna
a gotoSimple(). Utiliza la API de Zend_Controller_Action::_forward(): setGotoSimple($action,
$controller = null, $module = null, array $params = array());
• setGotoRoute() puede ser utilizada para establecer una URL basada en una ruta. Pasarla en un array de pares
clave/valor y un nombre de ruta, y que ensamblarán la URL según la definición y el tipo de ruta.
• setGotoUrl() puede ser utilizada para establecer una URL por defecto si no se pasa ninguna a gotoUrl().
Acepta un solo string URL.
• setPrependBase() puede ser utilizada para anteponer la URL base del objeto solicitud a una URL especificada
con setGotoUrl(), gotoUrl(), o gotoUrlAndExit().
204
Zend_Controller
• setUseAbsoluteUri() puede ser utilizada para forzar al Redirector a usar URIs absolutas cuando
está redireccionando. Cuando se establece esta opción, se utiliza el valor de $_SERVER['HTTP_HOST'],
$_SERVER['SERVER_PORT'], y $_SERVER['HTTPS'] para formar una URI completa a la URL
especificada por uno de los métodos de redirección. Esta opción está desactivada por defecto, pero podrá ser activada
por defecto en versiones posteriores.
Además, hay una variedad de métodos en el redireccionamiento para realizar las redirecciones actuales:
• gotoSimple() usa setGotoSimple() (_forward()-tipo API) para construir una URL y realizar un
redireccionamiento.
• gotoUrl() usa setGotoUrl() (URL string) para construir una URL y realizar un redireccionamiento.
Por último, usted puede determinar la redirección actual de la URL en cualquier momento usando
getRedirectUrl().
$this->_redirector->setCode(303)
->setExit(false)
->setGotoSimple("this-action",
"some-controller");
}
205
Zend_Controller
// EJEMPLO ALTERNATIVO
class AlternativeController extends Zend_Controller_Action
{
/**
* Redirector - definido para completar el código
*
* @var Zend_Controller_Action_Helper_Redirector
*/
protected $_redirector = null;
$this->_redirector
->gotoUrl('/my-controller/my-action/param1/test/param2/test2');
return; // nunca alcanzado dado que por defecto es ir a URL y salir
}
}
206
Zend_Controller
Dado un array con el año fijado a 2006, mes a 4, día a 24, e id a 42, entonces construye la siguiente URL /
blog/2006/4/24/42.
207
Zend_Controller
ViewRenderer
Introducción
El ayudante ViewRenderer está diseñado para satisfacer los siguientes objetivos:
• Eliminar la necesidad de instanciar objetos de vista dentro de los controladores; los objetos de vista quedarán
registrados automáticamente con el contralor.
• Establece automáticamente el script de vista, el ayudante, y los paths de los filtros basados en el módulo actual.
Asocia automáticamente el nombre del módulo actual como un prefijo de clase para las clases ayudante y filtro.
• Crea un objeto de vista, disponible globalmente para todos los controladores y acciones despachados.
• Permite al desarrollador establecer por defecto las opciones de renderizado para todos los controladores.
• Agrega la capacidad para renderizar automáticamente los scripts de vista sin ninguna intervención.
• Permite al desarrollador crear sus propias especificaciones para el path base de vistas y para el path de los scripts
de vista.
Nota
Si realiza un _forward(), redirecciona, o render manualmente, el autorendering no se llevará a cabo,
como está realizando cualquiera de estas acciones le está diciendo al ViewRenderer que usted está
determinando su propia salida.
Nota
El ViewRenderer está habilitado por defecto. Puede desactivarlo vía parámetro
del front controller noViewRenderer ($front->setParam('noViewRenderer',
true)) o eliminando al ayudante del stack de ayudantes
(Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer')).
Si desea modificar los settings del ViewRenderer antes de despachar el front controller, puede hacerlo
en una de las dos maneras:
208
Zend_Controller
$viewRenderer =
Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->setView($view)
->setViewSuffix('php');
API
En su uso más básico, simplemente instancie a ViewRenderer y páselo al ayudante de acciones. La forma más fácil
para instanciar y registrar de una sola vez es utilizando el método del ayudante getStaticHelper():
Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
La primera vez que se instancia un controlador de acción, se disparará ViewRenderer para instanciar al objeto
vista. Cada vez que el controlador es instanciado, se llama al método init() de ViewRenderer, que lo llevará
a establecer la propiedad del controlador de acción, y llama a addScriptPath() con un path relativo al módulo
actual; este será llamado con un prefijo de clase nombrada después del módulo actual, haciendo efectivamente el
namespacing de todas las clases de ayudantes y filtros que define para el módulo.
Cad vez que llama a postDispatch(), este llamará a render() para la acción actual.
...
209
Zend_Controller
El ViewRenderer también define una serie de accededores para permitir establecer y recuperar opciones de vista:
• setView($view) le permite establecer el objeto vista para ViewRenderer. Se vuelve como una propiedad
de clase pública $view.
• setNeverRender($flag = true) puede ser utilizado para activar o desactivar globalmente el autorendering,
es decir, para todos los controladores. Si es verdadero, postDispatch() no llamará automáticamente a
render() en el controlador actual. getNeverRender() recupera el valor actual.
• setNoRender($flag = true) puede ser utilizado para activar o desactivar el autorendering. Si es verdadero,
postDispatch() no llamará automáticamente a render() en el controlador actual. Este ajuste se reseteará
cada vez que se llame a preDispatch() (es decir, usted necesita establecer este flag para cada controlador para
el cual no quiera que el autorenderering se ejecute). getNoRender() recupera el valor actual.
• setNoController($flag = true) pude ser usado para decirle a render() que no busque el script
de acción en un subdirectorio nombrado después de que el controlador (que es el comportamiento por defecto)
getNoController() recupere el valor actual.
• setScriptAction($name) puede ser utilizado para especificar el script de acción a renderizar. $name
debe ser el nombre del script menos el sufijo del archivo (y sin el subdirectorio del controlador, a menos que
noController se haya activado). Si no se ha especificado, busca un script de vista nombrado después de la
acción en el objeto solicitud. getScriptAction() recupera el valor actual.
• setResponseSegment($name) puede ser utilizado para especificar qué segmento del objeto respuesta
nombrado renderizar. Si no se especifica, se hace en el segmento por defecto. getResponseSegment() recupera
el valor actual.
• initView($path, $prefix, $options) puede ser llamado para especificar el path base de las vistas,
prefijos de clase para scripts de ayudantes y filtros, y las opciones de ViewRenderer. Puede pasar cualquiera de
los siguientes flags: neverRender, noRender, noController, scriptAction, y responseSegment.
Nota
setRender() y direct() realmente no renderiza el script de vista, sino que establece indicaciones
que postDispatch() y render() utlizarán para renderizar la vista.
El constructor le permite opcionalmente pasar el objeto vista y las opciones de ViewRenderer; acepta los mismos
flags que initView():
210
Zend_Controller
Hay varios métodos adicionales para personalizar especificaciones del path, usados para determinar el path base del
script de vista para añadir al objeto vista, y el path del script de vista a usar cuando esté autodeterminando el script de
vista a renderizar. Cada uno de estos métodos toma uno o más de los siguientes localizadores:
• :moduleDir hace referencia a la actual directorio base del módulo(por convención, el directorio padre del
directorio del módulo controlador).
• :suffix hace referencia al sufijo del script de vista (que puede ser definido via setViewSuffix()).
Para un control más refinado sobre el path de especificaciones, puede usar Zend_Filter_Inflector. Bajo el capó,
ViewRenderer ya usa un inflector para realizar mapeos del path. Para interactuar con el inflector -- ya sea para
establecerlo para uso propio, o para modificar el inflector por defecto, se pueden utilizar los siguientes métodos:
• getInflector() recupera el inflector. Si no existe todavía en ViewRenderer, se crea uno utilizando las
reglas predeterminadas.
Por defecto, utiliza reglas de referencias estáticas para el sufijo y directorio de módulos, así como una meta estática;
esto permite que diversas propiedades de ViewRenderer tengan la capacidad de modificar dinámicamente al
inflector.
211
Zend_Controller
• :module: MixedCase y camelCasedWords están separados por guiones, y el string completo se convierte
a minúsculas. Por ejemplo: "FooBarBaz" pasa a ser "foo-bar-baz".
• :action: MixedCase y camelCasedWords están separados por guiones; los caracteres no alfanuméricos
son traducidos a guiones, y el string emitido a minúsculas. Ejemplos: "fooBar" pasa a ser "foo-bar"; "foo-
barBaz" pasa a ser "foo-bar-baz".
Los últimos temas en la API de ViewRenderer son los métodos para determinar realmente los paths de los scripts
de vista y el rendering de las vistas. Estos incluyen:
• renderScript($script, $name) permite renderizar un script con una ruta que especifique, opcionalmente
a un segmento nombrado del path. Cuando se utiliza este método, ViewRenderer no autodetermina el nombre del
script, en cambio pasa directamente a $script el argumento directamente al método del objeto vista render().
Nota
Una vez que la vista ha sido renderizada al objeto respuesta, se establece noRender para evitar
accidentalmente renderizar el mismo script de vista varias veces.
Nota
Por defecto, Zend_Controller_Action::renderScript() le delega a ViewRenderer el
método renderScript().
• getViewScript($action, $vars) crea el path a un script de vista basado en la acción pasada y/o cualquier
variables pasadas en $vars. Las claves para este array pueden incluir cualquiera de las claves de especificación
de paths ('moduleDir', 'module', 'controller', 'action', y 'suffix'). Se utilizarán cualquiera de la variables pasadas; de
lo contrario, se utilizarán valores basados en la petición actual.
Los delimitadores de palabras encontrados en un módulo, controlador o nombres de acción serán reemplazados por
guiones ('-'). Así pues, si tiene el nombre de controlador 'foo.bar' y la acción 'baz:bat', utilizando la especificación
por defecto del path se traducirá en un path al script de vista 'foo-bar/baz-bat.phtml'.
Nota
Por defecto, Zend_Controller_Action::getViewScript() delega el método
getViewScript() de ViewRenderer.
212
Zend_Controller
Nota
Hay que ser conscientes de los efectos secundarios al usar render(): los valores que usted pasa para el
nombre del segmento respuesta y para el flag noController persistirán en el objeto. Además, noRender será
establecido después de completar la renderización.
Nota
Por defecto, Zend_Controller_Action::render() delega a ViewRenderer el método
render().
• renderBySpec($action, $vars, $name) permite pasar variables de especificación del path a fin de
determinar el path para la creación del script de vista. Este pasa $action y $vars a getScriptPath(), y
luego pasa el path del script resultante y $name a renderScript().
// En su bootstrap:
Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
...
213
Zend_Controller
Tener en cuenta que el en el segundo ejemplo, el módulo es todavía el módulo por defecto, pero que, debido
a la existencia de un separador de paths, el controlador recibe el nombre Bar_BazController, en Bar/
BazController.php. El ViewRenderer imita la jerarquía del directorio del controlador.
Nota
En muchos casos, no tiene sentido desactivar el autorendering globalmente (ala setNeverRender()), y
la única cosa que puede ganar de ViewRenderer es el autosetup del objeto de vista.
214
Zend_Controller
215
Zend_Controller
{
// Obtener el objeto vista y establecer
// el callback de escape a 'htmlspecialchars'
$view = $this->_helper->viewRenderer->view;
$view->setEscape('htmlspecialchars');
}
}
Para los fines de este ejemplo, supongamos que el path base de las vistas debería ser '/opt/vendor/templates', y que
desea para que los scripts de vista sean referenciados por ':moduleDir/:controller/:action.:suffix'; si el flag noController
ha sido establecido, quiere renderizar fuera del nivel superior en lugar de en un subdirectorio (':action.:suffix'). Por
último, que quiere utilizar 'tpl' como el sufijo del nombre de archivo del script de vista.
/**
* En su bootstrap:
*/
Ejemplo 12.15. Rendering Múltiples Scripts de Vista desde una Sola Acción
A veces, puede que necesite renderizar múltiples scripts de vista desde una sola acción. Esto es muy sencillo --
simplemente hacer múltiples llamadas a render():
216
Zend_Controller
$this->render('form');
$this->render('results');
}
• init(), triggered by the helper broker at instantiation, can be used to trigger initialization in the helper; this can
be useful for resetting state when multiple controllers use the same helper in chained actions.
• postDispatch() is triggered when a dispatched action is done -- even if a preDispatch() plugin has skipped
the action. Mainly useful for cleanup.
• getName() retrieves the helper name. It retrieves the portion of the class name following the
last underscore character, or the full class name otherwise. As an example, if the class is named
Zend_Controller_Action_Helper_Redirector, it will return Redirector; a class named FooMessage
will simply return itself.
You may optionally include a direct() method in your helper class. If defined, it allows you to treat the helper as a
method of the helper broker, in order to allow easy, one-off usage of the helper. As an example, the redirector defines
direct() as an alias of goto(), allowing use of the helper like this:
// Redirect to /blog/view/item/id/42
$this->_helper->redirector('item', 'view', 'blog', array('id' => 42));
Internally, the helper broker's __call() method looks for a helper named redirector, then checks to see if that helper
has a defined direct() method, and calls it with the arguments provided.
Once you have created your own helper class, you may provide access to it as described in the sections above.
217
Zend_Controller
$front->throwExceptions(true);
$response->sendResponse();
Nota
By default, the front controller calls sendResponse() when it has finished dispatching the
request; typically you will never need to call it. However, if you wish to manipulate the response
or use it in testing, you can override this behaviour by setting the returnResponse flag with
Zend_Controller_Front::returnResponse(true):
$front->returnResponse(true);
$response = $front->dispatch();
Developers should make use of the response object in their action controllers. Instead of directly rendering output and
sending headers, push them to the response object:
By doing this, all headers get sent at once, just prior to displaying the content.
Nota
If using the action controller view integration, you do not need to set the rendered view script content in the
response object, as Zend_Controller_Action::render() does this by default.
Should an exception occur in an application, check the response object's isException() flag, and retrieve the
exception using getException(). Additionally, one may create custom response objects that redirect to error
pages, log exception messages, do pretty formatting of exception messages (for development environments), etc.
You may retrieve the response object following the front controller dispatch(), or request the front controller to
return the response object instead of rendering output.
// retrieve post-dispatch:
$front->dispatch();
$response = $front->getResponse();
if ($response->isException()) {
// log, mail, etc...
}
218
Zend_Controller
// do some processing...
By default, exception messages are not displayed. This behaviour may be overridden by calling
renderExceptions(), or enabling the front controller to throwExceptions(), as shown above:
$response->renderExceptions(true);
$front->dispatch($request, $response);
// or:
$front->returnResponse(true);
$response = $front->dispatch();
$response->renderExceptions();
$response->sendResponse();
// or:
$front->throwExceptions(true);
$front->dispatch();
Manipulating Headers
As stated previously, one aspect of the response object's duties is to collect and emit HTTP response headers. A variety
of methods exist for this:
• canSendHeaders() is used to determine if headers have already been sent. It takes an optional flag indicating
whether or not to throw an exception if headers have already been sent. This can be overridden by setting the property
headersSentThrowsException to FALSE.
Before setting the header, it checks with canSendHeaders() to see if this operation is allowed at this point, and
requests that an exception be thrown.
• setRedirect($url, $code = 302) sets an HTTP Location header for a redirect. If an HTTP status code
has been provided, it will use that status code.
Internally, it calls setHeader() with the $replace flag on to ensure only one such header is ever sent.
• getHeaders() returns an array of all headers. Each array element is an array with the keys 'name' and 'value'.
• setRawHeader() can be used to set headers that are not key and value pairs, such as an HTTP status header.
219
Zend_Controller
• clearAllHeaders() clears both regular key and value headers as well as raw headers.
In addition to the above methods, there are accessors for setting and retrieving the HTTP response code for the current
request, setHttpResponseCode() and getHttpResponseCode().
Named Segments
The response object has support for "named segments". This allows you to segregate body content into different
segments and order those segments so output is returned in a specific order. Internally, body content is saved as an
array, and the various accessor methods can be used to indicate placement and names within that array.
As an example, you could use a preDispatch() hook to add a header to the response object, then have the action
controller add body content, and a postDispatch() hook add a footer:
// Assume that this plugin class is registered with the front controller
class MyPlugin extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$response = $this->getResponse();
$view = new Zend_View();
$view->setBasePath('../views/scripts');
$response->prepend('header', $view->render('header.phtml'));
}
$response->append('footer', $view->render('footer.phtml'));
}
}
In the above example, a call to /my/foo will cause the final body content of the response object to have the following
structure:
array(
'header' => ..., // header content
'default' => ..., // body content from MyController::fooAction()
'footer' => ... // footer content
220
Zend_Controller
);
When this is rendered, it will render in the order in which elements are arranged in the array.
• setBody() and appendBody() both allow you to pass a second value, $name, indicating a named segment. In
each case, if you provide this, it will overwrite that specific named segment or create it if it does not exist (appending
to the array by default). If no named segment is passed to setBody(), it resets the entire body content array. If no
named segment is passed to appendBody(), the content is appended to the value in the 'default' name segment.
• prepend($name, $content) will create a segment named $name and place it at the beginning of the array.
If the segment exists already, it will be removed prior to the operation (i.e., overwritten and replaced).
• append($name, $content) will create a segment named $name and place it at the end of the array. If the
segment exists already, it will be removed prior to the operation (i.e., overwritten and replaced).
• insert($name, $content, $parent = null, $before = false) will create a segment named
$name. If provided with a $parent segment, the new segment will be placed either before or after that segment
(based on the value of $before) in the array. If the segment exists already, it will be removed prior to the operation
(i.e., overwritten and replaced).
• clearBody($name = null) will remove a single named segment if a $name is provided (and the entire
array otherwise).
• getBody($spec = false) can be used to retrieve a single array segment if $spec is the name of a named
segment. If $spec is FALSE, it returns a string formed by concatenating all named segments in order. If $spec
is TRUE, it returns the body content array.
• hasExceptionOfCode($code) allows you to determine if an exception with a specific code is in the stack.
• getExceptionByType($type) allows you to retrieve all exceptions of a specific class from the stack. It will
return FALSE if none are found, and an array of exceptions otherwise.
• getExceptionByMessage($message) allows you to retrieve all exceptions with a specific message from
the stack. It will return FALSE if none are found, and an array of exceptions otherwise.
• getExceptionByCode($code) allows you to retrieve all exceptions with a specific code from the stack. It
will return FALSE if none are found, and an array of exceptions otherwise.
221
Zend_Controller
• renderExceptions($flag) allows you to set a flag indicating whether or not exceptions should be emitted
when the response is sent.
The base response class is Zend_Controller_Response_Abstract, and any subclass you create should
extend that class or one of its derivatives. The various methods available have been listed in the previous sections.
Reasons to subclass the response object include modifying how output is returned based on the request environment
(e.g., not sending headers for CLI or PHP-GTK requests), adding functionality to return a final view based on content
stored in named segments, etc.
Plugins
Introduction
The controller architecture includes a plugin system that allows user code to be called when certain events occur in
the controller process lifetime. The front controller uses a plugin broker as a registry for user plugins, and the plugin
broker ensures that event methods are called on each plugin registered with the front controller.
The event methods are defined in the abstract class Zend_Controller_Plugin_Abstract, from which user
plugin classes inherit:
• routeStartup() is called before Zend_Controller_Front calls on the router to evaluate the request
against the registered routes.
• preDispatch() is called before an action is dispatched by the dispatcher. This callback allows
for proxy or filter behavior. By altering the request and resetting its dispatched flag (via
Zend_Controller_Request_Abstract::setDispatched(false)), the current action may be
skipped and/or replaced.
• postDispatch() is called after an action is dispatched by the dispatcher. This callback allows
for proxy or filter behavior. By altering the request and resetting its dispatched flag (via
Zend_Controller_Request_Abstract::setDispatched(false)), a new action may be specified
for dispatching.
Writing Plugins
In order to write a plugin class, simply include and extend the abstract class
Zend_Controller_Plugin_Abstract:
222
Zend_Controller
// ...
}
None of the methods of Zend_Controller_Plugin_Abstract are abstract, and this means that plugin classes
are not forced to implement any of the available event methods listed above. Plugin writers may implement only those
methods required by their particular needs.
Zend_Controller_Plugin_Abstract also makes the request and response objects available to controller
plugins via the getRequest() and getResponse() methods, respectively.
Using Plugins
Plugin classes are registered with Zend_Controller_Front::registerPlugin(), and may be registered
at any time. The following snippet illustrates how a plugin may be used in the controller chain:
223
Zend_Controller
$front = Zend_Controller_Front::getInstance();
$front->setControllerDirectory('/path/to/controllers')
->setRouter(new Zend_Controller_Router_Rewrite())
->registerPlugin(new MyPlugin());
$front->dispatch();
Assuming that no actions called emit any output, and only one action is called, the functionality of the above plugin
would still create the following output:
<p>routeStartup() called</p>
<p>routeShutdown() called</p>
<p>dispatchLoopStartup() called</p>
<p>preDispatch() called</p>
<p>postDispatch() called</p>
<p>dispatchLoopShutdown() called</p>
Nota
Plugins may be registered at any time during the front controller execution. However, if an event has passed
for which the plugin has a registered event method, that method will not be triggered.
• getPlugin($class) allows you to retrieve a plugin by class name. If no plugins match, it returns FALSE. If
more than one plugin of that class is registered, it returns an array.
• unregisterPlugin($plugin) allows you to remove a plugin from the stack. You may pass a plugin object,
or the class name of the plugin you wish to unregister. If you pass the class name, any plugins of that class will
be removed.
ActionStack
The ActionStack plugin allows you to manage a stack of requests, and operates as a postDispatch plugin. If a forward
(i.e., a call to another action) is already detected in the current request object, it does nothing. However, if not, it checks
its stack and pulls the topmost item off it and forwards to the action specified in that request. The stack is processed
in LIFO order.
You can retrieve the plugin from the front controller at any time using
Zend_Controller_Front::getPlugin('Zend_Controller_Plugin_ActionStack'). Once you
have the plugin object, there are a variety of mechanisms you can use to manipulate it.
• getRegistry() and setRegistry(). Internally, ActionStack uses a Zend_Registry instance to store the
stack. You can substitute a different registry instance or retrieve it with these accessors.
224
Zend_Controller
• getRegistryKey() and setRegistryKey(). These can be used to indicate which registry key to use when
pulling the stack. Default value is 'Zend_Controller_Plugin_ActionStack'.
• pushStack() and popStack() allow you to add to and pull from the stack, respectively. pushStack()
accepts a request object.
An additional method, forward(), expects a request object, and sets the state of the current request object in the
front controller to the state of the provided request object, and markes it as undispatched (forcing another iteration
of the dispatch loop).
Zend_Controller_Plugin_ErrorHandler
Zend_Controller_Plugin_ErrorHandler provides a drop-in plugin for handling exceptions thrown by your
application, including those resulting from missing controllers or actions; it is an alternative to the methods listed in
the MVC Exceptions section.
In other words, the ErrorHandler plugin is designed to handle HTTP 404-type errors (page missing) and 500-type
errors (internal error). It is not intended to catch exceptions raised in other plugins or routing.
• setErrorHandler() takes an associative array, which may contain any of the keys 'module', 'controller', or
'action', with which it will set the appropriate values.
Additionally, you may pass an optional associative array to the constructor, which will then proxy to
setErrorHandler().
If an exception occurs dispatching the error handler, the plugin will tell the front controller to throw exceptions, and
rethrow the last exception registered with the response object.
Exceptions captured are logged in an object registered in the request. To retrieve it, use
Zend_Controller_Action::_getParam('error_handler'):
225
Zend_Controller
Once you have the error object, you can get the type via $errors->type;. It will be one of the following:
You can then test for either of the first two types, and, if so, indicate a 404 page:
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
// 404 error -- controller or action not found
$this->getResponse()
->setRawHeader('HTTP/1.1 404 Not Found');
Finally, you can retrieve the exception that triggered the error handler by grabbing the exception property of the
error_handler object:
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
226
Zend_Controller
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
// 404 error -- controller or action not found
$this->getResponse()
->setRawHeader('HTTP/1.1 404 Not Found');
// ...
If you wish to render errors inline in such pages, no changes will be necessary. If you do not wish to render such
content, you should clear the response body prior to rendering any views:
$this->getResponse()->clearBody();
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Zend_Controller_Plugin_ErrorHandler());
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(array(
'module' => 'mystuff',
'controller' => 'static',
'action' => 'error'
)));
227
Zend_Controller
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin($plugin);
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
// 404 error -- controller or action not found
$this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found');
$content =<<<EOH
<h1>Error!</h1>
<p>The page you requested was not found.</p>
EOH;
break;
default:
// application error
$content =<<<EOH
<h1>Error!</h1>
<p>An unexpected error occurred. Please try again later.</p>
EOH;
break;
}
$this->view->content = $content;
}
}
Zend_Controller_Plugin_PutHandler
Zend_Controller_Plugin_PutHandler provides a drop-in plugin for marshalling PUT request bodies into
request parameters, just like POST request bodies. It will inspect the request and, if PUT, will use parse_str to parse
the raw PUT body into an array of params which is then set on the request. E.g.,
228
Zend_Controller
title=Hello&body=World
To receive the 'title' and 'body' params as regular request params, register the plugin:
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Zend_Controller_Plugin_PutHandler());
Then you can access the PUT body params by name from the request inside your controller:
...
public function putAction()
{
$title = $this->getRequest()->getParam('title'); // $title = "Hello"
$body = $this->getRequest()->getParam('body'); // $body = "World"
}
...
docroot/
index.php
application/
default/
controllers/
IndexController.php
FooController.php
models/
views/
scripts/
index/
foo/
helpers/
filters/
blog/
controllers/
IndexController.php
models/
views/
scripts/
index/
helpers/
229
Zend_Controller
filters/
news/
controllers/
IndexController.php
ListController.php
models/
views/
scripts/
index/
list/
helpers/
filters/
In this paradigm, the module name serves as a prefix to the controllers it contains. The above
example contains three module controllers, 'Blog_IndexController', 'News_IndexController', and
'News_ListController'. Two global controllers, 'IndexController' and 'FooController' are also
defined; neither of these will be namespaced. This directory structure will be used for examples in this chapter.
So, how do you implement such a directory layout using the Zend Framework MVC components?
With setControllerDirectory(), you will need to pass an associative array and specify key and value pairs
of module name and directory paths. The special key default will be used for global controllers (those not needing
a module namespace). All entries should contain a string key pointing to a single path, and the default key must be
present. As an example:
$front->setControllerDirectory(array(
'default' => '/path/to/application/controllers',
'blog' => '/path/to/application/blog/controllers'
));
addControllerDirectory() will take an optional second argument. When using modules, pass the module
name as the second argument; if not specified, the path will be added to the default namespace. As an example:
$front->addControllerDirectory('/path/to/application/news/controllers',
'news');
Saving the best for last, the easiest way to specify module directories is to do so en masse, with all modules under a
common directory and sharing the same structure. This can be done with addModuleDirectory():
/**
230
Zend_Controller
The above example will define the default, foo, and bar modules, each pointing to the controllers/ subdirectory
of their respective module.
You may customize the controller subdirectory to use within your modules by using
setModuleControllerDirectoryName():
/**
* Change the controllers subdirectory to be 'con'
* application/
* modules/
* default/
* con/
* foo/
* con/
* bar/
* con/
*/
$front->setModuleControllerDirectoryName('con');
$front->addModuleDirectory('/path/to/application/modules');
Nota
You can indicate that no controller subdirectory be used for your modules by passing an empty value to
setModuleControllerDirectoryName().
Routing to Modules
The default route in Zend_Controller_Router_Rewrite is an object of type
Zend_Controller_Router_Route_Module. This route expects one of the following routing schemas:
• :module/:controller/:action/*
• :controller/:action/*
In other words, it will match a controller and action by themselves or with a preceding module. The rules for matching
specify that a module will only be matched if a key of the same name exists in the controller directory array passed
to the front controller and dispatcher.
231
Zend_Controller
first looks for this default controller in the module path, and then falls back on the default controller found in the
'default', global, namespace.
If you wish to always default to the global namespace, set the $useDefaultControllerAlways parameter in
the front controller:
$front->setParam('useDefaultControllerAlways', true);
Excepciones MVC
Introducción
Los componentes MVC en Zend Framework utilizan un Front Controller, lo que significa que todas las solicitudes
de un determinado sitio pasarán por un solo punto de entrada. Como resultado, todas las excepciones burbujearán
eventualmente hacia arriba hasta el Front Controller, permitiendo al desarrollador manejarlos en un solo lugar.
Sin embargo, los mensajes de excepción y la información de backtrace contienen a menudo información sensible del
sistema, como declaraciones SQL, ubicaciones de archivos y otras cosas más. Para ayudar a proteger su sitio, por
defecto Zend_Controller_Front captura todas las excepciones y las registra con el objeto respuesta; a su vez,
y por defecto, el objeto respuesta no muestra mensajes de excepción.
• Por defecto, el error handler plugin está registrado y activo. Este plugin fue diseñado para manejar:
Operan como un plugin de postDispatch(), y comprueban para ver si un despachador, controlador de acción,
o de otra excepción ha ocurrido. Si así fuera, lo remite a un controlador de manejo de errores.
Este manejador abarcará la mayoría de las situaciones excepcionales, y maneja airosamente controladores y acciones
perdidos.
• Zend_Controller_Front::throwExceptions()
Pasando a este método un valor booleano verdadero, puede decirle al front controller que, en lugar de sumar
excepciones en el objeto respuesta o utilizando el plugin de manejo de errores, prefiere manejarlos usted mismo.
Como ejemplo:
$front->throwExceptions(true);
try {
$front->dispatch();
} catch (Exception $e) {
// usted mismo maneja las excepciones
}
Este método es probablemente la forma más fácil de añadir un manejo de excepciones personalizado que abarque
toda la gama de posibles excepciones a su aplicación de front controller.
232
Zend_Controller
• Zend_Controller_Response_Abstract::renderExceptions()
Al pasar a este método un valor booleano verdadero, le esta diciendo al objeto respuesta que debe emitir un mensaje
de excepción y backtrace cuando se renderiza a sí mismo. En este escenario, se mostrará cualquier excepción
planteada por su aplicación. Esto no es recomendable para entornos de producción, pero sí en desarrollo.
• Zend_Controller_Front::returnResponse() y
Zend_Controller_Response_Abstract::isException().
$front->returnResponse(true);
$response = $front->dispatch();
if ($response->isException()) {
$exceptions = $response->getException();
// maneje las excepciones ...
} else {
$response->sendHeaders();
$response->outputBody();
}
$front->setParam('useDefaultControllerAlways', true);
// o
$dispatcher->setParam('useDefaultControllerAlways', true);
Cuando este flag está establecido, el despachador utilizará el controlador y la acción por defecto en lugar de
lanzar una excepción. La desventaja de este método es que cualquier error ortográfico que un usuario haga cuando
acceda a su sitio lo resolverá y mostrará su página de inicio, y que puede causar estragos con la optimización
para los motores de búsqueda.
233
Zend_Controller
El ejemplo anterior intercepta cualquier llamada a un método de acción indefinido y redirecciona a la acción
predeterminada en el controlador.
return $action;
}
}
234
Zend_Controller
El código anterior comprueba para ver que las acciones solicitadas existan en la clase del controlador; si no, se
restablece la acción a la acción por defecto.
Este método es agradable porque puede alterar transparentemente la acción antes del último despacho. Sin
embargo, también significa que errores ortográficos en la URL todavía pueden despacharse correctamente, lo que
no es muy bueno para la optimización en un motor de búsqueda.
• Use Zend_Controller_Action::preDispatch() o
Zend_Controller_Plugin_Abstract::preDispatch() para identificar acciones inválidas.
Alternativamente, puede verificar esta información en un plugin global. Esto tiene la ventaja de ser independiente
del controlador de acción; si su aplicación consiste en una variedad de controladores de acción, y no todos ellos
heredan de la misma clase, este método puede añadir coherencia a su manejo de clases diferentes.
Como ejemplo:
$r = new ReflectionClass($class);
$action = $dispatcher->getActionMethod($request);
if (!$r->hasMethod($action)) {
$defaultAction = $dispatcher->getDefaultAction();
$controllerName = $request->getControllerName();
$response = $front->getResponse();
$response->setRedirect('/' . $controllerName
. '/' . $defaultAction);
$response->sendHeaders();
exit;
}
}
}
En este ejemplo, vamos a consultar para ver si la acción solicitada está disponible en el controlador. Si no,
redireccionamos a la acción predeterminada en el controlador, y salimos inmediatamente de la ejecución del script.
235
236
Capítulo 13. Zend_Currency
Introduction to Zend_Currency
Zend_Currency is part of the strong support for i18n in Zend Framework. It handles all issues related to currency,
money representation and formatting. It also provides additional methods which provide localized information on
currencies, such as which currency is used in which region.
Zend_Currency works with all available locales and therefore knows about over 100 different localized
currencies. This includes currency names, abbreviations, money signs and more.
Zend_Currency does not include the value of the currency. This is the reason why its functionality is not included
in Zend_Locale_Format. Zend_Currency has the advantage that already defined currency representations
can be reused.
• Fluent Interface
• Additional Methods
Zend_Currency includes additional methods that offer information about which regions a currency is used in
or which currency is used in a specified region.
Zend_Currency also supports the usage of an application-wide locale. You can set a Zend_Locale instance in
the registry as shown below. With this notation you can avoid setting the locale manually for each instance when you
want to use the same locale throughout the application.
237
Zend_Currency
Nota
If your system has no default locale, or if the locale of your system can not be detected automatically,
Zend_Currency will throw an exception. If see this exception, you should consider setting the locale
manually.
Depending on your needs, several parameters can be speicified at instantiation. Each of these parameters is optional
and can be omitted. Even the order of the parameters can be switched. The meaning of each parameter is described
in this list:
• currency:
A locale can include several currencies. Therefore the first parameter 'currency' can define which currency should
be used by giving the short name or full name of that currency. If that currency in not recognized in any locale an
exception will be thrown. Currency short names are always made up of 3 letters, written in uppercase. Well known
currency shortnames include USD or EUR.
• locale:
The 'locale' parameter defines which locale should be used for formatting the currency. The specified locale will
also be used to get the script and symbol for this currency if these parameters are not given.
Nota
Note that Zend_Currency only accepts locales which include a region. This means that all locales that
only include a language will result in an exception. For example the locale en will cause an exception to
be thrown whereas the locale en_US will return USD as currency.
You can omit any of the parameters to Zend_Currency's constructor if you want to use the default values. This has
no negative effect on handling the currencies. It can be useful if, for example, you don't know the default currency
for a region.
238
Zend_Currency
Nota
For many countries there are several known currencies. Typically, one currency will currently be in use, with
one or more ancient currencies. If the 'currency' parameter is suppressed the contemporary currency will
be used. The region 'de' for example knows the currencies 'EUR' and 'DEM'... 'EUR' is the contemporary
currency and will be used if the currency parameter is omitted.
If you have a localized number you will have to convert it first to an normalized number with
Zend_Locale_Format::getNumber(). It may then be used with toCurrency() to create a currency output string.
toCurrency(array $options) accepts an array with options which can be used to temporarily set another
format or currency representation. For details about which options can be used see Changing the Format of a Currency.
The symbol of the currency is normally displayed within the currency output string. It can be suppressed when
needed or even overwritten.
• Currency position:
The position of the currency symbol is normally automatically defined by the locale. It can be changed if necessary.
• Script:
The script which shall be used to display digits. Detailed information about scripts and their usage can be found in
the documentation of Zend_Locale in the chapter Numeral System Conversion.
239
Zend_Currency
• Number formatting:
The amount of currency (formally known as value of money) is formatted by the usage of formatting rules within
the locale. For example is in English the ',' sign used as separator for thousands, and in German the '.' sign.
So if you need to change the format, you should the setFormat() method. It takes an array which should include
every option you want to change. The $options array supports the following settings:
• position: Defines the position at which the currency description should be displayed. The supported positions can
be found in this table.
• script: Defined which script should be used for displaying digits. The default script for most locales is 'Latn', which
includes the digits 0 to 9. Other scripts such as 'Arab' (Arabian) can be used.
• format: Defines the format which should be used for displaying numbers. This number-format includes for example
the thousand separator. You can eighter use a default format by giving a locale identifier, or define the number-
format manually. If no format is set the locale from the Zend_Currency object will be used.
• display: Defines which part of the currency should be used for displaying the currency representation. There are 4
representations which can be used and which are all described in this table.
• precision: Defines the precision which should be used for the currency representation. The default value is 2.
• name: Defines the full currency name which should be displayed. This option overwrites any currency name which
is set through the creation of Zend_Currency.
• currency: Defines the international abbreviation which should be displayed. This option overwrites any abbreviation
which is set through the creation of Zend_Currency.
• symbol: Defines the currency symbol which should be displayed. This option overwrites any symbol which is set
through the creation of Zend_Currency.
Tabla 13.2. Constants for the selecting the position of the currency description
constant description
STANDARD Set the standard position as defined within the locale
RIGHT Display the currency representation at the right side of
the value
LEFT Display the currency representation at the left side of the
value
240
Zend_Currency
• getSymbol():
Returns the known symbol of the set currency or a given currency. For example $ for the US Dollar within the
locale 'en_US.
• getShortName():
Returns the abbreviation of the set currency or a given currency. For example USD for the US Dollar within the
locale 'en_US.
• getName():
Returns the full name of the set currency of a given currency. For example US Dollar for the US Dollar within
the locale 'en_US.
• getRegionList():
Returns a list of regions where the set currency or a given one is known to be used. It is possible that a currency is
used within several regions, so the return value is always an array.
• getCurrencyList():
The function getSymbol(), getShortName() and getName() accept two optional parameters. If no parameter
is given the expected data will be returned from the set currency. The first parameter takes the shortname of a currency.
Short names are always three lettered, for example EUR for euro or USD for US Dollar. The second parameter defines
from which locale the data should be read. If no locale is given, the set locale is used.
241
Zend_Currency
// prints '$'
echo $currency->getSymbol();
// prints 'EUR'
echo $currency->getShortName('EUR');
// returns an array with all currencies which were ever used in this
// region
print_r($currency->getCurrencyList('de_AT');
242
Zend_Currency
Zend_Currency::setCache($cache);
243
244
Capítulo 14. Zend_Date
Introducción
El componente Zend_Date ofrece una API detallada pero simple para manipular fechas y horas. Sus métodos
aceptan una gran variedad de tipos de información, incluyendo partes de fecha, en numerosas combinaciones
provocando muchas características y posibilidades más allá de las funciones de fecha PHP relacionadas. Para las
últimas actualizaciones manuales, por favor ver el siguiente link our online manual (sincronizado frecuentemente con
Subversion) [http://framework.zend.com/wiki/display/ZFDOCDEV/Home] .
Aunque la simplicidad sea el objetivo, trabajar con fechas y tiempos localizados mientras se modifican, combinan y
comparan partes, provoca una complejidad inevitable. Las fechas, así como los tiempos, a menudo son escritos de
forma diferente en zonas locales distintas. Por ejemplo, algunos colocan primero el mes, mientras otros escriben el año
en primer lugar cuando expresan fechas del calendario. Para más información relacionada con manejo de localizaciones
y normalización, por favor vea el manual de Zend_Locale .
Zend_Date también soporta nombres de meses abreviados en varios idiomas. Zend_Locale facilita la
normalización de meses localizados y nombres de días de la semana a timestamps, los cuales pueden, a su vez, ser
mostrados localizados a otras regiones.
¡Al crear instancias de Zend_Date, su zona horaria se convertirá automáticamente en la zona horaria por defecto
actual! De esta forma, la configuración de zona horaria tendrá en cuenta cualquier cambio de hora de invierno/verano
(Daylight Saving Time, DST), eliminando la necesidad de especificarlo explícitamente.
Tenga en cuenta que las zonas horarias UTC y GMT no incluyen el cambio de hora de invierno/verano (Daylight
Saving Time, DST). Esto significa que aunque defina a mano que Zend_Date deba trabajar con DST, podría ser
anulado por las instancias de Zend_Date que han sido fijadas a UTC o GMT.
• API sencilla
245
Zend_Date
Zend_Date aporta una API muy sencilla, que combina lo mejor de la funcionalidad fecha/hora de cuatro lenguajes
de programación. Es posible, por ejemplo, añadir o comparar dos horas dentro de una misma columna.
• Completamente internacionalizado
Todos los nombres de meses y días de la semana completos y abreviados están incluidos para más de 130 idiomas.
Los métodos admiten tanto entrada como salida de fechas usando los nombres localizados de meses y días de la
semana.
• Timestamps ilimitados
A pesar de que la documentación de PHP 5.2 indice: "El intervalo de valores admitidos de timestamps es desde el
13 Dec 1901 20:45:54 GMT al 19 Ene 2038 03:14:07 GMT," Zend_Date admite un rango casi ilimitado, con la
ayuda de la extensión BCMath. Si BCMath no está disponible, Zend_Date tendrá una funcionalidad de timestamps
reducida al rango del tipo float soportado por su servidor. El tamaño de un float es dependiente de la plataforma,
aunque un máximo de ~1.8e308 con una precisión de cerca de 14 dígitos decimales es un valor habitual (formato
64 bit IEEE)." [ http://www.php.net/float ]. Adicionalmente, las limitaciones heredadas de los tipos de dato float,
y errores de redondeo de números flotantes pueden introducir errores en los cálculos. Para evitar estos problemas,
los componentes ZF I18n usan la extensión BCMath, si está disponible.
Las especificaciones de fecha ISO_8601 están aceptadas. Incluso las especificaciones de fecha ISO_8601
parcialmente autorizadas serán identificadas. Estos formatos de fecha son particularmente útiles al trabajar con
bases de datos. Por ejemplo, aunque MsSQL y MySQL [http://dev.mysql.com/doc/refman/5.0/en/date-and-time-
functions.html] difieren ligeramente uno de otro, ambos tienen soporte por parte de Zend_Date usando la constante
de especificación de formato Zend_Date::ISO_8601. Cuando las cadenas de fecha sean del tipo "Y/m/d" o "Y-
m-d H:i:s", de acuerdo con los tokens de formato PHP date(), use el soporte integrado de Zend_Date para fechas
formateadas ISO 8601.
Las horas de amanecer y puesta de sol pueden mostrarse para cualquier lugar y día, para que no pierda ni un segundo
de luz diurna para trabajar en su proyecto PHP favorito :)
Theory of Operation
Why is there only one class Zend_Date for handling dates and times in Zend Framework?
Many languages split the handling of times and calendar dates into two classes. However, Zend Framework strives
for extreme simplicity, and forcing the developer to manage different objects with different methods for times and
dates becomes a burden in many situations. Since Zend_Date methods support working with ambiguous dates that
might not include all parts (era, year, month, day, hour, minute, second, timezone), developers enjoy the flexibility
and ease of using the same class and the same methods to perform the same manipulations (e.g. addition, subtraction,
comparison, merging of date parts, etc.). Splitting the handling of these date fragments into multiple classes would
create complications when smooth interoperation is desired with a small learning curve. A single class reduces code
duplication for similar operations, without the need for a complex inheritance hierarchy.
Internals
• UNIX Timestamp
All dates and times, even ambiguous ones (e.g. no year), are represented internally as absolute moments in time,
represented as a UNIX timestamp expressing the difference between the desired time and January 1st, 1970 00:00:00
GMT. This was only possible, because Zend_Date is not limited to UNIX timestamps nor integer values. The
246
Zend_Date
BCMath extension is required to support extremely large dates outside of the range Fri, 13 Dec 1901 20:45:54 GMT
to Tue, 19 Jan 2038 03:14:07 GMT. Additional, tiny math errors may arise due to the inherent limitations of float
data types and rounding, unless using the BCMath extension.
Thus, an instance object representing three hours would be expressed as three hours after January 1st, 1970 00:00:00
GMT -i.e. 0 + 3 * 60 * 60 = 10800.
• PHP functions
Basic Methods
The following sections show basic usage of Zend_Date primarily by example. For this manual, "dates" always imply
a calendar date with a time, even when not explicitly mentioned, and vice-versa. The part not specified defaults to an
internal representation of "zero". Thus, adding a date having no calendar date and a time value of 12 hours to another
date consisting only of a calendar date would result in a date having that calendar date and a time of "noon".
Setting only a specific date, with no time part, implies a time set to 00:00:00. Conversely, setting only a specific time
implies a date internally set to 01.01.1970 plus the number of seconds equal to the elapsed hours, minutes, and seconds
identified by the time. Normally, people measure things from a starting point, such as the year 0 A.D. However, many
software systems use the first second of the year 1970 as the starting point, and denote times as a timestamp offset
counting the number of seconds elapsed from this starting point.
Current Date
Without any arguments, constructing an instance returns an object in the default locale with the current, local date
using PHP's time() function to obtain the UNIX timestamp [http://en.wikipedia.org/wiki/Unix_Time] for the object.
Make sure your PHP environment has the correct default timezone .
Zend_Date by Ejemplo
Reviewing basic methods of Zend_Date is a good place to start for those unfamiliar with date objects in other
languages or frameworks. A small example will be provided for each method below.
Output a Date
The date in a Zend_Date object may be obtained as a localized integer or string using the get() method. There
are many available options, which will be explained in later sections.
247
Zend_Date
Setting a Date
The set() method alters the date stored in the object, and returns the final date value as a timestamp (not an object).
Again, there are many options which will be explored in later sections.
Comparison of Dates
All basic Zend_Date methods can operate on entire dates contained in the objects, or can operate on date parts, such
as comparing the minutes value in a date to an absolute value. For example, the current minutes in the current time
may be compared with a specific number of minutes using compare(), as in the example below.
248
Zend_Date
Opciones Zend_Date
Selecting the Date Format Type
Several methods use date format strings, in a way similar to PHP's date(). If you are
more comfortable with PHP's date format specifier than with ISO format specifiers, then
you can use Zend_Date::setOptions(array('format_type' => 'php')). Afterward,
use PHP's date format specifiers for all functions which accept a $format parameter. Use
Zend_Date::setOptions(array('format_type' => 'iso')) to switch back to the default mode of
supporting only ISO date format tokens. For a list of supported format codes, see the section called “Self-Defined
OUTPUT Formats Using PHP's date() Format Specifiers”
If your actual timezone within the instance of Zend_Date is set to UTC or GMT the option 'fix_dst' will not be
used because these two timezones do not work with DST. When you change the timezone for this instance again to a
timezone which is not UTC or GMT the previous set 'fix_dst' option will be used again for date "math".
Month Calculations
When adding or substracting months from an existing date, the resulting value for the day of the month might be
unexpected, if the original date fell on a day close to the end of the month. For example, when adding one month to
January 31st, people familiar with SQL will expect February 28th as the result. On the other side, people familiar with
249
Zend_Date
Excel and OpenOffice will expect March 3rd as the result. The problem only occurs, if the resulting month does not
have the day, which is set in the original date. For Zend Framework developers, the desired behavior is selectable
using the extend_month option to choose either the SQL behaviour, if set to FALSE, or the spreadsheet behaviour
when set to TRUE. The default behaviour for extend_month is FALSE, providing behavior compatible to SQL. By
default, Zend_Date computes month calculations by truncating dates to the end of the month (if necessary), without
wrapping into the next month when the original date designates a day of the month exceeding the number of days
in the resulting month. Use Zend_Date::setOptions(array('extend_month' => true)); to make
month calculations work like popular spreadsheet programs.
Beware of mixing and matching operations with date parts between date objects for different timezones, which
generally produce undesireable results, unless the manipulations are only related to the timestamp. Operating on
Zend_Date objects having different timezones generally works, except as just noted, since dates are normalized to
UNIX timestamps on instantiation of Zend_Date.
Most methods expect a constant selecting the desired $part of a date, such as Zend_Date::HOUR. These
constants are valid for all of the functions below. A list of all available constants is provided in the section called
“List of All Constants”. If no $part is specified, then Zend_Date::TIMESTAMP is assumed. Alternatively,
a user-specified format may be used for $part, using the same underlying mechanism and format codes as
Zend_Locale_Format::getDate(). If a date object is constructed using an obviously invalid date (e.g. a month
number greater than 12), then Zend_Date will throw an exception, unless no specific date format has been selected
-i.e. $part is either NULL or Zend_Date::DATES (a "loose" format).
250
Zend_Date
If the optional $locale parameter is provided, then the $locale disambiguates the $date operand by replacing
month and weekday names for string $date operands, and even parsing date strings expressed according to
the conventions of that locale (see Zend_Locale_Format::getDate() ). The automatic normalization
of localized $date operands of a string type occurs when $part is one of the Zend_Date::DATE* or
Zend_Date::TIME* constants. The locale identifies which language should be used to parse month names and
weekday names, if the $date is a string containing a date. If there is no $date input parameter, then the $locale
parameter specifies the locale to use for localizing output (e.g. the date format for a string representation). Note that
the $date input parameter might actually have a type name instead (e.g. $hour for addHour()), although that
does not prevent the use of Zend_Date objects as arguments for that parameter. If no $locale was specified, then
the locale of the current object is used to interpret $date, or select the localized format for output.
Since Zend Framework 1.7.0 Zend_Date does also support the usage of an application wide locale. You can simply
set a Zend_Locale instance to the registry like shown below. With this notation you can forget about setting the
locale manually with each instance when you want to use the same locale multiple times.
Convenience methods exist for each combination of the basic operations and several common date parts as shown
in the tables below. These convenience methods help us lazy programmers avoid having to type out the date part
constants when using the general methods above. Conveniently, they are named by combining a prefix (name of a
basic operation) with a suffix (type of date part), such as addYear(). In the list below, all combinations of "Date
Parts" and "Basic Operations" exist. For example, the operation "add" exists for each of these date parts, including
addDay(), addYear(), etc.
These convenience methods have the same equivalent functionality as the basic operation methods, but expect string
and integer $date operands containing only the values representing the type indicated by the suffix of the convenience
251
Zend_Date
method. Thus, the names of these methods (e.g. "Year" or "Minute") identify the units of the $date operand, when
$date is a string or integer.
252
Zend_Date
253
Zend_Date
Comparing Dates
The following basic operations do not have corresponding convenience methods for the date parts listed in the section
called “Zend_Date API Overview” .
Method Explanation
equals() equals($date, $part = null, $locale = null)
254
Zend_Date
Method Explanation
Use isLeapYear() to determine if the
current object is a leap year, or use
Zend_Date::checkLeapYear($year) to check
$year, which can be a string, integer, or instance of
Zend_Date. Is the year a leap year?
isDate() isDate($date, $format = null, $locale = null)
• Number of month as
'month' (Zend_Date::MONTH_SHORT)
• Abbreviated timezone as
'timezone' (Zend_Date::TIMEZONE)
• Unix timestamp as
'timestamp' (Zend_Date::TIMESTAMP)
255
Zend_Date
Method Explanation
• Number of weekday as
'weekday' (Zend_Date::WEEKDAY_DIGIT)
• Day of year as
'dayofyear' (Zend_Date::DAY_OF_YEAR)
Method Explanation
getFractionalPrecision() Return the precision of the part seconds
setFractionalPrecision() Set the precision of the part seconds
Sunrise / Sunset
Three methods provide access to geographically localized information about the Sun, including the time of sunrise
and sunset.
256
Zend_Date
Creation of Dates
Zend_Date provides several different ways to create a new instance of itself. As there are different needs the most
convenient ways will be shown in this chapter.
$date = Zend_Date::now();
Of course each database can be said to convert the output of a defined column to a special value. For example you could
convert a datetime value to output a minute value. But this is time expensive and often you are in need of handling
dates in an other way than expected when creating the database query.
So we have one quick and one convenient way of creating dates from database values.
257
Zend_Date
258
Zend_Date
Using Constants
For example, the constant Zend_Date::HOUR can be used in the ways shown below. When working with days
of the week, calendar dates, hours, minutes, seconds, and any other date parts that are expressed differently when in
different parts of the world, the object's timezone will automatically be used to compute the correct value, even though
the internal timestamp is the same for the same moment in time, regardless of the user's physical location in the world.
Regardless of the units involved, output must be expressed either as GMT or UTC or localized to a locale. The example
output below reflects localization to Europe/GMT+1 hour (e.g. Germany, Austria, France).
259
Zend_Date
260
Zend_Date
261
Zend_Date
Especially note Zend_Date::DATES, since this format specifier has a unique property within Zend_Date as an
input format specifier. When used as an input format for $part, this constant provides the most flexible acceptance
of a variety of similar date formats. Heuristics are used to automatically extract dates from an input string and then
"fix" simple errors in dates (if any), such as swapping of years, months, and days, when possible.
262
Zend_Date
The accepted format specifiers can be changed from ISO Format to PHP's date format if you are more comfortable
with it. However, not all formats defined in the ISO norm are supported with PHP's date format specifiers. Use
the Zend_Date::setOptions(array('format_type' => 'php')) method to switch Zend_Date
methods from supporting ISO format specifiers to PHP date() type specifiers (see Self-Defined OUTPUT Formats
Using PHP's date() Format Specifiers below).
263
Zend_Date
264
Zend_Date
Nota
Note that the default ISO format differs from PHP's format which can be irritating if you have not used in
previous. Especially the format specifiers for Year and Minute are often not used in the intended way.
For year there are two specifiers available which are often mistaken. The Y specifier for the ISO year and
the y specifier for the real year. The difference is small but significant. Y calculates the ISO year, which is
265
Zend_Date
often used for calendar formats. See for example the 31. December 2007. The real year is 2007, but it is the
first day of the first week in the week 1 of the year 2008. So, if you are using 'dd.MM.yyyy' you will get
'31.December.2007' but if you use 'dd.MM.YYYY' you will get '31.December.2008'. As you see this is no
bug but a expected behaviour depending on the used specifiers.
For minute the difference is not so big. ISO uses the specifier m for the minute, unlike PHP which uses i. So
if you are getting no minute in your format check if you have used the right specifier.
The following table shows the list of PHP date format specifiers with their equivalent Zend_Date
constants and CLDR and ISO equivalent format specifiers. In most cases, when the CLDR and ISO
format does not have an equivalent format specifier, the PHP format specifier is not altered by
Zend_Locale_Format::convertPhpToIsoFormat(), and the Zend_Date methods then recognize these
"peculiar" PHP format specifiers, even when in the default "ISO" format mode.
266
Zend_Date
267
Zend_Date
Working Ejemplos
Within this chapter, we will describe several additional functions which are also available through Zend_Date. Of
course all described functions have additional examples to show the expected working and the simple API for the
proper using of them.
Checking Dates
Probably most dates you will get as input are strings. But the problem with strings is that you can not be sure if the
string is a real date. Therefor Zend_Date has spent an own static function to check date strings. Zend_Locale has
an own function getDate($date, $locale) which parses a date and returns the proper and normalized date
parts. A monthname for example will be recognised and returned just a month number. But as Zend_Locale does
not know anything about dates because it is a normalizing and localizing class we have integrated an own function
isDate($date) which checks this.
isDate($date, $format, $locale) can take up to 3 parameters and needs minimum one parameter. So
what we need to verify a date is, of course, the date itself as string. The second parameter can be the format which the
date is expected to have. If no format is given the standard date format from your locale is used. For details about how
formats should look like see the chapter about self defined formats.
268
Zend_Date
The third parameter is also optional as the second parameter and can be used to give a locale. We need the locale to
normalize monthnames and daynames. So with the third parameter we are able to recognise dates like '01.Jänner.2000'
or '01.January.2000' depending on the given locale.
isDate() of course checks if a date does exist. Zend_Date itself does not check a date. So it is possible to create a
date like '31.February.2000' with Zend_Date because Zend_Date will automatically correct the date and return
the proper date. In our case '03.March.2000'. isDate() on the other side does this check and will return FALSE on
'31.February.2000' because it knows that this date is impossible.
// Checking dates
$date = '01.03.2000';
if (Zend_Date::isDate($date)) {
print "String $date is a date";
} else {
print "String $date is NO date";
}
As most people do not know the location of their city we have also spent a helper class which provides the location
data for about 250 capital and other big cities around the whole world. Most people could use cities near themself as
the difference for locations situated to each other can only be measured within some seconds.
For creating a listbox and choosing a special city the function Zend_Date_Cities::getCityList() can be
used. It returns the names of all available predefined cities for the helper class.
269
Zend_Date
The location itself can be received with the Zend_Date_Cities::city() function. It accepts the name of the
city as returned by the Zend_Date_Cities::getCityList() function and optional as second parameter the
horizon to set.
There are 4 defined horizons which can be used with locations to receive the exact time of sunset and sunrise. The
'$horizon' parameter is always optional in all functions. If it is not set, the 'effective' horizon is used.
Of course also a self-defined location can be given and calculated with. Therefor a 'latitude' and a 'longitude' has to
be given and optional the 'horizon'.
As now all needed data can be set the next is to create a Zend_Date object with the day where sunset or sunrise should
be calculated. For the calculation there are 3 functions available. It is possible to calculate sunset with 'getSunset()',
sunrise with 'getSunrise()' and all available informations related to the sun with 'getSunInfo()'. After the
calculation the Zend_Date object will be returned with the calculated time.
// create a date object for the day for which the sun has to be calculated
$date = new Zend_Date('10.03.2007', Zend_Date::ISO_8601, 'de');
// calculate sunset
$sunset = $date->getSunset($city);
print $sunset->get(Zend_Date::ISO_8601);
270
Zend_Date
$info = $date->getSunInfo($city);
foreach ($info as $sun) {
print "\n" . $sun->get(Zend_Date::ISO_8601);
}
Time Zones
Time zones are as important as dates themselves. There are several time zones depending on where in the world a
user lives. So working with dates also means to set the proper timezone. This may sound complicated but it's easier
as expected. As already mentioned in the first chapter of Zend_Date the default timezone has to be set. Either by
php.ini or by definition within the bootstrap file.
A Zend_Date object of course also stores the actual timezone. Even if the timezone is changed after the creation of
the object it remembers the original timezone and works with it. It is also not necessary to change the timezone within
the code with PHP functions. Zend_Date has two built-in functions which makes it possible to handle this.
getTimezone() returns the actual set timezone of within the Zend_Date object. Remember that Zend_Date is
not coupled with PHP internals. So the returned timezone is not the timezone of the PHP script but the timezone of the
object. setTimezone($zone) is the second function and makes it possible to set new timezone for Zend_Date.
A given timezone is always checked. If it does not exist an exception will be thrown. Additionally the actual scripts
or systems timezone can be set to the date object by calling setTimezone() without the zone parameter. This is
also done automatically when the date object is created.
Zend_Date always takes the actual timezone for object creation as shown in the first lines of the example. Changing
the timezone within the created object also has an effect to the date itself. Dates are always related to a timezone.
Changing the timezone for a Zend_Date object does not change the time of Zend_Date. Remember that internally
dates are always stored as timestamps and in GMT. So the timezone means how much hours should be substracted or
added to get the actual global time for the own timezone and region.
271
Zend_Date
Having the timezone coupled within Zend_Date has another positive effect. It is possible to have several dates with
different timezones.
272
Capítulo 15. Zend_Db
Zend_Db_Adapter
Zend_Db y sus clases relacionadas proporcionan una interfaz simple de base de datos SQL para Zend Framework. El
Zend_Db_Adapter es la clase base que se utiliza para conectar su aplicación PHP A una base de datos (RDBMS).
Existen diferentes clases Adapters(Adaptador) para cada tipo de base de datos (RDBMS).
Las clases Adapters de Zend_Db crean un puente entre las extensiones de base de datos de PHP hacia una interfaz
común, para ayudarle a escribir aplicaciones PHP una sola vez y poder desplegar múltiples tipos de base de datos
(RDBMS) con muy poco esfuerzo.
La Interfaz de la clase adaptador (adapter) es similar a la intefaz de la extensión PHP Data Objects [http://www.php.net/
pdo] . Zend_Db proporciona clases Adaptadoras para los drivers PDO de los siguientes tipos de RDBMS:
• IBM DB2 e Informix Dynamic Server (IDS), usando la extensión PHP pdo_ibm [http://www.php.net/pdo-ibm]
Ademas, Zend_Db proporciona clases Adaptadoras que utilizan las extensiones de base de datos de PHP de los
siguientes tipos:
Nota
Cada Zend_Db_Adaptador utiliza una extensión PHP. Se debe de tener habilitada la respectiva extensión en su
entorno PHP para utilizar un Zend_Db_Adapter. Por ejemplo, si se utiliza una clase Zend_Db_Adapter
basada en PDO, tiene que habilitar tanto la extensión PDO como el driver PDO del tipo de base de datos
que se utiliza.
273
Zend_Db
El primer argumento es una cadena que nombra al nombre base de la clase Adaptador. Por ejemplo, la cadena
'Pdo_Mysql' corresponde a la clase Zend_Db_Adapter_Pdo_Mysql. El segundo argumento es el mismo array de
parámetros que hubiera enviado al constructor del adaptador.
// require_once 'Zend/Db/Adapter/Pdo/Mysql.php';
Si crea su propia clase que extiende a Zend_Db_Adapter_Abstract, pero no nombra su clase con el prefijo de paquete
"Zend_Db_Adapter", se puede utilizar el método factory() para cargar su adaptador si se especifica la parte
principal de la clase del adaptador con la clave "adapterNamespace" en el conjunto de parámetros
Ejemplo 15.3. Usando el método factory para una clase Adaptador personalizada
274
Zend_Db
Si el primer argumento es un objeto de configuración, se espera que contenga una propiedad llamada adapter ,
conteniendo la cadena que da nombre al nombre base de la clase de adaptador. Opcionalmente, el objeto puede contener
una propiedad llamada params , con subpropiedades correspondientes a nombres de parámetros del adaptador. Esto
es usado sólo si el segundo argumento del método factory() se ha omitido.
Ejemplo 15.4. Uso del método factory del Adaptador con un objeto Zend_Config
En el siguiente ejemplo, un objeto Zend_Config es creado usando un array. También puedes cargar los datos de un
archivo externo, por ejemplo con Zend_Config_Ini o Zend_Config_Xml .
$db = Zend_Db::factory($config->database);
El segundo argumento del método factory() puede ser un array asociativo con entradas correspondientes a los
parámetros del adaptador. Este argumento es opcional. Si el primer argumento es de tipo Zend_Config, se asume que
tiene todos los parametros, y el segundo argumento es ignorado.
• host : una string conteniendo un nombre de host o dirección IP del servidor de base de datos. Si la base de datos
está corriendo sobre el mismo host que la aplicación PHP, usted puede utilizar 'localhost' o '127.0.0.1'.
• password : la contraseña de la cuenta para la autenticación de credenciales de conexión con el servidor RDBMS
• port : algunos servidores RDBMS pueden aceptar conexiones de red sobre un número de puerto específico. El
parámetro del puerto le permite especificar el puerto al que su aplicación PHP se conecta, para que concuerde el
puerto configurado en el servidor RDBMS.
275
Zend_Db
• options : este parámetro es un array asociativo de opciones que son genéricas a todas las clases
Zend_Db_Adapter.
• driver_options : este parámetro es un array asociativo de opciones adicionales para una extensión de base de datos
dada. un uso típico de este parámetro es establecer atributos de un driver PDO.
• adapterNamespace : nombre de la parte inicial del nombre de las clase para el adaptador, en lugar de
'Zend_Db_Adapter'. Utilice esto si usted necesita usar el método factory() para cargar un adaptador de clase
de base de datos que no sea de Zend.
$options = array(
Zend_Db::CASE_FOLDING => Zend_Db::CASE_UPPER
);
$params = array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test',
'options' => $options
);
$options = array(
Zend_Db::AUTO_QUOTE_IDENTIFIERS => false
);
$params = array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test',
'options' => $options
);
276
Zend_Db
$pdoParams = array(
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
);
$params = array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test',
'driver_options' => $pdoParams
);
echo $db->getConnection()
->getAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY);
$options = array(
Zend_Db::ALLOW_SERIALIZATION => false
);
$params = array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test',
'options' => $options
);
If you need to force the Adapter to connect to the RDBMS, use the getConnection() method. This method returns
an object for the connection as represented by the respective PHP database extension. For example, if you use any of
the Adapter classes for PDO drivers, then getConnection() returns the PDO object, after initiating it as a live
connection to the specific database.
It can be useful to force the connection if you want to catch any exceptions it throws as a result of invalid account
credentials, or other failure to connect to the RDBMS server. These exceptions are not thrown until the connection is
made, so it can help simplify your application code if you handle the exceptions in one place, instead of at the time
of the first query against the database.
277
Zend_Db
Additionally, an adapter can get serialized to store it, for example, in a session variable. This
can be very useful not only for the adapter itself, but for other objects that aggregate it, like a
Zend_Db_Select object. By default, adapters are allowed to be serialized, if you don't want it, you
should consider passing the Zend_Db::ALLOW_SERIALIZATION=false option, see the example above.
To respect lazy connections principle, the adapter won't reconnect itself after being unserialized. You must
then call getConnection() yourself. You can make the adapter auto-reconnect by passing the
Zend_Db::AUTO_RECONNECT_ON_UNSERIALIZE=true as an adapter option.
try {
$db = Zend_Db::factory('Pdo_Mysql', $parameters);
$db->getConnection();
} catch (Zend_Db_Adapter_Exception $e) {
// perhaps a failed login credential, or perhaps the RDBMS is not running
} catch (Zend_Exception $e) {
// perhaps factory() failed to load the specified Adapter class
}
• accounts almacena información sobre cada usuario que hace el seguimiento de bugs.
• products almacena información sobre cada producto para el que pueden registrarse bugs.
• bugs almacena información sobre bugs, incluyendo el estado actual del bug, la persona que informó sobre el bug,
la persona que está asignada para corregir el bug, y la persona que está asignada para verificar la corrección.
• bugs_products stores a relationship between bugs and products. This implements a many-to-many relationship,
because a given bug may be relevant to multiple products, and of course a given product can have multiple bugs.
La siguiente definición de datos SQL en lenguaje pseudocódigo describe las tablas de esta base de datos de ejemplo.
Estas tablas de ejemplo son usadas ampliamente por los tests unitarios automatizados de Zend_Db.
278
Zend_Db
);
Also notice that the bugs table contains multiple foreign key references to the accounts table. Each of these foreign
keys may reference a different row in the accounts table for a given bug.
The diagram below illustrates the physical data model of the example database.
The first argument to this method is a string containing a SELECT statement. Alternatively, the first argument can
be an object of class Zend_Db_Select. The Adapter automatically converts this object to a string representation of
the SELECT statement.
The second argument to fetchAll() is an array of values to substitute for parameter placeholders in the SQL
statement.
279
Zend_Db
You can specify a different style of fetching results using the setFetchMode() method. The modes supported are
identified by constants:
• Zend_Db::FETCH_ASSOC: return data in an array of associative arrays. The array keys are column names, as
strings. This is the default fetch mode for Zend_Db_Adapter classes.
Note that if your select-list contains more than one column with the same name, for example if they are from two
different tables in a JOIN, there can be only one entry in the associative array for a given name. If you use the
FETCH_ASSOC mode, you should specify column aliases in your SELECT query to ensure that the names result
in unique array keys.
By default, these strings are returned as they are returned by the database driver. This is typically the spelling of the
column in the RDBMS server. You can specify the case for these strings, using the Zend_Db::CASE_FOLDING
option. Specify this when instantiating the Adapter. See Ejemplo 15.5, “ Passing the case-folding option to the
factory ”.
• Zend_Db::FETCH_NUM: return data in an array of arrays. The arrays are indexed by integers, corresponding to
the position of the respective field in the select-list of the query.
• Zend_Db::FETCH_BOTH: return data in an array of arrays. The array keys are both strings as used in the
FETCH_ASSOC mode, and integers as used in the FETCH_NUM mode. Note that the number of elements in the
array is double that which would be in the array if you used either FETCH_ASSOC or FETCH_NUM.
• Zend_Db::FETCH_COLUMN: return data in an array of values. The value in each array is the value returned by
one column of the result set. By default, this is the first column, indexed by 0.
• Zend_Db::FETCH_OBJ: return data in an array of objects. The default class is the PHP built-in class stdClass.
Columns of the result set are available as public properties of the object.
$db->setFetchMode(Zend_Db::FETCH_OBJ);
280
Zend_Db
$db->setFetchMode(Zend_Db::FETCH_OBJ);
$db->setFetchMode(Zend_Db::FETCH_OBJ);
$result = $db->fetchCol(
'SELECT bug_description, bug_id FROM bugs WHERE bug_id = ?', 2);
You should design the SELECT query so that the first column returned has unique values. If there are duplicates values
in the first column, entries in the associative array will be overwritten.
$db->setFetchMode(Zend_Db::FETCH_OBJ);
echo $result[2];
281
Zend_Db
$db->setFetchMode(Zend_Db::FETCH_OBJ);
Inserting Data
You can add new rows to a table in your database using the insert() method. The first argument is a string that
names the table, and the second argument is an associative array, mapping column names to data values.
$data = array(
'created_on' => '2007-03-22',
'bug_description' => 'Something wrong',
'bug_status' => 'NEW'
);
$db->insert('bugs', $data);
Columns you exclude from the array of data are not specified to the database. Therefore, they follow the same rules
that an SQL INSERT statement follows: if the column has a DEFAULT clause, the column takes that value in the row
created, otherwise the column is left in a NULL state.
By default, the values in your data array are inserted using parameters. This reduces risk of some types of security
issues. You don't need to apply escaping or quoting to values in the data array.
You might need values in the data array to be treated as SQL expressions, in which case they should not be quoted. By
default, all data values passed as strings are treated as string literals. To specify that the value is an SQL expression
and therefore should not be quoted, pass the value in the data array as an object of type Zend_Db_Expr instead
of a plain string.
282
Zend_Db
$data = array(
'created_on' => new Zend_Db_Expr('CURDATE()'),
'bug_description' => 'Something wrong',
'bug_status' => 'NEW'
);
$db->insert('bugs', $data);
If your table is defined with an auto-incrementing primary key, you can call the lastInsertId() method after the
insert. This method returns the last value generated in the scope of the current database connection.
$db->insert('bugs', $data);
Some RDBMS brands support a sequence object, which generates unique values to serve as primary key values. To
support sequences, the lastInsertId() method accepts two optional string arguments. These arguments name
the table and the column, assuming you have followed the convention that a sequence is named using the table and
column names for which the sequence generates values, and a suffix "_seq". This is based on the convention used
by PostgreSQL when naming sequences for SERIAL columns. For example, a table "bugs" with primary key column
"bug_id" would use a sequence named "bugs_bug_id_seq".
$db->insert('bugs', $data);
If the name of your sequence object does not follow this naming convention, use the lastSequenceId() method
instead. This method takes a single string argument, naming the sequence literally.
$db->insert('bugs', $data);
283
Zend_Db
For RDBMS brands that don't support sequences, including MySQL, Microsoft SQL Server, and SQLite, the arguments
to the lastInsertId() method are ignored, and the value returned is the most recent value generated for any table by
INSERT operations during the current connection. For these RDBMS brands, the lastSequenceId() method always
returns NULL.
Using a strong transaction isolation mode such as "repeatable read" can mitigate this risk, but some RDBMS
brands don't support the transaction isolation required for this, or else your application may use a lower
transaction isolation mode by design.
Furthermore, using an expression like "MAX(id)+1" to generate a new value for a primary key is not safe,
because two clients could do this query simultaneously, and then both use the same calculated value for their
next INSERT operation.
All RDBMS brands provide mechanisms to generate unique values, and to return the last value generated.
These mechanisms necessarily work outside of the scope of transaction isolation, so there is no chance of two
clients generating the same value, and there is no chance that the value generated by another client could be
reported to your client's connection as the last value generated.
Updating Data
You can update rows in a database table using the update() method of an Adapter. This method takes three
arguments: the first is the name of the table; the second is an associative array mapping columns to change to new
values to assign to these columns.
The values in the data array are treated as string literals. See the section called “Inserting Data” for information on
using SQL expressions in the data array.
The third argument is a string containing an SQL expression that is used as criteria for the rows to change. The values
and identifiers in this argument are not quoted or escaped. You are responsible for ensuring that any dynamic content
is interpolated into this string safely. See the section called “Quoting Values and Identifiers” for methods to help you
do this.
The return value is the number of rows affected by the update operation.
$data = array(
'updated_on' => '2007-03-23',
'bug_status' => 'FIXED'
);
284
Zend_Db
If you omit the third argument, then all rows in the database table are updated with the values specified in the data array.
If you provide an array of strings as the third argument, these strings are joined together as terms in an expression
separated by AND operators.
$data = array(
'updated_on' => '2007-03-23',
'bug_status' => 'FIXED'
);
Deleting Data
You can delete rows from a database table using the delete() method. This method takes two arguments: the first
is a string naming the table.
The second argument is a string containing an SQL expression that is used as criteria for the rows to delete. The
values and identifiers in this argument are not quoted or escaped. You are responsible for ensuring that any dynamic
content is interpolated into this string safely. See the section called “Quoting Values and Identifiers” for methods to
help you do this.
The return value is the number of rows affected by the delete operation.
If you omit the second argument, the result is that all rows in the database table are deleted.
If you provide an array of strings as the second argument, these strings are joined together as terms in an expression
separated by AND operators.
285
Zend_Db
$name = "O'Reilly";
$sql = "SELECT * FROM bugs WHERE reported_by = '$name'";
echo $sql;
// SELECT * FROM bugs WHERE reported_by = 'O'Reilly'
Even worse is the risk that such code mistakes might be exploited deliberately by a person who is trying to manipulate
the function of your web application. If they can specify the value of a PHP variable through the use of an HTTP
parameter or other mechanism, they might be able to make your SQL queries do things that you didn't intend them to
do, such as return data to which the person should not have privilege to read. This is a serious and widespread technique
for violating application security, known as "SQL Injection" (see http://en.wikipedia.org/wiki/SQL_Injection).
The Zend_Db Adapter class provides convenient functions to help you reduce vulnerabilities to SQL Injection attacks
in your PHP code. The solution is to escape special characters such as quotes in PHP values before they are interpolated
into your SQL strings. This protects against both accidental and deliberate manipulation of SQL strings by PHP
variables that contain special characters.
Using quote()
The quote() method accepts a single argument, a scalar string value. It returns the value with special characters
escaped in a manner appropriate for the RDBMS you are using, and surrounded by string value delimiters. The standard
SQL string value delimiter is the single-quote (').
$name = $db->quote("O'Reilly");
echo $name;
// 'O\'Reilly'
echo $sql;
// SELECT * FROM bugs WHERE reported_by = 'O\'Reilly'
Note that the return value of quote() includes the quote delimiters around the string. This is different from some
functions that escape special characters but do not add the quote delimiters, for example mysql_real_escape_string()
[http://www.php.net/mysqli_real_escape_string].
Values may need to be quoted or not quoted according to the SQL datatype context in which they are used. For instance,
in some RDBMS brands, an integer value must not be quoted as a string if it is compared to an integer-type column
or expression. In other words, the following is an error in some SQL implementations, assuming intColumn has
a SQL datatype of INTEGER
You can use the optional second argument to the quote() method to apply quoting selectively for the SQL datatype
you specify.
$value = '1234';
286
Zend_Db
Each Zend_Db_Adapter class has encoded the names of numeric SQL datatypes for the respective
brand of RDBMS. You can also use the constants Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, and
Zend_Db::FLOAT_TYPE to write code in a more RDBMS-independent way.
Zend_Db_Table specifies SQL types to quote() automatically when generating SQL queries that reference a
table's key columns.
Using quoteInto()
The most typical usage of quoting is to interpolate a PHP variable into a SQL expression or statement. You can use
the quoteInto() method to do this in one step. This method takes two arguments: the first argument is a string
containing a placeholder symbol (?), and the second argument is a value or PHP variable that should be substituted
for that placeholder.
The placeholder symbol is the same symbol used by many RDBMS brands for positional parameters, but the
quoteInto() method only emulates query parameters. The method simply interpolates the value into the string,
escapes special characters, and applies quotes around it. True query parameters maintain the separation between the
SQL string and the parameters as the statement is parsed in the RDBMS server.
echo $sql;
// SELECT * FROM bugs WHERE reported_by = 'O\'Reilly'
You can use the optional third parameter of quoteInto() to specify the SQL datatype. Numeric datatypes are not
quoted, and other types are quoted.
$sql = $db
->quoteInto("SELECT * FROM bugs WHERE bug_id = ?", '1234', 'INTEGER');
echo $sql;
// SELECT * FROM bugs WHERE reported_by = 1234
Using quoteIdentifier()
Values are not the only part of SQL syntax that might need to be variable. If you use PHP variables to name tables,
columns, or other identifiers in your SQL statements, you might need to quote these strings too. By default, SQL
identifiers have syntax rules like PHP and most other programming languages. For example, identifiers should not
contain spaces, certain punctuation or special characters, or international characters. Also certain words are reserved
for SQL syntax, and should not be used as identifiers.
However, SQL has a feature called delimited identifiers, which allows broader choices for the spelling of identifiers. If
you enclose a SQL identifier in the proper types of quotes, you can use identifiers with spellings that would be invalid
without the quotes. Delimited identifiers can contain spaces, punctuation, or international characters. You can also use
SQL reserved words if you enclose them in identifier delimiters.
287
Zend_Db
The quoteIdentifier() method works like quote(), but it applies the identifier delimiter characters to
the string according to the type of Adapter you use. For example, standard SQL uses double-quotes (") for
identifier delimiters, and most RDBMS brands use that symbol. MySQL uses back-quotes (`) by default. The
quoteIdentifier() method also escapes special characters within the string argument.
echo $sql
// SELECT * FROM "order"
SQL delimited identifiers are case-sensitive, unlike unquoted identifiers. Therefore, if you use delimited identifiers,
you must use the spelling of the identifier exactly as it is stored in your schema, including the case of the letters.
In most cases where SQL is generated within Zend_Db classes, the default is that all identifiers are delimited
automatically. You can change this behavior with the option Zend_Db::AUTO_QUOTE_IDENTIFIERS. Specify
this when instantiating the Adapter. See Ejemplo 15.6, “ Passing the auto-quoting option to the factory ”.
Alternatively, you can specify the beginning and resolution of a transaction, and thus control how many SQL queries are
included in a single group that is committed (or rolled back) as a single operation. Use the beginTransaction()
method to initiate a transaction. Subsequent SQL statements are executed in the context of the same transaction until
you resolve it explicitly.
To resolve the transaction, use either the commit() or rollBack() methods. The commit() method marks
changes made during your transaction as committed, which means the effects of these changes are shown in queries
run in other transactions.
The rollBack() method does the opposite: it discards the changes made during your transaction. The changes are
effectively undone, and the state of the data returns to how it was before you began your transaction. However, rolling
back your transaction has no effect on changes made by other transactions running concurrently.
After you resolve this transaction, Zend_Db_Adapter returns to auto-commit mode until you call
beginTransaction() again.
try {
// Attempt to execute one or more queries:
288
Zend_Db
$db->query(...);
$db->query(...);
$db->query(...);
The describeTable() method returns an associative array of metadata about a table. Specify the name of the
table as a string in the first argument to this method. The second argument is optional, and names the schema in which
the table exists.
The keys of the associative array returned are the column names of the table. The value corresponding to each column
is also an associative array, with the following keys and values:
289
Zend_Db
• AUTO_INCREMENT - MySQL
• SERIAL - PostgreSQL
• SEQUENCE - Oracle
If no table exists matching the table name and optional schema name specified, then describeTable() returns
an empty array.
Closing a Connection
Normally it is not necessary to close a database connection. PHP automatically cleans up all resources and the end of a
request. Database extensions are designed to close the connection as the reference to the resource object is cleaned up.
However, if you have a long-duration PHP script that initiates many database connections, you might need
to close the connection, to avoid exhausting the capacity of your RDBMS server. You can use the Adapter's
closeConnection() method to explicitly close the underlying database connection.
Since release 1.7.2, you could check you are currently connected to the RDBMS server with the method
isConnected(). This means that a connection resource has been initiated and wasn't closed. This function is not
currently able to test for example a server side closing of the connection. This is internally use to close the connection.
It allow you to close the connection multiple times without errors. It was already the case before 1.7.2 for PDO adapters
but not for the others.
$db->closeConnection();
Using persistent connections can cause an excess of idle connections on the RDBMS server, which
causes more problems than any performance gain you might achieve by reducing the overhead of making
connections.
290
Zend_Db
Database connections have state. That is, some objects in the RDBMS server exist in session scope. Ejemplos
are locks, user variables, temporary tables, and information about the most recently executed query, such as
rows affected, and last generated id value. If you use persistent connections, your application could access
invalid or privileged data that were created in a previous PHP request.
For example, all SQL statements run by Zend_Db are prepared, then executed. However, some database features are
incompatible with prepared statements. DDL statements like CREATE and ALTER cannot be prepared in MySQL.
Also, SQL statements don't benefit from the MySQL Query Cache [http://dev.mysql.com/doc/refman/5.1/en/query-
cache-how.html], prior to MySQL 5.1.17.
Most PHP database extensions provide a method to execute SQL statements without preparing them. For example,
in PDO, this method is exec(). You can access the connection object in the PHP extension directly using
getConnection().
Similarly, you can access other methods or properties that are specific to PHP database extensions. Be aware, though,
that by doing this you might constrain your application to the interface provided by the extension for a specific brand
of RDBMS.
In future versions of Zend_Db, there will be opportunities to add method entry points for functionality that is common
to the supported PHP database extensions. This will not affect backward compatibility.
$version = $db->getServerVersion();
if (!is_null($version)) {
if (version_compare($version, '5.0.0', '>=')) {
// do something
} else {
// do something else
}
} else {
// impossible to read server version
}
291
Zend_Db
IBM DB2
• Specify this Adapter to the factory() method with the name 'Db2'.
• IBM DB2 supports both sequences and auto-incrementing keys. Therefore the arguments to lastInsertId()
are optional. If you give no arguments, the Adapter returns the last value generated for an auto-increment key. If you
give arguments, the Adapter returns the last value generated by the sequence named according to the convention
'table_column_seq'.
MySQLi
• Specify this Adapter to the factory() method with the name 'Mysqli'.
• MySQL does not support sequences, so lastInsertId() ignores its arguments and always returns the last value
generated for an auto-increment key. The lastSequenceId() method returns NULL.
Oracle
• Specify this Adapter to the factory() method with the name 'Oracle'.
• Oracle does not support auto-incrementing keys, so you should specify the name of a sequence to
lastInsertId() or lastSequenceId().
• The Oracle extension does not support positional parameters. You must use named parameters.
• Currently the Zend_Db::CASE_FOLDING option is not supported by the Oracle adapter. To use this option with
Oracle, you must use the PDO OCI adapter.
• By default, LOB fields are returned as OCI-Lob objects. You could retrieve them as string for all requests by
using driver options 'lob_as_string' or for particular request by using setLobAsString(boolean) on
adapter or on statement.
• You must use at least PDO_IBM extension version 1.2.2. If you have an earlier version of this extension, you must
upgrade the PDO_IBM extension from PECL.
• Microsoft SQL Server does not support sequences, so lastInsertId() ignores its arguments and always returns
the last value generated for an auto-increment key. The lastSequenceId() method returns NULL.
292
Zend_Db
• If you are working with unicode strings in an encoding other than UCS-2 (such as UTF-8), you may have to perform
a conversion in your application code or store the data in a binary column. Please refer to Microsoft's Knowledge
Base [http://support.microsoft.com/kb/232580] for more information.
• You can specify pdoType as a key in the options array. The value can be "mssql" (the default), "dblib",
"freetds", or "sybase". This option affects the DSN prefix the adapter uses when constructing the DSN string. Both
"freetds" and "sybase" imply a prefix of "sybase:", which is used for the FreeTDS [http://www.freetds.org/] set
of libraries. See also http://www.php.net/manual/en/ref.pdo-dblib.connection.php [http://www.php.net/manual/en/
ref.pdo-dblib.connection.php] for more information on the DSN prefixes used in this driver.
PDO MySQL
• Specify this Adapter to the factory() method with the name 'Pdo_Mysql'.
• MySQL does not support sequences, so lastInsertId() ignores its arguments and always returns the last value
generated for an auto-increment key. The lastSequenceId() method returns NULL.
PDO Oracle
• Specify this Adapter to the factory() method with the name 'Pdo_Oci'.
• Oracle does not support auto-incrementing keys, so you should specify the name of a sequence to
lastInsertId() or lastSequenceId().
PDO PostgreSQL
• Specify this Adapter to the factory() method with the name 'Pdo_Pgsql'.
• PostgreSQL supports both sequences and auto-incrementing keys. Therefore the arguments to lastInsertId()
are optional. If you give no arguments, the Adapter returns the last value generated for an auto-increment key. If you
give arguments, the Adapter returns the last value generated by the sequence named according to the convention
'table_column_seq'.
PDO SQLite
• Specify this Adapter to the factory() method with the name 'Pdo_Sqlite'.
• SQLite does not support sequences, so lastInsertId() ignores its arguments and always returns the last value
generated for an auto-increment key. The lastSequenceId() method returns NULL.
• To connect to an SQLite2 database, specify 'sqlite2'=>true in the array of parameters when creating an
instance of the Pdo_Sqlite Adapter.
293
Zend_Db
• To connect to an in-memory SQLite database, specify 'dbname'=>':memory:' in the array of parameters when
creating an instance of the Pdo_Sqlite Adapter.
• Older versions of the SQLite driver for PHP do not seem to support the PRAGMA commands necessary to ensure
that short column names are used in result sets. If you have problems that your result sets are returned with keys of
the form "tablename.columnname" when you do a join query, then you should upgrade to the current version of PHP.
Firebird/Interbase
• This Adapter uses the PHP extension php_interbase.
• Firebird/interbase does not support auto-incrementing keys, so you should specify the name of a sequence to
lastInsertId() or lastSequenceId().
• Currently the Zend_Db::CASE_FOLDING option is not supported by the Firebird/interbase adapter. Unquoted
identifiers are automatically returned in upper case.
We recommend to update the gds32.dll (or linux equivalent) bundled with php, to the same version of the server.
For Firebird the equivalent gds32.dll is fbclient.dll.
By default all identifiers (tables names, fields) are returned in upper case.
Zend_Db_Statement
Además de algunos métodos convenientes tales como fetchAll() e insert() documentados en the section
called “Zend_Db_Adapter”, puede usarse un objeto de declaración para obtener más opciones al ejecutar consultas
y devolver conjuntos de resultados. Esta sección describe cómo obtener una instancia de un objeto de declaración y
cómo usar sus métodos.
Zend_Db_Statement está basado en el objeto PDOStatement en la extensión PHP Data Objects [http://
www.php.net/pdo].
$stmt = $db->query(
'SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?',
array('goofy', 'FIXED')
);
El objeto de declaración corresponde a una declaración SQL que ha sido preparada y ejecutada una vez con valores
vinculados especificados. Si la declaración fue una consulta SELECT u otro tipo de declaración que devuelve un
conjunto de resultados, ahora estará lista para extraer resultados.
294
Zend_Db
Puede crear una declaración con su constructor, pero éste es un uso menos típico. No hay un método factory para
crear el objeto, así que es necesario cargar una clase de declaración específica y llamar a su constructor. Pase el objeto
Adaptador como el primer parámetro, y un string conteniendo la declaración SQL como el segundo parámetro. La
declaración es preparada pero no ejecutada.
Ejecutando la declaración
Necesita ejecutar un objeto de declaración si lo crea con el constructor, o si desea ejecutar la misma declaración varias
veces. Use el método execute() del mismo objeto de declaración. El único parámetro es un array de valores a
vincular a posiciones de parámetros en la declaración.
Si usa parámetros posicionales, o los que están marcados por un signo de interrogación (?), pase los valores de
vinculación en un array plano.
$stmt->execute(array('goofy', 'FIXED'));
Si usa parámetros nombrados, o los que son indicados por un string identificador precedido por un caracter de dos
puntos (:), pase el valor en un array asociativo. Las claves de este array deben coincidir con el nombre de los
parámetros.
Las declaraciones PDO soportan tanto parámetros posicionales como parámetros nombrados, pero no ambos tipos en
la misma declaración SQL. Algunas clases Zend_Db_Statement para extensiones no-PDO soportan solo un tipo
de parámetro o el otro.
295
Zend_Db
resultados. Puede ejecutar las últimas declaraciones de SQL usando Zend_Db_Statement, pero no puede llamar
a los métodos que extraen filas de resultados desde éste.
• Estilo de Extracción es el primer parámetro. Éste controla la estructura en la que será devuelta la fila. Vea the section
called “Changing the Fetch Mode” para la descripción de un valor válido los correspondientes formatos de datos.
fetch() devuelve false si todas las filas del conjunto de resultados han sido extraídas.
$rows = $stmt->fetchAll();
echo $rows[0]['bug_description'];
296
Zend_Db
para establecer el modo de extracción. Especifique el modo de extracción usando las constantes de la clase Zend_Db:
FETCH_ASSOC, FETCH_NUM, FETCH_BOTH, FETCH_COLUMN, and FETCH_OBJ. Vea the section called
“Changing the Fetch Mode” para más información de estos modos. Llamadas subsiguientes a los métodos de la
declaración fetch() o fetchAll() usan el modo de extracción especificado.
$stmt->setFetchMode(Zend_Db::FETCH_NUM);
$rows = $stmt->fetchAll();
echo $rows[0][0];
Note que este método opera diferente que el método fetchCol() de la clase Adaptadora. El método
fetchColumn() de una declaración devuelve un único valor desde una fila. El método fetchCol() de un
adaptador devuelve un array de valores, tomados desde la primera columa de todas las del conjunto de resultados.
$bug_status = $stmt->fetchColumn(2);
$obj = $stmt->fetchObject();
echo $obj->bug_description;
297
Zend_Db
Zend_Db_Profiler
Introducción
Zend_Db_Profiler puede ser habilitado para Perfilar las consultas. Los Perfiles incluyen la consulta procesada
por el adaptador como el tiempo as transcurrido en la ejecución de las consultas, permitiendo inspeccionar las consultas
realizadas win necesidad de agregar información de depuración extra en el código de las clases. El uso avanzado
también permite que el desarrollador filtre las consultas que desea perfilar.
Habilite el perfilador pasando una directiva al al constructor del adaptador, o pidiendole al adaptador permitirlo más
adelante.
$params = array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test'
'profiler' => true // enciende el perfilador
// establezca false para deshabilitar (está deshabilitado por defec
);
// apagar el perfilador:
$db->getProfiler()->setEnabled(false);
// encender el perfilador:
$db->getProfiler()->setEnabled(true);
El valor de la opción 'profiler' es flexible. Es interpretada de distintas formas dependiendo del tipo. Normalmente,
debería usar un valor booleano simple, pero otros tipos le permiten personalizar el comportamiento del perfilador.
Un argumento booleano establece el perfilador como habilitado si el valor es true, o deshabilitado si es false. La
clase del perfilador es el la clase de perfilador por defecto del adaptador, Zend_Db_Profiler.
$params['profiler'] = true;
$db = Zend_Db::factory('PDO_MYSQL', $params);
Una instancia del objeto perfilador hace que el adaptador use ese objeto. El tipo del objeto debe ser
Zend_Db_Profiler o una subclase de este. Habilitar el perfilador se hace por separado.
$profiler = MyProject_Db_Profiler();
$profiler->setEnabled(true);
$params['profiler'] = $profiler;
$db = Zend_Db::factory('PDO_MYSQL', $params);
El argumento puede ser un array asociativo conteniendo algunas o todas las claves 'enabled', 'instance', y
'class'. Las claves 'enabled' e 'instance' corresponden a los tipos booleano y la instancia documentada
previamente. La clave 'class' es usada para nombrar la clase que usará el perfilador personalizado. La clase debe
ser Zend_Db_Profiler o una subclase. La clase es instanciada sin argumentos de constructor. La opción 'class'
es ignorada cuando la opción 'instance' está dada.
298
Zend_Db
$params['profiler'] = array(
'enabled' => true,
'class' => 'MyProject_Db_Profiler'
);
$db = Zend_Db::factory('PDO_MYSQL', $params);
Finalmente, el argumento puede ser un objeto de tipo Zend_Config conteniendo las propiedades, que son tratadas
como las claves de array descritas recién. Por ejemplo, un archivo "config.ini" puede contener los siguientes datos:
[main]
db.profiler.class = "MyProject_Db_Profiler"
db.profiler.enabled = true
Usando el Perfilador
En este punto, obtenemos el perfilador usando el método getProfiler() del adaptador:
$profiler = $db->getProfiler();
Este retorna una instancia del objeto Zend_Db_Profiler. Con esta instancia, el desarrollador puede examinar las
consultar usando una variedad de métodos:
• getTotalElapsedSecs() retorna el número total de segundos transcurridos en todas las consultas perfiladas.
299
Zend_Db
• getQuery() retorna el texto SQL de la consulta. El texto SQL de una declaración preparada con parámetros es
el texto al tiempo en que la consulta fué preparada, por lo que contiene marcadores de posición, no los valores
utilizados cuando la declaración se ejecuta.
• getQueryParams() retorna un array de los valores de los parámetros usados cuando se ejecuta una consulta
preparada. Este incluye ambos parámetros y argumentos vinculados al método execute() de la declaración. Las
claves del array son las posiciones (basado en 1) o indices de parámetros nombrados (string).
La información que Zend_Db_Profiler provee es útil para perfilar cuellos de botella en aplicaciones, y para
depurar consultas que han sido ejecutadas. Por instancia, para ver la consulta exacta que tuvo la última ejecución:
$query = $profiler->getLastQueryProfile();
echo $query->getQuery();
Tal vez una página se genera lentamente; use el perfilador para determinar primero el número total de segundos de
todas las consultas, y luego recorrer paso a paso a través de las consultas para encontrar la más lenta:
$totalTime = $profiler->getTotalElapsedSecs();
$queryCount = $profiler->getTotalNumQueries();
$longestTime = 0;
$longestQuery = null;
300
Zend_Db
• Zend_Db_Profiler::QUERY: consultas generales a la base de datos que no calzan con otros tipos.
• Zend_Db_Profiler::INSERT: cualquier consulta que agrega filas a la base de datos, generalmente un SQL
INSERT.
• Zend_Db_Profiler::DELETE: cualquier consulta que elimine datos existentes, usualmente un SQL DELETE.
• Zend_Db_Profiler::SELECT: cualquier consulta que retorne datos existentes, usualmente un SQL SELECT.
Asi como con setFilterElapsedSecs(), puedes remover cualquier filtro existente pasando un NULL como
único argumento.
301
Zend_Db
$profiles = $profiler->getQueryProfiles(Zend_Db_Profiler::SELECT);
Perfiladores Especializados
Un Perfilador Especializado es un objeto que hereda de Zend_Db_Profiler. Los Perfiladores Especializados tratan
la información de perfilado de maneras más especificas.
Todos los datos son enviados a través del componente Zend_Wildfire_Channel_HttpHeaders que usa
cabeceras HTTP para asegurar que el contenido de la página no sea alterado. Depurar peticiones AJAX que requieren
respuestas JSON y XML es perfectamente posible con este enfoque.
Requerimientos:
• Navegador web Firefox idealmente versión 3, pero la versión 2 tambien está soportada.
• Extensión Firebug para Firefox, la cual puede descargarse desde https://addons. mozilla .org/en-US/firefox/
addon/1843 [https://addons.mozilla.org/en-US/firefox/addon/1843].
// En tu archivo bootstrap
// Todas las consultas a la base de datos en tus archivos modelo, vista y controlador
// ahora serán perfilados y enviados a Firebug
302
Zend_Db
$profiler->setEnabled(true);
// Ahora se pueden ejecutar las consultas a la Base de Datos para ser perfiladas
Zend_Db_Select
Descripción del Objeto Select
El objeto Zend_Db_Select object representa una declaración de consulta SELECT de SQL. La clase tiene métodos
para agregar partes individuales a la consulta. Se pueden especificar algunas partes de la consulta usando los métodos
en PHP y sus estructuras de datos, y la clase forma la sintaxis SLQ correcta. Después de construir la consulta, puede
ejecutarla como si se hubiera escrito como un string.
• Entrecomillado de identificadores y valores, para ayudar a reducir el riesgo de ataque por inyección SQL.
El uso de Zend_Db_Select no es obligatorio. Para consultas SELECT muy simples, es usualmente más simple
especificar la consulta completa como un string y ejecutarla usando un método del Adapter como query() o
fetchAll(). Usar Zend_Db_Select es útil si se necesita ensamblar una consulta SELECT proceduralmente, o
basada en condiciones lógicas en la aplicación.
303
Zend_Db
Otra manera de crear el objeto Zend_Db_Select es con su constructor, especificando el adaptador de base de datos
como un argumento.
También puede utilizar la mayoría de los métodos del objeto Zend_Db_Select con una interfaz fluida. Una
interfaz fluida significa que cada método devuelve una referencia al objeto que se ha llamado, así se puede llamar
inmediatamente a otro método.
$select = $db->select()
->from( ...specify table and columns... )
->where( ...specify search criteria... )
->order( ...specify sorting criteria... );
Los ejemplos en esta sección muestran el uso de la interfaz fluída, pero también se puede usar la interfaz no-fluída
en todos los casos. A menudo es necesario utilizar la interfaz no-fluída, por ejemplo, si su aplicación necesita realizar
cierta lógica antes de añadir una cláusula a la consulta.
304
Zend_Db
// Construye la consulta:
// SELECT *
// FROM "products"
$select = $db->select()
->from( 'products' );
Puede especificar un nombre de correlación (también llamado a veces "alias de tabla") para una tabla. En lugar de un
string, se usa un array asociativo que mapee el nombre de correlación con el nombre de la tabla. En otras cláusulas de
consulta SQL, utilice nombre de correlación. Si su consulta se une con más de una tabla, Zend_Db_Select genera
una correlación unica de nombres basados en el nombre de la tabla, para una tabla a la cual no se le espicifique un
nombre de correlación.
$select = $db->select()
->from( array('p' => 'products') );
Algunos RDBMS apoyan el uso de un especificador de esquema para una tabla. Puede especificar el nombre
de la tabla como "nombreDeEsquema.nombre DeTabla", donde Zend_Db_Select entrecomillará cada
parte individualmente, o tambien puedes especificar el nombre de esquema por separado. Un nombre de esquema
especificado en el nombre de la tabla toma precedencia en sobre un esquema dado por separado en el caso de que
ambos sean dados.
$select = $db->select()
->from( 'myschema.products' );
// o
$select = $db->select()
->from('products', '*', 'myschema');
Agregando Columnas
En el segundo argumento del método from(), puede especificar las columnas que seleccionar desde la tabla
respectiva. Si no especifica columnas, por defecto será "*", el comodín SQL para "todas las columnas".
Puede listar las columnas en un simple array de strings, o en un array asociativo mapeando los alias de columnas a
su nombre de tabla. Si solo se especifica una columna en la consulta y no necesita especificar un alias de columna,
puede listarla solo con un string en lugar de un array.
305
Zend_Db
Si se entrega un array vacío como el argumento de las tablas, no se incluirán columnas en el resultado. Vea un código
de ejemplo en la sección del método join().
$select = $db->select()
->from(array('p' => 'products'),
array('product_id', 'product_name'));
$select = $db->select()
->from(array('p' => 'products'),
array('p.product_id', 'p.product_name'));
$select = $db->select()
->from(array('p' => 'products'),
array('prodno' => 'product_id', 'product_name'));
Tambien puede crear un objeto de tipo Zend_Db_Expr explícitamente, para prevenir que el string sea tratado como
columna. Zend_Db_Expr es una clase mínima, que contiene un simple string. Zend_Db_Select reconoce el
objeto de tipo Zend_Db_Expr y lo convierte de vuelta en el string, pero no le aplica ninguna alteración, tal como
el entrecomillado o la correlación de nombres.
Nota
El Uso de Zend_Db_Expr para nombres de columnas no es necesario si la expresión de la columna contiene
paréntesis; Zend_Db_Select reconoce y trata el string como expresión, saltándose el entrecomillado y la
correlación de nombres.
306
Zend_Db
$select = $db->select()
->from(array('p' => 'products'),
array('product_id', 'LOWER(product_name)'));
$select = $db->select()
->from(array('p' => 'products'),
array('product_id',
'cost_plus_tax' => '(p.cost * 1.08)')
);
$select = $db->select()
->from(array('p' => 'products'),
array('product_id',
'cost_plus_tax' =>
new Zend_Db_Expr('p.cost * 1.08'))
);
En los casos anteriores, Zend_Db_Select no altera el string para aplicar correlación de nombres o entrecomillado de
identificadores. Si estos cambios son necesarios para resolver ambigüedades, deberías realizar cambios manualmente
en el string.
Si el nombre de su columna es alguna palabra reservada de SQL o contiene caracteres especiales, debería usar el método
quoteIdentifier() del Adapdator e interpolar el resultado en un string. El método quoteIdentifier()
usa entrecomillado SQL para delimitar el identificador, the identifier, dejando en claro que es un identificador de tabla
o columna y no otra parte de la sintaxis SQL.
Su código es más independiente de la base de datos si se usa el método quoteIdentifier() en vez de las
excribir literalmente las comillas en la cadena, debido a que algunos RDBMS no usan simbolos estándar para
entrecomillar identificadores. El método quoteIdentifier() está diseñado para usar los símbolos apropiados
para entrecomillar basado en el tipo del adaptador. El método quoteIdentifier() también escapa cual caracter
de comilla que aparezca en el nombre del identificador mismo.
307
Zend_Db
$select = $db->select()
->from(array('p' => 'products'),
array('origin' =>
'(p.' . $db->quoteIdentifier('from') . ' + 10)')
);
// Construir la consulta:
// SELECT p."product_id", p."product_name"
// FROM "products" AS p
$select = $db->select()
->from(array('p' => 'products'), 'product_id')
->columns('product_name');
$select = $db->select()
->from(array('p' => 'products'), 'p.product_id')
->columns('product_name', 'p');
// Alternativamente puede usar columns('p.product_name')
$select = $db->select()
->from(array('p' => 'products'),
array('product_id', 'product_name'))
->join(array('l' => 'line_items'),
'p.product_id = l.product_id');
308
Zend_Db
El segundo argumento join() es un string que es usado como condición de unión. Esta es una expresión que declara
un criterio por el cual las filas en una tabla concuerdan con las filas de la otra tabla. Puedes especificar correlación
de nombres en esta expresión.
Nota
No se aplica entrecomillado en la expresión especificada para la condición de unión; si tienes problemas con
nombres que necesitan ser entrecomillados, deberás usar quoteIdentifier() para formar el string de
condición de unión.
El tercer argumento join() es un array de nombres de columnas, como al usar el método from(). Este es por
defecto "*", soporta correlación de nombres, expresiones, y Zend_Db_Expr de la misma manera que el array de
nombres de columnas en el método from().
Para no seleccionar columnas de una tabla, use un array vacío para la lista de columnas. El uso de esto trabaja con el
método from() también, pero en general deseará algunas columnas de la tabla primaria en sus consultas, a la vez
que no se desean columnas de la tabla unida.
$select = $db->select()
->from(array('p' => 'products'),
array('product_id', 'product_name'))
->join(array('l' => 'line_items'),
'p.product_id = l.product_id',
array() ); // empty list of columns
Note el array vacío array() en el ejemplo anterior en lugar de una lista de columnas de la tabla unida.
SQL tiene muchos tipos de uniones. Vea una lista a continuación para los métodos que soporta cada tipo de unión
en Zend_Db_Select.
• INNER JOIN con los métodos join(table, join, [columns]) o joinInner(table, join,
[columns]).
Éste es el tipo de unión más comun. Las filas de cada tabla son comparadas usando la condición de unión
especificada. El resultado incluye solo las filas que satisfacen la condición. El resultado puede ser vacío si no hay
filas que satisfagan la condición.
Todas las filas de tabla a la izquierda del operando son incluidas, pareando las filas de la tabla a la derecha del
operando, y las columnas de la tabla a la derecha del operando son rellenadas con NULLs si no existen filas que
coincidan con la tabla a la izquierda.
309
Zend_Db
Unión exterior por la derecha es el complementario de la unión exterior por la izquierda. Todas las filas de la tabla
a la derecha del operando son incluidas, pareando las filas de la tabla a la izquierda del operando incluidas, y las
columnas de la tabla a la izquierda del operando son rellenadas con NULLs si no existen filas que coincidan con
la tabla de la derecha.
Algunos RDBMS no soportan este tipo de join, pero en general, cualquier unión por la derecha puede representarse
por una unión por la izquierda invirtiendo el orden de las tablas.
Una unión externa total es como una combinación de una unión exterior por la izquierda y una unión exterior por la
derecha. Todas las filas de ambas tablas son incluidas, vinculadas entre sí en la misma fila si satisfacen la condición
de unión, y en otro caso, se vinculan con valores nulos en lugar de columnas de la otra tabla.
Una unión cruzada es un Producto Cartesiano. Cada fila en la primera tabla es pareada con cada una en la segunda
tabla. Por lo tanto, el número de filas en el resultado es igual al producto del número de filas en cada tabla. Puede
filtrar el conjunto de resultados con el uso de condiciones en un cláusula WHERE; de esta forma una unión cruzada
es similar a la antigua sintaxis de unión en SQL-89.
El método joinCross() no tiene parámetros para especificar una condición de unión. Algunos RDBMS no
soportan este tipo de unión.
Una unión natural compara cualquier columa(s) que aparezca con el nombre en ambas tablas. La comparación es el
equivalente de todas las columna(s); comparando las columnas usando desigualdad no es una unión natural. Solo la
unión interna natural es soportada por este API, aun cuando SQL permita una unión externa natural.
Además de los métodos de unión, puede simplificar las consultas usando métodos JoinUsing. En vez de proveer
una condición completa a la unión, simplemente pase el nombre de columna en la que se hará la unión y el objeto
Zend_Db_Select completa la condición.
$select = $db->select()
->from('table1')
->joinUsing('table2', 'column1')
->where('column2 = ?', 'foo');
Cada uno de los métodos aplicables para uniones en el componente Zend_Db_Select tiene su correspondiente
método 'using' (usando)
310
Zend_Db
$select = $db->select()
->from('products',
array('product_id', 'product_name', 'price'))
->where('price > 100.00');
Nota
No se aplica entrecomillado en una expresión dada en el método where() u orWhere(). Si tiene nombres
de columnas que necesitan ser entrecomillados, debe usar el método quoteIdentifier() para formar
el string de la condición.
El segundo argumento del método where() es opcional. Es un valor para sustituir en la expresión.
Zend_Db_Select entrecomilla el valor y lo sustituye por un signo de interrogación ("?") en la expresión.
Este método acepta solo un parámetro. Si tiene una expresión en la cual necesita sustituir múltiples variables, deberá
formar el string manualmente, interpolando variables y realizando entrecomillado manualmente.
$minimumPrice = 100;
$select = $db->select()
->from('products',
array('product_id', 'product_name', 'price'))
->where('price > ?', $minimumPrice);
Puede invocar el método where() múltiples veces en el mismo objeto Zend_Db_Select. La consulta resultante
combina los términos multiples usando AND entre ellos.
311
Zend_Db
$minimumPrice = 100;
$maximumPrice = 500;
$select = $db->select()
->from('products',
array('product_id', 'product_name', 'price'))
->where('price > ?', $minimumPrice)
->where('price < ?', $maximumPrice);
Si necesita combinar terminos usandoOR, use el método orWhere(). Este método se usa del mismo modo que el
método where(), excepto que el término especificado es precedido por OR, en lugar de AND.
$minimumPrice = 100;
$maximumPrice = 500;
$select = $db->select()
->from('products',
array('product_id', 'product_name', 'price'))
->where('price < ?', $minimumPrice)
->orWhere('price > ?', $maximumPrice);
Zend_Db_Select automáticamente pone paréntesis alrededor de cada expresión que especifique usando el método
where() u orWhere(). Esto ayuda a asegurar que la precedencia del operador Booleano no cause resultados
inesperados.
$minimumPrice = 100;
$maximumPrice = 500;
312
Zend_Db
$prod = 'Apple';
$select = $db->select()
->from('products',
array('product_id', 'product_name', 'price'))
->where("price < $minimumPrice OR price > $maximumPrice")
->where('product_name = ?', $prod);
En el ejemplo anterior, los resultados deberían ser diferentes sin paréntesis, porque AND tiene precedencia más alta
respecto a OR. Zend_Db_Select aplica el parentesis con un efecto tal que la expresión en sucesivas llamadas al
método where() vincula de forma más fuerte el AND que combina las expresiones.
En Zend_Db_Select, puede especificar la(s) columna(s) que usar para el cálculo de grupos de filas usando el
método group(). El argumento de este método es una columna o un array de columnas que se usarán en la cláusula
GROUP BY.
$select = $db->select()
->from(array('p' => 'products'),
array('product_id'))
->join(array('l' => 'line_items'),
'p.product_id = l.product_id',
array('line_items_per_product' => 'COUNT(*)'))
->group('p.product_id');
Como el array de columnas del método from(), se puede usar correlación de nombres en el string de nombre de
columna, y la columna será entrecomillada como un identificador, salvo que el string contenga paréntesis o sea un
objeto de tipo Zend_Db_Expr.
En Zend_Db_Select, puede especificar condiciones para restringir grupos usando el método having(). Su uso
es similar al del método where(). El primer agumento es un string conteniendo una expresión SQL. El segundo
argumento es un valor que es usado para reemplazar un parámetro marcador de posición en la expresión SQL. Las
expresiones dadas en multiples invocaciones al método having() son combinadas usando el operador Booleano
AND, o el operador OR si usa el método orHaving().
313
Zend_Db
$select = $db->select()
->from(array('p' => 'products'),
array('product_id'))
->join(array('l' => 'line_items'),
'p.product_id = l.product_id',
array('line_items_per_product' => 'COUNT(*)'))
->group('p.product_id')
->having('line_items_per_product > 10');
Nota
No se aplica entrecomillado a expresiones dadas al método having() u orHaving(). Si tiene nombres
de columnas que deban ser entrecomillados, deberá usar quoteIdentifier() para formar el string de
la condición.
En Zend_Db_Select, puede usar el método order() para especificar una columna o un array de columnas por
el cual ordenar. Cada elemento del array es un string nombrando la columna. Opcionalmente con la palabra reservada
ASC o DESC siguiendola, separada por un espacio.
Como en el método from() y group(), los nombres de columnas son entrecomillados como identificadores, a
menos que contengan paréntesis o sean un obheto de tipo Zend_Db_Expr.
$select = $db->select()
->from(array('p' => 'products'),
array('product_id'))
->join(array('l' => 'line_items'),
314
Zend_Db
'p.product_id = l.product_id',
array('line_items_per_product' => 'COUNT(*)'))
->group('p.product_id')
->order(array('line_items_per_product DESC',
'product_id'));
En Zend_Db_Select, puede usar el método limit() para especificar la cantidad de filas y el número de filas
que saltar. El primer argumento es el método es el número de filas deseado. El segundo argument es el número de
filas que saltar.
$select = $db->select()
->from(array('p' => 'products'),
array('product_id', 'product_name'))
->limit(10, 20);
Nota
La sintaxis de LIMIT no está soportada por todos los RDBMS brands. Algunos RDBMS requieren diferente
sintaxis para soportar una funcionalidad similar Cada clase Zend_Db_Adapter_Abstract incluye un
método para producir el SQL apropiado para cada RDBMS.
Use el método limitPage() como un modo alternativo de especificar la cantidad de filas y el offset. Este método
permite limitar el conjunto resultado a una serie de subconjuntos de tamaño fijo de filas del total del resultado de
la consulta. En otras palabras, puede especificar el tamaño de una "página" de resultados, y el número ordinal de la
página simple donde se espera que devuelva la consulta. El número de página es el primer argumento del método
limitPage(), y la longitud de la página es el segundo argumento. Ambos son argumentos requeridos; no tienen
valores por omisión.
$select = $db->select()
->from(array('p' => 'products'),
array('product_id', 'product_name'))
315
Zend_Db
->limitPage(2, 10);
$select = $db->select()
->distinct()
->from(array('p' => 'products'), 'product_name');
$select = $db->select()
->forUpdate()
->from(array('p' => 'products'));
El método query() devuelve un objeto de tipo Zend_Db_Statement o PDOStatement, dependiendo del tipo de
adaptador.
Ejemplo 15.71. Ejemplo usando el método adaptador query() del Adaptador de Base de datos
$select = $db->select()
->from('products');
$stmt = $db->query($select);
$result = $stmt->fetchAll();
316
Zend_Db
$select = $db->select()
->from('products');
$stmt = $select->query();
$result = $stmt->fetchAll();
$select = $db->select()
->from('products');
$sql = $select->__toString();
echo "$sql\n";
// La salida es el string:
// SELECT * FROM "products"
Otros Métodos
Esta sección describe otros métodos de Zend_Db_Select que no han sido cubiertos antes: getPart() y
reset().
El valor de retorno no es un string conteniendo un fragmento de la sintaxis SQL. El valor de retorno es una
representación, típicamente un array con una estructura que contiene valores y expresiones. Cada parte de la consulta
tiene una estructura diferente.
El único argumento del método getPart() es un string que identifica qué parte del la consulta Select va a devolver.
Por ejemplo, el string 'from' identifica la parte del objeto Select que almacena la información de las tablas de la
cláusula FROM, incluyendo uniones de tablas.
La clase Zend_Db_Select define constantes que puedes usar para las partes de la consulta SQL. Puede usar estas
definiciones de constantes, o los strings literales.
317
Zend_Db
$select = $db->select()
->from('products')
->order('product_id');
El argumento es opcional. Puede especificar la parte de la consulta que será limpiada, usando los mismos strings que
usa el argumento del método getPart(). La parte de la consulta que especifique se reestablecerá a su estado por
omisión.
Si omite el parámetro, reset() cambia todas las partes de la consulta a su estado por omisión. Esto hace que el
objeto Zend_Db_Select sea equivalente a crear un nuevo objeto, como si acabase de instanciarlo.
318
Zend_Db
$select = $db->select()
->from(array('p' => 'products')
->order('product_name');
Zend_Db_Table
Introduction
The Zend_Db_Table class is an object-oriented interface to database tables. It provides methods for many common
operations on tables. The base class is extensible, so you can add custom logic.
If you don't specify the table name, it defaults to the name of the class. If you rely on this default, the class name must
match the spelling of the table name as it appears in the database.
319
Zend_Db
You can also declare the schema for the table, either with the protected variable $_schema, or with the schema
prepended to the table name in the $_name property. Any schema specified with the $_name property takes
precedence over a schema specified with the $_schema property. In some RDBMS brands, the term for schema is
"database" or "tablespace," but it is used similarly.
// First alternative:
class Bugs extends Zend_Db_Table_Abstract
{
protected $_schema = 'bug_db';
protected $_name = 'bugs';
}
// Second alternative:
class Bugs extends Zend_Db_Table_Abstract
{
protected $_name = 'bug_db.bugs';
}
The schema and table names may also be specified via constructor configuration directives, which override any default
values specified with the $_name and $_schema properties. A schema specification given with the name directive
overrides any value provided with the schema option.
// First alternative:
// Second alternative:
320
Zend_Db
If you don't specify the schema name, it defaults to the schema to which your database adapter instance is connected.
If you don't specify the primary key, Zend_Db_Table_Abstract tries to discover the primary key based on the
information provided by the describeTable()´ method.
Nota
Every table class must know which column(s) can be used to address rows uniquely. If no primary key
column(s) are specified in the table class definition or the table constructor arguments, or discovered in the
table metadata provided by describeTable(), then the table cannot be used with Zend_Db_Table.
• _setupDatabaseAdapter() checks that an adapter has been provided; gets a default adapter from the registry
if needed. By overriding this method, you can set a database adapter from some other source.
321
Zend_Db
• _setupTableName() defaults the table name to the name of the class. By overriding this method, you can set
the table name before this default behavior runs.
• _setupMetadata() sets the schema if the table name contains the pattern "schema.table"; calls
describeTable() to get metadata information; defaults the $_cols array to the columns reported by
describeTable(). By overriding this method, you can specify the columns.
• _setupPrimaryKey() defaults the primary key columns to those reported by describeTable(); checks
that the primary key columns are included in the $_cols array. By overriding this method, you can specify the
primary key columns.
Tabla initialization
If application-specific logic needs to be initialized when a Table class is constructed, you can select to move your tasks
to the init() method, which is called after all Table metadata has been processed. This is recommended over the
__construct method if you do not need to alter the metadata in any programmatic way.
322
Zend_Db
// Later...
It can be convenient to create the database adapter object in a central place of your application, such as the bootstrap,
and then store it as the default adapter. This gives you a means to ensure that the adapter instance is the same throughout
your application. However, setting a default adapter is limited to a single adapter instance.
// Later...
Like setting the default adapter, this gives you the means to ensure that the same adapter instance is used throughout
your application. Using the registry is more flexible, because you can store more than one adapter instance. A given
adapter instance is specific to a certain RDBMS brand and database instance. If your application needs access to
multiple databases or even multiple database brands, then you need to use multiple adapters.
$data = array(
'created_on' => '2007-03-22',
'bug_description' => 'Something wrong',
'bug_status' => 'NEW'
);
323
Zend_Db
$table->insert($data);
By default, the values in your data array are inserted as literal values, using parameters. If you need them to be treated
as SQL expressions, you must make sure they are distinct from plain strings. Use an object of type Zend_Db_Expr
to do this.
$data = array(
'created_on' => new Zend_Db_Expr('CURDATE()'),
'bug_description' => 'Something wrong',
'bug_status' => 'NEW'
);
In the examples of inserting rows above, it is assumed that the table has an auto-incrementing primary key. This is the
default behavior of Zend_Db_Table_Abstract, but there are other types of primary keys as well. The following
sections describe how to support different types of primary keys.
In Zend_Db_Table_Abstract, if you define the protected variable $_sequence to be the Boolean value true,
then the class assumes that the table has an auto-incrementing primary key.
MySQL, Microsoft SQL Server, and SQLite are examples of RDBMS brands that support auto-incrementing primary
keys.
PostgreSQL has a SERIAL notation that implicitly defines a sequence based on the table and column name, and uses
the sequence to generate key values for new rows. IBM DB2 has an IDENTITY notation that works similarly. If you
use either of these notations, treat your Zend_Db_Table class as having an auto-incrementing column with respect
to declaring the $_sequence member as true.
324
Zend_Db
If you define $_sequence to be a string, then Zend_Db_Table_Abstract assumes the string to name a
sequence object in the database. The sequence is invoked to generate a new value, and this value is used in the INSERT
operation.
Oracle, PostgreSQL, and IBM DB2 are examples of RDBMS brands that support sequence objects in the database.
PostgreSQL and IBM DB2 also have syntax that defines sequences implicitly and associated with columns. If you use
this notation, treat the table as having an auto-incrementing key column. Define the sequence name as a string only in
cases where you would invoke the sequence explicitly to get the next key value.
If you define the $_sequence to be the Boolean value false, then Zend_Db_Table_Abstract assumes that
the table has a natural primary key. You must provide values for the primary key columns in the array of data to the
insert() method, or else this method throws a Zend_Db_Table_Exception.
Nota
All RDBMS brands support tables with natural keys. Ejemplos of tables that are often declared as having
natural keys are lookup tables, intersection tables in many-to-many relationships, or most tables with
compound primary keys.
325
Zend_Db
$data = array(
'updated_on' => '2007-03-23',
'bug_status' => 'FIXED'
);
$table->update($data, $where);
Since the table update() method proxies to the database adapter update() method, the second argument can be
an array of SQL expressions. The expressions are combined as Boolean terms using an AND operator.
Nota
The values and identifiers in the SQL expression are not quoted for you. If you have values or
identifiers that require quoting, you are responsible for doing this. Use the quote(), quoteInto(), and
quoteIdentifier() methods of the database adapter.
$table->delete($where);
Since the table delete() method proxies to the database adapter delete() method, the argument can also be an
array of SQL expressions. The expressions are combined as Boolean terms using an AND operator.
Nota
The values and identifiers in the SQL expression are not quoted for you. If you have values or
identifiers that require quoting, you are responsible for doing this. Use the quote(), quoteInto(), and
quoteIdentifier() methods of the database adapter.
326
Zend_Db
$rows = $table->find(1234);
If you specify a single value, the method returns at most one row, because a primary key cannot have duplicate values
and there is at most one row in the database table matching the value you specify. If you specify multiple values in an
array, the method returns at most as many rows as the number of distinct values you specify.
The find() method might return fewer rows than the number of values you specify for the primary key, if some of
the values don't match any rows in the database table. The method even may return zero rows. Because the number of
rows returned is variable, the find() method returns an object of type Zend_Db_Table_Rowset_Abstract.
If the primary key is a compound key, that is, it consists of multiple columns, you can specify the additional columns
as additional arguments to the find() method. You must provide as many arguments as the number of columns in
the table's primary key.
To find multiple rows from a table with a compound primary key, provide an array for each of the arguments. All of
these arrays must have the same number of elements. The values in each array are formed into tuples in order; for
example, the first element in all the array arguments define the first compound primary key value, then the second
elements of all the arrays define the second compound primary key value, and so on.
327
Zend_Db
The following statements are all legal and functionally identical, however it is recommended to update your
code to take advantage of the new usage where possible.
// Fetching a rowset
$rows = $table->fetchAll('bug_status = "NEW"', 'bug_id ASC', 10, 0);
$rows = $table->fetchAll($table->select()->where('bug_status = ?', 'NEW')
->order('bug_id ASC')
->limit(10, 0));
The Zend_Db_Table_Select object is an extension of the Zend_Db_Select object that applies specific
restrictions to a query. The enhancements and restrictions are:
• You can elect to return a subset of columns within a fetchRow or fetchAll query. This can provide optimization
benefits where returning a large set of results for all columns is not desirable.
• You can specify columns that evaluate expressions from within the selected table. However this will mean that the
returned row or rowset will be readOnly and cannot be used for save() operations. A Zend_Db_Table_Row with
readOnly status will throw an exception if a save() operation is attempted.
• You can not specify columns from a JOINed tabled to be returned in a row/rowset. Doing so will trigger a PHP
error. This was done to ensure the integrity of the Zend_Db_Table is retained. i.e. A Zend_Db_Table_Row
should only reference columns derived from its parent table.
$select = $table->select();
$select->where('bug_status = ?', 'NEW');
$rows = $table->fetchAll($select);
Fluent interfaces are implemented across the component, so this can be rewritten this in a more abbreviated form.
$rows =
$table->fetchAll($table->select()->where('bug_status = ?', 'NEW'));
Fetching a rowset
You can query for a set of rows using any criteria other than the primary key values, using the fetchAll() method
of the Table class. This method returns an object of type Zend_Db_Table_Rowset_Abstract.
328
Zend_Db
$rows = $table->fetchAll($select);
You may also pass sorting criteria in an ORDER BY clause, as well as count and offset integer values, used to make
the query return a specific subset of rows. These values are used in a LIMIT clause, or in equivalent logic for RDBMS
brands that do not support the LIMIT syntax.
$order = 'bug_id';
$rows = $table->fetchAll($select);
All of the arguments above are optional. If you omit the ORDER clause, the result set includes rows from the table in
an unpredictable order. If no LIMIT clause is set, you retrieve every row in the table that matches the WHERE clause.
Advanced usage
For more specific and optimized requests, you may wish to limit the number of columns returned in a row/
rowset. This can be achieved by passing a FROM clause to the select object. The first argument in the FROM
clause is identical to that of a Zend_Db_Select object with the addition of being able to pass an instance of
Zend_Db_Table_Abstract and have it automatically determine the table name.
$select = $table->select();
$select->from($table, array('bug_id', 'bug_description'))
->where('bug_status = ?', 'NEW');
$rows = $table->fetchAll($select);
Important
The rowset contains rows that are still 'valid' - they simply contain a subset of the columns of a table. If a
save() method is called on a partial row then only the fields available will be modified.
329
Zend_Db
You can also specify expressions within a FROM clause and have these returned as a readOnly row/rowset. In this
example we will return a rows from the bugs table that show an aggregate of the number of new bugs reported by
individuals. Note the GROUP clause. The 'count' column will be made available to the row for evaluation and can be
accessed as if it were part of the schema.
$select = $table->select();
$select->from($table,
array('COUNT(reported_by) as `count`', 'reported_by'))
->where('bug_status = ?', 'NEW')
->group('reported_by');
$rows = $table->fetchAll($select);
You can also use a lookup as part of your query to further refine your fetch operations. In this example the accounts
table is queried as part of a search for all new bugs reported by 'Bob'.
$select = $table->select();
$select->where('bug_status = ?', 'NEW')
->join('accounts', 'accounts.account_name = bugs.reported_by')
->where('accounts.account_name = ?', 'Bob');
$rows = $table->fetchAll($select);
The Zend_Db_Table_Select is primarily used to constrain and validate so that it may enforce the criteria
for a legal SELECT query. However there may be certain cases where you require the flexibility of the
Zend_Db_Table_Row component and do not require a writable or deletable row. For this specific user case, it
is possible to retrieve a row/rowset by passing a false value to setIntegrityCheck. The resulting row/rowset will be
returned as a 'locked' row (meaning the save(), delete() and any field-setting methods will throw an exception).
$select = $table->select()->setIntegrityCheck(false);
$select->where('bug_status = ?', 'NEW')
->join('accounts',
'accounts.account_name = bugs.reported_by',
'account_name')
->where('accounts.account_name = ?', 'Bob');
$rows = $table->fetchAll($select);
330
Zend_Db
$row = $table->fetchRow($select);
This method returns an object of type Zend_Db_Table_Row_Abstract. If the search criteria you specified match
no rows in the database table, then fetchRow() returns PHP's NULL value.
$info = $table->info();
The keys of the array returned by the info() method are described below:
• metadata => an associative array, mapping column names to information about the columns. This is the information
returned by the describeTable() method.
• rowClass => the name of the concrete class used for Row objects returned by methods of this table instance. This
defaults to Zend_Db_Table_Row.
• rowsetClass => the name of the concrete class used for Rowset objects returned by methods of this table instance.
This defaults to Zend_Db_Table_Rowset.
• referenceMap => an associative array, with information about references from this table to any parent tables. See
the section called “Defining Relationships”.
• dependentTables => an array of class names of tables that reference this table. See the section called “Defining
Relationships”.
• schema => the name of the schema (or database or tablespace) for this table.
331
Zend_Db
• insert()
• find()
• info()
In some circumstances, particularly when many table objects are instantiated against the same database table, querying
the database for the table metadata for each instance may be undesirable from a performance standpoint. In such cases,
users may benefit by caching the table metadata retrieved from the database.
There are two primary ways in which a user may take advantage of table metadata caching:
In both cases, the cache specification must be either NULL (i.e., no cache used) or an instance of Zend_Cache_Core.
The methods may be used in conjunction when it is desirable to have both a default metadata cache and the ability
to change the cache for individual table objects.
Ejemplo 15.105. Using a Default Metadata Cache for all Table Objects
The following code demonstrates how to set a default metadata cache to be used for all table objects:
<
// First, set up the Cache
$frontendOptions = array(
'automatic_serialization' => true
);
$backendOptions = array(
'cache_dir' => 'cacheDir'
);
$cache = Zend_Cache::factory('Core',
'File',
$frontendOptions,
$backendOptions);
332
Zend_Db
// ...
}
$backendOptions = array(
'cache_dir' => 'cacheDir'
);
$cache = Zend_Cache::factory('Core',
'File',
$frontendOptions,
$backendOptions);
Though the above examples use Zend_Cache_Backend_File, developers may use whatever cache backend is
appropriate for the situation. Please see Zend_Cache for more information.
333
Zend_Db
An easy way to get the appropriate values is to use the metadata cache, and then to deserialize values stored in the cache.
// At instantiation:
$bugs = new Bugs(array('metadataCacheInClass' => false));
// Or later:
$bugs->setMetadataCacheInClass(false);
The flag is enabled by default, which ensures that the $_metadata array is only populated once per instance.
You can specify Row and Rowset classes using the Table constructor's options array, in keys 'rowClass' and
'rowsetClass' respectively. Specify the names of the classes using strings.
334
Zend_Db
You can change the classes by specifying them with the setRowClass() and setRowsetClass() methods.
This applies to rows and rowsets created subsequently; it does not change the class of any row or rowset objects you
have created previously.
$table->setRowClass('My_Row');
$table->setRowsetClass('My_Rowset');
For more information on the Row and Rowset classes, see the section called “Zend_Db_Table_Row” and the section
called “Zend_Db_Table_Rowset”.
335
Zend_Db
For example, if your table class name is "BugsProducts", it would match the physical table in the database called
"bugs_products," if you omit the explicit declaration of the $_name class property. In this inflection mapping,
the class name spelled in "CamelCase" format would be transformed to lower case, and words are separated with an
underscore.
You can specify the database table name independently from the class name by declaring the table name with the
$_name class property in each of your table classes.
336
Zend_Db
Zend_Db_Table_Abstract performs no inflection to map the class name to the table name. If you omit the
declaration of $_name in your table class, the class maps to a database table that matches the spelling of the class
name exactly.
It is inappropriate to transform identifiers from the database, because this can lead to ambiguity or make
some identifiers inaccessible. Using the SQL identifiers exactly as they appear in the database makes
Zend_Db_Table_Abstract both simpler and more flexible.
If you prefer to use inflection, then you must implement the transformation yourself, by overriding the
_setupTableName() method in your Table classes. One way to do this is to define an abstract class that extends
Zend_Db_Table_Abstract, and then the rest of your tables extend your new abstract class.
You are responsible for writing the functions to perform inflection transformation. Zend Framework does not provide
such a function.
Zend_Db_Table_Row
Introduction
Zend_Db_Table_Row is a class that contains an individual row of a Zend_Db_Table object. When you run a
query against a Table class, the result is returned in a set of Zend_Db_Table_Row objects. You can also use this
object to create new rows and add them to the database table.
Fetching a Row
Zend_Db_Table_Abstract provides methods find() and fetchAll(), which each return an object
of type Zend_Db_Table_Rowset, and the method fetchRow(), which returns an object of type
Zend_Db_Table_Row.
337
Zend_Db
Nota
Earlier versions of Zend_Db_Table_Row mapped these column accessors to the database column names
using a string transformation called inflection.
Currently, Zend_Db_Table_Row does not implement inflection. Accessed property names need to match
the spelling of the column names as they appear in your database.
338
Zend_Db
The array returned from toArray() is not updateable. You can modify values in the array as you can with any array,
but you cannot save changes to this array to the database directly.
Using a column accessor to set a value changes the column value of the row object in your application, but it does not
commit the change to the database yet. You can do that with the save() method.
339
Zend_Db
The optional argument to the createRow() method is an associative array, with which you can populate fields of the
new row.
$data = array(
'bug_description' => '...description...',
'bug_status' => 'NEW'
);
Nota
The createRow() method was called fetchNew() in earlier releases of Zend_Db_Table. You are
encouraged to use the new method name, even though the old name continues to work for the sake of backward
compatibility.
Deleting a row
You can call the delete() method on a Row object. This deletes rows in the database matching the primary key
in the Row object.
340
Zend_Db
You do not have to call save() to apply the delete; it is executed against the database immediately.
Serializing a Row
Simply use PHP's serialize() function to create a string containing a byte-stream representation of the Row
object argument.
Note that the Row object returned is in a disconnected state. You can read the Row object and its properties, but you
cannot change values in the Row or execute other methods that require a database connection (for example, queries
against related tables).
$rowClone = unserialize($serializedRow);
341
Zend_Db
$rowClone = unserialize($serializedRow);
// Now you can make changes to the row and save them
$rowClone->bug_status = 'FIXED';
$rowClone->save();
342
Zend_Db
Row initialization
If application-specific logic needs to be initialized when a row is constructed, you can select to move your tasks
to the init() method, which is called after all row metadata has been processed. This is recommended over the
__construct method if you do not need to alter the metadata in any programmatic way.
If you need to do custom logic in a specific table, and the custom logic must occur for every operation on that table,
it may make more sense to implement your custom code in the insert(), update() and delete() methods of
your Table class. However, sometimes it may be necessary to do custom logic in the Row class.
Below are some example cases where it might make sense to implement custom logic in a Row class instead of in
the Table class:
You need data operations on this table to record the operation to a Zend_Log object, but only if the application
configuration has enabled this behavior.
343
Zend_Db
$log = Zend_Registry::get('database_log');
$log->info(Zend_Debug::dump($this->_data,
"INSERT: $this->_tableClass",
false)
);
}
}
Ejemplo 15.127. Ejemplo of a Row class that logs insert data for multiple tables
The custom logic may be common to multiple tables. Instead of implementing the same custom logic in every one of
your Table classes, you can implement the code for such actions in the definition of a Row class, and use this Row
in each of your Table classes.
344
Zend_Db
Zend_Db classes do not implement inflection by default. See the section called “Define Inflection in Zend_Db_Table”
for an explanation of this policy.
If you prefer to use inflection, then you must implement the transformation yourself, by overriding the
_transformColumn() method in a custom Row class, and using that custom Row class when you perform queries
against your Table class.
You are responsible for writing the functions to perform inflection transformation. Zend Framework does not provide
such a function.
Zend_Db_Table_Rowset
Introduction
When you run a query against a Table class using the find() or fetchAll() methods, the result is returned in
an object of type Zend_Db_Table_Rowset_Abstract. A Rowset contains a collection of objects descending
from Zend_Db_Table_Row_Abstract. You can iterate through the Rowset and access individual Row objects,
reading or modifying data in the Rows.
Fetching a Rowset
Zend_Db_Table_Abstract provides methods find() and fetchAll(), each of which returns an object of
type Zend_Db_Table_Rowset_Abstract.
345
Zend_Db
A legitimate query returns zero rows when no rows in the database match the query conditions. Therefore, a
Rowset object might contain zero Row objects. Since Zend_Db_Table_Rowset_Abstract implements the
Countable interface, you can use count() to determine the number of Rows in the Rowset.
$rowCount = count($rowset);
if ($rowCount > 0) {
echo "found $rowCount rows";
} else {
echo 'no rows matched the query';
}
If the Rowset contains zero rows, current() returns PHP's NULL value.
346
Zend_Db
// retrieve it
$row9 = $rowset->current();
// and use it
$row9->assigned_to = 'mmouse';
$row9->save();
getRow() allows you to get a specific row in the Rowset, knowing its position; don't forget however that positions
start with index zero. The first parameter for getRow() is an integer for the position asked. The second optional
parameter is a boolean; it tells the Rowset iterator if it must seek to that position in the same time, or not (default is
false). This method returns a Zend_Db_Table_Row object by default. If the position requested does not exist, an
exception will be thrown. Here is an example :
347
Zend_Db
After you have access to an individual Row object, you can manipulate the Row using methods described in the section
called “Zend_Db_Table_Row”.
$rowsetArray = $rowset->toArray();
$rowCount = 1;
foreach ($rowsetArray as $rowArray) {
echo "row #$rowCount:\n";
foreach ($rowArray as $column => $value) {
echo "\t$column => $value\n";
}
++$rowCount;
echo "\n";
}
The array returned from toArray() is not updateable. That is, you can modify values in the array as you can with
any array, but changes to the array data are not propagated to the database.
348
Zend_Db
Note that the Rowset object returned is in a disconnected state. You can iterate through the Rowset and read the Row
objects and their properties, but you cannot change values in the Rows or execute other methods that require a database
connection (for example, queries against related tables).
$rowsetDisconnected = unserialize($serializedRowset);
// Now you can use object methods and properties, but read-only
$row = $rowsetDisconnected->current();
echo $row->bug_description;
You can reactivate a disconnected Rowset using the setTable() method. The argument to this method is a valid
object of type Zend_Db_Table_Abstract, which you create. Creating a Table object requires a live connection
to the database, so by reassociating the Table with the Rowset, the Rowset gains access to the database. Subsequently,
you can change values in the Row objects contained in the Rowset and save the changes to the database.
$rowset = unserialize($serializedRowset);
$row = $rowset->current();
// Now you can make changes to the row and save them
$row->bug_status = 'FIXED';
$row->save();
Reactivating a Rowset with setTable() also reactivates all the Row objects contained in that Rowset.
349
Zend_Db
// ...customizations
}
Typically, the standard Zend_Db_Rowset concrete class is sufficient for most usage. However, you might find it
useful to add new logic to a Rowset, specific to a given Table. For example, a new method could calculate an aggregate
over all the Rows in the Rowset.
Zend_Db_Table Relationships
Introduction
Tables have relationships to each other in a relational database. An entity in one table can be linked to one or more
entities in another table by using referential integrity constraints defined in the database schema.
350
Zend_Db
The Zend_Db_Table_Row class has methods for querying related rows in other tables.
Defining Relationships
Define classes for each of your tables, extending the abstract class Zend_Db_Table_Abstract, as described in
the section called “Defining a Table Class”. Also see the section called “La base de datos de ejemplo” for a description
of the example database for which the following example code is designed.
351
Zend_Db
If you use Zend_Db_Table to emulate cascading UPDATE and DELETE operations, declare the
$_dependentTables array in the class for the parent table. List the class name for each dependent table. Use the
class name, not the physical name of the SQL table.
Nota
Skip declaration of $_dependentTables if you use referential integrity constraints in the RDBMS
server to implement cascading operations. See the section called “Cascading Write Operations” for more
information.
Declare the $_referenceMap array in the class for each dependent table. This is an associative array of reference
"rules". A reference rule identifies which table is the parent table in the relationship, and also lists which columns in
the dependent table reference which columns in the parent table.
The rule key is a string used as an index to the $_referenceMap array. This rule key is used to identify each
reference relationship. Choose a descriptive name for this rule key. It's best to use a string that can be part of a PHP
method name, as you will see later.
In the example PHP code above, the rule keys in the Bugs table class are: 'Reporter', 'Engineer',
'Verifier', and 'Product'.
The value of each rule entry in the $_referenceMap array is also an associative array. The elements of this rule
entry are described below:
• columns => A string or an array of strings naming the foreign key column name(s) in the dependent table.
It's common for this to be a single column, but some tables have multi-column keys.
• refTableClass => The class name of the parent table. Use the class name, not the physical name of the SQL table.
It's common for a dependent table to have only one reference to its parent table, but some tables have multiple
references to the same parent table. In the example database, there is one reference from the bugs table to the
products table, but three references from the bugs table to the accounts table. Put each reference in a separate
entry in the $_referenceMap array.
• refColumns => A string or an array of strings naming the primary key column name(s) in the parent table.
It's common for this to be a single column, but some tables have multi-column keys. If the reference uses a
multi-column key, the order of columns in the 'columns' entry must match the order of columns in the
'refColumns' entry.
It is optional to specify this element. If you don't specify the refColumns, the column(s) reported as the primary
key columns of the parent table are used by default.
352
Zend_Db
• onDelete => The rule for an action to execute if a row is deleted in the parent table. See the section called “Cascading
Write Operations” for more information.
• onUpdate => The rule for an action to execute if values in primary key columns are updated in the parent table. See
the section called “Cascading Write Operations” for more information.
$row->findDependentRowset($table, [$rule]);
This method returns a Zend_Db_Table_Rowset_Abstract object, containing a set of rows from the dependent
table $table that refer to the row identified by the $row object.
The first argument $table can be a string that specifies the dependent table by its class name. You can also specify
the dependent table by using an object of that table class.
$bugsReportedByUser = $user1234->findDependentRowset('Bugs');
The second argument $rule is optional. It is a string that names the rule key in the $_referenceMap array of the
dependent table class. If you don't specify a rule, the first rule in the array that references the parent table is used. If
you need to use a rule other than the first, you need to specify the key.
In the example code above, the rule key is not specified, so the rule used by default is the first one that matches the
parent table. This is the rule 'Reporter'.
You can also add criteria, ordering and limits to your relationships using the parent row's select object.
353
Zend_Db
$bugsAssignedToUser = $user1234->findDependentRowset('Bugs',
'Engineer',
$select);
Alternatively, you can query rows from a dependent table using a special mechanism called a "magic method".
Zend_Db_Table_Row_Abstract invokes the method: findDependentRowset('<TableClass>',
'<Rule>') if you invoke a method on the Row object matching either of the following patterns:
• $row->find<TableClass>()
• $row->find<TableClass>By<Rule>()
In the patterns above, <TableClass> and <Rule> are strings that correspond to the class name of the dependent
table, and the dependent table's rule key that references the parent table.
Nota
Some application frameworks, such as Ruby on Rails, use a mechanism called "inflection" to allow the
spelling of identifiers to change depending on usage. For simplicity, Zend_Db_Table_Row does not
provide any inflection mechanism. The table identity and the rule key named in the method call must match
the spelling of the class and rule key exactly.
$row->findParentRow($table, [$rule]);
There always should be exactly one row in the parent table referenced by a dependent row, therefore this method
returns a Row object, not a Rowset object.
354
Zend_Db
The first argument $table can be a string that specifies the parent table by its class name. You can also specify the
parent table by using an object of that table class.
$reporter = $bug1->findParentRow('Accounts');
The second argument $rule is optional. It is a string that names the rule key in the $_referenceMap array of the
dependent table class. If you don't specify a rule, the first rule in the array that references the parent table is used. If
you need to use a rule other than the first, you need to specify the key.
In the example above, the rule key is not specified, so the rule used by default is the first one that matches the parent
table. This is the rule 'Reporter'.
Alternatively, you can query rows from a parent table using a "magic method". Zend_Db_Table_Row_Abstract
invokes the method: findParentRow('<TableClass>', '<Rule>') if you invoke a method on the Row
object matching either of the following patterns:
• $row->findParent<TableClass>([Zend_Db_Table_Select $select])
• $row->findParent<TableClass>By<Rule>([Zend_Db_Table_Select $select])
In the patterns above, <TableClass> and <Rule> are strings that correspond to the class name of the parent table,
and the dependent table's rule key that references the parent table.
Nota
The table identity and the rule key named in the method call must match the spelling of the class and rule
key exactly.
Ejemplo 15.146. Fetching the Parent Row using the Magic Method
This example shows finding parent Rows equivalent to those in the previous examples. In this case, the application
uses the magic method invocation instead of specifying the table and rule as strings.
355
Zend_Db
$row->findManyToManyRowset($table,
$intersectionTable,
[$rule1,
[$rule2,
[Zend_Db_Table_Select $select]
]
]);
This method returns a Zend_Db_Table_Rowset_Abstract containing rows from the table $table, satisfying
the many-to-many relationship. The current Row object $row from the origin table is used to find rows in the
intersection table, and that is joined to the destination table.
The first argument $table can be a string that specifies the destination table in the many-to-many relationship by its
class name. You can also specify the destination table by using an object of that table class.
The second argument $intersectionTable can be a string that specifies the intersection table between the two
tables in the the many-to-many relationship by its class name. You can also specify the intersection table by using
an object of that table class.
$productsRowset = $bug1234->findManyToManyRowset('Products',
'BugsProducts');
The third and fourth arguments $rule1 and $rule2 are optional. These are strings that name the rule keys in the
$_referenceMap array of the intersection table.
The $rule1 key names the rule for the relationship from the intersection table to the origin table. In this example,
this is the relationship from BugsProducts to Bugs.
356
Zend_Db
The $rule2 key names the rule for the relationship from the intersection table to the destination table. In this example,
this is the relationship from Bugs to Products.
Similarly to the methods for finding parent and dependent rows, if you don't specify a rule, the method uses the first
rule in the $_referenceMap array that matches the tables in the relationship. If you need to use a rule other than
the first, you need to specify the key.
In the example code above, the rule key is not specified, so the rules used by default are the first ones that match. In
this case, $rule1 is 'Reporter' and $rule2 is 'Product'.
Ejemplo 15.148. Fetching a Rowset with the Many-to-many Method By a Specific Rule
This example shows geting a Row object from from the origin table Bugs, and finding rows from the destination table
Products, representing products related to that bug.
$productsRowset = $bug1234->findManyToManyRowset('Products',
'BugsProducts',
'Bug');
Alternatively, you can query rows from the destination table in a many-to-many relationship using a "magic method."
Zend_Db_Table_Row_Abstract invokes the method: findManyToManyRowset('<TableClass>',
'<IntersectionTableClass>', '<Rule1>', '<Rule2>') if you invoke a method matching any of the
following patterns:
• $row->find<TableClass>Via<IntersectionTableClass> ([Zend_Db_Table_Select
$select])
• $row->find<TableClass>Via<IntersectionTableClass>By<Rule1>
([Zend_Db_Table_Select $select])
• $row->find<TableClass>Via<IntersectionTableClass>By<Rule1>And<Rule2>
([Zend_Db_Table_Select $select])
In the patterns above, <TableClass> and <IntersectionTableClass> are strings that correspond to the
class names of the destination table and the intersection table, respectively. <Rule1> and <Rule2> are strings that
correspond to the rule keys in the intersection table that reference the origin table and the destination table, respectively.
Nota
The table identities and the rule keys named in the method call must match the spelling of the class and rule
key exactly.
357
Zend_Db
$bugsRowset = $bugsTable->find(1234);
$bug1234 = $bugsRowset->current();
For example, if you use MySQL's MyISAM storage engine, or SQLite, these solutions do not support DRI.
You may find it helpful to declare the cascading operations with Zend_Db_Table.
If your RDBMS implements DRI and the ON DELETE and ON UPDATE clauses, you should declare these
clauses in your database schema, instead of using the cascading feature in Zend_Db_Table. Declaring
cascading DRI rules in the RDBMS is better for database performance, consistency, and integrity.
Most importantly, do not declare cascading operations both in the RDBMS and in your Zend_Db_Table
class.
You can declare cascading operations to execute against a dependent table when you apply an UPDATE or a DELETE
to a row in a parent table.
$product1234->delete();
// Automatically cascades to Bugs table
// and deletes dependent rows.
Similarly, if you use UPDATE to change the value of a primary key in a parent table, you may want the value in foreign
keys of dependent tables to be updated automatically to match the new value, so that such references are kept up to date.
It's usually not necessary to update the value of a primary key that was generated by a sequence or other mechanism.
But if you use a natural key that may change value occasionally, it is more likely that you need to apply cascading
updates to dependent tables.
To declare a cascading relationship in the Zend_Db_Table, edit the rules in the $_referenceMap. Set the
associative array keys 'onDelete' and 'onUpdate' to the string 'cascade' (or the constant self::CASCADE).
Before a row is deleted from the parent table, or its primary key values updated, any rows in the dependent table that
refer to the parent's row are deleted or updated first.
358
Zend_Db
No cascading update is done in the example below if the primary key value in the parent class is changed. The
'onUpdate' element of the reference map entry is self::RESTRICT. You can get the same result by omitting
the 'onUpdate' entry.
This means that if your database implements and enforces referential integrity constraints, a cascading UPDATE
executed by a Zend_Db_Table class conflicts with the constraint, and results in a referential integrity violation.
You can use cascading UPDATE in Zend_Db_Table only if your database does not enforce that referential integrity
constraint.
Cascading DELETE suffers less from the problem of referential integrity violations. You can delete dependent rows
as a non-atomic action before deleting the parent row that they reference.
However, for both UPDATE and DELETE, changing the database in a non-atomic way also creates the risk that another
database user can see the data in an inconsistent state. For example, if you delete a row and all its dependent rows, there
is a small chance that another database client program can query the database after you have deleted the dependent
rows, but before you delete the parent row. That client program may see the parent row with no dependent rows, and
assume this is the intended state of the data. There is no way for that client to know that its query read the database
in the middle of a change.
The issue of non-atomic change can be mitigated by using transactions to isolate your change. But some RDBMS
brands don't support transactions, or allow clients to read "dirty" changes that have not been committed yet.
Cascading deletes and updates defined in your Zend_Db_Table classes are applied if you execute the save() or
delete() methods on the Row class. However, if you update or delete data using another interface, such as a query
tool or another application, the cascading operations are not applied. Even when using update() and delete()
methods in the Zend_Db_Adapter class, cascading operations defined in your Zend_Db_Table classes are not
executed.
No Cascading INSERT.
359
Zend_Db
There is no support for a cascading INSERT. You must insert a row to a parent table in one operation, and insert
row(s) to a dependent table in a separate operation.
Zend_Db_Table_Definition
Introduction
Zend_Db_Table_Definition is a class that can be used to describe the relationships and configuration options
that should be used when Zend_Db_Table is used via concrete instantiation.
Basic Usage
For all of the same options that are available when configuring an extended Zend_Db_Table_Abstract class,
those options are also available when describing a definition file. This definition file should be passed to the class at
instantiation time so that it can know the full definition of all tables in said definition.
Below is a definition that will describe the table names and relationships between table objects. Note: if 'name' is left
out of the definition, it will be taken as the key of the defined table (an example of this is in the 'genre' section in
the example below.)
360
Zend_Db
)
));
As you can see, the same options you'd generally see inside of an extended Zend_Db_Table_Abstract class are
documented in this array as well. When passed into Zend_Db_Table constructor, this definition is persisted to any
tables it will need to create in order to return the proper rows.
Below is an example of the primary table instantiation as well as the findDependentRowset() and
findManyToManyRowset() calls that will correspond to the data model described above:
Advanced Usage
Sometimes you want to use both paradigms for defining and using the table gateway: both by extension and
concrete instantiation. To do this simply leave out any table configurations out of the definition. This will allow
Zend_Db_Table to look for the actual refered class instead of the definition key.
Building on the example above, we will allow for one of the table configurations to be a
Zend_Db_Table_Abstract extended class, while keeping the rest of the tables as part of the definition. We will
also show how one would interact with this new definition.
361
Zend_Db
362
Capítulo 16. Zend_Debug
Mostrar información de variables(Dumping
Variables)
El método estático Zend_Debug::dump() imprime o devuelve información sobre una expresión. Esta sencilla
técnica de depuración es común, porque es fácil de utilizar en caliente y no requiere inicialización, herramientas
especiales, o la depuración del entorno.
El argumento $var especifica la expresión o variable sobre la cual el método Zend_Debug::dump() generará
información.
Puede ser útil comprender que el método Zend_Debug::dump() envuelve la función de PHP var_dump()
[http://php.net/var_dump]. Si el flujo de salida es detectado como una presentación de la web, la salida de
var_dump() es escapada usando htmlspecialchars() [http://php.net/htmlspecialchars] y envuelta con el tag
(X)HTML pre.
También considere el componente Zend_Log escribiendo el código de depuración más permanente. Por
ejemplo, puede utilizar el nivel de log de DEPURACIÓN y el Stream log writer, para mostrar la cadena de
salida de Zend_Debug::dump().
363
364
Capítulo 17. Zend_Dojo
Introducción
Zend Framework trae incorporado Dojo Toolkit [http://dojotoolkit.org] para brindar apoyo out-of-the-box al desarrollo
de aplicaciones "ricas de internet". Los puntos de integración con Dojo incluyen:
• Soporte JSON-RPC
• Compatibilidad dojo.data
dojo.data se utiliza a menudo con XmlHttpRequest para traer dinámicamente datos desde el servidor. El principal
mecanismo para esto es extender el QueryReadStore para que apunte a una URL y especificar la información a
consultar. El lado del servidor después devuelve los datos con el siguiente formato JSON:
{
identifier: '<name>',
<label: '<label>',>
items: [
{ name: '...', label: '...', someKey: '...' },
...
]
}
Zend_Dojo_Data ofrece una interfaz simple para la construcción de estas estructuras programáticamente,
interactuando con ellos, y serializándolos a un array o a JSON.
Uso de Zend_Dojo_Data
En su forma más simple, dojo.data requiere que se proporcione el nombre del campo identificador en cada item, y un
conjunto de items (datos). Puede pasarlos tanto via el constructor, o via mutators:
365
Zend_Dojo
También puede añadir un solo item a la vez, o agregar items utilizando addItem() y addItems().
$data->addItems($someMoreItems);
• Arrays asociativos
Puede adjuntar colecciones de los items anteriores via addItems() o setItems() (sobreescribe todos los items
previamente establecidos); cuando lo haga, puede pasar un solo argumento:
• Arrays
• Objectos implementando la interfaz Traversable, que incluye las interfaces Iterator y ArrayAccess.
Si quiere especificar un campo que actuará como una etiqueta para el item, llame a setLabel():
$data->setLabel('name');
Por último, también puede cargar un item Zend_Dojo_Data de un array JSON dojo.data, utilizando el método
fromJson().
$data->fromJson($json);
366
Zend_Dojo
Zend_Dojo_Data permite asignar propiedades a los metadatos como al objeto. Lo siguiente ilustra su uso:
Zend_Dojo_Data implementa las interfaces ArrayAccess, Iterator, y Countable. Por lo tanto, puede
usar la recopilación de datos casi como si fuera un array.
Todos los items son referenciados por el identificador de campo. Dado que los identificadores deben ser únicos, puede
usar los valores de este campo para sacar los registros individuales. Hay dos maneras de hacer esto: con el método
getItem(), o via notación de array.
// Usando getItem():
$item = $data->getItem('foo');
Si conoce el identificador, puede utilizarlo para recuperar un item, actualizarlo, borrarlo, crearlo, o probarlo:
367
Zend_Dojo
// Borrar un item:
unset($data['foo']);
También puede hacer un loop sobre todos los items. Internamente, todos los items se almacenan como arrays.
Por último, como la clase implementa __toString(), también puede convertirlo a JSON simplemente con "echo"
de PHP o convertirlo a string:
Métodos Disponibles
Además de los métodos necesarios para aplicar las interfaces enumeradas anteriormente, están disponibles los
siguientes métodos.
• setItems($items): establece varios items a la vez, sobrescribiendo cualquier item que figurase anteriormente
en el objeto. $items debe ser un array o un objeto Traversable.
• setItem($item, $id = null): establece un item individual, opcionalmente puede pasar un identificador
explícito. Sobreescribe el item si ya existe en la colección. Los items válidos incluyen a arrays asociativos, objetos
implementando toArray(), o cualquier objeto con propiedades públicas.
• addItem($item, $id = null): añade un item individual, opcionalmente puede pasar un identificador
explícito. Generará una excepción si el item ya existe en la colección. Los items válidos incluyen a arrays asociativos,
objetos implementando toArray(), o cualquier objeto con propiedades públicas.
• addItems($items): agrega múltiples items a la vez, añadiendolos a cualquiera de los items actuales. Generará
una excepción si alguno de los nuevos items tiene un identificador concordante a un identificador ya existente en
la colección. $items debe ser un array o un objeto Traversable.
• getItem($id): recupera un item con el identificador dado de la colección; el item retornado será un array
asociativo. Si ningún item coincide, se devolverá un valor null.
368
Zend_Dojo
• setIdentifier($identifier): establece el nombre del campo que representa el identificador único para
cada item en la colección.
• setLabel($label): establece el nombre de un campo para ser utilizado como la etiqueta a mostrar para un item.
• toArray(): enviar el objeto a un array. Como mínimo, el array contendrá las claves 'identifier', 'items', y 'label'
si una etiqueta de campo ha sido establecida en el objeto.
• dojo(): configura el medio ambiente de Dojo para su página, incluyendo valores de configuración de Dojo, paths de
módulos personalizados, sentencias de requerimientos de módulos, temas de hojas de estilo, uso de CDN, y más.
$view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
Alternativamente, puede usar el método de Zend_Dojo enableView() para que haga el trabajo por usted:
Zend_Dojo::enableView($view);
369
Zend_Dojo
En muchas páginas, el desarrollador no podrá utilizar Dojo para nada. Así, vamos a centrarnos primero en un view
script donde Dojo es necesario y luego en el layout script, en donde vamos a configurar algo del medio ambiente de
Dojo y luego lo mostraremos.
En primer lugar, tenemos que decir nuestro objeto vista que utilice el path del ayudante de vista de Dojo. Esto puede
hacerse en el arranque o en un plugin de ejecución temprana; simplemente apoderarse de su objeto vista y ejecutar
lo siguiente:
$view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
El paso siguiente, el view script. En este caso, vamos a especificar que vamos a estar utilizando un FilteringSelect
-- que consumirá un almacén personalizado basado en QueryReadStore, al que llamamos 'PairedStore' y almacenado
en nuestro módulo 'custom'.
En nuestro script de esquema, vamos entonces a comprobar si Dojo está habilitado, y si es así, haremos algunas
configuraciones más generales y lo ensamblaremos:
370
Zend_Dojo
</head>
<body class="tundra">
<?php echo $this->layout()->content ?>
<?php echo $this->inlineScript() ?>
</body>
</html>
En este punto, sólo necesita asegurarse de que sus archivos están en el lugar correcto y que ha creado el punto final
de acción para su FilteringSelect!
El uso de Programmatic permite al desarrollador decorar los elementos existentes tirando de ellos por ID o selectores
CSS y pasarlos a los constructores apropiados de objetos en Dojo. Debido a que no se usan atributos HTML no standard,
las páginas continúan con la validación.
En la práctica, ambos casos de uso permiten una degradación elegante cuando javascript está desactivado o los diversos
recursos de Dojo script están fuera de alcance. Para promover las normas y validación de documentos, Zend Framework
hace uso de los usos programáticos por defecto; los diversos ayudantes de vista generarán javascript y lo empujan al
ayudante de vista dojo() para su inclusión cuando sean presentados.
Utilizando esta técnica los desarrolladores pueden también desear explorar la posibilidad de escribir sus propia
decoración programática de la página. Uno de los beneficios sería la posibilidad de especificar handlers para eventos
dijit.
Para permitir esto, así como la posibilidad de usar sintaxis declarativa, hay disponibles una serie de métodos estáticos
para establecer globamente este comportamiento.
Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
Si decide más bien utilizar el uso programático, llame al método estático setUseProgrammatic():
Zend_Dojo_View_Helper_Dojo::setUseProgrammatic();
Por último, si quiere crear sus propias normas programáticas, debe especificar el uso programático, pero al pasarle el
valor '-1'; en esta situación, no se creará ningún javascript para decorar cualquier dijit usado.
Zend_Dojo_View_Helper_Dojo::setUseProgrammatic(-1);
Temas
Dojo permite la creación de los temas de su dijits (widgets). Puede seleccionar uno pasándolo en un path de módulo:
$view->dojo()->addStylesheetModule('dijit.themes.tundra');
371
Zend_Dojo
La ruta del módulo es descubierta por utilizar el carácter '.' como separador de directorio y utilizando el último valor
en la lista como el nombre del archivo CSS en ese directorio del tema a usar; en el ejemplo de arriba, Dojo buscará
el tema en 'dijit/themes/tundra/tundra.css'.
Cuando se utiliza un tema, es importante recordar pasar la calse del tema a, por lo menos un contenedor rodeando
cualquier dijits que se utilice; el caso de uso más común es pasárselo en el body:
<body class="tundra">
La respuesta de Dojo a esto es proporcionar la capacidad de crear custom builds (construcciones personalizadas. Las
contrucciones hacen varias cosas:
• Grupos de archivos necesarios en layers; una capa (layer) agrupa a todos archivos necesarios en un único archivo
JS. (De ahí el nombre de esta sección.)
• "Interns" no son archivos javascript usados por dijits (típicamente, archivos de plantilla). También están agrupados
en el mismo archivo JS como la capa.
• Pasa el archivo a través de ShrinkSafe, que elimina espacios en blanco y comentarios, así como acorta nombres
de variables.
Algunos archivos pueden no ser superpuestos, pero el proceso creará una versión especial del directorio con la archivo
capa y todos los otros archivos. Esto le permite tener una distribución reducida adaptada a su sitio o necesidades de
aplicación.
Para usar una capa, el ayudante de vista dojo() tiene el método addLayer() para añadir paths de capas requeridas:
$view->dojo()->addLayer('/js/foo/foo.js');
Para más información sobre la creación de construcciones personalizadas, por favor consulte la docuemntación de
Build de Dojo [http://dojotoolkit.org/book/dojo-book-0-9/part-4-meta-dojo/package-system-and-custom-builds].
Métodos Disponibles
El ayudante de vista dojo() siempre devuelve una instancia del contenedor del marcador de posición dojo. Ese objeto
contenedor dispone de los siguientes métodos:
372
Zend_Dojo
• getLayers(): conseguir una lista de todos los paths de capas registrados (construcción personalizada).
• removeLayer($path): eliminar la capa que concuerde con $path de la lista de capas registradas (construcción
personalizada).
• getCdnDojoPath(): recuperar el último segmento del path del CDN de la url apuntando al archivo dojo.js.
• setLocalPath($path): decirle al contenedor el path a una instalación local de Dojo (deberá ser una ruta
relativa al servidor, y contener el propio archivo dojo.js); implícitamente permite integración.
• addStylesheetModule($module): agregar una hoja de estilo sobre la base del tema de un módulo.
• addStylesheet($path): agregar una hoja de estilo local para su uso con Dojo.
• addOnLoad($spec, $function = null): agregar un lambda para dojo.onLoad para llamadas. Si se pasa
un argumento, se supone que que puede ser tanto el nombre de una función o dar por terminado javascript. Si se
pasan dos argumentos, el primero se supone que es el nombre de la variable de la instancia de un objeto y el segundo
ya sea un nombre de método en ese objeto o un cierre a utilizar con ese objeto.
373
Zend_Dojo
• getOnLoadActions(): recuperar todas las acciones dojo.onLoad registradas con el contenedor. Esto será un
array de arrays.
• onLoadCaptureStart($obj = null): capturar los datos que se utilizarán como lambda para dojo.onLoad().
Si se provee $obj, los códigos JS capturados serán considerados un cierre a utilizar con ese objeto Javascript.
• javascriptCaptureStart(): captura javascript arbitrario para ser incluido en Dojo JS (onLoad, require, etc.
statements).
• __toString(): emitir el contenedor a un string; muestra todo el estilo HTML y elementos del script.
Zend Framework viene con una variedad de ayudantes de vista que le permiten suministrar y utilizar dijits dentro de
sus scripts de vista. Hay tres tipos básicos:
• Layout Containers: estos están diseñados para ser utilizados dentro de sus scripts de vista o consumidos por
los decoradores de forms, para forms, sub forms, y mostrar grupos. Envuelven las distintas clases ofrecidas en
dijit.layout. Cada ayudante de vista dijit layout espera los siguientes argumentos:
• $params (opcional): parámetros específicos de dijit. Básicamente, cualquier atributo no HTML que pueda
utilizarse para configurar el esquema del contenedor dijit.
• $attribs (optional): cualquier de los otros atributos HTML que deberían utilizarse para mostrar el contenedor
div. Si se pasa la clave 'id' en este array, será utilizada para el elemento DOM id del form, y $id será utilizado
por su nombre.
Si no pasa argumentos a un ayudante de vista de esquemas dijit, el ayudante mismo será devuelto. Esto le permite
capturar contenido, que a menudo es una manera más fácil para pasar contenido al esquema del contenedor. Ejemplos
de esta funcionalidad se mostrarán más tarde en esta sección.
• Form Dijit: el dijit dijit.form.Form, aunque no es completamente necesario para su uso con elementos de dijit forms,
se asegurará de que si hay un intento de enviar un form que no valide contra las validaciones del lado del cliente,
se detendrá el envío y se emitirán mensajes de error de validación. El ayudante de vista dijit espera los siguientes
argumentos:
• $attribs (opcional): cualquier otro de los atributos HTML que deberían ser utilizados para mostrar el div del
contenedor.
• $content (opcional): el contenido a envolver en el form. Si no se pasa ninguno, se utilizará un string vacío.
El orden de los argumentos varía de los demás dijits a fin de mantener la compatibilidad con el standard form()
del ayudante de vista.
374
Zend_Dojo
• Elementos del Form: éstos están diseñados para ser consumidos con Zend_Form, pero pueden ser utilizados
standalone también en sus scripts de vista. Cada elemento de ayudante de vista dijit espera los siguientes argumentos:
• $params (opcional): parámetros específicos de dijit. Básicamente, cualquier atributo no HTML que pueda
utilizarse para configurar un dijit.
• $attribs (opcional): cualquiera de los otros atributos adicionales HTML que deberían ser utilizados para
mostrar el dijit. Si se pasa la clave 'id' en este array, será utilizado por el elemento DOM id del form, y $id será
utilizado por su nombre.
Algunos de los elementos requieren más argumentos; esto se observó con el elemento individual ayudante de
descripciones.
Con el fin de utilizar estos ayudantes de vista, necesita registrar el path a los ayudantes de vista dojo con su objeto vista.
$view->addHelperPath('Zend/Dojo/View/Helper', 'Zend_Dojo_View_Helper');
Todos los elementos de los esquemas dijit tienen la firma string ($id = null, $content = '', array
$params = array(), array $attribs = array()). En todos los casos, si no pasa argumentos, será
devuelto el mismo objeto ayudante. Esto le da acceso a los métodos captureStart() y captureEnd(), que
permiten capturar contenido en lugar de pasarlo al esquema del contenedor.
• AccordionContainer: dijit.layout.AccordionContainer. Apilará todos los cuadros juntos verticalmente; con un click
en la barra del título de un cuadro, se expandirá y mostrará ese cuadro en particular.
375
Zend_Dojo
$content,
array(
'title' => 'Pane Title',
),
array(
'style' => 'background-color: lightgray;',
),
); ?>
376
Zend_Dojo
• StackContainer: dijit.layout.StackContainer. Todos los cuadros dentro de un StackContainer se colocan en una pila;
crea botones o funcionalidades a ser revelados uno a uno.
• TabContainer: dijit.layout.TabContainer. Todos los cuadros dentro de un TabContainer se colocan en una pila, con
pestañas colocadas a un lado para cambiar entre ellos.
Los siguientes métodos de captura están disponibles para todos los esquemas de contenedores:
Los contenedores pueden estar anidados durante la captura, mientras no hayan IDs duplicados.
• captureEnd($id): finalizar la captura del contenido a incluir en un contenedor. $id debe referirse a un id
anteriormente utilizado con la llamada captureStart(). Regresa el string representando al contenedor y su
contenido, como si simplemente pasara el contenido al ayudante mismo.
$view->borderContainer()->captureStart('masterLayout',
array('design' => 'headline'));
echo $view->contentPane(
'menuPane',
'This is the menu pane',
array('region' => 'top'),
array('style' => 'background-color: darkblue;')
);
echo $view->contentPane(
377
Zend_Dojo
'navPane',
'This is the navigation pane',
array('region' => 'left'),
array('style' => 'width: 200px; background-color: lightblue;')
);
echo $view->contentPane(
'mainPane',
'This is the main content pane area',
array('region' => 'center'),
array('style' => 'background-color: white;')
);
echo $view->contentPane(
'statusPane',
'Status area',
array('region' => 'bottom'),
array('style' => 'background-color: lightgray;')
);
echo $view->borderContainer()->captureEnd('masterLayout');
Los siguientes elementos de dijit form están disponibles en Zend Framework. Excepto cuando se señaló que todos
tienen la firma string ($id,$value = '', array $params = array(), array $attribs =
array()).
• un array indexado con dos valores, un valor verificado y otro no, en ese orden; o
378
Zend_Dojo
array(),
array('checkedValue' => 'foo', 'unCheckedValue' => 'bar')
); ?>
• ComboBox: dijit.layout.ComboBox. Los ComboBoxes son un híbrido entre un cuadro de selección y uno de texto
con autocompletado. La diferencia fundamental es que puede escribir una opción que no está en la lista de las
opciones disponibles, y que todavía considera válido el input. Opcionalmente acepta un quinto argumento, un array
asociativo $options; si lo hay, ComboBox será presentado como un select. Note también que la label values
del array $options será devuelto al form -- y no los valores en si mismos.
Alternativamente, puede pasar información sobre un datastrore dojo.data para utilizar con el elemento. Si la hay, el
ComboBox será presentado como un texto input, y traerá sus opciones vía ese datastore.
Para especificar un datastore, proporcionar una de las siguientes $params combinaciones de claves:
• La clave 'store', con un valor de array; el array debe contener las claves:
• store: el nombre de la variable javascript representando el datastore (este podría ser el nombre que desea para
su uso).
• params (opcional): un array asociativo de pares clave/valor a utilizar para configurar el datastore. El 'url' param
es un ejemplo típico.
• Las claves:
• storeParams: un array asociativo de pares clave/valor con los cuales configurar el datastore.
379
Zend_Dojo
echo $view->currencyTextBox(
'foo',
'$25.00',
array('currency' => 'USD'),
array('maxlength' => 20)
);
$view->headMeta()->appendHttpEquiv('Content-Type',
'text/html; charset=utf-8');
Por supuesto, esto significa que necesitará para garantizar que el marcador de posición headMeta() esté
dentro de una sentencia "echo" de PHP en su script.
echo $view->dateTextBox(
'foo',
'2008-07-11',
array('required' => true)
);
• Editor: dijit.Editor. Proporciona un editor WYSIWYG mediante el cual los usuarios pueden crear o editar el
contenido. dijit.Editor es un editor pluggable y extensible, con una variedad de parámetros que puede
utilizar para personalización; para más detalles vea la documentación de dijit.Editor [http://dojotoolkit.org/book/
dojo-book-0-9/part-2-dijit/advanced-editing-and-display/editor-rich-text].
echo $view->editor('foo');
• FilteringSelect: dijit.form.FilteringSelect. Similar a ComboBox, este es un híbrido de select/text que puede hacer
una lista de opciones o aquellos retornados vía un dojo.data datastore. A diferencia de ComboBox, sin embargo,
FilteringSelect no permite escribir una opción ni siquieraen en su lista. Además, opera como un select standard en
que los valores de opción, no las etiquetas, son devueltos cuando el form se envía.
380
Zend_Dojo
Por favor vea la información anterior sobre ComboBox para ejemplos y las opciones disponibles para definir
datastores.
• 'intermediateChanges' se puede ajustar para indicar cuando disparar o no eventos onChange mientras el handler
está siendo arrastrado.
• 'clickSelect' se establece para permitir hacer click en un lugar del deslizador para ajustar el valor.
• 'showButtons' se puede ajustar para permitir ver los botones de ambos extremos del deslizador para manipular
el valor.
La implementación de Zend Framework crea un elemento oculto para guardar el valor del deslizador.
Opcionalmente puede desear mostrar una regla o etiquetas para el deslizador. Para hacerlo, se asignará uno
o más de los parámetros dijit 'topDecoration' y/o 'bottomDecoration' (HorizontalSlider) o 'leftDecoration' y/o
'rightDecoration' (VerticalSlider). Cada uno de éstos espera las siguientes opciones:
• container: nombre del contenedor.
• labels (opcional): un array de etiquetas a utilizar. Use strings vacíos en cualquiera de los extremos para dotar de
etiquetas solamente a los valores internos. Es necesario cuando se especifica uno de las variantes dijit 'Labels'.
• dijit (opcional): uno de los siguientes será por defecto la dijits Rule (regla) HorizontalRule, HorizontalRuleLabels,
VerticalRule, o VerticalRuleLabels.
• params (opcional): los dijit params son para configurar la Regla dijit en uso. Los parámetros específicos de estos
dijits incluyen:
• attribs (opcional): atributos HTML para utilizar con las reglas/etiquetas. Esto debería seguir el formato de opción
params y ser un array asociativo con las claves 'container' y 'labels'.
echo $view->horizontalSlider(
'foo',
1,
array(
'minimum' => -10,
'maximum' => 10,
'discreteValues' => 11,
'intermediateChanges' => true,
'showButtons' => true,
'topDecoration' => array(
'container' => 'topContainer'
'dijit' => 'HorizontalRuleLabels',
381
Zend_Dojo
• NumberSpinner: dijit.form.NumberSpinner. Text box numérico para entrada, con botones para incremento y
decremento.
Espera bien un array asociativo del parámetro dijit 'constraints' o simplemente las claves 'min', 'max', y 'places' (estas
serían también las entradas esperadas del parámetro de restricciones). 'places' puede ser utilizada para indicar en
cuánto se incrementará y decrementará el número giratorio.
echo $view->numberSpinner(
'foo',
5,
array(
'min' => -10,
'max' => 10,
'places' => 2,
),
array(
'maxlenth' => 3,
382
Zend_Dojo
)
);
echo $view->numberTextBox(
'foo',
5,
array(
'places' => 4,
'type' => 'percent',
),
array(
'maxlength' => 20,
)
);
echo $view->passwordTextBox(
'foo',
'',
array(
'required' => true,
),
array(
'maxlength' => 20,
)
);
• RadioButton: dijit.form.RadioButton. Una serie de opciones entre las que sólo una puede ser seleccionada. Esta se
comporta en todos los sentidos como una de radio normal, pero tiene un look-and-feel consistente con otros dijits.
RadioButton acepta un cuarto argumento como opción, $options, un array asociativo de pares valor/etiqueta
utilizado como opciones de radio. También puede pasar estos como la clave options de $attribs.
echo $view->radioButton(
'foo',
'bar',
array(),
array(),
array(
'foo' => 'Foo',
'bar' => 'Bar',
'baz' => 'Baz',
)
);
383
Zend_Dojo
• SimpleTextarea: dijit.form.SimpleTextarea. Estos actuarán como textareas normales, pero se estilizan usando el
tema actual de dijit. No necesita especificar los atributos ya sea de las filas o de las columnas; use ems o porcentajes
del ancho y del alto en su lugar.
echo $view->simpleTextarea(
'foo',
'Start writing here...',
array(),
array('style' => 'width: 90%; height: 5ems;')
);
• SubmitButton: un dijit.form.Button está atado a un elemento de entrada a enviar. Vea el ayudante de vista de Button
para más detalles; la diferencia fundamental es que este botón puede enviar un form.
• Textarea: dijit.form.Textarea. Éstas actuarán como textareas normales, salvo que en lugar de un determinado número
de filas, se expanden a medida que el usuario tipea. El ancho debe especificarse mediante una regla de estilo.
echo $view->textarea(
'foo',
'Start writing here...',
array(),
array('style' => 'width: 300px;')
);
• TextBox: dijit.form.TextBox. Este elemento está presente principalmente para proporcionar un look-and-feel común
entre los diversos elementos dijit, y a ofrecer funcionalidades de base para otras clases derivadas de TextBox
(ValidationTextBox, NumberTextBox, CurrencyTextBox, DateTextBox, y TimeTextBox).
El parámetro común de dijit para los flags incluyen 'lowercase' (emitido a minúsculas), 'uppercase' (emitido a
mayúsculas), 'propercase' (emitido a Proper Case), y trim (elimina los espacios en blanco iniciales y finales); todos
aceptan valores booleanos. Además, puede especificar los parámetros 'size' y 'maxLength'.
echo $view->textBox(
'foo',
'some text',
array(
'trim' => true,
'propercase' => true,
'maxLength' => 20,
),
array(
'size' => 20,
)
);
echo $view->timeTextBox(
'foo',
384
Zend_Dojo
'',
array(
'am.pm' => true,
'visibleIncrement' => 'T00:05:00', // 5-minute increments
'visibleRange' => 'T02:00:00', // show 2 hours of increments
),
array(
'size' => 20,
)
);
• ValidationTextBox: dijit.form.ValidateTextBox. Proporciona validaciones del lado del cliente para un elemento de
texto. Se hereda desde TextBox.
• regExp: una expresión regular a utilizar para validar el texto. La Expresión Regular no requiere de marcadores
de límites.
• required: si el elemento es necesario o no. Si fuera necesario, y el elemento está incrustado en un dijit.form.Form,
será marcado como inválido y no se enviará.
echo $view->validationTextBox(
'foo',
'',
array(
'required' => true,
'regExp' => '[\w]+',
'invalidMessage' => 'No se permiten espacios o caracteres especiales',
'promptMessage' => 'Una palabra consiste de caracteres ' .
'alfanuméricos y underscores solamente',
),
array(
'maxlength' => 20,
)
);
Dijits Personalizados
Si ahonda mucho en Dojo, se encontrará escribiendo bastantes dijits personalizados, o utilizando dijits experimentales
de Dojox. Si bien Zend Framework no puede apoyar a todos los dijit directamente, si proporciona algún apoyo
rudimentario para tipos dijit arbitrarios vía el ayudante de vista CustomDijit.
La API del ayudante de vista CustomDijit es exactamente lo que cualquier otro dijit es, con una diferencia
importante: el tercer argumento de "params" debe contener el atributo "dojotype". El valor de este atributo debe ser
la clase Dijit que planea usar.
CustomDijit extiende la base del ayudante de vista DijitContainer, que también le permite capturar el
contenido (utilizando el par de métodos captureStart()/captureEnd()). captureStart() también espera
que pase el atributo "dojoType" a su argumento "params".
385
Zend_Dojo
Si quiere capturar el contenido en su lugar, simplemente use el método captureStart(), y pase el "DojoType"
al argumento de "params":
<?php $this->customDijit()->captureStart(
'foo',
array(
'dojoType' => 'dojox.layout.ContentPane',
'title' => 'Custom pane',
'region' => 'center'
)
); ?>
This is the content of the pane
<?php echo $this->customDijit()->captureEnd('foo'); ?>
Fácilmente puede extender también CustomDijit para crear apoyo para sus propios dijits personalizados. Como
ejemplo, si extiende dijit.layout.ContentPane para crear su propia clase foo.ContentPane, puede crear
el siguiente ayudante de apoyo:
class My_View_Helper_FooContentPane
extends Zend_Dojo_View_Helper_CustomDijit
{
protected $_defaultDojoType = 'foo.ContentPane';
Mientras que su dijit personalizado siga la misma base API que los dijits oficiales, utilizar o extender CustomDijit
debería funcionar correctamente.
386
Zend_Dojo
Hay tres opciones para utilizar los elementos de Dojo form con sus formularios:
• Use Zend_Dojo::enableForm(). Esto le permitirá añadir recursivamente paths de plugins para decoradores
y elementos de todos los items de forms adjuntos. Además, el objeto vista será habilitado para Dojo. Note, sin
embargo, que cualquier subformulario que agregue después de esta llamada también tendrá que ser pasado mediante
Zend_Dojo::enableForm().
• Por último, y lo más tedioso, puede establecer por sí mismo el path apropiado para el decorador y
para el elemento, establecer por defecto la clase DisplayGroup, y habilitar la vista para Dojo. Dado que
Zend_Dojo::enableForm() ya hizo esto, hay pocas razones para seguir esta vía.
"Pero espere", podría decir; "Ya extendí Zend_Form con mi propia clase personalizada de form! ¿Cómo puede
habilitarlo para Dojo?"
En primer lugar, y lo más fácil, simplemente cambie de extender Zend_Form a extender Zend_Dojo_Form,
y actualizar todos los lugares donde se instancie a Zend_Form_SubForm para instanciar a
Zend_Dojo_Form_SubForm.
El uso de los decoradores y elementos específicos de dijit form es como usar cualquier otro decorador o elemento
de formularios.
387
Zend_Dojo
Todos los decoradores dijit buscan la propiedad dijitParams del elemento que va a ser decorado, y mandarlos
como un array $params al ayudante de vista dijit que se está usando. Luego, éstos son separados de cualquiera de
las otras propiedades para evitar la duplicación de información.
Decorador DijitElement
Al igual que el decorador ViewHelper, DijitElement espera una propiedad del helper en el elemento que luego
usará como el ayudante de vista cuando lo renderice. Los parámetros Dijit suelen ser arrastrados directamente desde
el elemento, pero también pueden ser pasados como opciones vía la clave dijitParams (el valor de esa clave debe
ser un array asociativo de opciones).
Es importante que cada elemento tenga un único ID (como traído desde el método getId() del elemento). Si se
detectan duplicados dentro del ayudante de vista dojo(), el decorador accionará un aviso, pero luego creará un único
ID añadiéndole lo que devuelve uniqid() al identificador.
El uso estándar es simplemente asociar este decorador como el primero de su cadena de decoradores, sin opciones
adicionales.
$element->setDecorators(array(
'DijitElement',
'Errors',
'Label',
'ContentPane',
));
Decorador DijitForm
El decorador DijitForm es muy similar al Form decorator; de hecho, básicamente puede ser utilizado de manera
intercambiable con él, ya que utiliza el mismo nombre de ayudante de vista ('form').
Dado que dijit.form.Form no requiere ningún parámetro dijit para la configuración, la principal diferencia es que el
ayudante de vista dijit del form exige que se pase un DOM ID para garantizar que la creación programática del dijit
pueda trabajar. El decorador garantiza esto, pasando el nombre del form como el identificador.
• AccordionContainer
388
Zend_Dojo
• AccordionPane
• BorderContainer
• ContentPane
• SplitContainer
• StackContainer
• TabContainer
// Use un ContentPane en su sub form (que puede utilizarse con todos, pero no
// con AccordionContainer):
$subForm->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'dl')),
'ContentPane',
));
• setDijitParams(array $params): establecer varios parámetros dijit a la vez. Cualquiera de los parámetros
pasados que concuerden con los ya presentes se sobreescribirán.
389
Zend_Dojo
Los parámetros Dijit se almacenan en la propiedad pública dijitParams. Así, puede habilitar dijit para un elemento
de un formulario existente simplemente estableciendo esta propiedad en el elemento; sencillamante no tendrá los
accessors anteriores a fin de facilitar la manipulación de parámetros.
Además, los elementos específicos de dijit implementan una lista diferente de decoradores, correspondientes a lo
siguiente:
$element->addDecorator('DijitElement')
->addDecorator('Errors')
->addDecorator('HtmlTag', array('tag' => 'dd'))
->addDecorator('Label', array('tag' => 'dt'));
Finalmente, el elemento base Dijit asegura que el path del ayudante de vista de Dojo se establezca en la vista.
Una variante de DijitElement, DijitMulti, ofrece la funcionalidad del elemento abstracto del formulario Multi,
permitiendo al desarrollador especificar 'multiOptions' -- típicamente opciones "select" u opciones de "radio".
Los siguientes elementos dijit están incluídos en la distribución standard de Zend Framework.
Button
Si bien no derivan del elemento standard Button ???, implementan la misma funcionalidad, y pueden ser utilizados
como una sustitución de drop-in, como se expone en la siguiente funcionalidad:
• getLabel() utilizará el nombre del elemento como el rótulo del botón si no se ha provisto el nombre. Además,
traducirá el nombre si un adaptador de traducción encuentra concordancia con un mensaje disponible.
• isChecked() determina si el valor enviado coincide con la etiqueta; si así fuera, devuelve true. Esto es útil para
determinar qué botón se utilizó cuando se envió un formulario.
Además, sólo los decoradores DijitElement y DtDdWrapper se utilizan para elementos Button.
$form->addElement(
'Button',
'foo',
array(
'label' => 'Button Label',
)
);
CheckBox
Si bien no derivan del elemento standard Checkbox ???, aplican la misma funcionalidad. Esto significa exponer los
siguientes métodos:
• getCheckedValue(): obtener el valor del item a usar cuando está comprobado (checked).
• setUncheckedValue($value): establecer el valor del item a utilizar cuando está desactivado (unchecked).
• getUncheckedValue(): obtener el valor del item a utilizar cuando está desactivado (unchecked).
390
Zend_Dojo
$form->addElement(
'CheckBox',
'foo',
array(
'label' => 'A check box',
'checkedValue' => 'foo',
'uncheckedValue' => 'bar',
'checked' => true,
)
);
ComboBox y FilteringSelect
Como se señaló en la documentación del ayudante de vista de ComboBox dijit ???, los ComboBoxes son un híbrido
entre "select" y "text input", permitiendo el autocompletado y la capacidad para especificar una alternativa a las
opciones provistas. FilteringSelects es lo mismo, pero no permite entradas arbitrarias.
Los elementos de forms de ComboBox y FilteringSelect proporcionan accessors y mutators para examinar y establecer
las opciones seleccionadas, así como para especificar un datastore dojo.data (si se usa). Se extienden desde DijitMulti,
que le permite especificar opciones de selección vía los métodos setMultiOptions() y setMultiOption().
Además, están disponibles los siguientes métodos:
• getStoreInfo(): Obtener del datastore toda la información establecida actualmente. Devuelve un array vacío
si no hay datos actualmente establecidos.
• getStoreParams(): obtiene cualquiera de los parámetros del datastore actualmente establecido; si no hay
ninguno, se devuelve un array vacío.
• setAutocomplete($flag): indica si será usado o no el elemento seleccionado una vez que el usuario deje
el elemento.
391
Zend_Dojo
Por defecto, si no hay ningún dojo.data registrado con el elemento, este elemento registra un validador InArray
que valida contra las claves del array de las opciones registradas. Puede desactivar este comportamiento ya sea
llamando a setRegisterInArrayValidator(false), o pasando un valor false a la clave de configuración
registerInArrayValidator.
$form->addElement(
'ComboBox',
'foo',
array(
'label' => 'ComboBox (select)',
'value' => 'blue',
'autocomplete' => false,
'multiOptions' => array(
'red' => 'Rouge',
'blue' => 'Bleu',
'white' => 'Blanc',
'orange' => 'Orange',
'black' => 'Noir',
'green' => 'Vert',
),
)
);
$form->addElement(
'ComboBox',
'foo',
array(
'label' => 'ComboBox (datastore)',
'storeId' => 'stateStore',
'storeType' => 'dojo.data.ItemFileReadStore',
'storeParams' => array(
'url' => '/js/states.txt',
),
'dijitParams' => array(
'searchAttr' => 'name',
),
)
);
CurrencyTextBox
El CurrencyTextBox principalmente brinda apoyo a la entrada de moneda. La moneda puede ser localizada, y puede
manejar tanto a valores fraccionarios como no fraccionarios.
392
Zend_Dojo
$form->addElement(
'CurrencyTextBox',
'foo',
array(
'label' => 'Currency:',
'required' => true,
'currency' => 'USD',
'invalidMessage' => 'Invalid amount. ' .
'Include dollar sign, commas, and cents.',
'fractional' => false,
)
);
DateTextBox
DateTextBox establece un calendario desplegable (drop-down) para seleccionar una fecha, así como validación y
formateo de fechas del lado del clente.
Internamente, DateTextBox deriva de ValidationTextBox y TextBox; todos los métodos disponibles a esas clases están
disponibles. Además, los siguientes métodos pueden utilizarse para establecer restricciones individuales:
• setAmPm($flag) y getAmPm(): Cuándo usar o no los strings AM/PM en los fortmatos de horas.
• setStrict($flag) y getStrict(): Cuándo usar o no el matching para una expresión regular estricta al
validar la entrada. Si es falso, que es el valor por defecto, será indulgente sobre espacios en blanco y algunas
abreviaturas.
• setSelector($selector) y getSelector(): proporciona y recupera el estilo del selector; debe ser "date"
o "time".
393
Zend_Dojo
$form->addElement(
'DateTextBox',
'foo',
array(
'label' => 'Date:',
'required' => true,
'invalidMessage' => 'Invalid date specified.',
'formatLength' => 'long',
)
);
Editor
Editor proporciona un editor WYSIWYG que puede ser utilizado tanto para crear como para editar contenidos
HTML ricos. dijit.Editor es pluggable y podrá ampliarse con plugins personalizados si lo desea; para más detalles
vea en la documentación de dijit.Editor [http://dojotoolkit.org/book/dojo-book-0-9/part-2-dijit/advanced-editing-and-
display/editor-rich-text].
El elemento form de Editor proporciona un número de accessors y mutators para manipular diversos parámetros dijit,
tal como sigue:
• captureEvents son eventos que se conectan al área de edición en si. Los siguientes accessors y mutators están
disponibles para manipular la captura de eventos:
• addCaptureEvent($event)
• addCaptureEvents(array $events)
• setCaptureEvents(array $events)
• getCaptureEvents()
• hasCaptureEvent($event)
• removeCaptureEvent($event)
• clearCaptureEvents()
• events son eventos DOM estándar, como onClick, onKeyup, etc. Los siguientes accessors y mutators están
disponibles para manipular eventos:
• addEvent($event)
• addEvents(array $events)
• setEvents(array $events)
• getEvents()
• hasEvent($event)
• removeEvent($event)
• clearEvents()
394
Zend_Dojo
• plugins añaden funcionalidad al Editor -- herramientas adicionales para la barra de herramientas, estilos adicionales
a permitir, etc. Los siguientes accessors y mutators están disponibles para manipular plugins:
• addPlugin($plugin)
• addPlugins(array $plugins)
• setPlugins(array $plugins)
• getPlugins()
• hasPlugin($plugin)
• removePlugin($plugin)
• clearPlugins()
• editActionInterval se utiliza para agrupar eventos para deshacer operaciones. Por defecto, este valor es de 3 segundos.
El método setEditActionInterval($interval) puede ser usado para establecer el valor, mientras que
getEditActionInterval() lo recuperará.
• focusOnLoad se utiliza para determinar si este editor en particular recibirá atención cuando la página se haya cargado.
Por defecto, esto es falso. El método setFocusOnLoad($flag) puede usarse para establecer el valor, mientras
que getFocusOnLoad() lo recuperará.
• height especifica la altura del editor; por defecto, es de 300px. El método setHeight($height) puede ser
utilizado para establecer el valor, mientras que getHeight() lo recupera.
• inheritWidth se utiliza para determinar si el editor utilizará el ancho del contenedor padre o simplemente toma
por defecto el 100% del ancho. Por defecto, esto es falso (es decir, llenará el ancho de la ventana). El método
setInheritWidth($flag) puede ser utilizado para establecer el valor, mientras que getInheritWidth()
lo recuperará.
• minHeight indica la altura mínima del editor; por defecto, es de 1em. El método setMinHeight($height)
puede ser utilizado para establecer el valor, mientras que getMinHeight() lo recuperará.
• styleSheets indica qué otras hojas de estilo CSS deberían ser utilizadas para incidir sobre la pantalla del Editor. Por
defecto, ninguna está registrada, y hereda la página de estilos. Los siguientes accessors y mutators están disponibles
para manipular al editor de hojas de estilo (stylesheets):
• addStyleSheet($styleSheet)
• addStyleSheets(array $styleSheets)
• setStyleSheets(array $styleSheets)
• getStyleSheets()
• hasStyleSheet($styleSheet)
• removeStyleSheet($styleSheet)
• clearStyleSheets()
395
Zend_Dojo
<sect3 id="zend.dojo.form.elements.horizontalSlider">
<title>HorizontalSlider</title>
<para>
HorizontalSlider proporciona un widget deslizador de UI para
seleccionar un valor numérico dentro de un rango.
Internamente, establece el valor de un elemento oculto que es
enviado por el formulario.
</para>
<para>
HorizontalSlider proviene del elemento abstracto <link
linkend="zend.dojo.form.elements.slider">Slider dijit</link>.
Además, tiene una variedad de métodos de ajuste y configuración
de reglas deslizantes y etiquetas para esas reglas.
</para>
<itemizedlist>
<listitem>
<para>
<methodname>setTopDecorationDijit($dijit)</methodname> y
<methodname>setBottomDecorationDijit($dijit)</methodname>: establecen e
nombre de la dijit a utilizar bien para la parte superior o
inferior de la barra deslizante.
Esto no debería incluir el prefijo "dijit.form.",
sino sólo el último nombre -- "HorizontalRule" o
"HorizontalRuleLabels".
</para>
</listitem>
<listitem>
<para>
<methodname>setTopDecorationContainer($container)</methodname> y
<methodname>setBottomDecorationContainer($container)</methodname>:
especifican el nombre a utilizar para el elemento
contenedor de las reglas; por ejemplo 'Toprule',
'topContainer', etc.
</para>
</listitem>
<listitem>
<para>
<methodname>setTopDecorationLabels(array $labels)</methodname> y
396
Zend_Dojo
<methodname>setBottomDecorationLabels(array $labels)</methodname>:
establecen las etiquetas a usar por uno de los tipos
RuleLabels dijit. Debe ser un array indexado; especificar un
único espacio vacío para saltar a la posición de una
determinada etiqueta (como ser al comienzo o al final).
</para>
</listitem>
<listitem>
<para>
<methodname>setTopDecorationParams(array $params)</methodname> y
<methodname>setBottomDecorationParams(array $params)</methodname>:
parámetros dijit para utilizar al configurar la Regla o
RuleLabels dijit.
</para>
</listitem>
<listitem>
<para>
<methodname>setTopDecorationAttribs(array $attribs)</methodname> y
<methodname>setBottomDecorationAttribs(array $attribs)</methodname>:
atributos HTML para especificar una Regla dada o el
elemento contenedor de HTML RuleLabels.
</para>
</listitem>
<listitem>
<para>
<methodname>getTopDecoration()</methodname> y
<methodname>getBottomDecoration()</methodname>: recuperar todos los
metadatos para una determinada Regla o definición de
RuleLabels, tal como han sido provistos por los mutators
anteriores.
</para>
</listitem>
</itemizedlist>
<example id="zend.dojo.form.elements.horizontalSlider.example">
<title>Ejemplo de Uso del Elemento HorizontalSlider dijit</title>
<para>
Lo siguiente creará un deslizador horizontal de selección con
valores enteros que van desde -10 a 10. La parte superior tendrá
etiquetas en las marcas del 20%, 40%, 60%, y 80%.
La parte inferior será una regla con marcas en el 0, 50%, y 100%.
Cada vez que se cambie el valor, el elemento oculto almacenará
el valor actualizado.
</para>
<programlisting language="php"><![CDATA[
$form->addElement(
'HorizontalSlider',
'horizontal',
array(
397
Zend_Dojo
NumberSpinner
Un número spinner es un elemento de texto para introducir valores numéricos; también incluye elementos de
incremento y decremento del valor por una cantidad fija.
398
Zend_Dojo
$form->addElement(
'NumberSpinner',
'foo',
array(
'value' => '7',
'label' => 'NumberSpinner',
'smallDelta' => 5,
'largeDelta' => 25,
'defaultTimeout' => 500,
'timeoutChangeRate' => 100,
'min' => 9,
'max' => 1550,
'places' => 0,
'maxlength' => 20,
)
);
NumberTextBox
Un cuadro de texto numérico es un elemento de texto de introducción de valores numéricos; a diferencia de
NumberSpinner, se introducen manualmente. Se pueden proporcionar validaciones y restricciones para garantizar que
el número permanece en un rango o formato particular.
Internmente, NumberTextBox proviene de ValidationTextBox y TextBox; todos los métodos disponibles a esas clases
están disponibles. Además, los siguientes métodos pueden utilizarse para establecer restricciones individuales:
• setType($type) y getType(): establece y recupera el tipo de formato numérico a utilizar (deberán ser uno
de 'decimal', 'percent', o 'currency').
• setStrict($flag) y getStrict(): establece y recupera el valor estricto del flag, que indica cuánta
indulgencia es permitida en relación con espacios en blanco y con caracteres no numéricos.
399
Zend_Dojo
$form->addElement(
'NumberTextBox',
'elevation',
array(
'label' => 'NumberTextBox',
'required' => true,
'invalidMessage' => 'Invalid elevation.',
'places' => 0,
'constraints' => array(
'min' => -20000,
'max' => 20000,
),
)
);
PasswordTextBox
PasswordTextBox es simplemente un ValidationTextBox que está ligado a una contraseña; su único objetivo es
permitir la entrada de texto de contraseñas de dijit que también proporciona validación del lado del cliente.
Internmente, NumberTextBox proviene de ValidationTextBox y TextBox; todos los métodos disponibles a esas clases
están disponibles.
$form->addElement(
'PasswordTextBox',
'password',
array(
'label' => 'Password',
'required' => true,
'trim' => true,
'lowercase' => true,
'regExp' => '^[a-z0-9]{6,}$',
'invalidMessage' => 'Invalid password; ' .
'must be at least 6 alphanumeric characters',
)
);
RadioButton
RadioButton envuelve a elementos standard de entrada tipo radio para brindar un look-and-feel consistente con otros
dojo dijits.
RadioButton se extiende desde DijitMulti, que le permite especificar la selección de opciones vía los métodos
setMultiOptions() y setMultiOption().
Por defecto, este elemento registra un validador InArray que valida contra las calves del
array de las opciones registradas. Puede desactivar este comportamiento ya sea llamando a
setRegisterInArrayValidator(false), o pasando un valor falso a la clave de configuración
registerInArrayValidator.
400
Zend_Dojo
$form->addElement(
'RadioButton',
'foo',
array(
'label' => 'RadioButton',
'multiOptions' => array(
'foo' => 'Foo',
'bar' => 'Bar',
'baz' => 'Baz',
),
'value' => 'bar',
)
);
SimpleTextarea
SimpleTextarea actúa principalmente como un textarea estándar de HTML. Sin embargo, no permite establecer filas
ni columnas. En su lugar, el ancho de textarea debe especificarse utilizando medidas CSS estándar. A diferencia de
Textarea, esta no aumentará automáticamente.
$form->addElement(
'SimpleTextarea',
'simpletextarea',
array(
'label' => 'SimpleTextarea',
'required' => true,
'style' => 'width: 80em; height: 25em;',
)
);
401
Zend_Dojo
SubmitButton
Si bien no hay Dijit llamado SubmitButton, incluimos uno aquí para proporcionar un botón dijit capaz de enviar un
formulario sin que se exijan ligaduras con javascript. Funciona exactamente igual que el Button dijit.
$form->addElement(
'SubmitButton',
'foo',
array(
'required' => false,
'ignore' => true,
'label' => 'Submit Button!',
)
);
TextBox
Textbox se incluyó principalmente para proporcionar una entrada de texto con apariencia coherente y con el look-
and-feel de los demás dijits. Sin embargo, también incluye algunas pequeñas capacidades de filtrado y validación,
representadas en los métodos siguientes:
• setUppercase($flag) y getUppercase(): establece y recupera el flag que indica si la entrada debe ser
presentada como mayúsculas (UPPERCASE).
• setTrim($flag) y getTrim(): establece y recupera el flag que indica si los espacios al comienzo o al final
deben ser eliminados o no.
$form->addElement(
'TextBox',
'foo',
array(
'value' => 'some text',
'label' => 'TextBox',
'trim' => true,
402
Zend_Dojo
Textarea
Textarea actúa principalmente como un textarea estándar de HTML. Sin embargo, no permite establecer filas y
columnas. En su lugar, el ancho de la textarea debe especificarse utilizando medidas CSS estándar; las filas debe
omitirse totalmente. Luego, la textarea crecerá verticalmente tanto como texto se añada a ella.
$form->addElement(
'Textarea',
'textarea',
array(
'label' => 'Textarea',
'required' => true,
'style' => 'width: 200px;',
)
);
TimeTextBox
TimeTextBox es una entrada de texto que proporciona una lista desplegable (drop-down) para seleccionar un tiempo
(fecha y hora). La lista desplegable, puede ser configurada para mostrar una cierta ventana de tiempo, con incrementos
especificados.
Internamente, TimeTextBox proviene de DateTextBox, ValidationTextBox y TextBox; todos los métodos disponibles
a esas clases están disponibles. Además, los siguientes métodos pueden utilizarse para establecer restricciones
individuales:
$form->addElement(
'TimeTextBox',
'foo',
array(
403
Zend_Dojo
ValidationTextBox
ValidationTextBox ofrece la posibilidad de añadir validaciones y limitaciones a una entrada de texto. Internamente,
proviene de TextBox, y añade los siguientes accessors y mutators para manejar parámetros dijit:
$form->addElement(
'ValidationTextBox',
'foo',
array(
'label' => 'ValidationTextBox',
'required' => true,
'regExp' => '[\w]+',
'invalidMessage' => 'Invalid non-space text.',
)
);
VerticalSlider
VerticalSlider es el hermano de HorizontalSlider, y opera en todos los sentidos como ese elemento. La única diferencia
real es que los métodos 'top*' y 'bottom*' son sustituidos por 'left*' y 'right*', y en lugar de utilizar HorizontalRule y
HorizontalRuleLabels, debe usarse VerticalRule y VerticalRuleLabels.
404
Zend_Dojo
$form->addElement(
'VerticalSlider',
'foo',
array(
'label' => 'VerticalSlider',
'value' => 5,
'style' => 'height: 200px; width: 3em;',
'minimum' => -10,
'maximum' => 10,
'discreteValues' => 11,
'intermediateChanges' => true,
'showButtons' => true,
'leftDecorationDijit' => 'VerticalRuleLabels',
'leftDecorationContainer' => 'leftContainer',
'leftDecorationLabels' => array(
' ',
'20%',
'40%',
'60%',
'80%',
' ',
),
'rightDecorationDijit' => 'VerticalRule',
'rightDecorationContainer' => 'rightContainer',
'rightDecorationLabels' => array(
'0%',
'50%',
'100%',
),
)
);
405
Zend_Dojo
/**
* Inicialización del Formn
*
* @return void
*/
public function init()
{
$this->setMethod('post');
$this->setAttribs(array(
'name' => 'masterForm',
));
$this->setDecorators(array(
'FormElements',
array('TabContainer', array(
'id' => 'tabContainer',
'style' => 'width: 600px; height: 500px;',
'dijitParams' => array(
'tabPosition' => 'top'
),
)),
'DijitForm',
));
$textForm = new Zend_Dojo_Form_SubForm();
$textForm->setAttribs(array(
'name' => 'textboxtab',
'legend' => 'Text Elements',
'dijitParams' => array(
'title' => 'Text Elements',
),
));
$textForm->addElement(
'TextBox',
'textbox',
array(
'value' => 'some text',
'label' => 'TextBox',
'trim' => true,
'propercase' => true,
)
)
->addElement(
'DateTextBox',
'datebox',
array(
'value' => '2008-07-05',
406
Zend_Dojo
407
Zend_Dojo
array(
'label' => 'Textarea',
'required' => true,
'style' => 'width: 200px;',
)
);
$editorForm = new Zend_Dojo_Form_SubForm();
$editorForm->setAttribs(array(
'name' => 'editortab',
'legend' => 'Editor',
'dijitParams' => array(
'title' => 'Editor'
),
))
$editorForm->addElement(
'Editor',
'wysiwyg',
array(
'label' => 'Editor',
'inheritWidth' => 'true',
)
);
408
Zend_Dojo
array(
'label' => 'CheckBox',
'checkedValue' => 'foo',
'uncheckedValue' => 'bar',
'checked' => true,
)
)
->addElement(
'RadioButton',
'radiobutton',
array(
'label' => 'RadioButton',
'multiOptions' => array(
'foo' => 'Foo',
'bar' => 'Bar',
'baz' => 'Baz',
),
'value' => 'bar',
)
);
$selectForm = new Zend_Dojo_Form_SubForm();
$selectForm->setAttribs(array(
'name' => 'selecttab',
'legend' => 'Select Elements',
));
$selectForm->addElement(
'ComboBox',
'comboboxselect',
array(
'label' => 'ComboBox (select)',
'value' => 'blue',
'autocomplete' => false,
'multiOptions' => $this->_selectOptions,
)
)
->addElement(
'ComboBox',
'comboboxremote',
array(
'label' => 'ComboBox (remoter)',
'storeId' => 'stateStore',
'storeType' => 'dojo.data.ItemFileReadStore',
'storeParams' => array(
'url' => '/js/states.txt',
),
'dijitParams' => array(
'searchAttr' => 'name',
),
)
)
->addElement(
'FilteringSelect',
'filterselect',
array(
409
Zend_Dojo
410
Zend_Dojo
'font-size=75%;color:gray;',
),
),
'bottomDecorationDijit' => 'HorizontalRule',
'bottomDecorationContainer' => 'bottomContainer',
'bottomDecorationLabels' => array(
'0%',
'50%',
'100%',
),
'bottomDecorationParams' => array(
'list' => array(
'style' => 'height:1em; ' .
'font-size=75%;color:gray;',
),
),
)
)
->addElement(
'VerticalSlider',
'vertical',
array(
'label' => 'VerticalSlider',
'value' => 5,
'style' => 'height: 200px; width: 3em;',
'minimum' => -10,
'maximum' => 10,
'discreteValues' => 11,
'intermediateChanges' => true,
'showButtons' => true,
'leftDecorationDijit' => 'VerticalRuleLabels',
'leftDecorationContainer' => 'leftContainer',
'leftDecorationLabels' => array(
' ',
'20%',
'40%',
'60%',
'80%',
' ',
),
'rightDecorationDijit' => 'VerticalRule',
'rightDecorationContainer' => 'rightContainer',
'rightDecorationLabels' => array(
'0%',
'50%',
'100%',
),
)
);
$this->addSubForm($textForm, 'textboxtab')
->addSubForm($editorForm, 'editortab')
->addSubForm($toggleForm, 'toggletab')
->addSubForm($selectForm, 'selecttab')
411
Zend_Dojo
->addSubForm($sliderForm, 'slidertab');
}
}
Este primer ejemplo muestra como decorar una instancia de un form existente:
// ...
}
}
Por supuesto, si puede hacerlo... podría y debería simplemente alterar la clase a heredar de Zend_Dojo_Form, que
es una sustitución del drop-in de Zend_Form que ya está habilitada por Dojo....
412
Zend_Dojo
and compiles them to a single file, optionally stripping whitespace and comments, and performing code heuristics to
allow further minification of variable names. Additionally, it can do CSS minification.
In order to create a build layer, you would traditionally create a JavaScript file that has dojo.require statements
for each dependency, and optionally some additional code that might run when the script is loaded. As an example:
dojo.provide("custom.main");
dojo.require("dijit.layout.TabContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.form.Form");
dojo.require("dijit.form.Button");
dojo.require("dijit.form.TextBox");
Then, in your application's layout, you'd instruct Dojo to load this module:
<html>
<head>
<script type="text/javascript" src="/js/dojo/dojo.js"></script>
<script type="text/javascript">
dojo.registerModulePath("custom", "../custom/");
dojo.require("custom.main");
</script>
$view->dojo()->registerModulePath('custom', '../custom/')
->requireModule('custom.main');
But since Zend_Dojo aggregates your various dojo.require statements, how do you create your layer script?
You could open each page and view the generated dojo.require statements, and cut and paste them into a layer
script file manually.
However, a better solution exists: since Zend_Dojo aggregates this information already, you can simply pull that
information and build your layer file. This is the purpose of Zend_Dojo_BuildLayer.
As an example, let's say you wanted to create the module layer "custom.main". Assuming you follow the recommended
project directory structure, and that you are storing your JavaScript files under public/js/, you could do the
following:
413
Zend_Dojo
$layerContents = $build->generateLayerScript();
$filename = APPLICATION_PATH . '/../public/js/custom/main.js';
if (!dir_exists(dirname($filename))) {
mkdir(dirname($filename));
}
file_put_contents($filename, $layerContents);
When should you do the above? For it to work correctly, you need to do it after all view scripts and the layout have
been rendered, to ensure that the dojo() helper is fully populated. One easy way to do so is using a front controller
plugin, with a dispatchLoopShutdown() hook:
414
Zend_Dojo
BuildLayer options
The above functionality will suffice for most situations. For those needing more customization, a variety of options
may be invoked.
$build->setView($view);
$build->setLayerName("custom.main");
By default, these are not rendered; to enable them, pass the consumeOnLoad configuration key during instantiation:
$build->setConsumeOnLoad(true);
By default, these are not rendered; to enable them, pass the consumeJavascript configuration key during instantiation:
415
Zend_Dojo
$build->setConsumeJavascript(true);
The simplest use case is to utilize the generateBuildProfile() method and send the output to a file:
$profile = $build->generateBuildProfile();
$filename = APPLICATION_PATH . '/../misc/scripts/custom.profile.js';
file_put_contents($filename, $profile);
Just like generating layers, you may want to automate this via a dispatchLoopShutdown() plugin hook; you
could even simply modify the one shown for generating layers to read as follows:
As noted, with module layers, you should only create the file once.
416
Zend_Dojo
As an example, you may want to specify what type of optimizations should be performed, whether or not to optimize
CSS files in the layer, whether or not to copy tests into the build, etc. For a listing of available options, you should read
the Dojo Build documentation [http://docs.dojocampus.org/build/index] and accompanying documentation [http://
www.dojotoolkit.org/book/dojo-book-0-9/part-4-meta-dojo/package-system-and-custom-builds].
{
action: "release",
optimize: "shrinksafe",
layerOptimize: "shrinksafe",
copyTests: false,
loader: "default",
cssOptimize: "comments"
}
You can pass in whatever key and value pairs you want; the Dojo build script will ignore those it does not understand.
// A single option:
$build->addProfileOption('version', 'zend-1.3.1');
// Several options:
$build->addProfileOptions(array(
'loader' => 'xdomain',
'optimize' => 'packer',
));
// Or overwrite options:
$build->setProfileOptions(array(
'version' => 'custom-1.3.1',
'loader' => 'shrinksafe',
'optimize' => 'shrinksafe',
));
417
418
Capítulo 18. Zend_Dom
Introducción
Zend_Dom provee herramientas para trabajar con documentos y estructuras DOM. Actualmente, ofrecemos
Zend_Dom_Query, el cual provee una interfaz unificada para consultar documentos DOM utilizando selectores
XPath y CSS.
Zend_Dom_Query
Zend_Dom_Query provides mechanisms for querying XML and (X)HTML documents utilizing either XPath or
CSS selectors. It was developed to aid with functional testing of MVC applications, but could also be used for rapid
development of screen scrapers.
CSS selector notation is provided as a simpler and more familiar notation for web developers to utilize when
querying documents with XML structures. The notation should be familiar to anybody who has developed Cascading
Style Sheets or who utilizes Javascript toolkits that provide functionality for selecting nodes utilizing CSS selectors
(Prototype's $$() [http://prototypejs.org/api/utility/dollar-dollar] and Dojo's dojo.query [http://api.dojotoolkit.org/
jsdoc/dojo/HEAD/dojo.query] were both inspirations for the component).
Theory of Operation
To use Zend_Dom_Query, you instantiate a Zend_Dom_Query object, optionally passing a document to query (a
string). Once you have a document, you can use either the query() or queryXpath() methods; each method will
return a Zend_Dom_Query_Result object with any matching nodes.
The primary difference between Zend_Dom_Query and using DOMDocument + DOMXPath is the ability to select
against CSS selectors. You can utilize any of the following, in any combination:
• element types: provide an element type to match: 'div', 'a', 'span', 'h2', etc.
• style attributes: CSS style attributes to match: '.error', 'div.error', 'label.required', etc. If an element defines more
than one style, this will match as long as the named style is present anywhere in the style declaration.
• arbitrary attributes: arbitrary element attributes to match. Three different types of matching are provided:
• exact match: the attribute exactly matches the string: 'div[bar="baz"]' would match a div element with a "bar"
attribute that exactly matches the value "baz".
• word match: the attribute contains a word matching the string: 'div[bar~="baz"]' would match a div element with
a "bar" attribute that contains the word "baz". '<div bar="foo baz">' would match, but '<div bar="foo bazbat">'
would not.
• substring match: the attribute contains the string: 'div[bar*="baz"]' would match a div element with a "bar"
attribute that contains the string "baz" anywhere within it.
• direct descendents: utilize '>' between selectors to denote direct descendents. 'div > span' would select only 'span'
elements that are direct descendents of a 'div'. Can also be used with any of the selectors above.
• descendents: string together multiple selectors to indicate a hierarchy along which to search. 'div .foo span #one'
would select an element of id 'one' that is a descendent of arbitrary depth beneath a 'span' element, which is in turn
419
Zend_Dom
a descendent of arbitrary depth beneath an element with a class of 'foo', that is an descendent of arbitrary depth
beneath a 'div' element. For example, it would match the link to the word 'One' in the listing below:
<div>
<table>
<tr>
<td class="foo">
<div>
Lorem ipsum <span class="bar">
<a href="/foo/bar" id="one">One</a>
<a href="/foo/baz" id="two">Two</a>
<a href="/foo/bat" id="three">Three</a>
<a href="/foo/bla" id="four">Four</a>
</span>
</div>
</td>
</tr>
</table>
</div>
Once you've performed your query, you can then work with the result object to determine information about the nodes,
as well as to pull them and/or their content directly for examination and manipulation. Zend_Dom_Query_Result
implements Countable and Iterator, and store the results internally as DOMNodes/DOMElements. As an
example, consider the following call, that selects against the HTML above:
Zend_Dom_Query also allows straight XPath queries utilizing the queryXpath() method; you can pass any valid
XPath query to this method, and it will return a Zend_Dom_Query_Result object.
Methods Available
The Zend_Dom_Query family of classes have the following methods available.
Zend_Dom_Query
The following methods are available to Zend_Dom_Query:
420
Zend_Dom
• getDocumentType(): retrieve the document type of the document provided to the object; will be one of the
DOC_XML, DOC_XHTML, or DOC_HTML class constants.
Zend_Dom_Query_Result
As mentioned previously, Zend_Dom_Query_Result implements both Iterator and Countable, and as such
can be used in a foreach loop as well as with the count() function. Additionally, it exposes the following methods:
• getCssQuery(): return the CSS selector query used to produce the result (if any).
• getXpathQuery(): return the XPath query used to produce the result. Internally, Zend_Dom_Query converts
CSS selector queries to XPath, so this value will always be populated.
421
422
Capítulo 19. Zend_Exception
Using Exceptions
Zend_Exception is simply the base class for all exceptions thrown within Zend Framework.
try {
// Calling Zend_Loader::loadClass() with a non-existant class will cause
// an exception to be thrown in Zend_Loader
Zend_Loader::loadClass('nonexistantclass');
} catch (Zend_Exception $e) {
echo "Caught exception: " . get_class($e) . "\n";
echo "Message: " . $e->getMessage() . "\n";
// Other code to recover from the error
}
Zend_Exception can be used as a catch-all exception class in a catch block to trap all exceptions thrown by Zend
Framework classes. This can be useful when the program can not recover by catching a specific exception type.
The documentation for each Zend Framework component and class will contain specific information on which methods
throw exceptions, the circumstances that cause an exception to be thrown, and the class of all exceptions that may
be thrown.
423
424
Capítulo 20. Zend_Feed
Introduction
Zend_Feed provides functionality for consuming RSS and Atom feeds. It provides a natural syntax for accessing
elements of feeds, feed attributes, and entry attributes. Zend_Feed also has extensive support for modifying feed
and entry structure with the same natural syntax, and turning the result back into XML. In the future, this modification
support could provide support for the Atom Publishing Protocol.
In the example below, we demonstrate a simple use case of retrieving an RSS feed and saving relevant portions of the
feed data to a simple PHP array, which could then be used for printing the data, storing to a database, etc.
Be aware
Many RSS feeds have different channel and item properties available. The RSS specification provides for
many optional properties, so be aware of this when writing code to work with RSS data.
425
Zend_Feed
Importing Feeds
Zend_Feed enables developers to retrieve feeds very easily. If you know the URI of a feed, simply use the
Zend_Feed::import() method:
$feed = Zend_Feed::import('http://feeds.example.com/feedName');
You can also use Zend_Feed to fetch the contents of a feed from a file or the contents of a PHP string variable:
In each of the examples above, an object of a class that extends Zend_Feed_Abstract is returned upon
success, depending on the type of the feed. If an RSS feed were retrieved via one of the import methods above,
then a Zend_Feed_Rss object would be returned. On the other hand, if an Atom feed were imported, then a
Zend_Feed_Atom object is returned. The import methods will also throw a Zend_Feed_Exception object
upon failure, such as an unreadable or malformed feed.
Custom feeds
Zend_Feed enables developers to create custom feeds very easily. You just have to create an array and
to import it with Zend_Feed. This array can be imported with Zend_Feed::importArray() or with
Zend_Feed::importBuilder(). In this last case the array will be computed on the fly by a custom data source
implementing Zend_Feed_Builder_Interface.
array(
//required
'title' => 'title of the feed',
'link' => 'canonical url to the feed',
// optional
'lastUpdate' => 'timestamp of the update date',
'published' => 'timestamp of the publication date',
426
Zend_Feed
// required
'charset' => 'charset of the textual data',
// optional
'description' => 'short description of the feed',
'author' => 'author/publisher of the feed',
'email' => 'email of the author',
// optional
'copyright' => 'copyright notice',
'image' => 'url to image',
'generator' => 'generator',
'language' => 'language the feed is written in',
// optional, defaults to 80
'port' => 'port to connect to',
// required
'path' => 'path of the cloud, e.g. /RPC2',
'registerProcedure' => 'procedure to call, e.g. myCloud.rssPlsNotify',
'protocol' => 'protocol to use, e.g. soap or xml-rpc'
),
427
Zend_Feed
'hour in 24 format'
),
// optional
'block' => 'Prevent an episode from appearing (yes|no)',
// optional
'sub' => 'sub category'
)
),
// optional
'explicit' => 'parental advisory graphic (yes|no|clean)',
'keywords' => 'a comma separated list of 12 keywords maximum',
'new-feed-url' => 'used to inform iTunes of new feed URL location'
),
428
Zend_Feed
array(
//required
'title' => 'title of the feed entry',
'link' => 'url to a feed entry',
// optional
'guid' => 'id of the article, '
. 'if not given link value will used',
// optional
'lastUpdate' => 'timestamp of the publication date',
'comments' => 'comments page of the feed entry',
'commentRss' => 'the feed url of the associated comments',
// optional
'scheme' => 'url that identifies a categorization scheme'
),
array(
// data for the second category and so on
)
),
// optional
'type' => 'mime type of the enclosure',
'length' => 'length of the linked content in octets'
),
array(
429
Zend_Feed
array(
//data for the second entry and so on
)
)
);
References:
Nota
Zend_Feed_Builder serves as a concrete implementation to demonstrate the usage. Users are encouraged
to make their own classes to implement Zend_Feed_Builder_Interface.
430
Zend_Feed
$feedArray = Zend_Feed::findFeeds('http://www.example.com/news.html');
Here the findFeeds() method returns an array of Zend_Feed_Abstract objects that are referenced by
<link> tags on the news.html web page. Depending on the type of each feed, each respective entry in the
$feedArray array may be a Zend_Feed_Rss or Zend_Feed_Atom instance. Zend_Feed will throw a
Zend_Feed_Exception upon failure, such as an HTTP 404 response code or a malformed feed.
Once you have a feed object, you can access any of the standard RSS "channel" properties directly on the object:
echo $channel->title();
Note the function syntax. Zend_Feed uses a convention of treating properties as XML object if they are requested
with variable "getter" syntax ($obj->property) and as strings if they are access with method syntax ($obj-
>property()). This enables access to the full text of any individual node while still allowing full access to all
children.
If channel properties have attributes, they are accessible using PHP's array syntax:
echo $channel->category['domain'];
Since XML attributes cannot have children, method syntax is not necessary for accessing attribute values.
Most commonly you'll want to loop through the feed and do something with its entries. Zend_Feed_Abstract
implements PHP's Iterator interface, so printing all titles of articles in a channel is just a matter of:
431
Zend_Feed
If you are not familiar with RSS, here are the standard elements you can expect to be available in an RSS channel
and in individual RSS items (entries).
• pubDate - The publication date of this set of content, in RFC 822 date format
• category - One or more (specified by multiple tags) categories the channel belongs to
RSS <item> elements do not have any strictly required elements. However, either title or description must
be present.
• pubDate - The date the item was published, in RFC 822 date format
In your code you can always test to see if an element is non-empty with:
if ($item->propname()) {
// ... proceed.
}
If you use $item->propname instead, you will always get an empty object which will evaluate to TRUE, so your
check will fail.
For further information, the official RSS 2.0 specification is available at: http://blogs.law.harvard.edu/tech/rss
432
Zend_Feed
In an Atom feed you can expect to find the following feed properties:
• link - Feeds can have multiple links, which are distinguished by a type attribute
The equivalent to RSS's channel link would be type="text/html". if the link is to an alternate version of the
same content that's in the feed, it would have a rel="alternate" attribute.
• content - The full content of the entry; can be skipped if the feed just contains summaries
• published - the date the entry was published, in RFC 3339 format
• updated - the date the entry was last updated, in RFC 3339 format
If you read a single entry, you will still have a Zend_Feed_Atom object, but it will automatically create an
"anonymous" feed to contain the entry.
433
Zend_Feed
$entry = $feed->current();
Alternatively, you could instantiate the entry object directly if you know you are accessing an <entry>-only
document:
Ejemplo 20.4. Using the Entry Object Directly for a Single-Entry Atom Feed
echo $entry->saveXML();
This will output a full (includes <?xml ... > prologue) XML representation of the new entry, including any
necessary XML namespaces.
Note that the above will work even if the existing entry does not already have an author tag. You can use as many
levels of -> access as you like before getting to an assignment; all of the intervening levels will be created for you
automatically if necessary.
If you want to use a namespace other than atom:, rss:, or osrss: in your entry, you need to register the namespace
with Zend_Feed using Zend_Feed::registerNamespace(). When you are modifying an existing element,
it will always maintain its original namespace. When adding a new element, it will go into the default namespace if
you do not explicitly specify another namespace.
434
Zend_Feed
echo $entry->saveXML();
Here is an example of a custom Atom entry class that handles its own myns: namespace entries. Note that it also makes
the registerNamespace() call for you, so the end user doesn't need to worry about namespaces at all.
Ejemplo 20.7. Extending the Atom Entry Class with Custom Namespaces
/**
* The custom entry class automatically knows the feed URI (optional) and
* can automatically add extra namespaces.
*/
class MyEntry extends Zend_Feed_Entry_Atom
{
Zend_Feed::registerNamespace('myns',
'http://www.example.com/myns/1.0');
}
default:
return parent::__get($var);
}
}
435
Zend_Feed
default:
parent::__set($var, $value);
}
}
default:
return parent::__call($var, $unused);
}
}
}
Then to use this class, you'd just instantiate it directly and set the myUpdated property:
Zend_Feed_Reader
Introduction
Zend_Feed_Reader is a component used to consume RSS and Atom feeds of any version, including RDF/RSS
1.0, RSS 2.0 and Atom 0.3/1.0. The API for retrieving feed data is deliberately simple since Zend_Feed_Reader
is capable of searching any feed of any type for the information requested through the API. If the typical elements
containing this information are not present, it will adapt and fall back on a variety of alternative elements instead.
This ability to choose from alternatives removes the need for users to create their own abstraction layer on top of the
component to make it useful or have any in-depth knowledge of the underlying standards, current alternatives, and
namespaced extensions.
Internally, Zend_Feed_Reader works almost entirely on the basis of making XPath queries against the feed XML's
Document Object Model. The DOM is not exposed though a chained property API like Zend_Feed though the
underlying DOMDocument, DOMElement and DOMXPath objects are exposed for external manipulation. This
singular approach to parsing is consistent and the component offers a plugin system to add to the Feed and Entry level
API by writing Extensions on a similar basis.
Performance is assisted in three ways. First of all, Zend_Feed_Reader supports caching using Zend_Cache to
maintain a copy of the original feed XML. This allows you to skip network requests for a feed URI if the cache is
436
Zend_Feed
valid. Second, the Feed and Entry level API is backed by an internal cache (non-persistant) so repeat API calls for
the same feed will avoid additional DOM/XPath use. Thirdly, importing feeds from a URI can take advantage of
HTTP Conditional GET requests which allow servers to issue an empty 304 response when the requested feed has not
changed since the last time you requested it. In the final case, an instance of Zend_Cache will hold the last received
feed along with the ETag and Last-Modified header values sent in the HTTP response.
In relation to Zend_Feed, Zend_Feed_Reader was formulated as a free standing replacement for Zend_Feed
but it is not backwards compatible with Zend_Feed. Rather it is an alternative following a different ideology focused
on being simple to use, flexible, consistent and extendable through the plugin system. Zend_Feed_Reader is also
not capable of constructing feeds through this will be addressed at a future date.
Importing Feeds
Importing a feed with Zend_Feed_Reader is not that much different to Zend_Feed. Feeds can be imported
from a string, file, URI or an instance of type Zend_Feed_Abstract. Importing from a URI can additionally
utilise a HTTP Conditional GET request. If importing fails, an exception will be raised. The end result
will be an object of type Zend_Feed_Reader_FeedInterface, the core implementations of which are
Zend_Feed_Reader_Feed_Rss and Zend_Feed_Reader_Feed_Atom (Zend_Feed took all the short
names!). Both objects support multiple (all existing) versions of these broad feed types.
In the following example, we import an RDF/RSS 1.0 feed and extract some basic information that can be saved to
a database or elsewhere.
$feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
$data = array(
'title' => $feed->getTitle(),
'link' => $feed->getLink(),
'dateModified' => $feed->getDateModified(),
'description' => $feed->getDescription(),
'language' => $feed->getLanguage(),
'entries' => array(),
);
The example above demonstrates Zend_Feed_Reader's API, and it also demonstrates some of it's internal
operation. In reality, the RDF feed selected does not have any native date or author elements, however it does utilise
the Dublin Core 1.1 module which offers namespaced creator and date elements. Zend_Feed_Reader falls back
on these and similar options if no relevant native elements exist. If it absolutely cannot find an alternative it will
return NULL, indicating the information could not be found in the feed. You should note that classes implementing
Zend_Feed_Reader_FeedInterface also implement the SPL Iterator and Countable interfaces.
Feeds can also be imported from strings, files, and even objects of type Zend_Feed_Abstract.
437
Zend_Feed
// from a URI
$feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
// from a String
$feed = Zend_Feed_Reader::importString($feedXmlString);
// from a file
$feed = Zend_Feed_Reader::importFile('./feed.xml');
• saveXml() returns an XML string containing only the element representing the current object.
• getDomDocument() returns the DOMDocument object representing the entire feed (even if called from an Entry
object).
• getElement() returns the DOMElement of the current object (i.e. the Feed or current Entry).
• getXpath() returns the DOMXPath object for the current feed (even if called from an Entry object) with the
namespaces of the current feed type and all loaded Extensions pre-registered.
• getXpathPrefix() returns the query prefix for the current object (i.e. the Feed or current Entry) which includes
the correct XPath query path for that specific Feed or Entry.
Here's an example where a feed might include an RSS Extension not supported by Zend_Feed_Reader out of the
box. Notably, you could write and register an Extension (covered later) to do this, but that's not always warranted for
a quick check. You must register any new namespaces on the DOMXPath object before use unless they are registered
by Zend_Feed_Reader or an Extension beforehand.
$feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
$xpathPrefix = $feed->getXpathPrefix();
$xpath = $feed->getXpath();
$xpath->registerNamespace('admin', 'http://webns.net/mvcb/');
$reportErrorsTo = $xpath->evaluate('string('
. $xpathPrefix
. '/admin:errorReportsTo)');
Warning
If you register an already registered namespace with a different prefix name to that used internally by
Zend_Feed_Reader, it will break the internal operation of this component.
438
Zend_Feed
$frontendOptions = array(
'lifetime' => 7200,
'automatic_serialization' => true
);
$backendOptions = array('cache_dir' => './tmp/');
$cache = Zend_Cache::factory(
'Core', 'File', $frontendOptions, $backendOptions
);
Zend_Feed_Reader::setCache($cache);
Nota
While it's a little off track, you should also consider adding a cache to Zend_Loader_PluginLoader
which is used by Zend_Feed_Reader to load Extensions.
Using this method, you can request feeds from URIs and include their last known ETag and Last-Modified response
header values with the request (using the If-None-Match and If-Modified-Since headers). If the feed on the server
remains unchanged, you should receive a 304 response which tells Zend_Feed_Reader to use the cached version.
If a full feed is sent in a response with a status code of 200, this means the feed has changed and Zend_Feed_Reader
will parse the new version and save it to the cache. It will also cache the new ETag and Last-Modified header values
for future use.
These "conditional" requests are not guaranteed to be supported by the server you request a URI of, but can be attempted
regardless. Most common feed sources like blogs should however have this supported. To enable conditional requests,
you will need to provide a cache to Zend_Feed_Reader.
$frontendOptions = array(
'lifetime' => 86400,
'automatic_serialization' => true
);
$backendOptions = array('cache_dir' => './tmp/');
$cache = Zend_Cache::factory(
'Core', 'File', $frontendOptions, $backendOptions
);
Zend_Feed_Reader::setCache($cache);
Zend_Feed_Reader::useHttpConditionalGet();
439
Zend_Feed
$feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
In the example above, with HTTP Conditional GET requests enabled, the response header values for ETag and Last-
Modified will be cached along with the feed. For the next 24hrs (the cache lifetime), feeds will only be updated on the
cache if a non-304 response is received containing a valid RSS or Atom XML document.
If you intend on managing request headers from outside Zend_Feed_Reader, you can set the relevant If-None-
Matches and If-Modified-Since request headers via the URI import method.
$lastEtagReceived = '5e6cefe7df5a7e95c8b1ba1a2ccaff3d';
$lastModifiedDateReceived = 'Wed, 08 Jul 2009 13:37:22 GMT';
$feed = Zend_Feed_Reader::import(
$uri, $lastEtagReceived, $lastModifiedDateReceived
);
This method calls any URI and searches for the location of RSS, RDF and Atom feeds assuming the website's HTML
contains the relevant links. It then returns a value object where you can check for the existence of a RSS, RDF or
Atom feed URI.
The returned object is an ArrayObject called Zend_Feed_Reader_FeedSet so you can cast it to an array, or
iterate over it, to access all the detected links. However, as a simple shortcut, you can just grab the first RSS, RDF or
Atom link using its public properties as in the example below.
$links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
if(isset($links->rdf)) {
echo $links->rdf, "\n"; // http://www.planet-php.org/rdf/
}
if(isset($links->rss)) {
echo $links->rss, "\n"; // http://www.planet-php.org/rss/
}
if(isset($links->atom)) {
echo $links->atom, "\n"; // http://www.planet-php.org/atom/
}
Based on these links, you can then import from whichever source you wish in the usual manner.
This quick method only gives you one link for each feed type, but websites may indicate many links of any type.
Perhaps it's a news site with a RSS feed for each news category. You can iterate over all links using the ArrayObject's
iterator.
$links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
440
Zend_Feed
The available keys are href, rel which will always be 'alternate', type which will be one of application/
rss+xml, application/rdf+xml or application/atom+xml and feed. feed is only available if you
preserve the ArrayObject (i.e. do not cast it to an array) and using it triggers an attempt to load the feed into a
Zend_Feed_Reader_FeedAbstract instance. This is a lazy loaded attempt - feeds are never loaded until you
try to access them using this method.
Of course, we don't live in an ideal world so there may be times the API just does not cover what you're looking for.
To assist you, Zend_Feed_Reader offers a plugin system which allows you to write Extensions to expand the
core API and cover any additional data you are trying to extract from feeds. If writing another Extension is too much
trouble, you can simply grab the underlying DOM or XPath objects and do it by hand in your application. Of course,
we really do encourage writing an Extension simply to make it more portable and reusable.
Here's a summary of the Core API for Feeds. You should note it comprises not only the basic RSS and Atom standards,
but also accounts for a number of included Extensions bundled with Zend_Feed_Reader. The naming of these
Extension sourced methods remain fairly generic - all Extension methods operate at the same level as the Core API
though we do allow you to retrieve any specific Extension object separately if required.
441
Zend_Feed
getGenerator() Returns the generator of the feed, e.g. the software which
generated it. This may differ between RSS and Atom since
Atom defines a different notation.
getCopyright() Returns any copyright notice associated with the feed
getHubs() Returns an array of all Hub Server URI endpoints
which are advertised by the feed for using with the
Pubsubhubbub Protocol, allowing subscriptions to the
feed for real-time updates.
Given the variety of feeds in the wild, some of these methods will undoubtedly return NULL indicating the relevant
information couldn't be located. Where possible, Zend_Feed_Reader will fall back on alternative elements during
its search. For example, searching an RSS feed for a modification date is more complicated than it looks. RSS 2.0
feeds should include a <lastBuildDate> tag and/or a <pubDate> element. But what if it doesn't, maybe this
is an RSS 1.0 feed? Perhaps it instead has an <atom:updated> element with identical information (Atom may be
used to supplement RSS's syntax)? Failing that, we could simply look at the entries, pick the most recent, and use
its <pubDate> element. Assuming it exists... Many feeds also use Dublin Core 1.0/1.1 <dc:date> elements for
feeds/entries. Or we could find Atom lurking again.
The point is, Zend_Feed_Reader was designed to know this. When you ask for the modification date (or anything
else), it will run off and search for all these alternatives until it either gives up and returns NULL, or finds an alternative
that should have the right answer.
In addition to the above methods, all Feed objects implement methods for retrieving the DOM and XPath objects for
the current feeds as described earlier. Feed objects also implement the SPL Iterator and Countable interfaces. The
extended API is summarised below.
442
Zend_Feed
valid() Checks that the current entry index is valid, i.e. it does
fall below 0 and does not exceed the number of entries
existing.
getExtensions() Returns an array of all Extension objects loaded for
the current feed (note: both feed-level and entry-
level Extensions exist, and only feed-level Extensions
are returned here). The array keys are of the form
{ExtensionName}_Feed.
getExtension(string $name) Returns an Extension object for the feed registered under
the provided name. This allows more fine-grained access
to Extensions which may otherwise be hidden within the
implementation of the standard API methods.
getType() Returns a static class constant (e.g.
Zend_Feed_Reader::TYPE_ATOM_03, i.e. Atom
0.3) indicating exactly what kind of feed is being
consumed.
443
Zend_Feed
The extended API for entries is identical to that for feeds with the exception of the Iterator methods which are not
needed here.
Caution
There is often confusion over the concepts of modified and created dates. In Atom, these are two clearly
defined concepts (so knock yourself out) but in RSS they are vague. RSS 2.0 defines a single <pubDate>
element which typically refers to the date this entry was published, i.e. a creation date of sorts. This is not
always the case, and it may change with updates or not. As a result, if you really want to check whether an entry
has changed, don't rely on the results of getDateModified(). Instead, consider tracking the MD5 hash of
three other elements concatenated, e.g. using getTitle(), getDescription() and getContent().
If the entry was trully updated, this hash computation will give a different result than previously saved hashes
for the same entry. Further muddying the waters, dates in feeds may follow different standards. Atom and
Dublin Core dates should follow ISO 8601, and RSS dates should follow RFC 822 or RFC 2822 which is
also common. Date methods will throw an exception if Zend_Date cannot load the date string using one
of the above standards.
Warning
The values returned from these methods are not validated. This means users must perform validation on all
retrieved data including the filtering of any HTML such as from getContent() before it is output from
your application. Remember that most feeds come from external sources, and therefore the default assumption
should be that they cannot be trusted.
444
Zend_Feed
getExtension(string $name) Returns an Extension object for the entry registered under
the provided name. This allows more fine-grained access
to Extensions which may otherwise be hidden within the
implementation of the standard API methods.
getType() Returns a static class constant (e.g.
Zend_Feed_Reader::TYPE_ATOM_03, i.e. Atom
0.3) indicating exactly what kind of feed is being
consumed.
There are two types of Extensions possible, those which retrieve information from elements which are
immediate children of the root element (e.g. <channel> for RSS or <feed> for Atom) and those
who retrieve information from child elements of an entry (e.g. <item> for RSS or <entry> for
Atom). On the filesystem these are grouped as classes within a namespace based on the extension
standard's name. For example, internally we have Zend_Feed_Reader_Extension_DublinCore_Feed
and Zend_Feed_Reader_Extension_DublinCore_Entry classes which are two Extensions implementing
Dublin Core 1.0/1.1 support.
Extensions are loaded into Zend_Feed_Reader using Zend_Loader_PluginLoader, so their operation will
be familiar from other Zend Framework components. Zend_Feed_Reader already bundles a number of these
Extensions, however those which are not used internally and registered by default (so called Core Extensions) must
be registered to Zend_Feed_Reader before they are used. The bundled Extensions include:
The Core Extensions are somewhat special since they are extremely common and multi-faceted. For example, we have
a Core Extension for Atom. Atom is implemented as an Extension (not just a base class) because it doubles as a valid
RSS module - you can insert Atom elements into RSS feeds. I've even seen RDF feeds which use a lot of Atom in
place of more common Extensions like Dublin Core.
445
Zend_Feed
The additional non-Core Extensions are offered but not registered to Zend_Feed_Reader by default. If you want
to use them, you'll need to tell Zend_Feed_Reader to load them in advance of importing a feed. Additional non-
Core Extensions will be included in future iterations of the component.
Registering an Extension with Zend_Feed_Reader, so it is loaded and its API is available to Feed and Entry
objects, is a simple affair using the Zend_Loader_PluginLoader. Here we register the optional Slash Extension,
and discover that it can be directly called from the Entry level API without any effort. Note that Extension names are
case sensitive and use camel casing for multiple terms.
Zend_Feed_Reader::registerExtension('Syndication');
$feed = Zend_Feed_Reader::import('http://rss.slashdot.org/Slashdot/slashdot');
$updatePeriod = $feed->current()->getUpdatePeriod();
In the simple example above, we checked how frequently a feed is being updated using the getUpdatePeriod()
method. Since it's not part of Zend_Feed_Reader's core API, it could only be a method supported by the newly
registered Syndication Extension.
As you can also notice, the new methods from Extensions are accessible from the main API using PHP's magic methods.
As an alternative, you can also directly access any Extension object for a similar result as seen below.
Zend_Feed_Reader::registerExtension('Syndication');
$feed = Zend_Feed_Reader::import('http://rss.slashdot.org/Slashdot/slashdot');
$syndication = $feed->getExtension('Syndication');
$updatePeriod = $syndication->getUpdatePeriod();
As an example, let's take the case of a purely fictitious corporation named Jungle Books. Jungle Books have been
publishing a lot of reviews on books they sell (from external sources and customers), which are distributed as an RSS
2.0 feed. Their marketing department realises that web applications using this feed cannot currently figure out exactly
what book is being reviewed. To make life easier for everyone, they determine that the geek department needs to
extend RSS 2.0 to include a new element per entry supplying the ISBN-10 or ISBN-13 number of the publication the
entry concerns. They define the new <isbn> element quite simply with a standard name and namespace URI:
JungleBooks 1.0:
http://example.com/junglebooks/rss/module/1.0/
A snippet of RSS containing this extension in practice could be something similar to:
446
Zend_Feed
<jungle:dayPopular>http://example.com/junglebooks/book/938</jungle:dayPopular>
<item>
<title>Review Of Flatland: A Romance of Many Dimensions</title>
<link>http://example.com/junglebooks/review/987</link>
<author>Confused Physics Student</author>
<content:encoded>
A romantic square?!
</content:encoded>
<pubDate>Thu, 25 Jun 2009 20:03:28 -0700</pubDate>
<jungle:isbn>048627263X</jungle:isbn>
</item>
</channel>
</rss>
Implementing this new ISBN element as a simple entry level extension would require the following class (using your
own class namespace outside of Zend).
class My_FeedReader_Extension_JungleBooks_Entry
extends Zend_Feed_Reader_Extension_EntryAbstract
{
public function getIsbn()
{
if (isset($this->_data['isbn'])) {
return $this->_data['isbn'];
}
$isbn = $this->_xpath->evaluate(
'string(' . $this->getXpathPrefix() . '/jungle:isbn)'
);
if (!$isbn) {
$isbn = null;
}
$this->_data['isbn'] = $isbn;
return $this->_data['isbn'];
}
This extension is easy enough to follow. It creates a new method getIsbn() which runs an XPath query on the
current entry to extract the ISBN number enclosed by the <jungle:isbn> element. It can optionally store this to
the internal non-persistent cache (no need to keep querying the DOM if it's called again on the same entry). The value
is returned to the caller. At the end we have a protected method (it's abstract so it must exist) which registers the Jungle
Books namespace for their custom RSS module. While we call this an RSS module, there's nothing to prevent the same
element being used in Atom feeds - and all Extensions which use the prefix provided by getXpathPrefix() are
actually neutral and work on RSS or Atom feeds with no extra code.
Since this Extension is stored outside of Zend Framework, you'll need to register the path prefix for your Extensions
so Zend_Loader_PluginLoader can find them. After that, it's merely a matter of registering the Extension, if
it's not already loaded, and using it in practice.
447
Zend_Feed
if(!Zend_Feed_Reader::isRegistered('JungleBooks')) {
Zend_Feed_Reader::addPrefixPath(
'/path/to/My/FeedReader/Extension', 'My_FeedReader_Extension'
);
Zend_Feed_Reader::registerExtension('JungleBooks');
}
$feed = Zend_Feed_Reader::import('http://example.com/junglebooks/rss');
// ISBN for whatever book the first entry in the feed was concerned with
$firstIsbn = $feed->current()->getIsbn();
Writing a feed level Extension is not much different. The example feed from earlier included an unmentioned
<jungle:dayPopular> element which Jungle Books have added to their standard to include a link to the day's
most popular book (in terms of visitor traffic). Here's an Extension which adds a getDaysPopularBookLink()
method to the feel level API.
class My_FeedReader_Extension_JungleBooks_Feed
extends Zend_Feed_Reader_Extension_FeedAbstract
{
public function getDaysPopularBookLink()
{
if (isset($this->_data['dayPopular'])) {
return $this->_data['dayPopular'];
}
$dayPopular = $this->_xpath->evaluate(
'string(' . $this->getXpathPrefix() . '/jungle:dayPopular)'
);
if (!$dayPopular) {
$dayPopular = null;
}
$this->_data['dayPopular'] = $dayPopular;
return $this->_data['dayPopular'];
}
Let's repeat the last example using a custom Extension to show the method being used.
if(!Zend_Feed_Reader::isRegistered('JungleBooks')) {
Zend_Feed_Reader::addPrefixPath(
'/path/to/My/FeedReader/Extension', 'My_FeedReader_Extension'
);
Zend_Feed_Reader::registerExtension('JungleBooks');
}
$feed = Zend_Feed_Reader::import('http://example.com/junglebooks/rss');
448
Zend_Feed
// URI to the information page of the day's most popular book with visitors
$daysPopularBookLink = $feed->getDaysPopularBookLink();
// ISBN for whatever book the first entry in the feed was concerned with
$firstIsbn = $feed->current()->getIsbn();
Going through these examples, you'll note that we don't register feed and entry Extensions separately. Extensions
within the same standard may or may not include both a feed and entry class, so Zend_Feed_Reader only requires
you to register the overall parent name, e.g. JungleBooks, DublinCore, Slash. Internally, it can check at what level
Extensions exist and load them up if found. In our case, we have a full set of Extensions now: JungleBooks_Feed
and JungleBooks_Entry.
449
450
Capítulo 21. Zend_File
Zend_File_Transfer
Zend_File_Transfer provides extensive support for file uploads and downloads. It comes with built-in validators
for files plus functionality to change files with filters. Protocol adapters allow Zend_File_Transfer to expose
the same API for transport protocols like HTTP, FTP, WEBDAV and more.
Limitation
The current implementation of Zend_File_Transfer is limited to HTTP Post Uploads. Other adapters
supporting downloads and other protocols will be added in future releases. Unimplemented methods will
throw an exception. For now, you should use Zend_File_Transfer_Adapter_Http directly. As soon
as there are multiple adapters available you can use a common interface.
Forms
When you are using Zend_Form you should use the APIs provided by Zend_Form and not
Zend_File_Transfer directly. The file transfer support in Zend_Form is implemented with
Zend_File_Transfer, so the information in this chapter may be useful for advanced users of
Zend_Form.
The usage of Zend_File_Transfer is relatively simple. It consists of two parts. The HTTP form does the upload,
while the Zend_File_Transfer handles the uploaded files. See the following example:
This example illustrates basic file uploading. The first part is the file form. In our example there is one file to upload.
For convenience, you can use Zend_Form_Element_File instead of building the HTML manually.
The next step is to create the receiver of the upload. In our example the receiver is located at /file/upload. So
next we will create the file controller and the upload action.
$adapter->setDestination('C:\temp');
if (!$adapter->receive()) {
$messages = $adapter->getMessages();
echo implode("\n", $messages);
}
451
Zend_File
This code listing demonstrates the simplest usage of Zend_File_Transfer. A local destination is set with the
setDestination method, then the receive() method is called. if there are any upload errors, an error will be
returned.
Attention
This example is suitable only for demonstrating the basic API of Zend_File_Transfer. You should
never use this code listing in a production environment, because severe security issues may be introduced.
You should always use validators to increase security.
• ignoreNoFile: If this option is set to true, all validators will ignore files that have not been uploaded by the form.
The default value is false which results in an error if no files were specified.
Checking Files
Zend_File_Transfer has several methods that check for various states of the specified file. These are useful if
you must process files after they have been uploaded. These methods include:
• isValid($files = null): This method will check if the given files are valid, based on the validators that are attached to
the files. If no files are specified, all files will be checked. You can call isValid() before calling receive();
in this case, receive() will not call isValid internally again when receiving the file.
• isUploaded($files = null): This method will check if the specified files have been uploaded by the user. This is
useful when you have defined one or more optional files. When no files are specified, all files will be checked.
• isReceived($files = null): This method will check if the given files have already been received. When no files are
specified, all files will be checked.
452
Zend_File
// validators are ok ?
if (!$upload->isValid($file)) {
print "Sorry but $file is not what we wanted";
continue;
}
}
$upload->receive();
• getFileName($file = null, $path = true): This method will return the real file name of a transferred file.
• getFileInfo($file = null): This method will return all internal information for the given file.
• getFileSize($file = null): This method will return the real filesize for the given file.
• getHash($hash = 'crc32', $files = null): This method returns a hash of the content of a given transferred file.
• getMimeType($files = null): This method returns the mimetype of a given transferred file.
getFileName() accepts the name of the element as first parameter. If no name is given, all known filenames will
be returned in an array. If the file is a multifile, you will also get an array. If there is only a single file a string will
be returned.
By default file names will be returned with the complete path. If you only need the file name without path, you can
set the second parameter, $path, which will truncate the file path when set to false.
Nota
Note that the file name can change after you receive the file, because all filters will be applied once the file
is received. So you should always call getFileName() after the files have been received.
getFileSize() returns per default the real filesize in SI notation which means you will get 2kB instead of 2048.
If you need only the plain size set the useByteString option to false.
453
Zend_File
$upload->receive();
// Returns the sizes from all files as array if more than one file was uploaded
$size = $upload->getFileSize();
getHash() accepts the name of a hash algorithm as first parameter. For a list of known algorithms refer to PHP's
hash_algos method [http://php.net/hash_algos]. If you don't specify an algorithm, the crc32 algorithm will be used
by default.
// Returns the hashes from all files as array if more than one file was uploaded
$hash = $upload->getHash('md5');
Nota
Note that if the given file or form name contains more than one file, the returned value will be an array.
getMimeType() returns the mimetype of a file. If more than one file was uploaded it returns an array, otherwise
a string.
$mime = $upload->getMimeType();
Nota
Note that this method uses the fileinfo extension if it is available. If this extension can not be found, it uses
the mimemagic extension. When no extension was found it uses the mimetype given by the fileserver when
the file was uploaded.
454
Zend_File
First, you need to have either APC or uploadprogress to be enabled. Note that you can disable this feature of
APC within your php.ini.
Second, you need to have the proper hidden fields added in the form which sends the files. When you use
Zend_Form_Element_File this hidden fields are automatically added by Zend_Form.
When the above two points are provided then you are able to get the actual progress of the file upload by using the
getProgress method. Actually there are 2 official ways to handle this.
To archive this, you have to add the wished Zend_ProgressBar_Adapter to getProgress() when you are calling
it the first time. For details about the right adapter to use, look into the chapter Zend_ProgressBar Standard Adapters.
Ejemplo 21.7. Using the progressbar adapter to retrieve the actual state
$upload = null;
while (!$upload['done']) {
$upload = Zend_File_Transfer_Adapter_Http:getProgress($upload);
}
Call getProgress() without settings. It will return you an array with several keys. They differ according to the
used PHP extension. But the following keys are given independently of the extension:
• id: The ID of this upload. This ID identifies the upload within the extension. It is filled automatically. You should
never change or give this value yourself.
• done: Returns true when the upload is finished and false otherwise.
• message: The actual message. Eighter the progress as text in the form 10kB / 200kB, or a helpful message in the
case of a problem. Problems could be, that there is no upload in progress, that there was a failure while retrieving
the data for the progress, or that the upload has been canceled.
455
Zend_File
• session: This optional key takes the name of a session namespace which will be used within Zend_ProgressBar.
When this key is not given it defaults to Zend_File_Transfer_Adapter_Http_ProgressBar.
All other returned keys are provided directly from the extensions and will not be checked.
$upload = Zend_File_Transfer_Adapter_Http::getProgress();
while (!$upload['done']) {
$upload = Zend_File_Transfer_Adapter_Http:getProgress($upload);
print "\nActual progress:".$upload['message'];
// do whatever you need
}
• Count: This validator checks for the number of files. A minimum and maximum range can be specified. An error
will be thrown if either limit is crossed.
• Crc32: This validator checks for the crc32 hash value of the content from a file. It is based on the Hash validator
and provides a convenient and simple validator that only supports Crc32.
• ExcludeExtension: This validator checks the extension of files. It will throw an error when an given file has
a defined extension. With this validator, you can exclude defined extensions from being validated.
• ExcludeMimeType: This validator validates the MIME type of files. It can also validate MIME types and will
throw an error if the MIME type of specified file matches.
• Exists: This validator checks for the existence of files. It will throw an error when a specified file does not exist.
• Extension: This validator checks the extension of files. It will throw an error when a specified file has an
undefined extension.
• FilesSize: This validator checks the size of validated files. It remembers internally the size of all checked files
and throws an error when the sum of all specified files exceed the defined size. It also provides minimum and
maximum values.
• ImageSize: This validator checks the size of image. It validates the width and height and enforces minimum and
maximum dimensions.
• IsCompressed: This validator checks whether the file is compressed. It is based on the MimeType validator
and validates for compression archives like zip or arc. You can also limit it to other archives.
• IsImage: This validator checks whether the file is an image. It is based on the MimeType validator and validates
for image files like jpg or gif. You can also limit it to other image types.
• Hash: This validator checks the hash value of the content from a file. It supports multiple algorithms.
456
Zend_File
• Md5: This validator checks for the md5 hash value of the content from a file. It is based on the Hash validator and
provides a convenient and simple validator that only supports Md5.
• MimeType: This validator validates the MIME type of files. It can also validate MIME types and will throw an
error if the MIME type of a specified file does not match.
• NotExists: This validator checks for the existence of files. It will throw an error when an given file does exist.
• Sha1: This validator checks for the sha1 hash value of the content from a file. It is based on the Hash validator
and provides a convenient and simple validator that only supports sha1.
• Size: This validator is able to check files for its file size. It provides a minimum and maximum size range and will
throw an error when either of these thesholds are crossed.
• Upload: This validator is internal. It checks if an upload has resulted in an error. You must not set it, as it's
automatically set by Zend_File_Transfer itself. So you do not use this validator directly. You should only
know that it exists.
• WordCount: This validator is able to check the number of words within files. It provides a minimum and maximum
count and will throw an error when either of these thresholds are crossed.
• isValid($files = null): Checks the specified files using all validators. $files may be either a real
filename, the element's name or the name of the temporary file.
• addValidators(array $validators, $files = null): Adds the specified validators to the stack
of validators. Each entry may be either a validator type/options pair or an array with the key 'validator' specifying
the validator. All other options will be considered validator options for instantiation.
• setValidators(array $validators, $files = null): Overwrites any existing validators with the
validators specified. The validators should follow the syntax for addValidators().
457
Zend_File
// Set a file size with 20 bytes minimum and 20000 bytes maximum
$upload->addValidator('Size', false, array('min' => 20, 'max' => 20000));
// Set a file size with 20 bytes minimum and 20000 bytes maximum and
// a file count in one step
$upload->setValidators(array(
'Size' => array('min' => 20, 'max' => 20000),
'Count' => array('min' => 1, 'max' => 3),
));
// Set a file size with 20000 bytes and limits it only to 'file2'
$upload->addValidator('Size', false, 20000, 'file2');
Normally, you should use the addValidators() method, which can be called multiple times.
Nota
Note that setting the same validator multiple times is allowed, but doing so can lead to issues when using
different options for the same validator.
Last but not least, you can simply check the files using isValid().
458
Zend_File
if ($upload->isValid()) {
print "Validation failure";
}
Nota
Note that isValid() will be called automatically when you receive the files and have not called it
previously.
When validation has failed it is a good idea to get information about the problems found. To get this information,
you can use the methods getMessages() which returns all validation messages as array, getErrors() which
returns all error codes, and hasErrors() which returns true as soon as a validation error has been found.
Count Validator
The Count validator checks for the number of files which are provided. It supports the following option keys:
Nota
When using this option you must give the minimum number of files when calling this validator the first
time; otherwise you will get an error in return.
With this option you can define the minimum number of files you expect to receive.
With this option you can limit the number of files which are accepted but also detect a possible attack when more
files are given than defined in your form.
If you initiate this validator with a string or integer, the value will be used as max. Or you can also use the methods
setMin() and setMax() to set both options afterwards and getMin() and getMax() to retrieve the actual
set values.
Nota
Note that this validator stores the number of checked files internally. The file which exceeds the maximum
will be returned as error.
459
Zend_File
Crc32 Validator
The Crc32 validator checks the content of a transferred file by hashing it. This validator uses the hash extension from
PHP with the crc32 algorithm. It supports the following options:
• *: Sets any key or use a numeric array. The values will be used as hash to validate against.
You can set multiple hashes by using different keys. Each will be checked and the validation will fail only if all
values fail.
// Checks whether the content of the uploaded file has the given hash
$upload->addValidator('Crc32', false, '3b3652f');
ExcludeExtension Validator
The ExcludeExtension validator checks the file extension of the specified files. It supports the following options:
• *: Sets any key or use a numeric array. The values will be used to check whether the given file does not use this
file extension.
• case: Sets a boolean indicating whether validation should be case-sensitive. The default is not case sensitive. Note
that this key can be applied to for all available extensions.
This validator accepts multiple extensions, either as a comma-delimited string, or as an array. You may also use the
methods setExtension(), addExtension(), and getExtension() to set and retrieve extensions.
In some cases it is useful to match in a case-sensitive fashion. So the constructor allows a second parameter called
$case which, if set to true, validates the extension by comparing it with the specified values in a case-sensitive fashion.
// Do not allow files with extension php or exe, but use array notation
$upload->addValidator('ExcludeExtension', false, array('php', 'exe'));
460
Zend_File
Nota
Note that this validator only checks the file extension. It does not check the file's MIME type.
ExcludeMimeType Validator
The ExcludeMimeType validator checks the MIME type of transferred files. It supports the following options:
• *: Sets any key individually or use a numeric array. Sets the MIME type to validate against.
With this option you can define the MIME type of files that are not to be accepted.
• headerCheck: If set to TRUE this option will check the HTTP Information for the file type when the fileInfo or
mimeMagic extensions can not be found. The default value for this option is FALSE.
This validator accepts multiple MIME types, either as a comma-delimited string, or as an array. You may also use the
methods setMimeType(), addMimeType(), and getMimeType() to set and retrieve the MIME types.
// Does not allow MIME type of gif images for all files
$upload->addValidator('ExcludeMimeType', false, 'image/gif');
// Does not allow MIME type of gif and jpg images for all given files
$upload->addValidator('ExcludeMimeType', false, array('image/gif',
'image/jpeg');
// Does not allow MIME type of the group images for all given files
$upload->addValidator('ExcludeMimeType', false, 'image');
The above example shows that it is also possible to disallow groups of MIME types. For example, to disallow all
images, just use 'image' as the MIME type. This can be used for all groups of MIME types like 'image', 'audio', 'video',
'text', etc.
Nota
Note that disallowing groups of MIME types will disallow all members of this group even if this is not
intentional. When you disallow 'image' you will disallow all types of images like 'image/jpeg' or 'image/vasa'.
When you are not sure if you want to disallow all types, you should disallow only specific MIME types
instead of complete groups.
Exists Validator
The Exists validator checks for the existence of specified files. It supports the following options:
• *: Sets any key or use a numeric array to check if the specific file exists in the given directory.
This validator accepts multiple directories, either as a comma-delimited string, or as an array. You may also use the
methods setDirectory(), addDirectory(), and getDirectory() to set and retrieve directories.
461
Zend_File
Nota
Note that this validator checks whether the specified file exists in all of the given directories. The validation
will fail if the file does not exist in any of the given directories.
Extension Validator
The Extension validator checks the file extension of the specified files. It supports the following options:
• *: Sets any key or use a numeric array to check whether the specified file has this file extension.
• case: Sets whether validation should be done in a case-sensitive fashion. The default is no case sensitivity. Note
the this key is used for all given extensions.
This validator accepts multiple extensions, either as a comma-delimited string, or as an array. You may also use the
methods setExtension(), addExtension(), and getExtension() to set and retrieve extension values.
In some cases it is useful to test in a case-sensitive fashion. Therefore the constructor takes a second parameter $case,
which, if set to true, will validate the extension in a case-sensitive fashion.
// Limit the extensions to jpg and png files but use array notation
$upload->addValidator('Extension', false, array('jpg', 'png'));
Nota
Note that this validator only checks the file extension. It does not check the file's MIME type.
462
Zend_File
FilesSize Validator
The FilesSize validator checks for the aggregate size of all transferred files. It supports the following options:
• min: Sets the minimum aggregate file size. This option defines the minimum aggregate file size to be transferred.
This option limits the aggregate file size of all transferred files, but not the file size of individual files.
• bytestring: Defines whether a failure is to return a user-friendly number or the plain file size.
This option defines whether the user sees '10864' or '10MB'. The default value is true, so '10MB' is returned if you
did not specify otherwise.
You can initialize this validator with a string, which will then be used to set the max option. You can also use the
methods setMin() and setMax() to set both options after construction, along with getMin() and getMax()
to retrieve the values that have been set previously.
The size itself is also accepted in SI notation as handled by most operating systems. That is, instead of specifying
20000 bytes, 20kB may be given. All file sizes are converted using 1024 as the base value. The following Units are
accepted: kB, MB, GB, TB, PB and EB. Note that 1kB is equal to 1024 bytes, 1MB is equal to 1024kB, and so on.
// Limit the size of all files to be uploaded to maximum 4MB and mimimum 10kB
$upload->addValidator('FilesSize',
false,
array('min' => '10kB', 'max' => '4MB'));
// As before, but returns the plain file size instead of a user-friendly string
$upload->addValidator('FilesSize',
false,
array('min' => '10kB',
'max' => '4MB',
'bytestring' => false));
Nota
Note that this validator internally stores the file size of checked files. The file which exceeds the size will
be returned as an error.
ImageSize Validator
The ImageSize validator checks the size of image files. It supports the following options:
463
Zend_File
The methods setImageMin() and setImageMax() also set both minimum and maximum values, while the
methods getMin() and getMax() return the currently set values.
For your convenience there are also the setImageWidth() and setImageHeight() methods, which set the
minimum and maximum height and width of the image file. They, too, have corresponding getImageWidth() and
getImageHeight() methods to retrieve the currently set values.
To bypass validation of a particular dimension, the relevent option simply should not be set.
IsCompressed Validator
The IsCompressed validator checks if a transferred file is a compressed archive, such as zip or arc. This validator
is based on the MimeType validator and supports the same methods and options. You may also limit this validator
to particular compression types with the methods described there.
Nota
Note that there is no check if you set a MIME type that is not a archive. For example, it would be possible
to define gif files to be accepted by this validator. Using the 'MimeType' validator for files which are not
archived will result in more readable code.
464
Zend_File
IsImage Validator
The IsImage validator checks if a transferred file is a image file, such as gif or jpeg. This validator is based on the
MimeType validator and supports the same methods and options. You can limit this validator to particular image
types with the methods described there.
Nota
Note that there is no check if you set a MIME type that is not an image. For example, it would be possible
to define zip files to be accepted by this validator. Using the 'MimeType' validator for files which are not
images will result in more readable code.
Hash Validator
The Hash validator checks the content of a transferred file by hashing it. This validator uses the hash extension from
PHP. It supports the following options:
• *: Takes any key or use a numeric array. Sets the hash to validate against.
You can set multiple hashes by passing them as an array. Each file is checked, and the validation will fail only if
all files fail validation.
You can set multiple algorithm by calling the addHash() method multiple times.
// Checks if the content of the uploaded file contains the given hash
$upload->addValidator('Hash', false, '3b3652f');
465
Zend_File
array('315b3cd8273d44912a7',
'algorithm' => 'md5'));
Nota
This validator supports about 34 different hash algorithms. The most common include 'crc32', 'md5' and
'sha1'. A comprehesive list of supports hash algorithms can be found at the hash_algos method [http://php.net/
hash_algos] on the php.net site [http://php.net].
Md5 Validator
The Md5 validator checks the content of a transferred file by hashing it. This validator uses the hash extension for PHP
with the md5 algorithm. It supports the following options:
You can set multiple hashes by passing them as an array. Each file is checked, and the validation will fail only if
all files fail validation.
// Checks if the content of the uploaded file has the given hash
$upload->addValidator('Md5', false, '3b3652f336522365223');
MimeType Validator
The MimeType validator checks the MIME type of transferred files. It supports the following options:
• *: Sets any key or use a numeric array. Sets the MIME type to validate against.
• headerCheck: If set to TRUE this option will check the HTTP Information for the file type when the fileInfo or
mimeMagic extensions can not be found. The default value for this option is FALSE.
With this option you can define which magicfile to use. When it's not set or empty, the MAGIC constant will be
used instead. This option is available since Zend Framework 1.7.1.
This validator accepts multiple MIME type, either as a comma-delimited string, or as an array. You may also use the
methods setMimeType(), addMimeType(), and getMimeType() to set and retrieve MIME type.
You can also set the magicfile which shall be used by fileinfo with the 'magicfile' option. Additionally there are
convenient setMagicFile() and getMagicFile() methods which allow later setting and retrieving of the
magicfile parameter. This methods are available since Zend Framework 1.7.1.
466
Zend_File
// Limit the MIME type of all given files to gif and jpeg images
$upload->addValidator('MimeType', false, array('image/gif', 'image/jpeg');
// Limit the MIME type of all given files to the group images
$upload->addValidator('MimeType', false, 'image');
The above example shows that it is also possible to limit the accepted MIME type to a group of MIME types. To
allow all images just use 'image' as MIME type. This can be used for all groups of MIME types like 'image', 'audio',
'video', 'text, and so on.
Nota
Note that allowing groups of MIME types will accept all members of this group even if your application does
not support them. When you allow 'image' you will also get 'image/xpixmap' or 'image/vasa' which could
be problematic. When you are not sure if your application supports all types you should better allow only
defined MIME types instead of the complete group.
Nota
This component will use the fileinfo extension if it is available. If it's not, it will degrade to the
mime_content_type function. And if the function call fails it will use the MIME type which is given
by HTTP.
You should be aware of possible security problems when you have whether fileinfo nor
mime_content_type available. The MIME type given by HTTP is not secure and can be easily
manipulated.
NotExists Validator
The NotExists validator checks for the existence of the provided files. It supports the following options:
• *: Set any key or use a numeric array. Checks whether the file exists in the given directory.
This validator accepts multiple directories either as a comma-delimited string, or as an array. You may also use the
methods setDirectory(), addDirectory(), and getDirectory() to set and retrieve directories.
467
Zend_File
Nota
Note that this validator checks if the file does not exist in all of the provided directories. The validation will
fail if the file does exist in any of the given directories.
Sha1 Validator
The Sha1 validator checks the content of a transferred file by hashing it. This validator uses the hash extension for
PHP with the sha1 algorithm. It supports the following options:
You can set multiple hashes by passing them as an array. Each file is checked, and the validation will fail only if
all files fail validation.
// Checks if the content of the uploaded file has the given hash
$upload->addValidator('sha1', false, '3b3652f336522365223');
Size Validator
The Size validator checks for the size of a single file. It supports the following options:
• bytestring: Defines whether a failure is returned with a user-friendly number, or with the plain file size.
With this option you can define if the user gets '10864' or '10MB'. Default value is true which returns '10MB'.
You can initialize this validator with a string, which will then be used to set the max option. You can also use the
methods setMin() and setMax() to set both options after construction, along with getMin() and getMax()
to retrieve the values that have been set previously.
468
Zend_File
The size itself is also accepted in SI notation as handled by most operating systems. That is, instead of specifying
20000 bytes, 20kB may be given. All file sizes are converted using 1024 as the base value. The following Units are
accepted: kB, MB, GB, TB, PB and EB. Note that 1kB is equal to 1024 bytes, 1MB is equal to 1024kB, and so on.
// Limit the size a given file to maximum 4MB and mimimum 10kB
// Also returns the plain number in case of an error
// instead of a user-friendly number
$upload->addValidator('Size',
false,
array('min' => '10kB',
'max' => '4MB',
'bytestring' => false));
WordCount Validator
The WordCount validator checks for the number of words within provided files. It supports the following option keys:
If you initiate this validator with a string or integer, the value will be used as max. Or you can also use the methods
setMin() and setMax() to set both options afterwards and getMin() and getMax() to retrieve the actual
set values.
// Limit the amount of words within files to maximum 5000 and minimum 1000 words
$upload->addValidator('WordCount', false, array('min' => 1000, 'max' => 5000));
469
Zend_File
• Rename: This filter can rename files, change the location and even force overwriting of existing files.
• addFilter($filter, $options = null, $files = null): Adds the given filter to the filter stack
(optionally only to the file(s) specified). $filter may be either an actual filter instance, or a short name specifying
the filter type (e.g., 'Rename').
• addFilters(array $filters, $files = null): Adds the given filters to the stack of filters. Each
entry may be either a filter type/options pair, or an array with the key 'filter' specifying the filter (all other options
will be considered filter options for instantiation).
• setFilters(array $filters, $files = null): Overwrites any existing filters with the filters
specified. The filters should follow the syntax for addFilters().
• getFilters($files = null): Returns registered filters; if $files is passed, returns filters for that
particular file or set of files.
470
Zend_File
Generally you should simply use the addFilters() method, which can be called multiple times.
Nota
Note that even though setting the same filter multiple times is allowed, doing so can lead to issues when using
different options for the same filter.
Decrypt filter
The Decrypt filter allows to decrypt a encrypted file.
This filter makes use of Zend_Filter_Decrypt. It supports the Mcrypt and OpenSSL extensions from PHP.
Please read the related section for details about how to set the options for decryption and which options are supported.
This filter supports one additional option which can be used to save the decrypted file with another filename. Set the
filename option to change the filename where the decrypted file will be stored. If you suppress this option, the
decrypted file will overwrite the original encrypted file.
471
Zend_File
Encrypt filter
The Encrypt filter allows to encrypt a file.
This filter makes use of Zend_Filter_Encrypt. It supports the Mcrypt and OpenSSL extensions from PHP.
Please read the related section for details about how to set the options for encryption and which options are supported.
This filter supports one additional option which can be used to save the encrypted file with another filename. Set the
filename option to change the filename where the encrypted file will be stored. If you suppress this option, the
encrypted file will overwrite the original file.
LowerCase filter
The LowerCase filter allows to change the content of a file to lowercase. You should use this filter only on textfiles.
At initiation you can give a string which will then be used as encoding. Or you can use the setEncoding() method
to set it afterwards.
// Adds a filter to lowercase the uploaded file but only for uploadfile1
$upload->addFilter('LowerCase', null, 'uploadfile1');
472
Zend_File
Nota
Note that due to the fact that the options for the LowerCase filter are optional, you must give a null as second
parameter (the options) when you want to limit it to a single file element.
Rename filter
The Rename filter allows to change the destination of the upload, the filename and also to overwrite existing files.
It supports the following options:
• source: The name and destination of the old file which shall be renamed.
• overwrite: Sets if the old file overwrites the new one if it already exists. The default value is false.
Additionally you can also use the method setFile() to set files, which erases all previous set, addFile() to add
a new file to existing ones, and getFile() to get all actually set files. To simplify things, this filter understands
several notations and that methods and constructor understand the same notations.
You can use different notations. Below is a table where you will find a description and the intention for the supported
notations. Note that when you use the Adapter or the Form Element you will not be able to use all described notations.
Tabla 21.1. Different notations of the rename filter and their meaning
notation description
addFile('C:\uploads') Specifies a new location for all files when the given
string is a directory. Note that you will get an exception
when the file already exists, see the overwriting
parameter.
addFile('C:\uploads\file.ext') Specifies a new location and filename for all files when
the given string is not detected as directory. Note that
you will get an exception when the file already exists,
see the overwriting parameter.
addFile(array('C:\uploads\file.ext', Specifies a new location and filename for all files
'overwrite' => true)) when the given string is not detected as directory and
overwrites an existing file with the same target name.
Note, that you will get no notification that a file was
overwritten.
473
Zend_File
notation description
addFile(array('source' => 'C:\temp Specifies a new location for all files in the old location
\uploads', 'target' => 'C:\uploads')) when the given strings are detected as directory. Note
that you will get an exception when the file already
exists, see the overwriting parameter.
addFile(array('source' => 'C:\temp\uploads', 'target' => Specifies a new location for all files in the old location
'C:\uploads', 'overwrite' => true)) when the given strings are detected as directory and
overwrites and existing file with the same target name.
Note, that you will get no notification that a file was
overwritten.
UpperCase filter
The UpperCase filter allows to change the content of a file to uppercase. You should use this filter only on textfiles.
At initiation you can give a string which will then be used as encoding. Or you can use the setEncoding() method
to set it afterwards.
// Adds a filter to uppercase the uploaded file but only for uploadfile1
$upload->addFilter('UpperCase', null, 'uploadfile1');
Nota
Note that due to the fact that the options for the UpperCase filter are optional, you must give a null as second
parameter (the options) when you want to limit it to a single file element.
474
Capítulo 22. Zend_Filter
Introducción
El componente Zend_Filter proporciona un conjunto de filtros de datos comúnmente necesarios. También
proporciona un sencillo mecanismo de encadenar varios filtros que se puedan aplicar a un solo dato en un orden
definido por el usuario.
¿Qué es un filtro?
En el mundo físico, un filtro se suele utilizar para eliminar partes no deseadas de lo ingresado, y la vez lo ingresado
pasa a través de un filtro de salida (por ejemplo, el café). En este caso, un filtro es un operador que devuelve una parte
de los datos de entrada. Este tipo de filtro es útil para aplicaciones web, para la supresión de entradas ilegales y/o que
no se ajustan, eliminación de los espacios en blanco innecesarios, etc
This basic definition of a filter may be extended to include generalized transformations upon input. A common
transformation applied in web applications is the escaping of HTML entities. For example, if a form field is
automatically populated with untrusted input (e.g., from a web browser), this value should either be free of HTML
entities or contain only escaped HTML entities, in order to prevent undesired behavior and security vulnerabilities. To
meet this requirement, HTML entities that appear in the input must either be removed or escaped. Of course, which
approach is more appropriate depends on the situation. A filter that removes the HTML entities operates within the
scope of the first definition of filter - an operator that produces a subset of the input. A filter that escapes the HTML
entities, however, transforms the input (e.g., "&" is transformed to "&"). Supporting such use cases for web
developers is important, and "to filter," in the context of using Zend_Filter, means to perform some transformations
upon input data.
Esta definición básica de un filtro puede ser extendido para incluir transformaciones generalizadas sobre la entrada.
Una transformación común requerida en las aplicaciones web es la de escapar las entidades HTML. Por ejemplo, si un
campo del formulario es completado automáticamente y contiene datos no verificados (por ejemplo, datos ingresados
desde un navegador web), este valor debe estar libre de las entidades HTML o sólo contener entidades HTML de
forma escapada, a fin de evitar comportamientos no deseados y vulnerabilidades de seguridad. Para cumplir este
requerimiento, las entidades HTML que aparecen en los datos introducidos deben ser suprimidos o escapados. Por
supuesto, el enfoque más adecuado depende del contexto y de la situción. Un filtro que quita las entidades HTML
funciona dentro del ámbito o alcance de la primera definición del filtro - un operador que produce un subconjunto
de la entrada. Un filtro que escapa a las entidades HTML, sin embargo, transforma la entrada (por ejemplo, " &"
se transforma en " &"). El Apoyo a los casos de uso como para la web los desarrolladores es importante, y
"filtrar", en el contexto de la utilización de Zend_Filter , los medios para realizar algunas transformaciones
en los datos de entrada.
Following is a basic example of using a filter upon two input data, the ampersand (&) and double quote (") characters:
475
Zend_Filter
You can also pass an array of constructor arguments, if they are needed for the filter class.
The static usage can be convenient for invoking a filter ad hoc, but if you have the need to run a filter for multiple
inputs, it's more efficient to follow the first example above, creating an instance of the filter object and calling its
filter() method.
Also, the Zend_Filter_Input class allows you to instantiate and run multiple filter and validator classes on
demand to process sets of input data. See the section called “Zend_Filter_Input”.
Namespaces
When working with self defined filters you can give a forth parameter to Zend_Filter::filterStatic()
which is the namespace where your filter can be found.
echo Zend_Filter::filterStatic(
'"',
'MyFilter',
array($parameters),
array('FirstNamespace', 'SecondNamespace')
);
Zend_Filter allows also to set namespaces as default. This means that you can set them once in your bootstrap
and have not to give them again for each call of Zend_Filter::filterStatic(). The following code snippet
is identical to the above one.
Zend_Filter::setDefaultNamespaces(array('FirstNamespace', 'SecondNamespace'));
echo Zend_Filter::filterStatic('"', 'MyFilter', array($parameters));
echo Zend_Filter::filterStatic('"', 'OtherFilter', array($parameters));
For your convinience there are following methods which allow the handling of namespaces:
• Zend_Filter::setDefaultNamespaces(): Sets new default namespaces and overrides any previous set.
It accepts eighter a string for a single namespace of an array for multiple namespaces.
• Zend_Filter::hasDefaultNamespaces(): Returns true when one or more default namespaces are set,
and false when no default namespaces are set.
476
Zend_Filter
Alnum
Returns the string $value, removing all but alphabetic and digit characters. This filter includes an option to also
allow white space characters.
Nota
The alphabetic characters mean characters that makes up words in each language. However, the english
alphabet is treated as the alphabetic characters in following languages: Chinese, Japanese, Korean. The
language is specified by Zend_Locale.
Alpha
Returns the string $value, removing all but alphabetic characters. This filter includes an option to also allow white
space characters.
BaseName
Given a string containing a path to a file, this filter will return the base name of the file
Callback
This filter allows you to use own methods in conjunction with Zend_Filter. You don't have to create a new filter
when you already have a method which does the job.
print $filter->filter('Hello!');
// returns "!olleH"
As you can see it's really simple to use a callback to define a own filter. It is also possible to use a method, which is
defined within a class, by giving an array as callback.
// Our classdefinition
class MyClass
{
public function Reverse($param);
}
To get the actual set callback use getCallback() and to set another callback use setCallback().
477
Zend_Filter
It is also possible to define default parameters, which are given to the called method as array when the filter is executed.
This array will be concatenated with the value which will be filtered.
When you would call the above method definition manually it would look like this:
Nota
You should note that defining a callback method which can not be called will raise an exception.
• Bz2
• Gz
• Lzf
• Rar
• Tar
• Zip
Each compression format has different capabilities as described below. All compression filters may be used in
approximately the same ways, and differ primarily in the options available and the type of compression they offer
(both algorithmically as well as string vs. file vs. directory)
Generic handling
To create a compression filter you need to select the compression format you want to use. The following description
takes the Bz2 adapter. Details for all other adapters are described after this section.
The two filters are basically identical, in that they utilize the same backends. Zend_Filter_Compress should
be used when you wish to compress items, and Zend_Filter_Decompress should be used when you wish to
decompress items.
For instance, if we want to compress a string, we have to initiate Zend_Filter_Compress and indicate the desired
adapter.
478
Zend_Filter
You may also provide an array of options or Zend_Config object. If you do, provide minimally the key "adapter",
and then either the key "options" or "adapterOptions" (which should be an array of options to provide to the adapter
on instantiation).
Almost the same usage is we want to decompress a string. We just have to use the decompression filter in this case.
To get the compressed string, we have to give the original string. The filtered value is the compressed version of the
original string.
Creating an archive
Creating an archive file works almost the same as compressing a string. However, in this case we need an additional
parameter which holds the name of the archive we want to create.
479
Zend_Filter
In the above example the uncompressed string is compressed, and is then written into the given archive file.
When you want to compress a file, then you must give the name of the file with its path.
You may also specify a directory instead of a filename. In this case the whole directory with all its files and
subdirectories will be compressed into the archive.
Decompressing an archive
Decompressing an archive file works almost like compressing it. You must specify either the archive parameter, or
give the filename of the archive when you decompress the file.
Some adapters support decompressing the archive into another subdirectory. In this case you can set the target
parameter.
480
Zend_Filter
Bz2 Adapter
The Bz2 Adapter can compress and decompress:
• Strings
• Files
• Directories
• Archive: This parameter sets the archive file which should be used or created.
• Blocksize: This parameter sets the blocksize to use. It can be from '0' to '9'. The default value is '4'.
All options can be set at instantiation or by using a related method. For example, the related methods for 'Blocksize'
are getBlocksize() and setBlocksize(). You can also use the setOptions() method which accepts all
options as array.
Gz Adapter
The Gz Adapter can compress and decompress:
• Strings
• Files
• Directories
• Archive: This parameter sets the archive file which should be used or created.
• Level: This compression level to use. It can be from '0' to '9'. The default value is '9'.
• Mode: There are two supported modes. 'compress' and 'deflate'. The default value is 'compress'.
All options can be set at initiation or by using a related method. For example, the related methods for 'Level' are
getLevel() and setLevel(). You can also use the setOptions() method which accepts all options as array.
Lzf Adapter
The Lzf Adapter can compress and decompress:
481
Zend_Filter
• Strings
Rar Adapter
The Rar Adapter can compress and decompress:
• Files
• Directories
• Archive: This parameter sets the archive file which should be used or created.
• Target: The target where the decompressed files will be written to.
All options can be set at instantiation or by using a related method. For example, the related methods for 'Target' are
getTarget() and setTarget(). You can also use the setOptions() method which accepts all options as
array.
Tar Adapter
The Tar Adapter can compress and decompress:
• Files
• Directories
482
Zend_Filter
• Archive: This parameter sets the archive file which should be used or created.
• Mode: A mode to use for compression. Supported are either 'null' which means no compression at all, 'Gz' which
makes use of PHP's Zlib extension and 'Bz2' which makes use of PHP's Bz2 extension. The default value is 'null'.
• Target: The target where the decompressed files will be written to.
All options can be set at instantiation or by using a related method. For example, the related methods for 'Target' are
getTarget() and setTarget(). You can also use the setOptions() method which accepts all options as
array.
Directory usage
When compressing directories with Tar then the complete file path is used. This means that created Tar files
will not only have the subdirectory but the complete path for the compressed file.
Zip Adapter
The Zip Adapter can compress and decompress:
• Strings
• Files
• Directories
• Archive: This parameter sets the archive file which should be used or created.
• Target: The target where the decompressed files will be written to.
All options can be set at instantiation or by using a related method. For example, the related methods for 'Target' are
getTarget() and setTarget(). You can also use the setOptions() method which accepts all options as
array.
Decrypt
This filter will decrypt any given string with the provided setting. Therefor it makes use of Adapters. Actually there
are adapters for the Mcrypt and OpenSSL extensions from php.
For details about how to encrypt content look at the Encrypt filter. As the basics are covered within the Encrypt
filter, we will describe here only the needed additional methods and changes for decryption.
483
Zend_Filter
There is one emminent difference for you. When you did not provide a vector at encryption you need to get it after
you encrypted the content by using the getVector() method on the encryption filter. Without the correct vector
you will not be able to decrypt the content.
$decrypted = $filter->filter('encoded_text_normally_unreadable');
print $decrypted;
Nota
Note that you will get an exception if the mcrypt extension is not available in your environment.
Nota
You should also note that all settings which be checked when you create the instance or when you call
setEncryption(). If mcrypt detects problem with your settings an exception will be thrown.
• private: Your private key which will be used for decrypting the content. The private key can be eighter a filename
with path of the key file, or just the content of the key file itself.
• envelope: The encrypted envelope key from the user who encrypted the content. You can eigther provide the path
and filename of the key file, or just the content of the key file itself.
Nota
Note that the OpenSSL adapter will not work when you do not provide valid keys.
484
Zend_Filter
Optionally it could be necessary to provide the passphrase for decrypting the keys themself by using the
setPassphrase() method.
At last, decode the content. Our complete example for decrypting the previously encrypted content looks like this.
$decrypted = $filter->filter('encoded_text_normally_unreadable');
print $decrypted;
Digits
Returns the string $value, removing all but digit characters.
Dir
Returns directory name component of path.
Encrypt
This filter will encrypt any given string with the provided setting. Therefor it makes use of Adapters. Actually there
are adapters for the Mcrypt and OpenSSL extensions from php.
As these two encryption methodologies work completely different, also the usage of the adapters differ. You have to
select the adapter you want to use when initiating the filter.
485
Zend_Filter
To set another adapter you can also use setAdapter(), and the getAdapter() method to receive the actual
set adapter.
Nota
When you do not supply the adapter option or do not use setAdapter, then the Mcrypt adapter will be
used per default.
• key: The encryption key with which the input will be encrypted. You need the same key for decryption.
• algorithm: The algorithm which has to be used. It should be one of the algorithm ciphers which can be found under
PHP's mcrypt ciphers [http://php.net/mcrypt]. If not set it defaults to blowfish.
• algorithm_directory: The directory where the algorithm can be found. If not set it defaults to the path set within
the mcrypt extension.
• mode: The encryption mode which has to be used. It should be one of the modes which can be found under PHP's
mcrypt modes [http://php.net/mcrypt]. If not set it defaults to cbc.
• mode_directory: The directory where the mode can be found. If not set it defaults to the path set within the mcrypt
extension.
• vector: The initialization vector which shall be used. If not set it will be a random vector.
• salt: If the key should be used as salt value. The key used for encryption will then itself also be encrypted. Default
is false.
If you give a string instead of an array, this string will be used as key.
You can get/set the encryption values also afterwards with the getEncryption() and setEncryption()
methods.
Nota
Note that you will get an exception if the mcrypt extension is not available in your environment.
Nota
You should also note that all settings which be checked when you create the instance or when you call
setEncryption(). If mcrypt detects problem with your settings an exception will be thrown.
You can get/set the encryption vector by calling getVector() and setVector(). A given string will be truncated
or padded to the needed vector size of the used algorithm.
486
Zend_Filter
Nota
Note that when you are not using an own vector, you must get the vector and store it. Otherwise you will
not be able to decode the encoded string.
$encrypted = $filter->filter('text_to_be_encoded');
print $encrypted;
• public: The public key of the user whom you want to provide the encrpted content. You can give multiple public
keys by using an array. You can eigther provide the path and filename of the key file, or just the content of the
key file itself.
• private: Your private key which will be used for encrypting the content. Also the private key can be eighter a
filename with path of the key file, or just the content of the key file itself.
You can get/set the public keys also afterwards with the getPublicKey() and setPublicKey() methods. The
private key can also be get and set with the related getPrivateKey() and setPrivateKey() methods.
Nota
Note that the OpenSSL adapter will not work when you do not provide valid keys.
When you want to encode also the keys, then you have to provide a passphrase with the setPassphrase() method.
When you want to decode content which was encoded with a passphrase you will not only need the public key, but
also the passphrase to decode the encrypted key.
487
Zend_Filter
At last, when you use OpenSSL you need to give the receiver the encrypted content, the passphrase when have provided
one, and the envelope keys for decryption.
This means for you, that you have to get the envelope keys after the encryption with the getEnvelopeKey()
method.
So our complete example for encrypting content with OpenSSL look like this.
$encrypted = $filter->filter('text_to_be_encoded');
$envelope = $filter->getEnvelopeKey();
print $encrypted;
HtmlEntities
Returns the string $value, converting characters to their corresponding HTML entity equivalents where they exist.
Int
Returns (int) $value
LocalizedToNormalized
This filter will change any given localized input to it's normalized representation. It uses in Background
Zend_Locale to do this transformation for you.
488
Zend_Filter
This allows your user to enter informations in his own language notation, and you can then store the normalized value
into your database for example.
Nota
Please note that normalization is not equal to translation. This filter can not translate strings from one language
into another like you could expect with months or names of days.
• integer: Integer numbers, which are localized, will be normalized to the english notation.
• float: Float numbers, which are localized, will be normalized to the english notation.
• numbers: Other numbers, like real, will be normalized to the english notation.
Nota
You should note that normalized output is always given as string. Otherwise your environment would transfer
the normalized output automatically to the notation used by the locale your environment is set to.
Let's expect you have set the locale 'de' as application wide locale. Zend_Filter_LocalizedToNormalized
will take the set locale and use it to detect which sort of input you gave. In our example it was a value with precision.
Now the filter will return you the normalized representation for this value as string.
You can also control how your normalized number has to look like. Therefor you can give all options which are also
used by Zend_Locale_Format. The most common are:
• date_format
• locale
• precision
For details about how these options are used take a look into this Zend_Locale chapter.
Below is a example with defined precision so you can see how options work:
489
Zend_Filter
// Numeric Filter
$filter = new Zend_Filter_LocalizedToNormalized(array('precision' => 2));
$filter->filter('123.456');
// returns the value '123456.00'
$filter->filter('123.456,78901');
// returns the value '123456.79'
Let's expect you have set the locale 'de' again. Now the input is automatically detected as date, and you will get a
named array in return.
Of course you can also control how your date input looks like with the date_format and the locale option.
// Date Filter
$filter = new Zend_Filter_LocalizedToNormalized(
array('date_format' => 'ss:mm:HH')
);
$filter->filter('11:22:33');
// returns array('hour' => '33', 'minute' => '22', 'second' => '11')
NormalizedToLocalized
This filter is the reverse of the filter Zend_Filter_LocalizedToNormalized and will change any given
normalized input to it's localized representation. It uses in Background Zend_Locale to do this transformation for
you.
This allows you to give your user any stored normalised value in a localized manner, your user is more common to.
Nota
Please note that localization is not equal to translation. This filter can not translate strings from one language
into another like you could expect with months or names of days.
• integer: Integer numbers, which are normalized, will be localized to the set notation.
• float: Float numbers, which are normalized, will be localized to the set notation.
• numbers: Other numbers, like real, will be localized to the set notation.
490
Zend_Filter
Let's expect you have set the locale 'de' as application wide locale. Zend_Filter_NormalizedToLocalized
will take the set locale and use it to detect which sort of output you want to have. In our example it was a value with
precision. Now the filter will return you the localized representation for this value as string.
You can also control how your localized number has to look like. Therefor you can give all options which are also
used by Zend_Locale_Format. The most common are:
• date_format
• locale
• precision
For details about how these options are used take a look into this Zend_Locale chapter.
Below is a example with defined precision so you can see how options work:
// Numeric Filter
$filter = new Zend_Filter_NormalizedToLocalized(array('precision' => 2));
$filter->filter(123456);
// returns the value '123.456,00'
$filter->filter(123456.78901);
// returns the value '123.456,79'
491
Zend_Filter
Let's expect you have set the locale 'de' again. Now the input is automatically detected as date, and will be returned
in the format defined by the locale 'de'.
Of course you can also control how your date input looks like with the date_format, and the locale option.
// Date Filter
$filter = new Zend_Filter_LocalizedToNormalized(
array('date_format' => 'ss:mm:HH')
);
Null
This filter will change the given input to be NULL if it meets specific criteria. This is often necessary when you work
with databases and want to have a NULL value instead of a boolean or any other type.
This means that without providing any configuration, Zend_Filter_Null will accept all input types and return
NULL in the same cases as empty().
• zero: Converts a string containing the single character zero ('0') to NULL.
• all: Converts all above types to NULL. (This is the default behavior.)
There are several ways to select which of the above types are filtered. You can give one or multiple types and add
them, you can give an array, you can use constants, or you can give a textual string. See the following examples:
492
Zend_Filter
You can also give an instance of Zend_Config to set the wished types. To set types afterwards use setType().
StripNewlines
Returns the string $value without any newline control characters.
RealPath
This filter will resolve given links and pathnames and returns canonicalized absolute pathnames. References to '/./',
'/../' and extra '/' characters in the input path will be stripped. The resulting path will not have any symbolic
link, '/./' or '/../' character.
Zend_Filter_RealPath will return FALSE on failure, e.g. if the file does not exist. On BSD systems
Zend_Filter_RealPath doesn't fail if only the last path component doesn't exist, while other systems will return
FALSE.
// returns '/www/mypath'
Sometimes it is useful to get also paths when they don't exist, f.e. when you want to get the real path for a path which
you want to create. You can then either give a FALSE at initiation, or use setExists() to set it.
// returns '/www/non/existing/path'
// even when file_exists or realpath would return false
493
Zend_Filter
StringToLower
This filter converts any input to be lowercased.
print $filter->filter('SAMPLE');
// returns "sample"
Per default it will only handle characters from the actual locale of your server. Characters from other charsets would
be ignored. Still, it's possible to also lowercase them when the mbstring extension is available in your environment.
Simply set the wished encoding when initiating the StringToLower filter. Or use the setEncoding() method
to change the encoding afterwards.
// using UTF-8
$filter = new Zend_Filter_StringToLower('UTF-8');
// or do this afterwards
$filter->setEncoding('ISO-8859-1');
Also when you are trying to set an encoding which is not supported by your mbstring extension you will
get an exception.
StringToUpper
This filter converts any input to be uppercased.
print $filter->filter('Sample');
// returns "SAMPLE"
Like the StringToLower filter, this filter handles only characters from the actual locale of your server. Using
different character sets works the same as with StringToLower.
// or do this afterwards
$filter->setEncoding('ISO-8859-1');
StringTrim
Returns the string $value with characters stripped from the beginning and end.
494
Zend_Filter
StripTags
This filter returns the input string, with all HTML and PHP tags stripped from it, except those that have been explicitly
allowed. In addition to the ability to specify which tags are allowed, developers can specify which attributes are allowed
across all allowed tags and for specific tags only. Finally, this filter offers control over whether comments (e.g., <!-- ...
-->) are removed or allowed.
Filter Chains
Often multiple filters should be applied to some value in a particular order. For example, a login form accepts a
username that should be only lowercase, alphabetic characters. Zend_Filter provides a simple method by which
filters may be chained together. The following code illustrates how to chain together two filters for the submitted
username:
Filters are run in the order they were added to Zend_Filter. In the above example, the username is first removed
of any non-alphabetic characters, and then any uppercase characters are converted to lowercase.
Writing Filters
Zend_Filter supplies a set of commonly needed filters, but developers will often need to write custom
filters for their particular use cases. The task of writing a custom filter is facilitated by implementing
Zend_Filter_Interface.
Zend_Filter_Interface defines a single method, filter(), that may be implemented by user classes. An
object that implements this interface may be added to a filter chain with Zend_Filter::addFilter().
return $valueFiltered;
}
}
495
Zend_Filter
Zend_Filter_Input
Zend_Filter_Input provides a declarative interface to associate multiple filters and validators, apply them to
collections of data, and to retrieve input values after they have been processed by the filters and validators. Values are
returned in escaped format by default for safe HTML output.
Consider the metaphor that this class is a cage for external data. Data enter the application from external sources, such
as HTTP request parameters, HTTP headers, a web service, or even read from a database or another file. Data are first
put into the cage, and subsequently the application can access data only by telling the cage what the data should be
and how they plan to use it. The cage inspects the data for validity. It might apply escaping to the data values for the
appropriate context. The cage releases data only if it can fulfill these responsibilities. With a simple and convenient
interface, it encourages good programming habits and makes developers think about how data are used.
• Filters transform input values, by removing or changing characters within the value. The goal is to "normalize"
input values until they match an expected format. For example, if a string of numeric digits is needed, and the input
value is "abc123", then it might be a reasonable transformation to change the value to the string "123".
• Validators check input values against criteria and report whether they passed the test or not. The value is not changed,
but the check may fail. For example, if a string must look like an email address, and the input value is "abc123",
then the value is not considered valid.
• Escapers transform a value by removing magic behavior of certain characters. In some output contexts, special
characters have meaning. For example, the characters '<' and '>' delimit HTML tags, and if a string containing those
characters is output in an HTML context, the content between them might affect the output or functionality of the
HTML presentation. Escaping the characters removes the special meaning, so they are output as literal characters.
The following sections describe the steps for using this class.
The following example filter rule set that declares the field 'month' is filtered by Zend_Filter_Digits, and the
field 'account' is filtered by Zend_Filter_StringTrim. Then a validation rule set declares that the field 'account'
is valid only if it contains only alphabetical characters.
$filters = array(
'month' => 'Digits',
'account' => 'StringTrim'
);
$validators = array(
'account' => 'Alpha'
496
Zend_Filter
);
Each key in the $filters array above is the name of a rule for applying a filter to a specific data field. By default,
the name of the rule is also the name of the input data field to which to apply the rule.
$validators = array(
'month' => 'Digits',
);
$validators = array(
'month' => $digits
);
• An array, to declare a chain of filters or validators. The elements of this array can be strings mapping to class names
or filter/validator objects, as in the cases described above. In addition, you can use a third choice: an array containing
a string mapping to the class name followed by arguments to pass to its constructor.
$validators = array(
'month' => array(
'Digits', // string
new Zend_Validate_Int(), // object instance
array('Between', 1, 12) // string with constructor arguments
)
);
Nota
If you declare a filter or validator with constructor arguments in an array, then you must make an array for
the rule, even if the rule has only one filter or validator.
You can use a special "wildcard" rule key '*' in either the filters array or the validators array. This means that the
filters or validators declared in this rule will be applied to all input data fields. Note that the order of entries in the
filters array or validators array is significant; the rules are applied in the same order in which you declare them.
$filters = array(
'*' => 'StringTrim',
'month' => 'Digits'
);
497
Zend_Filter
You can specify input data as the third constructor argument. The data structure is an associative array. The keys
are field names, and the values are data values. The standard $_GET and $_POST superglobal variables in PHP are
examples of this format. You can use either of these variables as input data for Zend_Filter_Input.
$data = $_GET;
Alternatively, use the setData() method, passing an associative array of key/value pairs the same format as
described above.
The setData() method redefines data in an existing Zend_Filter_Input object without changing the filtering
and validation rules. Using this method, you can run the same rules against different sets of input data.
if ($input->isValid()) {
echo "OK\n";
}
This method accepts an optional string argument, naming an individual field. If the specified field passed validation
and is ready for fetching, isValid('fieldName') returns TRUE.
if ($input->isValid('month')) {
echo "Field 'month' is OK\n";
}
• Missing fields are those that are not present in the input data, but were declared with the metacommand
'presence'=>'required' (see the later section on metacommands).
• Unknown fields are those that are not declared in any rule in the array of validators, but appear in the input data.
498
Zend_Filter
if ($input->hasInvalid() || $input->hasMissing()) {
$messages = $input->getMessages();
}
if ($input->hasInvalid()) {
$invalidFields = $input->getInvalid();
}
if ($input->hasMissing()) {
$missingFields = $input->getMissing();
}
if ($input->hasUnknown()) {
$unknownFields = $input->getUnknown();
}
The results of the getMessages() method is an associative array, mapping a rule name to an array of error messages
related to that rule. Note that the index of this array is the rule name used in the rule declaration, which may be different
from the names of fields checked by the rule.
The getMessages() method returns the merge of the arrays returned by the getInvalid() and
getMissing(). These methods return subsets of the messages, related to validation failures, or fields that were
declared as required but missing from the input.
The getErrors() method returns an associative array, mapping a rule name to an array of error identifiers. Error
identifiers are fixed strings, to identify the reason for a validation failure, while messages can be customized. See the
section called “Uso básico de validadores” for more information.
You can specify the message returned by getMissing() using the 'missingMessage' option, as an argument to the
Zend_Filter_Input constructor or using the setOptions() method.
$options = array(
'missingMessage' => "Field '%field%' is required"
);
// alternative method:
And you can also add a translator which gives you the ability to provide multiple languages for the messages which
are returned by Zend_Filter_Input.
499
Zend_Filter
$input->setTranslator($translate);
When you are using an application wide translator, then it will also be used by Zend_Filter_Input. In this case
you will not have to set the translator manually.
The results of the getUnknown() method is an associative array, mapping field names to field values. Field names
are used as the array keys in this case, instead of rule names, because no rule mentions the fields considered to be
unknown fields.
By default, when retrieving a value, it is filtered with the Zend_Filter_HtmlEntities. This is the default
because it is considered the most common usage to output the value of a field in HTML. The HtmlEntities filter helps
prevent unintentional output of code, which can result in security problems.
Nota
As shown above, you can retrieve the unescaped value using the getUnescaped() method, but you must
write code to use the value safely, and avoid security issues such as vulnerability to cross-site scripting attacks.
But be warned that using this notation introduces a security leak which could be used for cross-site scripting
attacks. Therefor you should always set individual validators for each field.
You can specify a different filter for escaping values, by specifying it in the constructor options array:
In either usage, you can specify the escape filter as a string base name of the filter class, or as an object instance of a
filter class. The escape filter can be an instance of a filter chain, an object of the class Zend_Filter.
500
Zend_Filter
Filters to escape output should be run in this way, to make sure they run after validation. Other filters you declare in the
array of filter rules are applied to input data before data are validated. If escaping filters were run before validation, the
process of validation would be more complex, and it would be harder to provide both escaped and unescaped versions
of the data. So it is recommended to declare filters to escape output using setDefaultEscapeFilter(), not
in the $filters array.
There is only one method getEscaped(), and therefore you can specify only one filter for escaping (although this
filter can be a filter chain). If you need a single instance of Zend_Filter_Input to return escaped output using
more than one filtering method, you should extend Zend_Filter_Input and implement new methods in your
subclass to get values in different ways.
You can specify this metacommand using the class constant Zend_Filter_Input::FIELDS instead of the string.
$filters = array(
'month' => array(
'Digits', // filter name at integer index [0]
'fields' => 'mo' // field name at string index ['fields']
)
);
In the example above, the filter rule applies the 'digits' filter to the input field named 'mo'. The string 'month' simply
becomes a mnemonic key for this filtering rule; it is not used as the field name if the field is specified with the 'fields'
metacommand, but it is used as the rule name.
The default value of the 'fields' metacommand is the index of the current rule. In the example above, if the 'fields'
metacommand is not specified, the rule would apply to the input field named 'month'.
Another use of the 'fields' metacommand is to specify fields for filters or validators that require multiple fields as input.
If the 'fields' metacommand is an array, the argument to the corresponding filter or validator is an array of the values
of those fields. For example, it is common for users to specify a password string in two fields, and they must type the
same string in both fields. Suppose you implement a validator class that takes an array argument, and returns TRUE
if all the values in the array are equal to each other.
$validators = array(
'password' => array(
'StringEquals',
'fields' => array('password1', 'password2')
)
);
// Invokes hypothetical class Zend_Validate_StringEquals,
501
Zend_Filter
If the validation of this rule fails, the rule key ('password') is used in the return value of getInvalid(), not
any of the fields named in the 'fields' metacommand.
You can specify this metacommand using the class constant Zend_Filter_Input::PRESENCE instead of the
string.
$validators = array(
'month' => array(
'digits',
'presence' => 'required'
)
);
You can specify this metacommand using the class constant Zend_Filter_Input::DEFAULT_VALUE instead
of the string.
This default value is assigned to the field before any of the validators are invoked. The default value is applied to
the field only for the current rule; if the same field is referenced in a subsequent rule, the field has no value when
evaluating that rule. Thus different rules can declare different default values for a given field.
$validators = array(
'month' => array(
'digits',
'default' => '1'
)
);
If your rule uses the FIELDS metacommand to define an array of multiple fields, you can define an array for the
DEFAULT_VALUE metacommand and the defaults of corresponding keys are used for any missing fields. If FIELDS
defines multiple fields but DEFAULT_VALUE is a scalar, then that default value is used as the value for any missing
fields in the array.
502
Zend_Filter
If in your case an empty string should be considered valid, you can set the metacommand 'allowEmpty' to TRUE. Then
the input data passes validation if it is present in the input data, but has the value of an empty string.
You can specify this metacommand using the class constant Zend_Filter_Input::ALLOW_EMPTY instead of
the string.
$validators = array(
'address2' => array(
'Alnum',
'allowEmpty' => true
)
);
In the uncommon case that you declare a validation rule with no validators, but the 'allowEmpty' metacommand is
FALSE (that is, the field is considered invalid if it is empty), Zend_Filter_Input returns a default error message
that you can retrieve with getMessages(). You can specify this message using the 'notEmptyMessage' option, as
an argument to the Zend_Filter_Input constructor or using the setOptions() method.
$options = array(
'notEmptyMessage' => "A non-empty value is required for field '%field%'"
);
// alternative method:
Alternatively, if the value of the 'breakChainOnFailure' metacommand is TRUE, the validator chain terminates after
the first validator fails. The input data is not checked against subsequent validators in the chain, so it might cause more
violations even if you correct the one reported.
You can specify this metacommand using the class constant Zend_Filter_Input::BREAK_CHAIN instead of
the string.
$validators = array(
'month' => array(
'Digits',
503
Zend_Filter
new Zend_Validate_Between(1,12),
new Zend_Validate_GreaterThan(0),
'breakChainOnFailure' => true
)
);
$input = new Zend_Filter_Input(null, $validators);
The validator chain class, Zend_Validate, is more flexible with respect to breaking chain execution than
Zend_Filter_Input. With the former class, you can set the option to break the chain on failure independently
for each validator in the chain. With the latter class, the defined value of the 'breakChainOnFailure' metacommand
for a rule applies uniformly for all validators in the rule. If you require the more flexible usage, you should create the
validator chain yourself, and use it as an object in the validator rule definition:
You can specify this metacommand using the class constant Zend_Filter_Input::MESSAGES instead of the
string.
Below is a simple example of setting the default error message for a single validator.
$validators = array(
'month' => array(
'digits',
'messages' => 'A month must consist only of digits'
)
);
If you have multiple validators for which you want to set the error message, you should use an array for the value
of the 'messages' metacommand.
Each element of this array is applied to the validator at the same index position. You can specify a message for the
validator at position n by using the value n as the array index. Thus you can allow some validators to use their default
message, while setting the message for a subsequent validator in the chain.
504
Zend_Filter
$validators = array(
'month' => array(
'digits',
new Zend_Validate_Between(1, 12),
'messages' => array(
// use default message for validator [0]
// set new message for validator [1]
1 => 'A month value must be between 1 and 12'
)
)
);
If one of your validators has multiple error messages, they are identified by a message key. There are different keys
in each validator class, serving as identifiers for error messages that the respective validator class might generate.
Each validate class defines constants for its message keys. You can use these keys in the 'messages' metacommand
by passing an associative array instead of a string.
$validators = array(
'month' => array(
'digits', new Zend_Validate_Between(1, 12),
'messages' => array(
'A month must consist only of digits',
array(
Zend_Validate_Between::NOT_BETWEEN =>
'Month value %value% must be between ' .
'%min% and %max%',
Zend_Validate_Between::NOT_BETWEEN_STRICT =>
'Month value %value% must be strictly between ' .
'%min% and %max%'
)
)
)
);
You should refer to documentation for each validator class to know if it has multiple error messages, the keys of these
messages, and the tokens you can use in the message templates.
If you have only one validator in validation rule or all used validators has the same messages set, then they can be
referenced without additional array construction:
$validators = array(
'month' => array(
new Zend_Validate_Between(1, 12),
'messages' => array(
Zend_Validate_Between::NOT_BETWEEN =>
'Month value %value% must be between ' .
'%min% and %max%',
Zend_Validate_Between::NOT_BETWEEN_STRICT =>
'Month value %value% must be strictly between ' .
'%min% and %max%'
)
)
);
505
Zend_Filter
The 'fields', 'messages', and 'default' metacommands cannot be set using this technique.
If you write your own filter or validator classes, or use filters or validators provided by a third-party, the classes may
exist in different namespaces than Zend_Filter or Zend_Validate. You can tell Zend_Filter_Input to
search more namespaces. You can specify namespaces in the constructor options:
$input->addValidatorPrefixPath('Other_Namespace', 'Other/Namespace');
$input->addFilterPrefixPath('Foo_Namespace', 'Foo/Namespace');
506
Zend_Filter
// 3. Zend_Filter
You cannot remove Zend_Filter and Zend_Validate as namespaces, you only can add namespaces. User-
defined namespaces are searched first, Zend namespaces are searched last.
Nota
As of version 1.5 the function addNamespace($namespace) was deprecated and exchanged
with the plugin loader and the addFilterPrefixPath and addValidatorPrefixPath
were added. Also the constant Zend_Filter_Input::INPUT_NAMESPACE is
now deprecated. The constants Zend_Filter_Input::VALIDATOR_NAMESPACE and
Zend_Filter_Input::FILTER_NAMESPACE are available in releases after 1.7.0.
Nota
As of version 1.0.4, Zend_Filter_Input::NAMESPACE, having value namespace, was changed to
Zend_Filter_Input::INPUT_NAMESPACE, having value inputNamespace, in order to comply
with the PHP 5.3 reservation of the keyword namespace.
Zend_Filter_Inflector
Zend_Filter_Inflector is a general purpose tool for rules-based inflection of strings to a given target.
As an example, you may find you need to transform MixedCase or camelCasedWords into a path; for readability, OS
policies, or other reasons, you also need to lower case this, and you want to separate the words using a dash ('-'). An
inflector can do this for you.
$string = 'camelCasedWords';
$filtered = $inflector->filter(array('page' => $string));
// pages/camel-cased-words.html
$string = 'this_is_not_camel_cased';
$filtered = $inflector->filter(array('page' => $string));
// pages/this_is_not_camel_cased.html
Operation
An inflector requires a target and one or more rules. A target is basically a string that defines placeholders for variables
you wish to substitute. These are specified by prefixing with a ':': :script.
When calling filter(), you then pass in an array of key/value pairs corresponding to the variables in the target.
507
Zend_Filter
Each variable in the target can have zero or more rules associated with them. Rules may be either static or refer to a
Zend_Filter class. Static rules will replace with the text provided. Otherwise, a class matching the rule provided
will be used to inflect the text. Classes are typically specified using a short name indicating the filter name stripped
of any common prefix.
As an example, you can use any Zend_Filter concrete implementations; however, instead of referring to them as
'Zend_Filter_Alpha' or 'Zend_Filter_StringToLower', you'd specify only 'Alpha' or 'StringToLower'.
To set alternate paths, Zend_Filter_Inflector has a utility method that proxies to the plugin loader,
addFilterPrefixPath():
$inflector->addFilterPrefixPath('My_Filter', 'My/Filter/');
Alternatively, you can retrieve the plugin loader from the inflector, and interact with it directly:
$loader = $inflector->getPluginLoader();
$loader->addPrefixPath('My_Filter', 'My/Filter/');
For more options on modifying the paths to filters, please see the PluginLoader documentation.
You can change the identifier using the setTargetReplacementIdentifier() method, or passing it as the
third argument to the constructor:
// Via constructor:
$inflector = new Zend_Filter_Inflector('#foo/#bar.#sfx', null, '#');
// Via accessor:
$inflector->setTargetReplacementIdentifier('#');
Typically, you will set the target via the constructor. However, you may want to re-set the target later (for instance, to
modify the default inflector in core components, such as the ViewRenderer or Zend_Layout). setTarget()
can be used for this purpose:
$inflector = $layout->getInflector();
508
Zend_Filter
$inflector->setTarget('layouts/:script.phtml');
Additionally, you may wish to have a class member for your class that you can use to keep the inflector target updated
-- without needing to directly update the target each time (thus saving on method calls). setTargetReference()
allows you to do this:
class Foo
{
/**
* @var string Inflector target
*/
protected $_target = 'foo/:bar/:baz.:suffix';
/**
* Constructor
* @return void
*/
public function __construct()
{
$this->_inflector = new Zend_Filter_Inflector();
$this->_inflector->setTargetReference($this->_target);
}
/**
* Set target; updates target in inflector
*
* @param string $target
* @return Foo
*/
public function setTarget($target)
{
$this->_target = $target;
return $this;
}
}
Inflection Rules
As mentioned in the introduction, there are two types of rules: static and filter-based.
Nota
It is important to note that regardless of the method in which you add rules to the inflector, either one-by-
one, or all-at-once; the order is very important. More specific names, or names that might contain other rule
names, must be added before least specific names. For example, assuming the two rule names 'moduleDir' and
'module', the 'moduleDir' rule should appear before module since 'module' is contained within 'moduleDir'. If
'module' were added before 'moduleDir', 'module' will match part of 'moduleDir' and process it leaving 'Dir'
inside of the target uninflected.
Static Rules
Static rules do simple string substitution; use them when you have a segment in the target that will typically be static,
but which you want to allow the developer to modify. Use the setStaticRule() method to set or modify the rule:
509
Zend_Filter
// change it later:
$inflector->setStaticRule('suffix', 'php');
Much like the target itself, you can also bind a static rule to a reference, allowing you to update a single variable
instead of require a method call; this is often useful when your class uses an inflector internally, and you don't want
your users to need to fetch the inflector in order to update it. The setStaticRuleReference() method is used
to accomplish this:
class Foo
{
/**
* @var string Suffix
*/
protected $_suffix = 'phtml';
/**
* Constructor
* @return void
*/
public function __construct()
{
$this->_inflector = new Zend_Filter_Inflector(':script.:suffix');
$this->_inflector->setStaticRuleReference('suffix', $this->_suffix);
}
/**
* Set suffix; updates suffix static rule in inflector
*
* @param string $suffix
* @return Foo
*/
public function setSuffix($suffix)
{
$this->_suffix = $suffix;
return $this;
}
}
Rules may be added using setFilterRule() (which overwrites any previous rules for that variable) or
addFilterRule() (which appends the new rule to any existing rule for that variable). Filters are specified in one
of the following ways:
• String. The string may be a filter class name, or a class name segment minus any prefix set in the inflector's plugin
loader (by default, minus the 'Zend_Filter' prefix).
510
Zend_Filter
• Filter object. Any object instance implementing Zend_Filter_Interface may be passed as a filter.
Each method takes an array of variable/rule pairs, where the rule may be whatever the type of rule accepts (string,
filter object, or array). Variable names accept a special notation to allow setting static rules and filter rules, according
to the following notation:
// Static rule:
'suffix' => 'phtml'
));
Utility Methods
Zend_Filter_Inflector has a number of utility methods for retrieving and setting the plugin loader,
manipulating and retrieving rules, and controlling if and when exceptions are thrown.
• setPluginLoader() can be used when you have configured your own plugin loader and wish to use it with
Zend_Filter_Inflector; getPluginLoader() retrieves the currently set one.
511
Zend_Filter
• getRules($spec = null) can be used to retrieve all registered rules for all variables, or just the rules for
a single variable.
• getRule($spec, $index) fetches a single rule for a given variable; this can be useful for fetching a specific
filter rule for a variable that has a filter chain. $index must be passed.
• filterPrefixPath specifies one or more filter prefix/path pairs for use with the inflector.
• rules specifies an array of inflection rules; it should consist of keys that specify either values or arrays of values,
consistent with addRules().
// Or with setConfig():
$inflector = new Zend_Filter_Inflector();
$inflector->setConfig($config);
512
Capítulo 23. Zend_Form
Zend_Form
Zend_Form simplifica la creación y manipulación de formularios en tus aplicaciones web. Cumple los siguientes
objetivos:
• Ordenado de elementos
Se aprovecha en gran medida de otros componentes de Zend Framework para lograr sus objetivos, incluyendo
Zend_Config, Zend_Validate, Zend_Filter, Zend_Loader_PluginLoader y, opcionalmente,
Zend_View.
Para casos de uso avanzados, es posible desee crear una subclase de Zend_Form, pero para formularios simples,
puede programar la creación de un formulario usando un objeto Zend_Form
Si desea especificar el action y method del formulario (siempre buenas ideas), puede hacer uso de los accesos
setAction() y setMethod():
$form->setAction('/resource/process')
->setMethod('post');
El código de arriba establece el action del formulario a la URL parcial "/resource/process" y como method HTTP
POST. Esto se mostrará en la presentación final.
Usted puede establecer atributos HTML adicionales para la etiqueta <form> mediante el uso de los métodos
setAttrib() o setAttribs(). Por ejemplo, si desea especificar el id establezca el atributo "id":
$form->setAttrib('id', 'login');
513
Zend_Form
• button
• hidden
• image
• password
• radio
• reset
• submit
• text
• textarea
Tiene dos opciones para añadir elementos a un formulario; puede instanciar elementos concretos y pasarlos como
objetos, o simplemente puede pasar el tipo de elemento y Zend_Form instaciará por usted un objeto del tipo
correspondiente.
Algunos ejemplos:
De manera predeterminada, éstos no tienen validadores o filtros. Esto significa que tendrá que configurar sus elementos
con un mínimo de validadores, y potencialmente filtros. Puede hacer esto (a) antes de pasar el elemento al formulario,
(b) vía opciones de configuración pasadas cuando crea un elemento a través de Zend_Form, o (c) recuperar el
elemento del objeto form y configurándolo posteriormente.
Veamos primero la creación de validadores para la instancia de un elemento concreto. Puede pasar objetos
Zend_Validate_* o el nombre de un validador para utilizar:
Cuando se utiliza esta segunda opción, si el constructor del validador acepta argumentos, se pueden pasar en un array
como tercer parámetro:
// Pasando un patrón
514
Zend_Form
(El segundo parámetro se utiliza para indicar si el fallo debería prevenir la ejecución de validadores posteriores o no;
por defecto, el valor es false.)
Puede también desear especificar un elemento como requerido. Esto puede hacerse utilizando un método de acceso o
pasando una opción al crear el elemento. En el primer caso:
Cuando un elemento es requerido, un validador 'NotEmpty' (NoVacio) es añadido a la parte superior de la cadena de
validaciones, asegurando que el elemento tenga algún valor cuando sea requerido.
Los filtros son registrados básicamente de la misma manera que los validadores. Para efectos ilustrativos, vamos a
agregar un filtro para poner en minúsculas el valor final:
$username->addFilter('StringtoLower');
$username->addValidator('alnum')
->addValidator('regex', false, array('/^[a-z]/'))
->setRequired(true)
->addFilter('StringToLower');
Tan simple como esto, realizarlo para cada uno de los elementos del formulario puede resultar un
poco tedioso. Intentemos la opción (b) arriba mencionada. Cuando creamos un nuevo elemento utilizando
Zend_Form::addElement() como fábrica, opcionalmente podemos pasar las opciones de configuración. Éstas
pueden incluir validadores y los filtros que se van a utilizar. Por lo tanto, para hacer todo lo anterior implícitamente,
intente lo siguiente:
Nota
Si encuentra que está asignando elementos con las mismas opciones en varios lugares, podría considerar crear
su propia subclase de Zend_Form_Element y utilizar ésta; a largo plazo le permitirá escribir menos.
515
Zend_Form
Generar un formulario
Generar un formulario es simple. La mayoría de los elementos utilizan un auxiliar de Zend_View para generarse a
sí mismos, por lo tanto necesitan un objeto vista con el fin de generarse. Además, tiene dos opciones: usar el método
render() del formulario, o simplemente mostrarlo con echo.
Detrás del telón, Zend_Form utiliza "decoradores" (decorators) para generar la salida. Estos decoradores
pueden reemplazar, añadir o anteponer contenido, y tienen plena introspección al elemento que les es pasado.
Como resultado, puede combinar múltiples decoradores para lograr efectos personalizados. Predeterminadamente,
Zend_Form_Element actualmente combina cuatro decoradores para obtener su salida; la configuración sería como
sigue:
$element->addDecorators(array(
'ViewHelper',
'Errors',
array('HtmlTag', array('tag' => 'dd')),
array('Label', array('tag' => 'dt')),
));
(Donde <HELPERNAME> es el nombre de un view helper que utilizar, y varía según el elemento)
Puede cambiar los decoradores usados para un elemento si desea tener diferente salida; véase la sección sobre
decoradores para mayor información.
El propio formulario simplemente itera sobre los elementos y los cubre en un <form> HTML. El action y method que
proporcionó cuando definió el formulario se pasan a la etiqueta <form>, como cualquier atributo que establezca vía
setAttribs() y familia.
516
Zend_Form
Elementos son desplegados en el orden en el que fueron registrados, o, si el elemento contienen un atributo de orden,
ese orden será utilizado. Usted puede fijar el orden de un elemento usando:
$element->setOrder(10);
¿De dónde provienen los datos?. Puede usar $_POST o $_GET, o cualquier otra fuente de datos que tenga a mano
(solicitud de un servicio web, por ejemplo):
if ($form->isValid($_POST)) {
// ¡Correcto!
} else {
// ¡Fallo!
}
Con solicitudes AJAX, a veces puede ignorar la validación de un solo elemento, o grupo de elementos.
isValidPartial() validará parcialmente el formulario. A diferencia de isValid(), que como sea, si alguna
clave no esta presente, no ejecutará las validaciones para ese elemento en particular.
if ($form->isValidPartial($_POST)) {
// de los elementos presentes, todos pasaron las validaciones
} else {
// uno u más elementos probados no pasaron las validaciones
}
Un método adicional, processAjax(), puede también ser usado para validar formularios parciales. A diferencia
de isValidPartial(), regresa una cadena en formato JSON conteniendo mensajes de error en caso de fallo.
Asumiendo que sus validaciones han pasado, ahora puede obtener los valores filtrados:
$values = $form->getValues();
$unfiltered = $form->getUnfilteredValues();
517
Zend_Form
if (!$form->isValid($_POST)) {
echo $form;
Si quiere inspeccionar los errores, tiene dos métodos. getErrors() regresa una matriz asociativa de nombres /
códigos de elementos (donde códigos es una matriz de códigos de error). getMessages() regresa una matriz
asociativa de nombres / mensajes de elementos (donde mensajes es una matriz asociativa de pares código de error /
mensaje de error). Si un elemento no tiene ningún error, no será incluido en la matriz.
• usuario
• contraseña
• Botón de ingreso
Para nuestros propósitos, vamos a suponer que un usuario válido cumplirá con tener solo caracteres alfanuméricos,
comenzar con una letra, tener una longitud mínima de 6 caracteres y una longitud máxima de 20 caracteres; se
normalizarán en minúsculas. Las contraseñas deben tener un mínimo de 6 caracteres. Cuando se procese vamos
simplemente a mostrar el valor, por lo que puede permanecer inválido.
518
Zend_Form
$values = $form->getValues();
// ahora intenta y autentica...
}
}
<h2>Please login:</h2>
<?php echo $this->form
Como notará en el código del controlador, hay más trabajo por hacer: mientras la información enviada sea válida,
necesitará todavía realizar la autenticación usando Zend_Auth, por ejemplo.
[development]
519
Zend_Form
; elemento username
user.login.elements.username.type = "text"
user.login.elements.username.options.validators.alnum.validator = "alnum"
user.login.elements.username.options.validators.regex.validator = "regex"
user.login.elements.username.options.validators.regex.options.pattern = "/^[a-z]/i"
user.login.elements.username.options.validators.strlen.validator = "StringLength"
user.login.elements.username.options.validators.strlen.options.min = "6"
user.login.elements.username.options.validators.strlen.options.max = "20"
user.login.elements.username.options.required = true
user.login.elements.username.options.filters.lower.filter = "StringToLower"
; elemento password
user.login.elements.password.type = "password"
user.login.elements.password.options.validators.strlen.validator = "StringLength"
user.login.elements.password.options.validators.strlen.options.min = "6"
user.login.elements.password.options.required = true
; elemento submit
user.login.elements.submit.type = "submit"
Conclusión
Esperamos que después de este pequeño tutorial sea capaz de descubrir el poder y flexibilidad de Zend_Form.
Continúe leyendo para profundizar más en el tema.
520
Zend_Form
La clase base, Zend_Form_Element, funciona por defecto para varios casos, pero es mejor extender la clase para
elementos con fines especiales de uso común. Adicionalmente, Zend Framework contiene un número de elementos
XHTML estándar; puede leer de ellos en el capítulo Elementos Estándares
Cargadores de Plugin
Zend_Form_Element hace uso de Zend_Loader_PluginLoader para permitir a los desarrolladores especificar
ubicaciones de validadores, filtros y decoradores alternos. Cada uno tiene su propio cargador de plugin asociado a él
y métodos de acceso generales usados para su recuperación y modificación.
Los siguientes tipos de cargadores son usados con los varios métodos del cargador de plugin: 'validate', 'filter', y
'decorator'. Los nombres son sensibles a mayúsculas y minúsculas.
Los métodos usados para interactuar con los cargadores de plugin son los siguientes:
• addPrefixPath($prefix, $path, $type = null): agrega una asociación prefijo/ruta para el cargador
especificado por $type. Si $type es null, se intentará agregar la ruta a todos los cargadores, añadiendo el prefijo
a cada "_Validate", "_Filter" y "_Decorator"; y agregandole "Validate/", "Filter/" y "Decorator/" a la ruta. Si tiene
todas sus clases extras para elementos de formulario dentro de una jerarquía común, este método es conveniente
para establecer el prefijo para todas ellas.
• addPrefixPaths(array $spec): le permite añadir varias rutas de una sola vez a uno o más cargadores de
plugin. Se espera cada elemento de la matriz sea un array con claves 'path', 'prefix', y 'type'.
Validadores, filtros y decoradores personalizados son una manera simple de compartir funcionalidad entre formularios
y encapsular funcionalidad personalizada.
Comencemos con un decorador de etiqueta personalizado. Le daremos el prefijo "My_Decorator", y la clase estará
en el archivo "My/Decorator/Label.php".
521
Zend_Form
switch ($placement) {
case 'APPEND':
return $content . $separator . $label;
case 'PREPEND':
default:
return $label . $separator . $content;
}
}
}
Ahora diremos al elemento que use esta ruta cuando busque por decoradores:
Alternativamente, podemos hacerlo en el formulario para asegurar que todos los decoradores usen esta ruta:
Con esta ruta añadida, cuando agregue un decorador, la ruta 'My/Decorator' será consultada primero en búsqueda
de la existencia del decorador en este lugar. Como resultado, 'My_Decorator_Label' ahora será utilizado cuando el
decorador 'Label' sea requerido.
Filters
A menudo es útil y/o necesario realizar alguna normalización en la entrada antes de la validación – por ejemplo, puede
querer eliminar todo el HTML, pero realizar las validaciones sobre lo restante para asegurarse que el envío es válido.
O puede eliminar los espacios en blanco al inicio o fin de la entrada para asegurarse de que un validador StringLenth
(longitud de la cadena) no regrese un positivo falso. Estas operaciones pueden realizarse usando Zend_Filter, y
Zend_Form_Element que soportan cadenas de filtros, permitiéndole especificar múltiples filtros secuenciales a
utilizar. El filtrado sucede tanto en la validación como cuando recupera el valor del elemento vía getValue():
522
Zend_Form
$filtered = $element->getValue();
Los nombres cortos son típicamente el nombre del filtro sin el prefijo. En el caso predeterminado, esto se refiere a sin
el prefijo 'Zend_Filter_'. Además, la primera letra no necesita estar en mayúscula.
(Recuerde que el tercer argumento indica el cargador de plugin sobre el cual ha de ejecutarse la acción.)
$unfiltered = $element->getUnfilteredValue();
• addFilters(array $filters)
523
Zend_Form
Validadores
Si sigue el mantra de seguridad "filtrar la entrada, escapar la salida" querrá validar ("filtrar la entrada") los datos de
los formularios. En Zend_Form cada elemento contiene su propia cadena de validadores, consistente en validadores
Zend_Validate_*.
Los nombres cortos son típicamente el nombre del validador sin el prefijo. En el caso predeterminado, esto se refiere
a sin el prefijo 'Zend_Validate_'. Además, la primera letra no necesita estar en mayúscula.
(Recuerde que el tercer argumento indica el cargador de plugin sobre el cual ha de ejecutarse la acción.)
Si el fallo de un validador debe evitar validaciones posteriores, pase el boleano true como segundo parámetro:
$element->addValidator('alnum', true);
Si está usando la cadena nombre para añadir el validador, y la clase del validador acepta argumentos para su constructor,
puede pasarlos a el tercer parámetro de addValidator() como un array:
524
Zend_Form
Los argumentos pasados de esta manera deben estar en el orden en el cual son definidos en el constructor. El ejemplo
de arriba instanciará la clase Zend_Validate_StringLenth con los parámetros $min y $max:
Una opción mejor es usar Zend_Translate_Adapter con su formulario. Los códigos de error son
automáticamente pasados al adaptador por el decorador Errors por defecto; puede especificar su propias
cadenas de mensaje de error mediante la creación de traducciones para los varios códigos de error de sus
validadores.
Puede también establecer varios validadores a la vez, usando addValidators(). Su uso básico es pasar una matriz
de arrays, donde cada array contenga de 1 a 3 valores, correspondientes al constructor de addValidator():
$element->addValidators(array(
array('NotEmpty', true),
array('alnum'),
array('stringLength', false, array(6, 20)),
));
Si quiere ser más detallado o explícito, puede utilizar las claves 'validator', 'breakChainOnFailure', y 'options' en el
array:
$element->addValidators(array(
array(
'validator' => 'NotEmpty',
'breakChainOnFailure' => true),
array('validator' => 'alnum'),
array(
'validator' => 'stringLength',
'options' => array(6, 20)),
));
Este uso es bueno para ilustrar cómo puede configurar validadores en un archivo de configuración:
element.validators.notempty.validator = "NotEmpty"
element.validators.notempty.breakChainOnFailure = true
element.validators.alnum.validator = "Alnum"
element.validators.strlen.validator = "StringLength"
element.validators.strlen.options.min = 6
525
Zend_Form
element.validators.strlen.options.max = 20
Note que cada elemento tiene una clave, la necesite o no; esta es una limitación del uso de archivos de configuración
-- pero también ayuda a hacer más explicito el para qué son usados los argumentos. Sólo recuerde que cualesquiera
opciones del validador deben ser especificadas en orden.
if ($element->isValid($value)) {
// válido
} else {
// no válido
}
Contexto de validación
Zend_Form_Element::isValid()> soporta un argumento adicional, $context.
Zend_Form::isValid() pasa todo el conjunto de datos procesados a $context cuando valida un
formulario, y Zend_Form_Element::isValid()>, a su vez, lo pasa a cada validador. Esto significa
que puede escribir validadores que son conscientes de los datos pasados a otros elementos del formulario.
Como ejemplo, considere un formulario de registro estándar que tiene campos para la contraseña y la
confirmación de la contraseña; una validación sería que los dos campos coincidan. Este validador puede tener
un aspecto como el siguiente:
if (is_array($context)) {
if (isset($context['password_confirm'])
&& ($value == $context['password_confirm']))
{
return true;
}
} elseif (is_string($context) && ($value == $context)) {
return true;
}
526
Zend_Form
$this->_error(self::NOT_MATCH);
return false;
}
}
Los validadores son procesados en orden. Cada validador es procesado, a menos que un validador creado con un
valor true para breakChainOnFailure falle su validación. Asegúrese de especificar sus validadores en un orden
razonable.
Después de una validación fallida, puede recuperar los códigos y mensajes de error de la cadena del validador:
$errors = $element->getErrors();
$messages = $element->getMessages();
(Nota: los mensajes de error retornados son un array asociativo de pares código / mensaje de error.)
En adición a los validadores, puede especificar que un elemento es necesario, usando setRequired(true). Por
defecto, esta bandera es false, lo que significa que pasará su cadena de validadores si ningún valor es pasado a
isValid(). Puede modificar este comportamiento en un número de maneras:
• Por defecto, cuando un elemento es requerido, una bandera, 'allowEmpty', también es true. Esto quiere decir que si
un valor empty es evaluado pasándolo a isValid(), los validadores serán saltados. Puede intercalar esta bandera
usando el método de acceso setAllowEmpty($flag); cuando la bandera es false, si un valor es pasado, los
validadores seguirán ejecutándose.
• Por defecto, si un elemento es requerido, pero no contiene un validador 'NotEmpty', isValid() añadirá uno en la
cima de la pila, con la bandera breakChainOnFailure establecido. Esto hace que la bandera requerida tenga
un significado semántico: si ningún valor es pasado, inmediatamente invalidamos el envío y se le notifica al usuario,
e impedimos que otros validadores se ejecuten en lo que ya sabemos son datos inválidos.
527
Zend_Form
• addValidators(array $validators)
• clearErrorMessages(): remueve todos los mensajes de error personalizados que hayan sido definidos.
• addErrors(array $messages): añade varios mensajes a la pila de mensajes de error personalizados y marca
al elemento como inválido.
• setErrors(array $messages): sobreescribe el mensaje de error personalizado en la pila con los mensajes
previstos y marca al elemento como inválido.
Todos los errores establecidos de este modo pueden ser traducidos. Adicionalmente, puede insertar el marcador
"%value%" para representar el valor del elemento; este valor actual del elemento será sustituido cuando el mensaje
de error sea recuperado.
Decoradores
Una dolencia particular para muchos desarrolladores web es la creación del XHTML para formularios por ellos
mismos. Para cada elemento, el desarrollador necesita crear la marcación para el elemento mismo, comúnmente una
528
Zend_Form
etiqueta (label), y, si son amables con sus usuarios, la marcación para mostrar mensajes de errores de validación.
Cuanto más elementos en una página, menos trivial se convierte esta tarea.
Zend_Form_Element intenta resolver este problema mediante el uso de "decoradores". Los decoradores son
clases simples que tienen métodos de acceso al elemento y métodos para generar el contenido. Para obtener mayor
información sobre cómo trabajan los decoradores, consulte por favor la sección sobre Zend_Form_Decorator.
• ViewHelper: especifica un view helper que usar para general el elemento. El atributo 'helper' del elemento puede
usarse para especificar qué auxiliar vista usar. Por defecto, Zend_Form_Element especifica el auxiliar vista
'formText', pero cada subclase especifica diferentes auxiliares.
• Description: añade la descripción del elemento. Si no está presente, no se añade nada. Por defecto, la descripción
es generada dentro de una etiqueta <p> con un class 'description'.
Esta opción puede ser combinada junto con cualquier otra opción que pase, ya sea como un array de opciones
o en un objeto Zend_Config.
Ya que el orden en el cual los decoradores son registrados importa -- el primer decorador registrado es ejecutado
primero -- necesitará estar seguro de registrar sus decoradores en el orden apropiado, o asegurarse de que estableció
las opciones de colocación en el modo apropiado. Por dar un ejemplo, aquí esta el código que registran los decoradores
por defecto:
$this->addDecorators(array(
array('ViewHelper'),
array('Errors'),
array('Description', array('tag' => 'p', 'class' => 'description')),
array('HtmlTag', array('tag' => 'dd')),
array('Label', array('tag' => 'dt')),
));
El contenido inicial es creado por el decorador 'ViewHelper', que crea el propio elemento. En seguida, el decorador
'Errors' consulta los mensajes de error del elemento, y, si hay alguno presente, los pasa al auxiliar vista 'FormErrors'
529
Zend_Form
para mostrarlos. Si una descripción está presente, el decorador 'Description' añadirá un párrafo con class 'description'
conteniendo el texto descriptivo para el contenido agregado. El siguiente decorador, 'HtmlTag', envuelve al elemento,
los errores, y la descripción en una etiqueta HTML <dd>. Finalmente, el último decorador, 'label', recupera la etiqueta
del elemento y la pasa al auxiliar vista 'FormLabel', envolviéndolo en una etiqueta <dt>; por default el valor es añadido
al inicio del contenido. El resultado de la salida básicamente se ve así:
Para evitar esto, puede usar alias. En lugar de pasar un decorador o nombre de decorador como primer
argumento a addDecorator(), pase una matriz con un solo elemento, con el alias apuntando al nombre
o objeto decorador:
// Alias a 'FooBar':
$element->addDecorator(array('FooBar' => 'HtmlTag'),
array('tag' => 'div'));
// Y recuperandolo posteriormente:
$decorator = $element->getDecorator('FooBar');
// Y recuperándolos posteriormente:
530
Zend_Form
$htmlTag = $element->getDecorator('HtmlTag');
$fooBar = $element->getDecorator('FooBar');
• addDecorators(array $decorators)
Zend_Form_Element también utiliza la sobrecarga para permitir generar decoradores específicos. __call()
interceptará métodos que comiencen con el texto 'render' y utilizará el resto del nombre del método para buscar un
decorador; si se encuentra, entonces será generado sólo ese decorador. Cualquier argumento pasado al llamado del
método será usado como contenido para pasar al método render() del decorador. Como ejemplo:
Metadatos y atributos
Zend_Form_Element manipula una variedad de atributos y medatados del elemento. Atributos básicos incluyen:
• name: el nombre del elemento. Emplea los métodos de acceso setName() y getName().
• label: la etiqueta del elemento. Emplea los métodos de acceso setLabel() y getLabel().
• order: el índice en el cual los elementos deben ir mostrándose en el formulario. Emplea los métodos de acceso
setOrder() y getOrder().
• value: El valor del elemento actual. Emplea los métodos de acceso setValue() y getValue().
• description: una descripción del elemento; a menudo utilizada para proveer un tooltip o ayuda contextual con
javascript describiendo el propósito del elemento. Emplea los métodos de acceso setDescription() y
getDescription().
• required: bandera que indica si un elemento es requerido o no cuando se efectúa la validación del formulario. Emplea
los métodos de acceso setRequired() y getRequired(). Esta bandera es false por defecto.
• allowEmpty: bandera indicando si un elemento no-requerido (opcional) debe intentar validar o no valores vacíos.
Cuando es true, y la bandera required es false, valores vacíos no pasarán la cadena de validación, y se supone
531
Zend_Form
verdadero. Emplea los métodos de acceso setAllowEmpty() y getAllowEmpty(). Esta bandera es true por
defecto.
Los elementos del formulario pueden requerir metainformación adicional. Para elementos XHTML del formuladio,
por ejemplo, puede querer especificar atributos como el class o id. Para facilitar esto hay un conjunto de métodos
de acceso:
La mayoría de las veces, como sea, puede simplemente acceder a ellos como propiedades de objeto, ya que
Zend_Form_Element utiliza la sobrecarga para facilitar el acceso a ellos:
Por defecto, todos los atributos son pasados al auxiliar vista usado por el elemento durante la generación, y generados
como atributos de la etiqueta del elemento.
Elementos Estándar
Zend_Form contiene un buen número de elementos estándar; por favor lea el capítulo Elementos Estándar para todos
los detalles.
Métodos de Zend_Form_Element
Zend_Form_Element tiene muchos, muchos métodos. Lo que sigue es un sumario de sus funciones, agrupados
por tipo:
• Configuración:
• setOptions(array $options)
• setConfig(Zend_Config $config)
• I18n:
• getTranslator()
• setDisableTranslator($flag)
• translatorIsDisabled()
532
Zend_Form
• Propiedades:
• setName($name)
• getName()
• setValue($value)
• getValue()
• getUnfilteredValue()
• setLabel($label)
• getLabel()
• setDescription($description)
• getDescription()
• setOrder($order)
• getOrder()
• setRequired($flag)
• getRequired()
• setAllowEmpty($flag)
• getAllowEmpty()
• setAutoInsertNotEmptyValidator($flag)
• autoInsertNotEmptyValidator()
• setIgnore($flag)
• getIgnore()
• getType()
• setAttrib($name, $value)
• setAttribs(array $attribs)
• getAttrib($name)
• getAttribs()
• getPluginLoader($type)
• addPrefixPaths(array $spec)
533
Zend_Form
• Validación:
• addValidators(array $validators)
• setValidators(array $validators)
• getValidator($name)
• getValidators()
• removeValidator($name)
• clearValidators()
• getErrors()
• getMessages()
• Filtros:
• addFilters(array $filters)
• setFilters(array $filters)
• getFilter($name)
• getFilters()
• removeFilter($name)
• clearFilters()
• Generación:
• getView()
• addDecorators(array $decorators)
• setDecorators(array $decorators)
• getDecorator($name)
• getDecorators()
• removeDecorator($name)
• clearDecorators()
534
Zend_Form
Configuración
El constructor de Zend_Form_Element acepta tanto una matriz de opciones como un objeto Zend_Config
conteniendo opciones, y esto puede configurarse usando setOptions() o setConfig(). Hablando de manera
general, las claves son nombradas de la siguiente manera:
• Si 'set' + clave se refiere a un método de Zend_Form_Element, entonces el valor provisto será pasado a el método.
• setConfig
• setOptions
• setPluginLoader
• setTranslator
• setView
Como ejemplo, aquí esta un archivo de configuración pasado para cada tipo de dato configurable:
[element]
name = "foo"
value = "foobar"
label = "Foo:"
order = 10
required = true
allowEmpty = false
autoInsertNotEmptyValidator = true
description = "Foo elements are for examples"
ignore = false
attribs.id = "foo"
attribs.class = "element"
; sets 'onclick' attribute
onclick = "autoComplete(this, '/form/autocomplete/element')"
prefixPaths.decorator.prefix = "My_Decorator"
prefixPaths.decorator.path = "My/Decorator/"
disableTranslator = 0
validators.required.validator = "NotEmpty"
validators.required.breakChainOnFailure = true
validators.alpha.validator = "alpha"
validators.regex.validator = "regex"
validators.regex.options.pattern = "/^[A-F].*/$"
filters.ucase.filter = "StringToUpper"
535
Zend_Form
decorators.element.decorator = "ViewHelper"
decorators.element.options.helper = "FormText"
decorators.label.decorator = "Label"
Elementos personalizados
Usted puede crear sus propios elementos personalizados simplemente extendiendo la clase Zend_Form_Element.
Las razones comunes para hacer esto incluyen:
Hay dos métodos típicamente usados para extender un elemento: init(), el cual puede usarse para añadir una lógica
de inicialización personalizada a su elemento, y loadDefaultDecorators(), el cual puede usarse para establecer
una lista de decoradores usados por su elemento de manera predeterminada.
Como un ejemplo, digamos que todos los elementos de tipo texto en un formulario que está creando, necesitan ser
filtrados con StringTrim, validados con una expresión regular, y que quiere usar un decorador personalizado que ha
creado para mostrarlos, 'My_Decorator_TextItem'; adicionalmente, tiene un número de atributos estándars, incluyendo
'size', 'maxLength', y 'class' que quisiera especificar. Puede definir un elemento tal como sigue:
Entonces puede informar a su objeto formulario acerca del prefijo de ruta para elementos de ese tipo, y comenzar
creando elementos:
El elemento 'foo' será ahora del tipo My_Element_Text, y mostrará el comportamiento que ha especificado.
Otro método que puede querer sobreescribir cuando extienda Zend_Form_Element es el método
loadDefaultDecorators(). Este método carga condicionalmente un grupo de decoradores predefinidos para
su elemento; puede querer sustituir su propio decorador en su clase extendida:
536
Zend_Form
{
public function loadDefaultDecorators()
{
$this->addDecorator('ViewHelper')
->addDecorator('DisplayError')
->addDecorator('Label')
->addDecorator('HtmlTag',
array('tag' => 'div', 'class' => 'element'));
}
}
• Agregación de valor, incluyendo el llenado de elementos y recuperación de valores tanto filtrados como no filtrados
para todos los elementos
• Iteración sobre todos los elementos, en el orden en el cual han sido introducidos o basados en el orden recuperado
de cada elemento
• Generando el formulario entero, ya sea por un simple decorador que ejecuta un muestreo personalizado o por
iteración sobre cada elemento del formulario
Mientras los formularios creados con Zend_Form pueden ser complejos, probablemente su mejor uso es para
formularios simples; es mejor utilizarlo para desarrollar aplicaciones rápidas (RAD) y de prototipado.
Opcionalmente puede pasarlo en la configuración, la cual será usada para establecer el estado del objeto,
potencialmente así como también crear nuevos elementos:
Zend_Form es iterable, e iterará sobre elementos, grupos que mostrar y subformularios, usando el orden en el cual
han sido registrados y en cualquier índice de orden que cada uno pueda tener. Esto es útil en casos donde desee generar
los elementos manualmente en el orden apropiado.
La magia de Zend_Form radica en la habilidad para servir como fábrica para elementos y grupos, así como también
la habilidad de generarse a sí mismo a través de decoradores.
537
Zend_Form
Cargadores de Plugin
Zend_Form hace uso de Zend_Loader_PluginLoader para permitir a los desarroladores especificar la
ubicación de elementos y decoradores alternativos. Cada uno tiene su propio plugin loader asociado, y métodos de
acceso genéricos son usados para recuperar y modificar cada uno.
Los siguientes tipos de cargadores son usados con los variados métodos del plugin loader: 'element' y 'decorator'. Los
nombres de los tipos no distinguen mayúsculas de minúsculas.
Los métodos usados para interactuar con plugin loaders son los siguientes:
• setPluginLoader($loader, $type): $loader es el propio objeto plugin loader, mientras $type es uno de
los tipos especificados arriba. Esto establece el plugin loader para el tipo dado al objeto loader recién especificado.
• addPrefixPaths(array $spec): le permite añadir varias rutas en uno o mas plugin loaders. Se espera que
cada elemento del array sea un array con las claves 'path', 'prefix' y 'type'.
Adicionalmente, puede especificar prefijos de rutas para todos los elementos y mostrar grupos creados a través de una
instancia de Zend_Form usando los siguientes métodos:
Elementos y decoradores personalizados son una manera fácil de compartir funcionalidad entre formularios y
encapsular funcionalidad personalizada. Vea el ejemplo de Etiqueta Personalizada en la documentación de elementos
para un ejemplo de cómo elementos personalizados pueden ser usados como reemplazos para clases estándar.
Elementos
Zend_Form proporciona varios métodos de acceso para añadir y eliminar elementos de el formulario. Éstos pueden
tomar instancias de objetos de elemento o servir como fábricas para instanciar el objeto elemento a sí mismo.
El método más básico para añadir un elemento es addElement(). Este método puede tomar también un objeto de
tipo Zend_Form_Element (o de una clase extendiendo Zend_Form_Element), o argumentos para construir un
nuevo elemento -- incluyendo el elemento tipo, nombre y algunas opciones de configuración.
538
Zend_Form
//
// Crea un elemento de tipo Zend_Form_Element_Text con el
// nombre de 'foo':
$form->addElement('text', 'foo');
Si desea retornar el elemento, use createElement(), el cual es esbozado abajo. Tenga en cuenta de
cualquier manera que createElement() no adjunta el elemento al formulario.
Una vez que el elemento ha sido añadido al formulario, puede recuperarlo por el nombre. Puede también finalizar
usando el método getElement() o usando sobrecarga para acceder al elemento como una propiedad de objeto:
// getElement():
$foo = $form->getElement('foo');
Ocasionalmente, se quiere crear un elemento sin adjuntarlo al formulario (para instanciar, si se desea hacer uso de las
rutas de plugin introducidas con el formulario, pero después se desea adjuntar el objeto al subformulario). El método
createElement() permite hacer eso:
A veces se quiere llenar el formulario con valores especificos antes de generarlos. Éstos pueden ser llevados a cabo
ya sea con los métodos setDefaults() o populate():
$form->setDefaults($data);
539
Zend_Form
$form->populate($data);
Por otro lado, si se quisera limpiar el formulario antes de llenarlo o validarlo; se puede realizar usando el método
reset():
$form->reset();
Operaciones Globales
Ocasionalemnte se necesitarán ciertas operaciones que afecten a todos los elementos. Escenarios comunes incluyen la
necesidad de determinar rutas de acceso al prefijo complemento para todos los elementos, determinando decoradores
para todos los elementos y determinando filtros para todos los elementos. Como ejemplos:
Ejemplo 23.2. Determinando rutas de acceso de prefijos para todos los elementos
Se puede determinar rutas de acceso para prefijos para todos los elementos por tipo, o usando un prefijo global. Como
ejemplos:
$form->setElementDecorators(array(
'ViewHelper',
'Label'
));
540
Zend_Form
especificar un array de ese tipo determinará los decoradores especificados en esos elementos solamente. Puede tambien
pasar un tercer elemento, una bandera indicando si esta lista de elementos es para propósitos de inclusión o exclusión;
si es falso, decorará todos los elementos excepto los pasados en la lista, Como uso estándar del método, cualquier
decorador pasado reescribirá cualquier decorador previamente determinado en cada elemento.
En el siguiente fragmento, indicamos que queremos los decoradores ViewHelper y Label para los elementos 'foo' y
'bar':
$form->setElementDecorators(
array(
'ViewHelper',
'Label'
),
array(
'foo',
'bar'
)
);
Por otro lado, con este fragmento, indicaremos que queremos usar solamente los decoradores ViewHelper y Label
para cada elemento excepto los elementos 'foo' y 'bar':
$form->setElementDecorators(
array(
'ViewHelper',
'Label'
),
array(
'foo',
'bar'
),
false
);
Se puede usar el array de inclusión/exclusión para superar este problema como se ha notado en el ejemplo
anterior.
Entonces, use este método sabiamente y dése cuenta de que puede necesitar excluir o cambiar manualmente
algunos elementos decoradores para prevenir una salida no deseada.
541
Zend_Form
$form->setElementFilters(array('StringTrim'));
• addElements(array $elements)
• setElements(array $elements)
• getElement($name)
• getElements()
• removeElement($name)
• clearElements()
• setDefaults(array $defaults)
• setDefault($name, $value)
• getValue($name)
• getValues()
• getUnfilteredValue($name)
• getUnfilteredValues()
• setElementFilters(array $filters)
• setElementDecorators(array $decorators)
• addElementPrefixPaths(array $spec)
La clase base para los grupos de visualización es Zend_Form_DisplayGroup. Mientras puede ser instanciado
directamente, es mejor usar el método addDisplayGroup() de la claseZend_Form. Este método toma un
array de elementos como primer argumento y el nombre para el grupo de visualización como segundo argumento.
Opcionalmente, se puede pasar en una array de opciones o en un objeto Zend_Config como tercer argumento.
Asumiendo que los elementos 'username' y 'password' has sido determinados en el formulario, el siguiente código
podría agrupar estos elementos en un grupo de visualización 'login':
542
Zend_Form
Puede acceder a los grupos de visualización usando el método getDisplayGroup(), o mediante la sobrecarga
usando el nombre del grupo de visualización:
// Usando getDisplayGroup():
$login = $form->getDisplayGroup('login');
// Usando sobrecarga:
$login = $form->login;
$form->addDisplayGroup(
array('foo', 'bar'),
'foobar',
array('disableLoadDefaultDecorators' => true)
);
Esta opción puede ser una mezcla con otras opciones pasadas, ambas como opciones de array o en el objeto
Zend_Config
Operaciones Globales
Al igual que los elementos, existen algunas operaciones que pueden afectar a todos los grupos de visualización; éstas
incluyen determinar decoradores y fijar la ruta de acceso donde buscar los decoradores.
Ejemplo 23.6. Fijando el Prefijo de Ruta del Decorador para todos los Grupos de
Visualización
Por defecto, los grupos de visualización heredan cualquier ruta de decorador que use el formulario; sin embargo, si
deberían buscar en una ruta alternativa, puede usar el método addDisplayGroupPrefixPath() method.
$form->addDisplayGroupPrefixPath('My_Foo_Decorator', 'My/Foo/Decorator');
$form->setDisplayGroupDecorators(array(
'FormElements',
'Fieldset'
));
543
Zend_Form
permite pasar una instancia determinada, pero permite especificar la clase que usar como una de sus opciones, usando
la clave 'displayGroupClass':
También puede especificar una clase de grupo de visualización por defecto para usar con el formulario, de forma que
todos los grupos de visualización creados con el objeto formulario usen esa clase:
Esta funcionalidad puede especificarse en configuraciones como 'defaultDisplayGroupClass', y será cargada con
antelación para asegurar que todos los grupos de visualización usen esa clase.
• addDisplayGroups(array $groups)
• setDisplayGroups(array $groups)
• getDisplayGroup($name)
• getDisplayGroups()
• removeDisplayGroup($name)
• clearDisplayGroups()
• setDisplayGroupDecorators(array $decorators)
• addDisplayGroupPrefixPath($prefix, $path)
• setDefaultDisplayGroupClass($class)
• getDefaultDisplayGroupClass($class)
Métodos Zend_Form_DisplayGroup
Zend_Form_DisplayGroup tiene los siguientes métodos, agrupados por tipo:
• Configuración:
• setOptions(array $options)
• setConfig(Zend_Config $config)
544
Zend_Form
• Metadatos:
• setAttrib($key, $value)
• addAttribs(array $attribs)
• setAttribs(array $attribs)
• getAttrib($key)
• getAttribs()
• removeAttrib($key)
• clearAttribs()
• setName($name)
• getName()
• setDescription($value)
• getDescription()
• setLegend($legend)
• getLegend()
• setOrder($order)
• getOrder()
• Elementos:
• addElements(array $elements)
• setElements(array $elements)
• getElement($name)
• getElements()
• removeElement($name)
• clearElements()
• Cargadores Complemento:
• setPluginLoader(Zend_Loader_PluginLoader $loader)
• getPluginLoader()
• addPrefixPath($prefix, $path)
• addPrefixPaths(array $spec)
545
Zend_Form
• Decoratores:
• addDecorators(array $decorators)
• setDecorators(array $decorators)
• getDecorator($name)
• getDecorators()
• removeDecorator($name)
• clearDecorators()
• Generadores:
• getView()
• I18n:
• getTranslator()
• setDisableTranslator($flag)
• translatorIsDisabled()
Subformularios
Los Sub formularios sirven para diferentes propósitos:
• Crear grupos de elementos lógicos. Dado que los sub formularios son simplemente formularios, se pueden validar
subformularios como entidades individuales.
• Crear formularios multi-páginas. Dado que los sub formularios son simplemente formularios, se puede deplegar
un sub formulario por separado por página, incrementando formularios multi-páginas donde cada formulario tiene
su propia validación lógica. Solo una vez que todos los sub formularios se validen, el formulario se consideraría
completo.
• Agrupaciones de visualización. Como grupos de visualización, los sub formularios, cuando son generados como
parte de un formulario más grande, pueden ser usados para agrupar elementos. Sea consciente, de todas maneras,
que el objeto formulario principal no tendrá conocimiento de los elementos en un sub formulario.
Un sub formulario puede ser un objeto Zend_Form o mas originalmente, un objeto Zend_Form_SubForm. éste
último contiene decoradores apropiados para la inclusión en un formulario extenso (i.e., no se generan adicionales
formulario etiquetas HTML, pero si grupos de elementos). Para adjuntar un sub formulario, simplemente añádalo al
formulario y déle un nombre:
$form->addSubForm($subForm, 'subform');
546
Zend_Form
Se puede recuperar un sub formulario usando ya sea getSubForm($name) o sobrecarga usando el nombre del
sub formulario:
// Usando getSubForm():
$subForm = $form->getSubForm('subform');
// Usando sobrecarga:
$subForm = $form->subform;
Los Subformularios son incluidos en la interacción del formulario, sin embargo los elementos que lo contienen no
lo son
Operaciones Globales
Como los elementos y los grupos de visualización, existen algunas operaciones que pueden afectar a todos los sub
formularios. A diferencia de los grupos de visualización y los elementos, sin embargo, los sub formularios heredan
más funcionalidad del objeto formulario principal, y la única operación real que puede realizarse globalmente es
determinar decoradores para sub formularios. Para este propósito, existe el método setSubFormDecorators().
En el siguiente ejemplo, determinaremos el decorador para todos los subformularios que sera un simple campo (el
decorador FormElements es necesario para asegurar que los elementos son iterados):
$form->setSubFormDecorators(array(
'FormElements',
'Fieldset'
));
• addSubForms(array $subForms)
• setSubForms(array $subForms)
• getSubForm($name)
• getSubForms()
• removeSubForm($name)
• clearSubForms()
• setSubFormDecorators(array $decorators)
Metadatos y Atributos
Mientras la utilidad de un formulario primariamente deriva de los elementos que contiene, también pueden contener
otros metadatos, como un nombre (usado a menudo como ID único en el marcado HTML ); la accion y el método
del formulario; el número de elementos, grupos y sub formularios que lo contienen; y arbitrariamente metadatos
(usualmente usados para determinar atributos HTML para la etiqueta del propio formulario).
547
Zend_Form
// Determinar el nombre:
$form->setName('registration');
// Recuperar el nombre:
$name = $form->getName();
Para determinar la acción (url en el cual se envia el formulario) y método (método por el cual debería enviar, ej. 'POST'
or 'GET'), use los accesores acción y método:
Se puede también especificar el tipo de codificación del fomulario usando el enctype accessors. Zend_Form define dos
constantes, Zend_Form::ENCTYPE_URLENCODED y Zend_Form::ENCTYPE_MULTIPART, correspondiente
a los valores 'application/x-www-form-urlencoded' y 'multipart/form-data', respectivamente; sin embargo, puede
configurarlo con cualquier tipo de codificación.
Nota
El método, acción y enctype son solo usados internamente para generar, y no para algún tipo de validación.
Zend_Form implementa la interfaz Countable permitiéndole pasarlo como un argumento para contar:
$numItems = count($form);
Determinar metadatos arbitrariamente se realiza a través de los accesores 'atribs'. Dado que la sobrecarga en
Zend_Form es usada para acceder elementos, grupos de visualización y subformularios, este es el único método para
acceder a los metadatos.
// Determinando atributos:
$form->setAttrib('class', 'zend-form')
->addAttribs(array(
'id' => 'registration',
'onSubmit' => 'validate(this)',
));
// Recuperando atributos:
$class = $form->getAttrib('class');
$attribs = $form->getAttribs();
// Removiendo atributos:
$form->removeAttrib('onSubmit');
548
Zend_Form
$form->clearAttribs();
Decoradores
Crear el marcado para un formulario es a menudo una tarea que consume mucho tiempo, particularmente si se planea
reusar el mismo marcado para mostrar acciones tales como validación de errores, enviar valores, etc. La respuesta de
Zend_Form a este problema es los decoradores.
Los decoradores para objetos Zend_Form pueden ser usados para generar un formulario. El decorador FormElements
iterará a través de todos los elementos en un formulario -- elementos, grupos de visualización y subformularios -- y
los generará, devolviendo el resultado. Adicionalmente, los decoradores pueden ser usados para envolver el contenido
o anteponerlo o postponerlo.
Los decoradores por defecto de Zend_Form son FormElements, HtmlTag (envuelve una lista de definición) y Form;
el código equivalente para crearlos es como sigue:
$form->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'dl')),
'Form'
));
Algunos de los atributos se determinan en el objeto formulario que será usado como atributos HTML de la etiqueta
<form>.
Esta opción puede ser combinada con alguna otra opción que usted pueda pasar, tanto como opciones de array
o en un objeto Zend_Config
Para conseguir esto, se pueden usar alias. En vez de pasar un decorador o un nombre de decorador como
primer argumento a addDecorator(), pase un array con un solo elemento, con el alias apuntando al objeto
decorador o nombre:
549
Zend_Form
// y recuperarlo después:
$form = $element->getDecorator('FooBar');
// y recuperándolo después:
$htmlTag = $form->getDecorator('HtmlTag');
$fooBar = $form->getDecorator('FooBar');
Puede crear su propio decorador para generar el formulario. Un caso de uso común es si sabe el HTML exacto que desea
usar; su decorador puede crear el mismo HTML y simplemente retornarlo, potencialmente usando los decoradores de
individuales elementos o grupos de visualización.
Los siguientes métodos puden ser usados para interactuar con decoradores:
• addDecorators(array $decorators)
• setDecorators(array $decorators)
• getDecorator($name)
• getDecorators()
• removeDecorator($name)
• clearDecorators()
Zend_Form también usa sobrecarga que permite generar decoradores específicos. __call() interceptará métodos
que lleve con el texto 'render' y use el resto del nombre del método para buscar un decorador; si se encuentran, serán
generados por un solo decorador. Cualquier argumento pasado a la llamada del método será usado como contenido
que pasar al método render() del decorador. Como ejemplo:
550
Zend_Form
Validación
Un caso de uso primario para formularios es validar datos enviados. Zend_Form le permite validar un formulario
entero de una vez, o una parte de él, asi como también automatizar las respuestas de validación para XmlHttpRequests
(AJAX). Si los datos enviados no son válidos, contiene métodos para recuperar los distintos códigos errores y los
mensajes de elementos y subformularios de validaciones fallidas.
if (!$form->isValid($_POST)) {
// validación fallida
}
isValid() validará cada elemento requerido, y algún elemento no requerido contenido en la data sometida.
Algunas veces se necesitará validar sólo un subset del dato; para esto use isValidPartial($data):
if (!$form->isValidPartial($data)) {
// validación fallida
}
isValidPartial() sólo intenta validar aquellos elementos en la información para los cuales existen similares
elementos; si un elemento es no representado en la información, es pasado por alto.
Cuando se validan elementos o grupos de elementos para un requeirimiento AJAX, típicamente se validará un subset
del formulario, y quiere la respuesta en JSON. processAjax() precisamente realiza eso:
$json = $form->processAjax($data);
Entonces, puede simplemente enviar la respuesta JSON al cliente. Si el formulario es válido, ésta será una respuesta
booleana. Si no, será un objeto javascript conteniendo pares de clave/mensaje, donde cada 'message' es un array de
validación de mensajes de error.
Para los formularios que fallan la validación, se pueden recuperar ambos códigos de error y mensajes de error, usando
getErrors() y getMessages(), respectivamente:
$codes = $form->getErrors();
$messages = $form->getMessage();
Nota
Dado que los mensajes devueltos por getMessages() son un array de pares de errores código/mensaje,
getErrors() no es necesario.
Puede recuperar códigos y mensajes de error para elementos individuales simplemente pasando el nombre del elemento
a cada uno:
$codes = $form->getErrors('username');
551
Zend_Form
$messages = $form->getMessages('username');
Nota
Nota: Cuando validamos elementos, Zend_Form envía un segundo argumento a cada método isValid()
del elemento: el array de los datos que se están validando. Esto puede ser usado por validadores individuales
para permitirles utilizar otros valores enviados al determinar la validez de los datos. Un ejemplo sería un
formulario de registro que requiere tanto una contraseña como una confirmación de la contraseña; el elemento
contraseña puede usar la confirmación de la contraseña como parte de su validación.
• getErrorMessages(): recupera la lista de mensajes de error personalizados que han sido definidos.
• clearErrorMessages(): remueve todos los mensajes de error personalizados que han sido definidos.
• setErrors(array $messages): sobrescribe la pila de mensajes de error personalizada con los mensajes
proporcionados y señala el formulario como inválido.
Métodos
La siguiente lista es la lista completa de métodos disponibles para Zend_Form, agrupados por tipo:
• Configuración y opciones:
• setOptions(array $options)
• setConfig(Zend_Config $config)
552
Zend_Form
• getPluginLoader($type = null)
• addPrefixPaths(array $spec)
• addElementPrefixPaths(array $spec)
• addDisplayGroupPrefixPath($prefix, $path)
• Metadato:
• setAttrib($key, $value)
• addAttribs(array $attribs)
• setAttribs(array $attribs)
• getAttrib($key)
• getAttribs()
• removeAttrib($key)
• clearAttribs()
• setAction($action)
• getAction()
• setMethod($method)
• getMethod()
• setName($name)
• getName()
• Elementos:
• addElements(array $elements)
• setElements(array $elements)
• getElement($name)
• getElements()
• removeElement($name)
• clearElements()
553
Zend_Form
• setDefaults(array $defaults)
• setDefault($name, $value)
• getValue($name)
• getValues()
• getUnfilteredValue($name)
• getUnfilteredValues()
• setElementFilters(array $filters)
• setElementDecorators(array $decorators)
• Subformularios:
• addSubForms(array $subForms)
• setSubForms(array $subForms)
• getSubForm($name)
• getSubForms()
• removeSubForm($name)
• clearSubForms()
• setSubFormDecorators(array $decorators)
• Grupos de Visualización
• addDisplayGroups(array $groups)
• setDisplayGroups(array $groups)
• getDisplayGroup($name)
• getDisplayGroups()
• removeDisplayGroup($name)
• clearDisplayGroups()
• setDisplayGroupDecorators(array $decorators)
• Validación
• populate(array $values)
• isValid(array $data)
554
Zend_Form
• isValidPartial(array $data)
• processAjax(array $data)
• persistData()
• getErrors($name = null)
• getMessages($name = null)
• Generadores:
• getView()
• addDecorators(array $decorators)
• setDecorators(array $decorators)
• getDecorator($name)
• getDecorators()
• removeDecorator($name)
• clearDecorators()
• I18n:
• getTranslator()
• setDisableTranslator($flag)
• translatorIsDisabled()
Configuración
Zend_Form es totalmente configurable mediante setOptions() y setConfig() (o pasando opciones o un
objeto Zend_Config al constructor). Usando estos métodos, se pueden especificar elementos formulario, grupos de
visualización, decoradores, y metadatos.
Como regla general, si 'set' + la clave de opción (option key) hacen referencia a métodos Zend_Form, entonces el
valor proporcionado será pasado al método. Si el accessor no existe, se asume que la clave referencia a un atributo,
y será pasado a setAttrib().
555
Zend_Form
• setConfig
• setDefault
• setOptions
• setPluginLoader
• setSubForms
• setTranslator
• setView
Como un ejemplo, aquí esta un archivo de configuración que pasa la configuración por cada tipo de datos configurables:
[element]
name = "registration"
action = "/user/register"
method = "post"
attribs.class = "zend_form"
attribs.onclick = "validate(this)"
disableTranslator = 0
prefixPath.element.prefix = "My_Element"
prefixPath.element.path = "My/Element/"
elementPrefixPath.validate.prefix = "My_Validate"
elementPrefixPath.validate.path = "My/Validate/"
displayGroupPrefixPath.prefix = "My_Group"
displayGroupPrefixPath.path = "My/Group/"
elements.username.type = "text"
elements.username.options.label = "Username"
elements.username.options.validators.alpha.validator = "Alpha"
elements.username.options.filters.lcase = "StringToLower"
; more elements, of course...
elementFilters.trim = "StringTrim"
;elementDecorators.trim = "StringTrim"
displayGroups.login.elements.username = "username"
displayGroups.login.elements.password = "password"
displayGroupDecorators.elements.decorator = "FormElements"
displayGroupDecorators.fieldset.decorator = "Fieldset"
decorators.elements.decorator = "FormElements"
decorators.fieldset.decorator = "FieldSet"
decorators.fieldset.decorator.options.class = "zend_form"
556
Zend_Form
decorators.form.decorator = "Form"
El código de arriba fácilmente puede ser abstraído a un XML o un archivo de configuración basado en arrays PHP.
Formularios personalizados
Una alternativa a usar los formularios basados en configuraciones es realizar una subclase de Zend_Form. Esto tiene
muchos beneficios:
• Se puede centrar la prueba de su formulario facilmente para asegurar las validaciones y generar la ejecución esperada.
• Reutilización del objeto formulario, y mejor portabilidad (no se necesita seguir los archivos de configuración).
El caso mas típico de uso sería el método init() para determinar elementos de formulario específicos y de
configuración:
$this->addElements(array(
557
Zend_Form
$username,
$password,
$submit
));
$this->setDecorators(array(
'FormElements',
'Fieldset',
'Form'
));
}
}
y toda la funcionalidad está instalada y lista; no se necesitan archivos de configuración. (Note que este ejemplo esta
simplificado, no contiene validadores o filtros para los elementos.)
Otra razón común para la extension es definir un conjunto de decoradores por defecto. Puede hacerlo sobreescribiendo
el método loadDefaultDecorators():
Un número arbitrario de decoradores pueden estar junto a cada elemento (elements, display groups, sub forms o el
objeto form por si mismo); Sin embargo, solo un decorador de un tipo dado puede estar al lado de cada elemento. Los
decoradores son llamados en el orden en que han sido introducidos. Dependiendo del decorador, puede reemplazar el
contenido que se ha pasado, postponerlo o anteponerlo.
El estado del objeto es determinado a través de las opciones de configuración pasadas al constructor o el método
decorador setOptions(). Cuando se crean decoradores mediante funciones addDecorator() o métodos
relacionados, las opciones pueden ser pasadas como argumentos al método. Esto puese ser usado para una ubicación
especifica, un separador se usa entre el contenido pasado y el nuevo contenido generado y cualquier opción que el
decorador soporte.
558
Zend_Form
Antes de que el render() de cada decorador sea llamado, el item actual es determinado en el decorador usando
setElement(), dando al decorador conocimiento del item representado. Esto permite crear decoradores que sólo
representan porciones especificas del item -- tal como etiquetas, el valor, mensajes de error, etc. Encadenando muchos
decoradores que representan especificos segmentos, puede construir marcados complejos representando al item entero.
Operación
Para configurar un decorador, pase un array de opciones o un objeto Zend_Config a este constructor, a un array
setOptions(), o a un objeto Zend_Config setConfig().
• placement: La ubicación puede ser cualquiera de los dos 'append' o 'prepend' (caso insensitivo) e indica cualquier
contenido pasado a render() será postpuesto o antepuesto respectivamente. En el caso de que el decorador
reemplace el contenido, esta configuración es ignorada. La configuración por defecto es adjuntada.
• separator: El separator es usado entre el contenido pasado a render() y el nuevo contenido generado por
el decorador, o entre items generados por el decorador (ejemplo FormElements usa el separador entre cada item
generado). En el caso que un decorador reemplace el contenido, esta configuración puede ser ignorada. El valor
por defecto es PHP_EOL.
La interface del decorador especifica los métodos para interactuar con las opciones. Esto incluye:
Decoradores son diseñados para interactuar con varios tipos de clases Zend_Form: Zend_Form,
Zend_Form_Element, Zend_Form_DisplayGroup, y todas las clases derivan de ellas. El método
setElement() permite determinar el objeto del decorador que esta actualmente trabajando con, y getElement()
es usado para recuperarlo.
Cada método decorador render() acepta una cadena $content. Cuando el primer decorador es llamado, esta
cadena esta tipicamente vacía, mientras las subsecuentes llamadas serán puestas. Basados en el tipo de decorador y
en las opciones pasadas, el decorador ya sea reemplazará la cadena, antenpodrá la cadena o adjuntará la cadena; una
separador opcional será usado en las dos últimas situaciones.
Decoradores estándar
Zend_Form entrega muchos decoradores estándar; ver el capítulo Decoradores estándar para detalles.
Decoradores personalizados
Si encuentra que sus necesidades son complejas o necesita una enorme personalización, debería considerar crear un
decorador personalizado.
interface Zend_Decorator_Interface
559
Zend_Form
{
public function __construct($options = null);
public function setElement($element);
public function getElement();
public function setOptions(array $options);
public function setConfig(Zend_Config $config);
public function setOption($key, $value);
public function getOption($key);
public function getOptions();
public function removeOption($key);
public function clearOptions();
public function render($content);
}
Para hacerlo mas simple, simplemente puede extender Zend_Form_Decorator_Abstract, el cual implementa
todos los métodos excepto render().
Como ejemplo, digamos que quiere reducir el número de decoradores que utiliza, y construir un decorador compuesto
que se encargó de renderizar la etiqueta generadora, el elemento, cualquier mensaje de error, y descripción en un div
HTML. Puede construir como un decorador compuesto como sigue:
560
Zend_Form
if (empty($messages)) {
return '';
}
return '<div class="errors">' .
$element->getView()->formErrors($messages) . '</div>';
}
$separator = $this->getSeparator();
$placement = $this->getPlacement();
$label = $this->buildLabel();
$input = $this->buildInput();
$errors = $this->buildErrors();
$desc = $this->buildDescription();
switch ($placement) {
case (self::PREPEND):
return $output . $separator . $content;
case (self::APPEND):
default:
return $content . $separator . $output;
}
}
}
561
Zend_Form
// para un elemento:
$element->addPrefixPath('My_Decorator',
'My/Decorator/',
'decorator');
Mientras este ejemplo mostró cómo crear un decorador que genera salidas complejas de muchas propiedades de
elementos, puede también crear decoradores que manejen un solo aspecto de un elemento; los decoradores 'Decorator'
y 'Label' son excelentes ejemplos para esta práctica. Hacerlo le permite mezclar y combinar decoradores para llegar a
complejas salidas -- y también anular aspectos de decoración para personalizar sus necesidades.
Por ejemplo, si quiere simplemente desplegar que un error ha ocurrido cuando validábamos un elemento, pero no
desplegar individualmente cada uno de los mensajes de error, usted podría crear su propio decorador 'Errores':
class My_Decorator_Errors
{
public function render($content = '')
{
$output = '<div class="errors">El valor que proporcionó no es válido;
please try again</div>';
$placement = $this->getPlacement();
$separator = $this->getSeparator();
switch ($placement) {
case 'PREPEND':
return $output . $separator . $content;
case 'APPEND':
default:
return $content . $separator . $output;
}
}
}
En este ejemplo particular, debido al segmento del decorador final, 'Errors', se combina como
Zend_Form_Decorator_Errors, será generado en lugar de el decorador -- significa que no necesitará cambiar
ningún decorador para modificar la salida. Nombrando sus decoradores después de los decoradores existentes estándar,
usted puede modificar decoradores sin necesitad de modificar sus elementos decoradores.
562
Zend_Form
Esto puede ser útil particularmente cuando se genera un formulario con el decorador ViewScript; cada elemento puede
usar sus decoradores adjuntos para generar contenido, pero con un control minucioso.
Zend_Form_Element_Button
Usada para crear elementos HTML de tipo button, Zend_Form_Element_Button extiende
Zend_Form_Element_Submit, derivandi sy funcionalidad personalizada. It specifies the 'formButton' view helper for
decoration.
Like the submit element, it uses the element's label as the element value for display purposes; in other words, to set
the text of the button, set the value of the element. The label will be translated if a translation adapter is present.
Because the label is used as part of the element, the button element uses only the ViewHelper and DtDdWrapper
decorators.
Después de llenar o validar un formulario, se puede verificar si el botón dado fue pulsado usando el método
isChecked().
Zend_Form_Element_Captcha
Los CAPTCHAs son usados para prevenir el envio automático de formularios por los robots y otros procesos
automatizados.
The Captcha form element allows you to specify which Zend_Captcha adapter you wish to utilize as a form captcha.
It then sets this adapter as a validator to the object, and uses a Captcha decorator for rendering (which proxies to the
CAPTCHA adapter).
Adapters may be any adapters in Zend_Captcha, as well as any custom adapters you may have defined elsewhere.
To allow this, you may pass an additional plugin loader type key, 'CAPTCHA' or 'captcha', when specifying a plugin
loader prefix path:
563
Zend_Form
Los Captcha entonces pueden ser cargados usando el método setCaptcha(), el cual puede tomar una instancia
cualquiera de CAPTCHA instance, o el nombre corto del adaptador captcha:
// instancia concreta:
$element->setCaptcha(new Zend_Captcha_Figlet());
Si desea cargar sus elementos configuración, especifique la clave 'captcha' con un array conteniendo la clave 'captcha',
o ambas claves 'captcha' y 'captchaOptions':
El decorador usado es determinado consultando el adaptador captcha. Por defecto, es usado el Captcha decorator, pero
un adaptador puede especificar uno diferente vía su métodogetDecorator().
Como ha notado, el adaptador CAPTCHA actúa él mismo como un validador para el elemento. Adicionalmente, el
validador NotEmpty no es usado y el elemento es marcado como requerido. En la mayoría de los casos, usted no
necesitará hacer nada más para tener un captcha presente en su formulario.
Zend_Form_Element_Checkbox
Las casillas de verificación (checkboxes) HTML le permiten devolver un valor específico, pero básicamente funcionan
como los booleanos: cuando está marcada, el valor es enviado; cuando no está marcada, no se envía nada. Internamente,
Zend_Form_Element_Checkbox fuerza este estado.
Por defecto, si la casilla (checkbox) está marcada su valor es '1', y si no está marcada su valor es '0'. You can specify the
values to use using the setCheckedValue() and setUncheckedValue() accessors, respectively. Internally,
any time you set the value, if the provided value matches the checked value, then it is set, but any other value causes
the unchecked value to be set.
564
Zend_Form
Additionally, setting the value sets the checked property of the checkbox. You can query this using isChecked()
or simply accessing the property. Using the setChecked($flag) method will both set the state of the flag as well
as set the appropriate checked or unchecked value in the element. Please use this method when setting the checked
state of a checkbox element to ensure the value is set properly.
Zend_Form_Element_Checkbox uses the 'formCheckbox' view helper. The checked value is always used to
populate it.
Zend_Form_Element_File
The File form element provides a mechanism for supplying file upload fields to your form. It utilizes
Zend_File_Transfer internally to provide this functionality, and the FormFile view helper as also the File decorator
to display the form element.
By default, it uses the Http transfer adapter, which introspects the $_FILES array and allows you to attach validators
and filters. Validators and filters attached to the form element will be attached to the transfer adapter.
También debe asegurarse de que se ha provisto un tipo de codificación corecto al formulario; se debe utilizar 'multipart/
form-data'. Se puede hacer esto estableciendo el atributo 'enctype' en el formulario:
$form->setAttrib('enctype', 'multipart/form-data');
After the form is validated successfully, you must receive the file to store it in the final destination using receive().
Additionally you can determinate the final location using getFileName():
if (!$form->isValid) {
print "Ohoh... validation error";
}
if (!$form->foo->receive()) {
print "Error receiving the file";
}
$location = $form->foo->getFileName();
565
Zend_Form
Valores de archivo
Dentro de HTTP, un elemento file no tiene valor. Por tanto y a causa de razones de seguridad usted solo
obtendrá el nombre del archivo cargado llamando a getValue() y no el destino completo. si usted necesita la
información completa llame a getFileName() y le devolverá el destino y nombre de archivo completo.
Per default the file will automatically be received when you call getValues() on the form. The reason behind this
behaviour is, that the file itself is the value of the file element.
$form->getValues();
Nota
Therefor another call of receive() after calling getValues() will not have an effect. Also creating a
instance of Zend_File_Transfer will not have an effect as there no file anymore to receive.
Still, sometimes you may want to call getValues() without receiving the file. You can archive this by calling
setValueDisabled(true). To get the actual value of this flag you can call isValueDisabled().
Now the file will not be received when you call getValues(). So you must call receive() on the file element,
or an instance of Zend_File_Transfer yourself.
$values = $form->getValues();
if ($form->isValid($form->getPost())) {
if (!$form->foo->receive()) {
print "Upload error";
}
}
There are several states of the uploaded file which can be checked with the following methods:
• isFiltered(): Checks if the filters have already been applied to the file element or not.
566
Zend_Form
->setRequired(false);
$element->addValidator('Size', false, 102400);
$form->addElement($element, 'foo');
// The foo file element is optional but when it's given go into here
if ($form->foo->isUploaded()) {
// foo file given... do something
}
En su vista usted ahora obtendrá 3 elementos para carga de archivos idénticos los cuales comparten la misma
configuración. Para obtener el conjunto del número de archivos múltiples simplemente llame a getMultiFile().
Tan pronto como haya dos elementos file nombrados de forma idéntica, el segundo elemento no se mostrará
o enviará.
Para limitar el tamaño del archivo, el cual es cargado por el cliente, debe establecer el tamaño máximo de archivo que el
formulario acepta . Esto limitará el tamaño del archivo en el lado del cliente configurando la opción MAX_FILE_SIZE
en el formulario. Tan pronto como establezca este valor usando el método setMaxFileSize($size), será
generado con el elemento file.
567
Zend_Form
Note que usted puede establecer MAX_FILE_SIZE una sola vez, incluso si usa múltiples formularios.
Zend_Form_Element_Hidden
Los elementos Hidden simplemente inyectan datos que deben ser enviados, pero que el usuario no debe manipular.
Zend_Form_Element_Hidden logra esto a través del uso del helper de vista 'formHidden'.
Zend_Form_Element_Hash
Este elemento provee protección de ataques desde CSRF sobre formularios, asegurando que el dato es enviado por la
sesión del usuario que generó el formulario y no por un script malicioso. La protección se logra mediante la adición
de un elemento hash a un formulario y verificandolo cuando el formulario es enviado.
El nombre del elemento hash debe ser único. Se recomienda usar la opción salt para el elemento, dos hashes con
el mismo nombre y diferentes salts no chocan:
Zend_Form_Element_Image
Las imágenes pueden ser usadas como elementos de formulario, y le permiten especificar elementos gráficos como
botones de formulario.
Los elementos Image necesitan una imagen fuente. Zend_Form_Element_Image le permite especificar esto
usando el método de acceso setImage() (o clave de configuración 'image'). Opcionalmente, también puede
especificar un valor para utilizar al momento de enviar la imagen utilizando el método de acceso setImageValue()
(o clave de configuración 'imageValue'). Cuando el valor establecido para el elemento sea igual a imageValue,
entonces el método de acceso isChecked() devolverá true.
Los elementos Image usan el Decorador de Imagen para generar (así como el estandard Errors, HtmlTag, y decorador
Label). Opcionalmente, puede especificar una etiqueta para el decorador Image que luego envuelva al elemento
imagen.
Zend_Form_Element_MultiCheckbox
En ocasiones, se tiene un conjunto de checkboxes, y se desea agrupar los resultados. Esto es como un Multiselect, pero
en lugar de estar en una lista desplegable, necesita mostrarlos en pares checkbox/value (casilla de verificación/valor).
Zend_Form_Element_MultiCheckbox hace esto sencillo. Like all other elements extending the base Multi
element, you can specify a list of options, and easily validate against that same list. The 'formMultiCheckbox' view
helper ensures that these are returned as an array in the form submission.
568
Zend_Form
Por defecto, este elemnto requiere un validador InArray el cual valida contra el array de llaves de las opciones
registradas. Se puede desactivar esta caracteristica llamando a setRegisterInArrayValidator(false), o
pasando un valor falso a la configuración de llaves registerInArrayValidator.
• addMultiOption($option, $value)
• addMultiOptions(array $options)
• getMultiOption($option)
• getMultiOptions()
• removeMultiOption($option)
• clearMultiOptions()
Para marcar los valores confirmados, se necesita pasar un array de valores a setValue(). El siguiente código
verificará los valores "bar" y "bat":
$element->setValue(array('bar', 'bat'));
Zend_Form_Element_Multiselect
XHTML selector de elementos permite 'multiple' atributos, indicando multiples opciones pueden ser seleccionados
por submision, en vez de lo usual. Zend_Form_Element_Multiselect extiende Zend_Form_Element_Select,
y define los atributos multiple a 'multiple'. Como las otras clases que heredan de la clase base
Zend_Form_Element_Multi, se puede manipular las opciones del selector usando:
• addMultiOption($option, $value)
• addMultiOptions(array $options)
• getMultiOption($option)
• getMultiOptions()
• removeMultiOption($option)
• clearMultiOptions()
569
Zend_Form
Si un adaptador de tranducción es registrado con el formulario y/o elemnto, la opción valores será traducido para
propósito de despliegue.
Por defecto, este elemento registra un validador InArray el cual valida contra el array de llaves de opciones
registradas. se puede deshabilitar esta caracteristica llamando a setRegisterInArrayValidator(false), o
pasando un valor falso a la configuracion de llaves registerInArrayValidator.
Zend_Form_Element_Password
Elementos contraseña son basicamente elementos de texto -- excepto que tipicamente no se quiera desplegar la
contraseña en los mensajes de error o del elemnto en si cuando el formulario es re desplegado.
Zend_Form_Element_Radio
elementos de Radio permite especificar muchas opciones, de los cuales se necesita retornar un solo
valor. Zend_Form_Element_Radio extiende la clase base Zend_Form_Element_Multi, permitiendonos
especificar un numero de opciones, y luego usa la vista ayuda formRadio para desplegar.
Por defecto, este elemento registra un validador InArray el cual valida contra el array de llaves de opciones
registradas. se puede deshabilitar esta caracteristica llamando a setRegisterInArrayValidator(false), o
pasando un valor falso a la configuracion de llaves registerInArrayValidator. configuration key.
Como todos los elementos se extienden del elemento clase base Multi, los siguientes métodos pueden ser usados para
manipular las opciones de radio desplegadas:
• addMultiOption($option, $value)
• addMultiOptions(array $options)
• getMultiOption($option)
• getMultiOptions()
• removeMultiOption($option)
• clearMultiOptions()
Zend_Form_Element_Reset
Botones Reset son tipicamente usados para limpiar un formulario, y no son parte de la información sometida. Como
sea, como ellos sirven como propósito en el despliegue, son incluidos en los elementos estándar.
Zend_Form_Element_Select
Cajas selectoras son una manera común de limitar espeficias opciones para un dado formulario datum.
Zend_Form_Element_Select le permite generar esto rápido y fácil.
570
Zend_Form
Por defecto, este elemento registra un validador InArray el cual valida contra el array de llaves de opciones
registradas. se puede deshabilitar esta caracteristica llamando a setRegisterInArrayValidator(false), o
pasando un valor falso a la configuracion de llaves registerInArrayValidator. configuration key.
Como se extiende el elemento base Multi, los siguientes métodos pueden ser usados para manipular las opciones
seleccionadas:
• addMultiOption($option, $value)
• addMultiOptions(array $options)
• getMultiOption($option)
• getMultiOptions()
• removeMultiOption($option)
• clearMultiOptions()
Zend_Form_Element_Submit
Submit buttons are used to submit a form. You may use multiple submit buttons; you can use the button used to
submit the form to decide what action to take with the data submitted. Zend_Form_Element_Submit makes this
decisioning easy, by adding a isChecked() method; as only one button element will be submitted by the form, after
populating or validating the form, you can call this method on each submit button to determine which one was used.
Zend_Form_Element_Submit usa la etiqueta como el "valor" del botón sometido, traduciendolo si el adaptador
traducción esta presente. isChecked() verifica el valor sometido contra la etiqueta en orden to determinar si el
botón ha sido usado.
El ViewHelper y DtDdWrapper decoradores generan al elemento. no decorador de etiquetas es usado, como el botón
etiqueta es usado cuando se generan los elementos; asi tipicamente, no se asociarán errores con el elemnto sometido.
Zend_Form_Element_Text
Lejos el mas prevaleciente tipo de formulario es el elemento texto, permitido para entrada de texto limitado; es un
elemento ideal para la entrada de la información. Zend_Form_Element_Text simplemente usa la vista ayuda
'formText' para desplegar el elemento.
Zend_Form_Element_Textarea
Textareas son usadas cuando se espera una larga cantidad de texto y no limites en la cantidad de texto sometido ( otro
que el máximo tamaño tomado por su servidor or PHP). Zend_Form_Element_Textarea usa la vista ayuda
'textArea' para desplegar tales elementos, ocupando el valor como el contendio del elemento.
571
Zend_Form
Zend_Form_Decorator_Callback
El decorador Callback (llamada de retorno) permite ejecutar una llamada de retorno para mostrar el contenido. Las
llamadas de retorno deben especificarse a través de la opción 'callback' pasada en la configuración del decorador, y
pueden ser de cualquier valor de llamada de retorno PHP. Los Callbacks deben aceptar tres argumentos: $content
(el contenido original enviado al decorador), $element (el objeto que se está decorando), y un array de $options.
Un callback de ejemplo sería:
class Util
{
public static function label($content, $element, array $options)
{
return '<span class="label">' . $element->getLabel() . "</span>";
}
}
Esta llamada de retorno se especificaría como array('Util', 'label'), y generaría un (mal) código HTML
para la etiqueta. El decorador Callback reemplazará, antepondrá o postpondrá el contenido original con el que devuelva.
El decorador Callback permite especificar un valor null para la opción placement (colocación), que reemplazará el
contenido original con el valor devuelto de la llamada de retorno; 'prepend' (anteponer) y 'append' (postponer) siguen
siendo válidas.
Zend_Form_Decorator_Captcha
El decorador Captcha se usa junto con el elemento de formulario Captcha. Utiliza el método render() del adaptador
del captcha para generar la salida.
Una variante del decorador Captcha, 'Captcha_Word', es usada frecuentemente, y crea dos elementos, un id y una
entrada (input). El id indica el identificador de sesión que hay que comparar, y la entrada es para la verificación de
usuario del captcha. Éstos elementos son validados como un sólo elemento.
Zend_Form_Decorator_Description
El decorador Description puede ser usado para mostrar un conjunto de descripciones de un elemento
Zend_Form, Zend_Form_Element, o Zend_Form_DisplayGroup; toma la descripción usando el método
getDescription() del objeto.
Por defecto, si no se encuentra la descripción, no se genera ninguna salida. Si la descripción está presente, entonces se
envolverá en una etiqueta p HTML por defecto, aunque tiene la posibilidad de especificar una etiqueta pasando una
opción tag al crear el decorador, o llamando a setTag(). También puede especificar una clase para el tag usando
la opción class o llamando a setClass(); por defecto, se usa la clase 'hint'.
La descripción es escapada utilizando los mecanismos de escapado por defecto del objeto de vista. Puede desactivar
esto pasando un valor false a la opción 'escape' del decorador o el método setEscape().
Zend_Form_Decorator_DtDdWrapper
Los decoradores por defecto utilizan listas de definición (<dl>) para generar elementos de formulario (form). Dato
que los elementos de formulario pueden aparecer en cualquier orden, grupos de visualización y subformularios pueden
ser encapsulados dentro de otros elementos de formulario. Para mantener estos tipos de elemento particulares dentro de
la lista de definición, DtDdWrapper crea una nuevo término de definición vacío (definition term)(<dt>) y encapsula
su contenido en un nuevo dato de definición (<dd>). La salida queda como sigue:
572
Zend_Form
<dt></dt>
<dd><fieldset id="subform">
<legend>Información de Usuario</legend>
...
</fieldset></dd>
Este decorador reemplaza el contenido que se le provee envolviéndolo dentro del elemento <dd>.
Zend_Form_Decorator_Errors
Los errores de elemento obtienen su propio decorador con el decorador de errores. Este decorador sustituye al view
helper FormErrors, que genera mensajes de error en una lista no ordenada (<ul>) como elementos de lista (li). El
elemento <ul> recibe una clase de "errores".
Zend_Form_Decorator_Fieldset
Por defecto, los grupos de visualización y subformularios generan sus contenidos dentro de fieldsets, EL decorador
Fieldset busca la opción 'legend' o bien el método getLegend() en el elemento registrado, y lo usa como campo
"legend" si no es vacío. Cualquier contenido pasado es envuelto en el fieldset HTML, reemplazando al contenido
original. Cualquier atributo pasado al elemento decorado será generado como atributo del fieldset HTML.
Zend_Form_Decorator_File
Los elementos de tipo "File" (upload de ficheros) tienen una notación especial cuando se usan múltiples elementos file
o subformularios. El decorador File es usado por Zend_Form_Element_File y permite fijar múltiples elementos
file con una única llamada al método. Se usa automáticamente y fija el nombre de cada elemento.
Zend_Form_Decorator_Form
Los objetos Zend_Form normalmente necesitan generar una etiqueta HTML "form". El decorador Form utiliza la
ayuda del view helper Form. Encapsula cualquier contenido provista en un elemento HTML form, usando la acción y
el método del objeto Zend Form, y cualquier atributo como atributo HTML.
Zend_Form_Decorator_FormElements
Los formularios(forms), grupos de visualización y subformularios son colecciones de elementos. Para poder generar
estos elementos, utilizan el decorador FormElements, el cual itera sobre todos los elementos, llamando a render()
en cada uno de ellos y uniéndolos con el separador indicado. Puede anteponer o postponer al contenido que se le envía.
Zend_Form_Decorator_FormErrors
Algunos desarrolladores y diseñadores prefieren agrupar todos los mensajes de error en la parte superior del formulario.
El decorador FormErrors le permite hacer esto.
<ul class="form-errors">
<li><b>[etiqueta de elemento o nombre]</b><ul>
<li>[mensaje de error]</li>
573
Zend_Form
<li>[mensaje de error]</li>
</ul>
</li>
<li><ul>
<li><b>[etiqueta o nombre de elemento subformulario</b><ul>
<li>[mensaje de error]</li>
<li>[mensaje de error]</li>
</ul>
</li>
</ul></li>
</ul>
Puede pasar como parámetro varias opciones para configurar la salida generada:
• ignoreSubForms: se desactiva o no la recursividad en los subformularios. Por defecto: false (i.e., permitir
recursividad).
• markupElementLabelEnd: Marcado para postponer las etiquetas de elementos. Por defecto: '</b>'
• markupListEnd: Marcado para postponer listas de mensajes de error. Por defecto: '</ul>'.
• markupListItemEnd: Marcado para postponer mensajes de error individuales. Por defecto: '</li>'
• markupListItemStart: Marcado para anteponer mensajes de error individuales. Por defecto: '<li>'
• markupListStart: Marcado para anteponer listas de mensajes de error. Por defecto: '<ul class="form-errors">'
Zend_Form_Decorator_HtmlTag
El decorador HtmlTag le permite utilizar etiquetas HTML para decorador el contenido; la etiqueta utiliza es pasada
en la opción 'tag' , y cualquier otra opción es usada como atributo HTML de esa etiqueta. Por defecto, el contenido
generado reemplaza al contenido envolviéndolo en la etiqueta dada. De cualquier forma, se permite especificar una
localización de tipo 'append' (postponer) o 'prepend' (anteponer).
Zend_Form_Decorator_Image
El decorador Image le permite crear un input HTML de tipo image (<input type="image" ... />), y
opcionalmente mostrarlo dentro de otro tag HTML.
Por defecto, el decorador usa la propiedad src del elemento, que puede fijarse con el método setImage(), como la
ruta de la imagen ('src'). Adicionalmente, la etiqueta del elemento será usada como la etiqueta 'alt', y imageValue
(manipulado con los métodos setImageValue() y getImageValue()) será usada como el campo 'value'.
Para especificar una etiqueta HTML que utilizar con el elemento, pase la opción 'tag' al decorador, o llame
explícitamente a setTag().
Zend_Form_Decorator_Label
Comúnmente, los elementos de formulario tienen etiquetas (labels) y se usa el decorador Label para generar
esas etiquetas. Utiliza la ayuda del view helper FormLabel, y toma la etiqueta del elemento mediante el método
getLabel() de ese elemento. Si no se encuentra la etiqueta, no se genera. Por defecto, las etiquetas se traducen
cuando existe un adaptador de traducciones y existe una traducción para la etiqueta.
574
Zend_Form
Opcionalmente, se puede especificar la opción 'tag'; si se suministra, encapsula la etiqueta en la etiqueta HTML en
cuestión. Si la opción está presenta pero no hay etiqueta, la etiqueta será generada sin contenido. Puede especificar la
clase que usar con la etiqueta mediante la opción 'class' o llamando a setClass().
Adicionalmente, se pueden especificar prefijos o sufijos que usar al mostrar en pantalla los elementos, basados en si
la etiqueta es para un elemento opcional o requerido. Por ejemplo, podríamos querer añadir ':' a la etiqueta o un '*',
indicando que el elemento es requerido. Se puede realizar con las siguientes opciones y métodos:
• optionalPrefix: fija el texto antepuesto a la etiqueta cuando el elemento es opcional. Utilice los accesores
setOptionalPrefix() y getOptionalPrefix() para manipularlo.
• optionalSuffix: fija el texto pospuesto a la etiqueta cuando el elemento es opcional. Utilice los accesores
setOptionalSuffix() y getOptionalSuffix() para manipularlo.
• requiredPrefix: fija el texto antepuesto a la etiqueta cuando el elemento es requerido. Utilice los accesores
setRequiredPrefix() y getRequiredPrefix() para manipularlo.
• requiredSuffix: fija el texto antepuesto a la etiqueta cuando el elemento es requerido. Utilice los accesores
setRequiredSuffix() y getRequiredSuffix() para manipularlo.
Por defecto, el decorador Label antecede al contenido provisto; especifique la opción 'placement' (colocación) como
'append' para colocarlo después del contenido.
Zend_Form_Decorator_PrepareElements
Formularios, grupos de visualización, y subformularios son colecciones de elementos. Al usar el decorador ViewScript
con un formulario o subformulario, resulta útil el poder fijar recursívamente el objeto de vista, el traductor (translator)y
todos los nombres relacionados (determinados por la notiación de tabla del subformulario). Esta tarea puede realizarse
gracias al decorador 'PrepareElements'. Normalmente, se indicará como el primer decorador en al lista.
$form->setDecorators(array(
'PrepareElements',
array('ViewScript', array('viewScript' => 'form.phtml')),
));
Zend_Form_Decorator_ViewHelper
La mayoría de los elementos utiliza helpers Zend_View para generar el contenido, y esto se realiza con el decorador
ViewHelper. Con él, se puede especificar una etiqueta 'helper' para fijar explicitamente el view helper que utilizar;
si no se suministra ninguno, utiliza el último segmento del nombre de clase del elemento para determinar el helper,
anteponiéndole la cadena 'form': e.g., 'Zend_Form_Element_Text' buscaría un view helper del tipo 'formText'.
Cualquier atributo del elemento suministrado es pasado al view helper como atributo del elemento.
Por defecto, este decorador postpone el contenido; utilice la opción 'placement' para especificar una localización
distinta.
Zend_Form_Decorator_ViewScript
A veces es necesario usar un view script para crear elementos; De esta forma, se puede tener un control preciso sobre
los elementos; entregar el view script a un diseñador, o simplemente crear una forma fácil de sobreescribir basado en
el módulo que se esté usando. El decorador ViewScript soluciona este problema.
El decorador ViewScript requiere una opción 'viewScript', o bien suministrada al decorador, o bien como atributo del
elemento. Entonces genera ese script de vista como un script parcial, lo que significa que cada llamada a él tiene su
575
Zend_Form
propio espacio de variables; Ninguna variable de la vista será rellenada, aparte del elemento en sí. Distintas variables
son entonces rellenadas:
• Del mismo modo, todas las opciones pasadas al decorador a través de setOptions() que no son usadas
internamente (tales como placement, separator, etc.) son pasadas como variables de vista.
576
Zend_Form
Se recomienda usar el decorador ViewScript cuando necesite un control muy preciso sobre cómo generados sus
elementos.
Internacionalización de Zend_Form
Cada vez más, desarrolladores necesitan adaptar su contenido para multiples idiomas y regiones. Zend_Form intenta
hacer de ésta una tarea trivial, y provee funcionalidad en ambas Zend_Translate y Zend_Validate para realizar esa
funcionalidad.
Por defecto, no se realiza ninguna internacionalización (I18n). Para iniciar las caraterísticas de I18n en Zend_Form,
se necesitará instanciar un objeto Zend_Translate con un adaptador apropiado, y adjuntarlo a Zend_Form y/
o Zend_Validate. Ver la documentación Zend_Translate para más información sobre crear el objeto traducción
y los archivos de traducción
• Fácil: añadirlo al registro. Todos los componentes I18n de Zend Framework descubrirán automáticamente un
objeto traducción que está en el registro con la clave 'Zend_Translate' y lo usará para ejecutar la traducción y/o
la localización:
• Si todo lo que le preocupa es traducir los mensajes de error de validación, puede registrar el objeto traducción con
Zend_Validate_Abstract:
// Decir a todas las clases de validación que se use un adaptador especifico de traducció
Zend_Validate_Abstract::setDefaultTranslator($translate);
• Alternativamente, se puede adjuntar al objeto Zend_Form como un traductor global. Tiene el mismo efecto que
traduciendo los mensajes de error de validación.
// Decir a todas las clases del formulario usar un adaptador especifico, así como también
// use este adaptador para traducir mensajes de error de validación
Zend_Form::setDefaultTranslator($translate);
577
Zend_Form
• Finalmente, se puede adjuntar un traductor a una instancia especifica de un formulario o a elementos especificar
usando sus métodos setTranslator():
// Decir a *esta* instancia del formulario que use un adaptador especifico de traducción;
// será usado para traducir mensajes de error de validación para todos los
// elementos:
$form->setTranslator($translate);
• Mensajes de error de validación. Los mensajes de error de validación pueden ser traducidos. Para hacerlo, use la
variedad de constantes de códigos de error de Zend_Validate las clases de validación como los ID del mensaje.
Para más información sobre esos códigos, ver la documentación Zend_Validate.
Alternativamente, desde la versión 1.6.0, se pueden proveer cadenas de traducción usando los mensajes de error
actuales como mensajes identificadores. Este es el caso preferido de uso para 1.6.0 en adelante, así como también
se volverá obsoleta la traducción de las claves de mensajes en versiones futuras.
• Leyendas de campos. Grupos de visualización y subformularios se generan por defecto en fieldsets. El decorador
de fieldsets intenta traducir la leyenda antes de generar el fieldset.
• Descripciones de formularios y elementos. Todos los tipos de formulario (elemento, formulario, visualización de
grupos, subformularios) permiten especificar una descripción opcional. El decorador Description puede generarlo
y por defecto tomará el valor e intentará traducirlo.
• Valores multi-opción. Para los múltiples items que heredan de Zend_Form_Element_Multi (incluyendo
el MultiCheckbox, Multiselect y elementos Radio), la valores de opciones (no claves) serán traducidos si una
traducción esta disponible; eso significa que las etiquetas de opciones presentadas al usuario serán traducidas.
• Submit y etiquetas Button. Los múltiples elementos Submit y Button (Button, Submit y Reset) traducirán la etiqueta
mostrada al usuario.
Notación de array
A muchos desarroladores web experimentados les gusta agrupar elementos relacionados de formulario usando notación
de array en los nombres del elemento. Por ejemplo, si se tienen dos direcciones que se desean capturar, un envío y una
dirección de facturación, se pueden tener elementos idénticos; agrupándolos en un array se puede asegurar que son
capturados por separado. Nótese el siguiente formulario por ejemplo:
578
Zend_Form
<form>
<fieldset>
<legend>Shipping Address</legend>
<dl>
<dt><label for="recipient">Ship to:</label></dt>
<dd><input name="recipient" type="text" value="" /></dd>
<dt><label for="address">Address:</label></dt>
<dd><input name="address" type="text" value="" /></dd>
<dt><label for="municipality">City:</label></dt>
<dd><input name="municipality" type="text" value="" /></dd>
<dt><label for="province">State:</label></dt>
<dd><input name="province" type="text" value="" /></dd>
<fieldset>
<legend>Billing Address</legend>
<dl>
<dt><label for="payer">Bill To:</label></dt>
<dd><input name="payer" type="text" value="" /></dd>
<dt><label for="address">Address:</label></dt>
<dd><input name="address" type="text" value="" /></dd>
<dt><label for="municipality">City:</label></dt>
<dd><input name="municipality" type="text" value="" /></dd>
<dt><label for="province">State:</label></dt>
<dd><input name="province" type="text" value="" /></dd>
<dl>
<dt><label for="terms">I agree to the Terms of Service</label></dt>
<dd><input name="terms" type="checkbox" value="" /></dd>
<dt></dt>
<dd><input name="save" type="submit" value="Save" /></dd>
</dl>
</form>
En este ejemplo, la facturación y la dirección de envío contienen algunos campos idénticos, eso significa que uno
puede sobrescribir al otro. Nosotros podemos resolver esta solución usando una notación de array:
579
Zend_Form
<form>
<fieldset>
<legend>Shipping Address</legend>
<dl>
<dt><label for="shipping-recipient">Ship to:</label></dt>
<dd><input name="shipping[recipient]" id="shipping-recipient"
type="text" value="" /></dd>
<dt><label for="shipping-address">Address:</label></dt>
<dd><input name="shipping[address]" id="shipping-address"
type="text" value="" /></dd>
<dt><label for="shipping-municipality">City:</label></dt>
<dd><input name="shipping[municipality]" id="shipping-municipality"
type="text" value="" /></dd>
<dt><label for="shipping-province">State:</label></dt>
<dd><input name="shipping[province]" id="shipping-province"
type="text" value="" /></dd>
<fieldset>
<legend>Billing Address</legend>
<dl>
<dt><label for="billing-payer">Bill To:</label></dt>
<dd><input name="billing[payer]" id="billing-payer"
type="text" value="" /></dd>
<dt><label for="billing-address">Address:</label></dt>
<dd><input name="billing[address]" id="billing-address"
type="text" value="" /></dd>
<dt><label for="billing-municipality">City:</label></dt>
<dd><input name="billing[municipality]" id="billing-municipality"
type="text" value="" /></dd>
<dt><label for="billing-province">State:</label></dt>
<dd><input name="billing[province]" id="billing-province"
type="text" value="" /></dd>
<dl>
<dt><label for="terms">I agree to the Terms of Service</label></dt>
<dd><input name="terms" type="checkbox" value="" /></dd>
580
Zend_Form
<dt></dt>
<dd><input name="save" type="submit" value="Save" /></dd>
</dl>
</form>
En el ejemplo anterior, obtenemos direcciones separadas. En el formulario sometido, ahora tenemos tres elementos,
'guardar' elemento para someterlo, y dos arrays, 'envio' y 'cuenta', cada uno con llaves para los variados elementos.
Zend_Form intenta automatizar este proceso con los subformularios. Por defecto, los subformularios son generados
usando la notación de array como se muestra en el anterior formulario HTML listado completo con identificadores.
El nombre del array esta basado en el nombre del subformulario, con las llaves basados en los elementos contenidos
en el subformulario. Los subformularios pueder ser anidados arbitrariamente, y esto puede crear arrays anidados que
reflejan la estructura. Adicionalmente, las validaciones rutinarias en Zend_Form respetan la estructura del array,
asegurando que sus formularios sean validados correctamente, no importa cuan arbitrariamente anidados esten los
subformularios. No se necesita hacer nada para beneficiarse; éste comportamiento esta activo por defecto.
Adicionalmente, existen facilidades que le permiten activar condicionalmente la notación de un array, así como
también especificar el específico array al cual un elemento o coleccion pertenece:
Se deberá determinar si un formulario está siendo tratado como un array usando el accesor isArray().
Adicionalmente, a nivel del elemento, se pueden especificar elementos individuales que puedan pertenecer a arrays
particulares usando el método Zend_Form_Element::setBelongsTo(). Para descubrir el valor que tiene
-- sea o no sea definido explícitamente o implícitamente a través del formulario -- se puede usar el accesor
getBelongsTo().
Formularios Multi-Página
Actualmente, los formularios multi-página no están oficialmente soportados en Zend_Form; sin embargo, la mayoría
del soporte para implementarlos está disponible y puede ser utilizado con algunos retoques.
La clave para crear fomrularios multi-página es utilizar subformularios, pero solo para mostrar un solo subformulario
por página. Esto le permite someter un solo subformulario a la vez y validarlo, pero no procesar el formulario hasta
que todos los subformularios esten completos.
Primero, vamos a crear nuestro propio formulario, y definir varios subformularios dentro del mismo:
581
Zend_Form
582
Zend_Form
)),
Note que no hay botones de enviar, y que ni hemos hecho nada con los decoradores de subformularios -- lo que significa
que por defecto serán desplegados como campos. Necesitaremos hacer algo con ellos mientras desplegamos cada
subformulario individualmente, y añadir botones de manera que podamos procesarlos realmente -- el cual requerira
las propiedades acción y método. Vamos a añadir algunos andamios a nuestras clases para proveer esa información:
583
Zend_Form
/**
* Prepara un subformulario para mostrar
*
* @param string|Zend_Form_SubForm $spec
* @return Zend_Form_SubForm
*/
public function prepareSubForm($spec)
{
if (is_string($spec)) {
$subForm = $this->{$spec};
} elseif ($spec instanceof Zend_Form_SubForm) {
$subForm = $spec;
} else {
throw new Exception('Invalid argument passed to ' .
__FUNCTION__ . '()');
}
$this->setSubFormDecorators($subForm)
->addSubmitButton($subForm)
->addSubFormActions($subForm);
return $subForm;
}
/**
* Add form decorators to an individual sub form
*
* @param Zend_Form_SubForm $subForm
* @return My_Form_Registration
*/
public function setSubFormDecorators(Zend_Form_SubForm $subForm)
{
$subForm->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'dl',
'class' => 'zend_form')),
'Form',
));
return $this;
}
/**
* Añade un Boton de envio(submit) a cada subformulario
*
* @param Zend_Form_SubForm $subForm
* @return My_Form_Registration
*/
public function addSubmitButton(Zend_Form_SubForm $subForm)
{
$subForm->addElement(new Zend_Form_Element_Submit(
'save',
array(
'label' => 'Save and continue',
'required' => false,
'ignore' => true,
584
Zend_Form
)
));
return $this;
}
/**
* Añade el method y el action a cada subformulario
*
* @param Zend_Form_SubForm $subForm
* @return My_Form_Registration
*/
public function addSubFormActions(Zend_Form_SubForm $subForm)
{
$subForm->setAction('/registration/process')
->setMethod('post');
return $this;
}
}
Siguiente, necesitamos añadir andamios a nuestro action controller, y tener varias consideraciones. Primero,
necesitamos asegurar que persiste la información del formulario entre los requerimientos, de esa manera determinar
cuándo terminar. Segundo, necesitamos alguna lógica para determinar qué segmentos del formulario han sido
sometidos, y qué subformulario mostrar de acuerdo a la información. Usaremos Zend_Session_Namespace para
persistir la información, el cual nos ayudará a responder la pregunta de qué formulario someter.
Vamos a crear nuestro controlador, y añadir un método para recuperar un formulario instanciado:
Ahora, vamos a añadir algunas funcionalidades para determinar qué formulario mostrar. Básicamente, hasta que el
formulario entero sea considerado válido, necesitamos continuar mostrando segmentos de formulario. Adicionalmente,
queremos asegurar que están en un orden particular: usuario, demog, y después las listas. Podemos determinar qué
información ha sido sometida verificando nuestro session namespace para claves particulares representando cada
subformulario.
585
Zend_Form
/**
* Obtiene el namespace de la sesión que estamos usando
*
* @return Zend_Session_Namespace
*/
public function getSessionNamespace()
{
if (null === $this->_session) {
$this->_session =
new Zend_Session_Namespace($this->_namespace);
}
return $this->_session;
}
/**
* Obtiene la lista de Formularios que ya están almacenados en la sesión
*
* @return array
*/
public function getStoredForms()
{
$stored = array();
foreach ($this->getSessionNamespace() as $key => $value) {
$stored[] = $key;
}
return $stored;
}
/**
* Obtiene la lista de todos los subformularios disponibles
*
* @return array
*/
public function getPotentialForms()
{
return array_keys($this->getForm()->getSubForms());
}
/**
* ¿Qué subformulario se envio?
*
* @return false|Zend_Form_SubForm
*/
public function getCurrentSubForm()
{
$request = $this->getRequest();
if (!$request->isPost()) {
return false;
}
586
Zend_Form
if (is_array($data)) {
return $this->getForm()->getSubForm($name);
break;
}
}
}
return false;
}
/**
* Obtiene el siguiente subformulario para mostrarlo
*
* @return Zend_Form_SubForm|false
*/
public function getNextSubForm()
{
$storedForms = $this->getStoredForms();
$potentialForms = $this->getPotentialForms();
return false;
}
}
El método de arriba nos permite usar notaciones tal como "$subForm = $this->getCurrentSubForm();"
recuperar el actual subformulario para la validación, o "$next = $this->getNextSubForm();" obtener el
siguiente para mostrar.
Ahora, vamos a encontrar la manera para procesar y mostrar varios subformularios. Podemos usar
getCurrentSubForm() para determinar si algún subformulario ha sido sometido (los valores de retorno falso
indican que ninguno ha sido desplegado o sometido), y getNextSubForm() recupera el formulario que mostrar.
Podemos entonces usar el método del formulario prepareSubForm() para asegurar que el formulario está listo
para mostrar.
Cuando tenemos un formulario sometido, podemos validar el subformulario, y luego verificar si el formulario entero
es válido ahora. Para hacer esas tareas, necesitamos métodos adicionales que aseguren que la información sometida
es añadida a la sesión, y que cuando validamos el formulario entero, nosotros validamos contra todos los segmentos
de la sesión:
/**
* ¿Es válido el subformulario?
*
* @param Zend_Form_SubForm $subForm
* @param array $data
587
Zend_Form
* @return bool
*/
public function subFormIsValid(Zend_Form_SubForm $subForm,
array $data)
{
$name = $subForm->getName();
if ($subForm->isValid($data)) {
$this->getSessionNamespace()->$name = $subForm->getValues();
return true;
}
return false;
}
/**
* ¿Es válido todo el formulario?
*
* @return bool
*/
public function formIsValid()
{
$data = array();
foreach ($this->getSessionNamespace() as $key => $info) {
$data[$key] = $info;
}
return $this->getForm()->isValid($data);
}
}
Ahora que tenemos el trabajo preparado, vamos a construir las acciones para este controlador. Necesitaremos una
página de destino para el formulario, y luego una acción 'process' para procesar el formulario.
588
Zend_Form
if (!$this->subFormIsValid($form,
$this->getRequest()->getPost())) {
$this->view->form = $this->getForm()->prepareSubForm($form);
return $this->render('index');
}
if (!$this->formIsValid()) {
$form = $this->getNextSubForm();
$this->view->form = $this->getForm()->prepareSubForm($form);
return $this->render('index');
}
// Formulario Válido!
// Render information in a verification page
$this->view->info = $this->getSessionNamespace();
$this->render('verification');
}
}
Como se ha notado, el código actual para procesar el formulario es relativamente simple. Verificamos si tenemos un
subformulario actual sometido y si no, retornamos a la página de destino. Si tenemos un subformulario, intentaremos
validarlo, volviéndolo a mostrar si tiene fallos. Si el subformulario es válido, entonces verificaremos si el formulario
es válido, lo que debería indicar que hemos terminado; si no, mostraremos el siguiente segmento del formulario.
Finalmente, mostraremos una página de verificación con el contenido de la sesión.
<?
// Tienen que construir esto con los items que estan almacenados en los namespaces
// de la sesión
foreach ($this->info as $info):
foreach ($info as $form => $data): ?>
<h4><?php echo ucfirst($form) ?>:</h4>
<dl>
<? foreach ($data as $key => $value): ?>
<dt><?php echo ucfirst($key) ?></dt>
<? if (is_array($value)):
foreach ($value as $label => $val): ?>
<dd><?php echo $val ?></dd>
<? endforeach;
else: ?>
<dd><?php echo $this->escape($value) ?></dd>
<? endif;
589
Zend_Form
endforeach; ?>
</dl>
<? endforeach;
endforeach
Próximas novedades de Zend Framework incluirán componentes para hacer formularios multi páginas mas simples,
abstrayendo la sesión y la lógica de orden. Mientras tanto, el ejemplo de arriba debería servir como guia razonable
para alcanzar esta tarea en su web.
590
Capítulo 24. Zend_Gdata
Introduction
Google Data APIs provide programmatic interface to some of Google's online services. The Google data Protocol
is based upon the Atom Publishing Protocol [http://ietfreport.isoc.org/idref/draft-ietf-atompub-protocol/] and allows
client applications to retrieve data matching queries, post data, update data and delete data using standard HTTP and the
Atom syndication formation. The Zend_Gdata component is a PHP 5 interface for accessing Google Data from PHP.
The Zend_Gdata component also supports accessing other services implementing the Atom Publishing Protocol.
• Google Spreadsheets provides an online collaborative spreadsheets tool which can be used as a simple data store
for your applications.
• Google Documents List provides an online list of all spreadsheets, word processing documents, and presentations
stored in a Google account.
• Google Provisioning provides the ability to create, retrieve, update, and delete user accounts, nicknames, and email
lists on a Google Apps hosted domain.
• Google Base provides the ability to retrieve, post, update, and delete items in Google Base.
• YouTube provides the ability to search and retrieve videos, comments, favorites, subscriptions, user profiles and
more.
• Google CodeSearch allows you to search public source code from many projects.
Unsupported services
Zend_Gdata does not provide an interface to any other Google service, such as Search, Gmail, Translation,
or Maps. Only services that support the Google Data API are supported.
Structure of Zend_Gdata
Zend_Gata is composed of several types of classes:
• Service classes - inheriting from Zend_Gdata_App. These also include other classes such as Zend_Gdata,
Zend_Gdata_Spreadsheets, etc. These classes enable interacting with APP or GData services and provide
the ability to retrieve feeds, retrieve entries, post entries, update entries and delete entries.
• Query classes - inheriting from Zend_Gdata_Query. These also include other classes for specific services, such
as Zend_Gdata_Spreadsheets_ListQuery and Zend_Gdata_Spreadsheets_CellQuery. Query
591
Zend_Gdata
classes provide methods used to construct a query for data to be retrieved from GData services. Methods include
getters and setters like setUpdatedMin(), setStartIndex(), and getPublishedMin(). The query
classes also have a method to generate a URL representing the constructed query -- getQueryUrl. Alternatively,
the query string component of the URL can be retrieved used the getQueryString() method.
• Entry classes - inheriting from Zend_Gdata_App_Entry. These also include other classes such as
Zend_Gdata_Entry, and Zend_Gdata_Spreadsheets_ListEntry. These classes represent entries
retrieved from services or used for constructing data to send to services. In addition to being able to set the properties
of an entry (such as the spreadsheet cell value), you can use an entry object to send update or delete requests to
a service. For example, you can call $entry->save() to save changes made to an entry back to service from
which the entry initiated, or $entry->delete() to delete an entry from the server.
Note: A full example of working with Zend_Gdata is available in the demos/Zend/Gdata directory. This
example is runnable from the command-line, but the methods contained within are easily portable to a web application.
We've implemented a magic factory method in all service classes (such as Zend_Gdata_App, Zend_Gdata,
Zend_Gdata_Spreadsheets) that should make constructing new instances of data model, query and other classes
much easier. This magic factory is implemented by using the magic __call method to intercept all attempts to call
$service->newXXX(arg1, arg2, ...). Based off the value of XXX, a search is performed in all registered
'packages' for the desired class. Here's some examples:
592
Zend_Gdata
// creates a Zend_Gdata_App_Spreadsheets_CellEntry
$entry = $ss->newCellEntry();
// creates a Zend_Gdata_App_Spreadsheets_Extension_Cell
$cell = $ss->newCell();
$cell->setText('My cell value');
$cell->setRow('1');
$cell->setColumn('3');
$entry->cell = $cell;
Each service class in the inheritance tree is responsible for registering the appropriate 'packages' (directories) which
are to be searched when calling the magic factory method.
Most other types of queries against Google Data services do not require authentication.
Dependencies
Zend_Gdata makes use of Zend_Http_Client to send requests to google.com and fetch results. The response to most
Google Data requests is returned as a subclass of the Zend_Gdata_App_Feed or Zend_Gdata_App_Entry
classes.
Zend_Gdata assumes your PHP application is running on a host that has a direct connection to the Internet. The
Zend_Gdata client operates by contacting Google Data servers.
The single optional parameter to the Zend_Gdata_App constructor is an instance of Zend_Http_Client. If you don't
pass this parameter, Zend_Gdata creates a default Zend_Http_Client object, which will not have associated
credentials to access private feeds. Specifying the Zend_Http_Client object also allows you to pass configuration
options to that client object.
Beginning with Zend Framework 1.7, support has been added for protocol versioning. This allows the client and server
to support new features while maintaining backwards compatibility. While most services will manage this for you, if
you create a Zend_Gdata instance directly (as opposed to one of its subclasses), you may need to specify the desired
protocol version to access certain server functionality.
593
Zend_Gdata
Also see the sections on authentication for methods to create an authenticated Zend_Http_Client object.
• The q parameter specifies a full-text query. The value of the parameter is a string.
• The alt parameter specifies the feed type. The value of the parameter can be atom, rss, json, or json-in-
script. If you don't specify this parameter, the default feed type is atom. NOTE: Only the output of the atom
feed format can be processed using Zend_Gdata. The Zend_Http_Client could be used to retrieve feeds in
other formats, using query URLs generated by the Zend_Gdata_Query class and its subclasses.
• The maxResults parameter limits the number of entries in the feed. The value of the parameter is an integer. The
number of entries returned in the feed will not exceed this value.
• The startIndex parameter specifies the ordinal number of the first entry returned in the feed. Entries before
this number are skipped.
• The updatedMin and updatedMax parameters specify bounds on the entry date. If you specify a value for
updatedMin, no entries that were updated earlier than the date you specify are included in the feed. Likewise no
entries updated after the date specified by updatedMax are included.
You can use numeric timestamps, or a variety of date/time string representations as the value for these parameters.
The Zend_Gdata class also implements "magic" getter and setter methods, so you can use the name of the parameter
as a virtual member of the class.
594
Zend_Gdata
You can clear all parameters with the resetParameters() function. This is useful to do if you reuse a
Zend_Gdata object for multiple queries.
Fetching a Feed
Use the getFeed() function to retrieve a feed from a specified URI. This function returns an instance of class
specified as the second argument to getFeed, which defaults to Zend_Gdata_Feed.
See later sections for special functions in each helper class for Google Data services. These functions help you to get
feeds from the URI that is appropriate for the respective service.
function getNextPage($feed) {
$nextURL = $feed->getLink('next');
if ($nextURL !== null) {
return $gdata->getFeed($nextURL);
} else {
return null;
}
}
If you would prefer not to work with pages in your application, pass the first page of the feed into
Zend_Gdata_App::retrieveAllEntriesForFeed(), which will consolidate all entries from each page
into a single feed. This example shows how to use this function:
595
Zend_Gdata
$feed = $gdata->retrieveAllEntriesForFeed($gdata->getFeed($query));
Keep in mind when calling this function that it may take a long time to complete on large feeds. You may need to
increase PHP's execution time limit by calling set_time_limit().
Updating Entries
After retrieving an entry, you can update that entry and save changes back to the server. Here's an example:
You can use the data model classes for each service to construct the appropriate entry to post to Google's services.
The insertEntry() function will accept a child of Zend_Gdata_App_Entry as data to post to the service.
The method returns a child of Zend_Gdata_App_Entry which represents the state of the entry as it was returned
from the server.
596
Zend_Gdata
Alternatively, you could construct the XML structure for an entry as a string and pass the string to the
insertEntry() function.
$entry = $gdata->newEntry();
$entry->title = $gdata->newTitle('Playing football at the park');
$content =
$gdata->newContent('We will visit the park and play football');
$content->setType('text');
$entry->content = $content;
$entryResult = $gdata->insertEntry($entry,
'http://www.blogger.com/feeds/blogID/posts/default');
To post entries, you must be using an authenticated Zend_Http_Client that you created using the
Zend_Gdata_AuthSub or Zend_Gdata_ClientLogin classes.
Option 2: Alternatively, you can call $entry->delete() on an entry retrieved from a Google service.
To delete entries, you must be using an authenticated Zend_Http_Client that you created using the
Zend_Gdata_AuthSub or Zend_Gdata_ClientLogin classes.
The Google documentation says the ClientLogin mechanism is appropriate for "installed applications" whereas the
AuthSub mechanism is for "web applications." The difference is that AuthSub requires interaction from the user, and a
597
Zend_Gdata
browser interface that can react to redirection requests. The ClientLogin solution uses PHP code to supply the account
credentials; the user is not required to enter her credentials interactively.
The account credentials supplied via the AuthSub mechanism are entered by the user of the web application. Therefore
they must be account credentials that are known to that user.
Registered applications
Zend_Gdata currently does not support use of secure tokens, because the AuthSub authentication does not
support passing a digital certificate to acquire a secure token.
After Google's authentication server redirects the user's browser back to the current application, a GET request
parameter is set, called token. The value of this parameter is a single-use token that can be used for authenticated
access. This token can be converted into a multi-use token and stored in your session.
Then use the token value in a call to Zend_Gdata_AuthSub::getHttpClient(). This function returns an
instance of Zend_Http_Client, with appropriate headers set so that subsequent requests your application submits
using that Http Client are also authenticated.
Below is an example of PHP code for a web application to acquire authentication to use the Google Calendar service
and create a Zend_Gdata client object using that authenticated Http Client.
$my_calendar = 'http://www.google.com/calendar/feeds/default/private/full';
if (!isset($_SESSION['cal_token'])) {
if (isset($_GET['token'])) {
// You can convert the single-use token to a session token.
$session_token =
Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']);
// Store the session token in our session.
$_SESSION['cal_token'] = $session_token;
} else {
// Display link to generate single-use token
$googleUri = Zend_Gdata_AuthSub::getAuthSubTokenUri(
'http://'. $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'],
$my_calendar, 0, 1);
echo "Click <a href='$googleUri'>here</a> " .
"to authorize this application.";
exit();
}
}
598
Zend_Gdata
if (isset($_GET['logout'])) {
Zend_Gdata_AuthSub::AuthSubRevokeToken($_SESSION['cal_token']);
unset($_SESSION['cal_token']);
header('Location: ' . $php_self);
exit();
}
Security notes
The treatment of the $php_self variable in the example above is a general security guideline, it is not
specific to Zend_Gdata. You should always filter content you output to http headers.
Regarding revoking authentication tokens, it is recommended to do this when the user is finished with her
Google Data session. The possibility that someone can intercept the token and use it for malicious purposes
is very small, but nevertheless it is a good practice to terminate authenticated access to any service.
Your client application can use the Book Search Data API to issue full-text searches for books and to retrieve standard
book information, ratings, and reviews. You can also access individual users' library collections and public reviews
[http://books.google.com/googlebooks/mylibrary/]. Finally, your application can submit authenticated requests to
enable users to create and modify library collections, ratings, labels, reviews, and other account-specific entities.
For more information on the Book Search Data API, please refer to the official PHP Developer's Guide [http://
code.google.com/apis/books/gdata/developers_guide_php.html] on code.google.com.
599
Zend_Gdata
The most common action is to retrieve a list of books that match a search query. To do so you create a VolumeQuery
object and pass it to the Books::getVolumeFeed method.
For example, to perform a keyword query, with a filter on viewability to restrict the results to partial or full view books,
use the setMinViewability and setQuery methods of the VolumeQuery object. The following code snippet
prints the title and viewability of all volumes whose metadata or text matches the query term "domino":
$query->setQuery('domino');
$query->setMinViewability('partial_view');
$feed = $books->getVolumeFeed($query);
The Query class, and subclasses like VolumeQuery, are responsible for constructing feed URLs. The VolumeQuery
shown above constructs a URL equivalent to the following:
http://www.google.com/books/feeds/volumes?q=keyword&min-viewability=partial
Note: Since Book Search results are public, you can issue a Book Search query without authentication.
Here are some of the most common VolumeQuery methods for setting search parameters:
setQuery: Specifies a search query term. Book Search searches all book metadata and full text for books
matching the term. Book metadata includes titles, keywords, descriptions, author names, and subjects. Note that any
spaces, quotes or other punctuation in the parameter value must be URL-escaped. (Use a plus (+) for a space.) To
search for an exact phrase, enclose the phrase in quotation marks. For example, to search for books matching the
phrase "spy plane", set the q parameter to %22spy+plane%22. You can also use any of the advanced search
operators [http://books.google.com/advanced_book_search] supported by Book Search. For example, jane+austen
+-inauthor:austen returns matches that mention (but are not authored by) Jane Austen.
setStartIndex: Specifies the index of the first matching result that should be included in the result set. This
parameter uses a one-based index, meaning the first result is 1, the second result is 2 and so forth. This parameter works
in conjunction with the max-results parameter to determine which results to return. For example, to request the third
set of 10 results—results 21-30—set the start-index parameter to 21 and the max-results parameter to 10. Note:
This isn't a general cursoring mechanism. If you first send a query with ?start-index=1&max-results=10
and then send another query with ?start-index=11&max-results=10, the service cannot guarantee that the
results are equivalent to ?start-index=1&max-results=20, because insertions and deletions could have taken
place in between the two queries.
setMaxResults: Specifies the maximum number of results that should be included in the result set. This parameter
works in conjunction with the start-index parameter to determine which results to return. The default value of this
parameter is 10 and the maximum value is 20.
setMinViewability: Allows you to filter the results according to the books' viewability status [http://
code.google.com/apis/books/docs/dynamic-links.html#terminology]. This parameter accepts one of three values:
'none' (the default, returning all matching books regardless of viewability), 'partial_view' (returning only
600
Zend_Gdata
books that the user can preview or view in their entirety), or 'full_view' (returning only books that the user can
view in their entirety).
If you are a partner who wants to do Co-Branded Search using the Book Search Data API, you may do so by modifying
the feed URL above to point to your Co-Branded Search implementation. If, for example, a Co-Branded Search is
available at the following URL:
http://www.google.com/books/p/PARTNER_COBRAND_ID?q=ball
then you can obtain the same results using the Book Search Data API at the following URL:
http://www.google.com/books/feeds/p/PARTNER_COBRAND_ID/volumes?q=ball+-soccer
To specify an alternate URL when querying a volume feed, you can provide an extra parameter to newVolumeQuery
$query =
$books->newVolumeQuery('http://www.google.com/books/p/PARTNER_COBRAND_ID');
For additional information or support, visit our Partner help center [http://books.google.com/support/partner/].
To add a rating, add a Rating object to a VolumeEntry and post it to the annotation feed. In the example below,
we start from an empty VolumeEntry object.
Reviews
In addition to ratings, authenticated users can submit reviews or edit their reviews. For information on how to request
previously submitted reviews, see Retrieving annotations [#zend.gdata.books.retrieving_annotations].
Adding a review
To add a review, add a Review object to a VolumeEntry and post it to the annotation feed. In the example below,
we start from an existing VolumeEntry object.
$annotationUrl = $entry->getAnnotationLink()->href;
601
Zend_Gdata
Editing a review
To update an existing review, first you retrieve the review you want to update, then you modify it, and then you submit
it to the annotation feed.
$entryUrl = $entry->getId()->getText();
$review = new Zend_Gdata_Books_Extension_Review();
Labels
You can use the Book Search Data API to label volumes with keywords. A user can submit, retrieve and modify labels.
See Retrieving annotations [#zend.gdata.books.retrieving_annotations] for how to read previously submitted labels.
$annotationUrl = $entry->getAnnotationLink()->href;
$category = new Zend_Gdata_App_Extension_Category(
'rated',
'http://schemas.google.com/books/2008/labels');
$entry->setCategory(array($category));
$books->insertVolume($entry, Zend_Gdata_Books::MY_ANNOTATION_FEED_URI);
$feed = $books->getVolumeFeed(
'http://www.google.com/books/feeds/users/USER_ID/volumes');
<i>(or)</i>
$feed = $books->getUserAnnotationFeed();
602
Zend_Gdata
For a list of the supported query parameters, see the query parameters [#zend.gdata.books.query_parameters] section.
Deleting Annotations
If you retrieved an annotation entry containing ratings, reviews, and/or labels, you can remove all annotations by
calling deleteVolume on that entry.
$books->deleteVolume($entry);
The most important collection is the user's My Library, which represents the books the user would like to remember,
organize, and share with others. This is the collection the user sees when accessing his or her My Library page [http://
books.google.com/books?op=library].
$feed = $books->getUserLibraryFeed();
Note: The feed may not contain all of the user's books, because there's a default limit on the number
of results returned. For more information, see the max-results query parameter in Searching for books
[#zend.gdata.books.searching_for_books].
For example, the following query returns all the books in your library that contain the word "bear":
$query = $books->newVolumeQuery(
'http://www.google.com/books/feeds/users' .
'/USER_ID/collections/library/volumes');
$query->setQuery('bear');
$feed = $books->getVolumeFeed($query);
For a list of the supported query parameters, see the query parameters [#zend.gdata.books.query_pParameters] section.
In addition, you can search for books that have been labeled by the user [#zend.gdata.books.labels]:
603
Zend_Gdata
$query = $books->newVolumeQuery(
'http://www.google.com/books/feeds/users/' .
'USER_ID/collections/library/volumes');
$query->setCategory(
$query->setCategory('favorites');
$feed = $books->getVolumeFeed($query);
You can either create an entry from scratch if you know the volume ID, or insert an entry read from any feed.
The following example creates a new entry and adds it to the library:
$books->insertVolume(
$entry,
Zend_Gdata_Books::MY_LIBRARY_FEED_URI
);
$books->deleteVolume($entry);
The Google documentation says the ClientLogin mechanism is appropriate for "installed applications" whereas the
AuthSub mechanism is for "web applications." The difference is that AuthSub requires interaction from the user, and a
604
Zend_Gdata
browser interface that can react to redirection requests. The ClientLogin solution uses PHP code to supply the account
credentials; the user is not required to enter her credentials interactively.
The account credentials supplied via the ClientLogin mechanism must be valid credentials for Google services, but
they are not required to be those of the user who is using the PHP application.
The optional third parameter is the name of the Google Data service. For instance, this can be 'cl' for Google Calendar.
The default is "xapi", which is recognized by Google Data servers as a generic service name.
The optional fourth parameter is an instance of Zend_Http_Client. This allows you to set options in the client,
such as proxy server settings. If you pass NULL for this parameter, a generic Zend_Http_Client object is created.
The optional fifth parameter is a short string that Google Data servers use to identify the client application for logging
purposes. By default this is string "Zend-ZendFramework";
The optional sixth parameter is a string ID for a CAPTCHA™ challenge that has been issued by the server. It is only
necessary when logging in after receiving a CAPTCHA™ challenge from a previous login attempt.
The optional seventh parameter is a user's response to a CAPTCHA™ challenge that has been issued by the server. It
is only necessary when logging in after receiving a CAPTCHA™ challenge from a previous login attempt.
Below is an example of PHP code for a web application to acquire authentication to use the Google Calendar service
and create a Zend_Gdata client object using that authenticated Zend_Http_Client.
605
Zend_Gdata
Before any transactions can occur, this connection needs to be made. Creating a connection to the calendar servers
involves two steps: creating an HTTP client and binding a Zend_Gdata_Calendar service instance to that client.
Authentication
The Google Calendar API allows access to both public and private calendar feeds. Public feeds do not require
authentication, but are read-only and offer reduced functionality. Private feeds offers the most complete functionality
but requires an authenticated connection to the calendar servers. There are three authentication schemes that are
supported by Google Calendar:
• ClientAuth provides direct username/password authentication to the calendar servers. Since this scheme requires
that users provide your application with their password, this authentication is only recommended when other
authentication schemes are insufficient.
• AuthSub allows authentication to the calendar servers via a Google proxy server. This provides the same level of
convenience as ClientAuth but without the security risk, making this an ideal choice for web-based applications.
• MagicCookie allows authentication based on a semi-random URL available from within the Google Calendar
interface. This is the simplest authentication scheme to implement, but requires that users manually retrieve their
secure URL before they can authenticate, doesn't provide access to calendar lists, and is limited to read-only access.
The Zend_Gdata library provides support for all three authentication schemes. The rest of this chapter will assume
that you are familiar the authentication schemes available and how to create an appropriate authenticated connection.
For more information, please see section the Authentication section of this manual or the Authentication Overview in
the Google Data API Developer's Guide [http://code.google.com/apis/gdata/auth.html].
Once deciding on an authentication scheme, the next step is to create an instance of Zend_Gdata_Calendar. The
class constructor takes an instance of Zend_Http_Client as a single argument. This provides an interface for
AuthSub and ClientAuth authentication, as both of these require creation of a special authenticated HTTP client. If no
arguments are provided, an unauthenticated instance of Zend_Http_Client will be automatically created.
The example below shows how to create a Calendar service class using ClientAuth authentication:
606
Zend_Gdata
A Calendar service using AuthSub can be created in a similar, though slightly more lengthy fashion:
/*
* Retrieve the current URL so that the AuthSub server knows where to
* redirect the user after authentication is complete.
*/
function getCurrentUrl()
{
global $_SERVER;
if (isset($_SERVER['HTTPS']) &&
strtolower($_SERVER['HTTPS']) == 'on') {
$protocol = 'https://';
} else {
$protocol = 'http://';
}
$host = $_SERVER['HTTP_HOST'];
if ($_SERVER['HTTP_PORT'] != '' &&
(($protocol == 'http://' && $_SERVER['HTTP_PORT'] != '80') ||
($protocol == 'https://' && $_SERVER['HTTP_PORT'] != '443'))) {
$port = ':' . $_SERVER['HTTP_PORT'];
} else {
$port = '';
}
return $protocol . $host . $port . $php_request_uri;
}
/**
* Obtain an AuthSub authenticated HTTP client, redirecting the user
* to the AuthSub server to login if necessary.
*/
function getAuthSubHttpClient()
{
global $_SESSION, $_GET;
607
Zend_Gdata
$session = true;
$authSubUrl = Zend_Gdata_AuthSub::getAuthSubTokenUri($next,
$scope,
$secure,
$session);
header("HTTP/1.0 307 Temporary redirect");
exit();
}
// Make sure that the user has a valid session, so we can record the
// AuthSub session token once it is available.
session_start();
Finally, an unauthenticated server can be created for use with either public feeds or MagicCookie authentication:
Note that MagicCookie authentication is not supplied with the HTTP connection, but is instead specified along with
the desired visibility when submitting queries. See the section on retrieving events below for an example.
608
Zend_Gdata
The calendar list is always private and must be accessed over an authenticated connection. It is not possible to retrieve
another user's calendar list and it cannot be accessed using MagicCookie authentication. Attempting to access a
calendar list without holding appropriate credentials will fail and result in a 401 (Authentication Required) status code.
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Calendar($client);
try {
$listFeed= $service->getCalendarListFeed();
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e->getMessage();
}
Retrieving Events
Like the list of calendars, events are also retrieved using the Zend_Gdata_Calendar service class. The
event list returned is of type Zend_Gdata_Calendar_EventFeed and contains each event as an instance of
Zend_Gdata_Calendar_EventEntry. As before, the iterator and accessors contained within the event feed
instance allow inspection of individual events.
Queries
When retrieving events using the Calendar API, specially constructed query URLs are used to describe what events
should be returned. The Zend_Gdata_Calendar_EventQuery class simplifies this task by automatically
constructing a query URL based on provided parameters. A full list of these parameters is available at the Queries
section of the Google Data APIs Protocol Reference [http://code.google.com/apis/gdata/reference.html#Queries].
However, there are three parameters that are worth special attention:
• User is used to specify the user whose calendar is being searched for, and is specified as an email address. If no user
is provided, "default" will be used instead to indicate the currently authenticated user (if authenticated).
• Visibility specifies whether a users public or private calendar should be searched. If using an unauthenticated session
and no MagicCookie is available, only the public feed will be available.
• Projection specifies how much data should be returned by the server and in what format. In most cases you will
want to use the "full" projection. Also available is the "basic" projection, which places most meta-data into each
event's content field as human readable text, and the "composite" projection which includes complete text for any
comments alongside each event. The "composite" view is often much larger than the "full" view.
609
Zend_Gdata
$query = $service->newEventQuery();
$query->setUser('default');
// Set to $query->setVisibility('private-magicCookieValue') if using
// MagicCookie auth
$query->setVisibility('private');
$query->setProjection('full');
$query->setOrderby('starttime');
$query->setFutureevents('true');
Additional properties such as ID, author, when, event status, visibility, web content, and content, among others
are available within Zend_Gdata_Calendar_EventEntry. Refer to the Zend Framework API Documentation
[http://framework.zend.com/apidoc/core/] and the Calendar Protocol Reference [http://code.google.com/apis/gdata/
reference.html] for a complete list.
$query->setStartMin('2006-12-01');
$query->setStartMax('2006-12-16');
Note that startMin is inclusive whereas startMax is exclusive. As a result, only events through 2006-12-15
23:59:59 will be returned.
610
Zend_Gdata
$query->setQuery("dogfood");
$query = $service->newEventQuery();
$query->setUser('default');
$query->setVisibility('private');
$query->setProjection('full');
$query->setEvent($eventId);
try {
$event = $service->getCalendarEventEntry($query);
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e->getMessage();
}
In a similar fashion, if the event URL is known, it can be passed directly into getCalendarEntry() to retrieve
a specific event. In this case, no query object is required since the event URL contains all the necessary information
to retrieve the event.
$eventURL = "http://www.google.com/calendar/feeds/default/private"
. "/full/g829on5sq4ag12se91d10uumko";
try {
$event = $service->getCalendarEventEntry($eventURL);
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e->getMessage();
}
Creating Events
Creating Single-Occurrence Events
Events are added to a calendar by creating an instance of Zend_Gdata_EventEntry and populating it with the
appropriate data. The calendar service instance (Zend_Gdata_Calendar) is then used to used to transparently
covert the event into XML and POST it to the calendar server. Creating events requires either an AuthSub or ClientAuth
authenticated connection to the calendar server.
• Title provides the headline that will appear above the event within the Google Calendar UI.
• When indicates the duration of the event and, optionally, any reminders that are associated with it. See the next
section for more information on this attribute.
• Author provides information about the user who created the event.
611
Zend_Gdata
• Content provides additional information about the event which appears when the event details are requested from
within Google Calendar.
• Transparency indicates whether the event should be consume time on the user's free/busy list.
• Visibility allows the event to be hidden from the public event lists.
For a complete list of event attributes, refer to the Zend Framework API Documentation [http://framework.zend.com/
apidoc/core/] and the Calendar Protocol Reference [http://code.google.com/apis/gdata/reference.html]. Attributes that
can contain multiple values, such as where, are implemented as arrays and need to be created accordingly. Be aware
that all of these attributes require objects as parameters. Trying instead to populate them using strings or primitives
will result in errors during conversion to XML.
Once the event has been populated, it can be uploaded to the calendar server by passing it as an argument to the
calendar service's insertEvent() function.
// Create a new entry using the calendar service's magic factory method
$event= $service->newEventEntry();
$when = $service->newWhen();
$when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00";
$when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00";
$event->when = array($when);
612
Zend_Gdata
All-day events can be scheduled by specifying only the date omitting the time when setting startTime and
endTime. Likewise, zero-duration events can be specified by omitting the endTime. In all cases, date/time values
should be provided in RFC3339 [http://www.ietf.org/rfc/rfc3339.txt] format.
The when attribute also controls when reminders are sent to a user. Reminders are stored in an array and each event
may have up to find reminders associated with it.
For a reminder to be valid, it needs to have two attributes set: method and a time. Method can accept one of
the following strings: "alert", "email", or "sms". The time should be entered as an integer and can be set with either
the property minutes, hours, days, or absoluteTime. However, a valid request may only have one of these
attributes set. If a mixed time is desired, convert to the most precise unit available. For example, 1 hour and 30 minutes
should be entered as 90 minutes.
Exceptions to the recurrence pattern will usually be specified by a distinct recurrenceException attribute.
However, the iCalendar standard provides a secondary format for defining recurrences, and the possibility that either
may be used must be accounted for.
Due to the complexity of parsing recurrence patterns, further information on this them is outside the scope of this
document. However, more information can be found in the Common Elements section of the Google Data APIs
Developer Guide [http://code.google.com/apis/gdata/elements.html#gdRecurrence], as well as in RFC 2445.
// Create a new entry using the calendar service's magic factory method
$event= $service->newEventEntry();
613
Zend_Gdata
$recurrence = "DTSTART;VALUE=DATE:20070501\r\n" .
"DTEND;VALUE=DATE:20070502\r\n" .
"RRULE:FREQ=WEEKLY;BYDAY=Tu;UNTIL=20070904\r\n";
$event->recurrence = $service->newRecurrence($recurrence);
Using QuickAdd
QuickAdd is a feature which allows events to be created using free-form text entry. For example, the string "Dinner at
Joe's Diner on Thursday" would create an event with the title "Dinner", location "Joe's Diner", and date "Thursday". To
take advantage of QuickAdd, create a new QuickAdd property set to "true" and store the freeform text as a content
property.
// Create a new entry using the calendar service's magic factory method
$event= $service->newEventEntry();
Modifying Events
Once an instance of an event has been obtained, the event's attributes can be locally modified in the same way as when
creating an event. Once all modifications are complete, calling the event's save() method will upload the changes
to the calendar server and return a copy of the event as it was created on the server.
In the event another user has modified the event since the local copy was retrieved, save() will fail and the server
will return a 409 (Conflict) status code. To resolve this a fresh copy of the event must be retrieved from the server
before attempting to resubmit any modifications.
614
Zend_Gdata
Deleting Events
Calendar events can be deleted either by calling the calendar service's delete() method and providing the edit URL
of an event or by calling an existing event's own delete() method.
In either case, the deleted event will still show up on a user's private event feed if an updateMin query parameter
is provided. Deleted events can be distinguished from regular events because they will have their eventStatus
property set to "http://schemas.google.com/g/2005#event.canceled".
// Option 2: Events can be deleted supplying the edit URL of the event
// to the calendar service, if known
$service->delete($event->getEditLink()->href);
Working with comments is fundamentally similar to working with events, with the only significant difference being
that a different feed and event class should be used and that the additional meta-data for events such as where and when
does not exist for comments. Specifically, the comment's author is stored in the author property, and the comment
text is stored in the content property.
// Extract the comment URL from the first event in a user's feed list
$event = $eventFeed[0];
$commentUrl = $event->comments->feedLink->url;
615
Zend_Gdata
See http://code.google.com/apis/documents/overview.html for more information about the Google Documents List
API.
$service = Zend_Gdata_Docs::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$docs = new Zend_Gdata_Docs($client);
$feed = $docs->getDocumentListFeed();
The resulting Zend_Gdata_Docs_DocumentListFeed object represents the response from the server. This feed
contains a list of Zend_Gdata_Docs_DocumentListEntry objects ($feed->entries), each of which represents
a single Google Document.
Upload a Document
You can create a new Google Document by uploading a word processing document, spreadsheet, or presentation. This
example is from the interactive Docs.php sample which comes with the library. It demonstrates uploading a file and
printing information about the result from the server.
/**
* Upload the specified document
*
* @param Zend_Gdata_Docs $docs The service object to use for communicating
* with the Google Documents server.
* @param boolean $html True if output should be formatted for display in a
* web browser.
* @param string $originalFileName The name of the file to be uploaded. The
* MIME type of the file is determined from the extension on this file
* name. For example, test.csv is uploaded as a comma separated volume
* and converted into a spreadsheet.
* @param string $temporaryFileLocation (optional) The file in which the
* data for the document is stored. This is used when the file has been
* uploaded from the client's machine to the server and is stored in
* a temporary file which does not have an extension. If this parameter
* is null, the file is read from the originalFileName.
*/
function uploadDocument($docs, $html, $originalFileName,
$temporaryFileLocation) {
$fileToUpload = $originalFileName;
if ($temporaryFileLocation) {
$fileToUpload = $temporaryFileLocation;
616
Zend_Gdata
// Upload the file and convert it into a Google Document. The original
// file name is used as the title of the document and the MIME type
// is determined based on the extension on the original file name.
$newDocumentEntry = $docs->uploadFile($fileToUpload, $originalFileName,
null, Zend_Gdata_Docs::DOCUMENTS_LIST_FEED_URI);
if ($html) {
// Find the URL of the HTML view of this document.
$alternateLink = '';
foreach ($newDocumentEntry->link as $link) {
if ($link->getRel() === 'alternate') {
$alternateLink = $link->getHref();
}
}
// Make the title link to the document on docs.google.com.
echo "<a href=\"$alternateLink\">\n";
}
echo $newDocumentEntry->title."\n";
if ($html) {echo "</a>\n";}
}
$feed = $docs->getDocumentListFeed(
'http://docs.google.com/feeds/documents/private/full/-/document');
$feed = $docs->getDocumentListFeed(
'http://docs.google.com/feeds/documents/private/full/-/spreadsheet');
617
Zend_Gdata
• Read a user's Google Health profile or query for medical records that match particular criteria and then use the
results to provide personalized functionality based on the data.
• Add new medical records to a user's profile by including CCR data when sending a notice to a user's profile. Note:
The CCR data is stored as an XML blob within the <atom> entry. The library does not provide direct accessors to
the object model but it does have helpers for extracting specific fields.
There are three main feeds, each of which requires authentication. Unlike other Google Data APIs, each Google Health
feed has a limited set of HTTP operations you can perform on it, depending on which authentication method you
are using (ClientLogin or AuthSub/OAuth). For a list of permitted operations, see http://code.google.com/apis/health/
reference.html#Authentication.
• Profile Feed use the profile feed to query a user's health profile for specific information.
• Register Feed use the register feed to reconcile new CCR data into a profile.
• Profile List Feed the profile list feed should be used to determine which of the user's Health profiles to interact with.
This feed is only available when using ClientLogin.
Before any transactions can occur, a connection needs to be made. Creating a connection to the Health servers involves
two steps: creating an HTTP client and binding a Zend_Gdata_Health service instance to that client.
Authentication
The Google Health API allows programmatic access to a user's Health profile. There are three authentication schemes
that are supported by Google Health:
• ClientLogin provides direct username/password authentication to the Health servers. Since this method requires that
users provide your application with their password, this authentication scheme is only recommended for installed/
desktop applications.
• AuthSub allows a user to authorize the sharing of their private data. This provides the same level of convenience as
ClientLogin but without the security risk, making it an ideal choice for web-based applications. For Google Health,
AuthSub must be used in registered and secure mode--meaning that all requests to the API must be digitally signed.
• OAuth is an alternative to AuthSub. Although this authentication scheme is not discussed in this document,
more information can be found in the Health Data API Developer's Guide [http://code.google.com/apis/health/
developers_guide_protocol.html#OAuth].
618
Zend_Gdata
Once you've decided on an authentication scheme, the next step is to create an instance of Zend_Gdata_Health
. This class should be passed an instance of Zend_Gdata_HttpClient. This provides an interface for AuthSub/
OAuth and ClientLogin to create a special authenticated HTTP client.
To test against the H9 Developer's (/h9) instead of Google Health (/health), the Zend_Gdata_Health constructor
takes an optional third argument for you to specify the H9 service name 'weaver'.
The example below shows how to create a Health service class using ClientLogin authentication:
A Health service using AuthSub can be created in a similar, though slightly more lengthy fashion. AuthSub is the
recommend interface to communicate with Google Health because each token is directly linked to a specific profile
in the user's account. Unlike other Google Data APIs, it is required that all requests from your application be digitally
signed.
/*
* Retrieve the current URL so that the AuthSub server knows where to
* redirect the user after authentication is complete.
*/
function getCurrentUrl() {
$phpRequestUri = htmlentities(substr($_SERVER['REQUEST_URI'],
0,
strcspn($_SERVER['REQUEST_URI'],
"\n\r")),
ENT_QUOTES);
619
Zend_Gdata
$host = $_SERVER['HTTP_HOST'];
if ($_SERVER['SERVER_PORT'] != '' &&
(($protocol == 'http://' && $_SERVER['SERVER_PORT'] != '80') ||
($protocol == 'https://' && $_SERVER['SERVER_PORT'] != '443'))) {
$port = ':' . $_SERVER['SERVER_PORT'];
} else {
$port = '';
}
return $protocol . $host . $port . $phpRequestUri;
}
/*
* Redirect a user to AuthSub if they do not have a valid session token.
* If they're coming back from AuthSub with a single-use token, instantiate
* a new HTTP client and exchange the token for a long-lived session token
* instead.
*/
function setupClient($singleUseToken = null) {
$client = null;
$sessionToken =
Zend_Gdata_AuthSub::getAuthSubSessionToken(trim($singleUseToken),
$client);
620
Zend_Gdata
session_start();
$client = setupClient(@$_GET['token']);
NOTE: the remainder of this document will assume you are using AuthSub for authentication.
Profile Feed
To query the user's profile feed, make sure your initial AuthSub token was requested with the permission=1
parameter set. The process of extracting data from the profile requires two steps, sending a query and iterating through
the resulting feed.
When retrieving the profile using the Health API, specifically constructed query URLs are used to describe what
(CCR) data should be returned. The Zend_Gdata_Health_Query class helps simplify this task by automatically
constructing a query URL based on the parameters you set.
$profileFeed = $healthService->getHealthProfileFeed($query);
Using setDigest("true") returns all of user's CCR data in a single Atom <entry>.
The setCategory() helper can be passed an additional parameter to return more specific CCR information. For
example, to return just the medication Lipitor, use setCategory("medication", "Lipitor"). The same
methodology can be applied to other categories such as conditions, allergies, lab results, etc.
A full list of supported query parameters is available in the query parameters section [http://code.google.com/apis/
health/reference.html#Parameters] of the Health API Reference Guide.
621
Zend_Gdata
To retrieve the full CCR information from an entry, make a call to the Zend_Gdata_Health_ProfileEntry
class's getCcr() method. That returns a Zend_Gdata_Health_Extension_CCR:
$entries = $profileFeed->getEntries();
foreach ($entries as $entry) {
$medications = $entry->getCcr()->getMedications();
//$conditions = $entry->getCcr()->getConditions();
//$immunizations = $entry->getCcr()->getImmunizations();
// print the CCR xml (this will just be the entry's medications)
foreach ($medications as $med) {
$xmlStr = $med->ownerDocument->saveXML($med);
echo "<pre>" . $xmlStr . "</pre>";
}
}
Here, the getCcr() method is used in conjunction with a magic helper to drill down and extract just the
medication data from the entry's CCR. The formentioned magic helper takes the form getCATEGORYNAME(),
where CATEGORYNAME is a supported Google Health category. See the Google Health reference Guide [http://
code.google.com/apis/health/reference.html#CatQueries] for the possible categories.
To be more efficient, you can also use category queries to only return the necessary CCR from the Google Health
servers. Then, iterate through those results:
// Since the query contained digest=true, only one Atom entry is returned
$entry = $profileFeed->entry[0];
$conditions = $entry->getCcr()->getConditions();
// print the CCR xml (this will just be the profile's conditions)
foreach ($conditions as $cond) {
$xmlStr = $cond->ownerDocument->saveXML($cond);
echo "<pre>" . $xmlStr . "</pre>";
}
Since ClientLogin requires a profile ID with each of its feeds, applications will likely want to query this feed first in
order to select the appropriate profile. The profile list feed returns Atom entries corresponding each profile in the user's
Google Health account. The profile ID is returned in the Atom <content> and the profile name in the <title>
element.
622
Zend_Gdata
$client = Zend_Gdata_ClientLogin::getHttpClient('user@gmail.com',
'pa$$word',
'health');
$healthService = new Zend_Gdata_Health($client);
$feed = $healthService->getHealthProfileListFeed();
Once you've determined which profile to use, call setProfileID() with the profileID as an argument. This will
restrict subsequent API requests to be against that particular profile:
$profileFeed = $healthService->getHealthProfileFeed();
$profileID = $healthService->getProfileID();
echo '<p><b>Queried profileID</b>: ' . $profileID . '</p>';
Sending a notice
Notices can be sent by using the sendHealthNotice() method for the Health service:
623
Zend_Gdata
<ExactDateTime>2007-04-04T07:00:00Z</ExactDateTime>
</DateTime>
<Description>
<Text>Aortic valve disorders</Text>
<Code>
<Value>410.10</Value>
<CodingSystem>ICD9</CodingSystem>
<Version>2004</Version>
</Code>
</Description>
<Status><Text>Active</Text></Status>
</Problem>
</Problems>
</Body>
</ContinuityOfCareRecord>';
$responseEntry = $healthService->sendHealthNotice($subject,
$body,
"html",
$ccr);
Create a Spreadsheet
The Spreadsheets data API does not currently provide a way to programmatically create or delete a spreadsheet.
$service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$spreadsheetService = new Zend_Gdata_Spreadsheets($client);
$feed = $spreadsheetService->getSpreadsheetFeed();
624
Zend_Gdata
The resulting Zend_Gdata_Spreadsheets_WorksheetFeed object feed represents the response from the
server. Among other things, this feed contains a list of Zend_Gdata_Spreadsheets_WorksheetEntry
objects ($feed->entries), each of which represents a single worksheet.
The following sections describe how to get a list-based feed, add a row to a worksheet, and send queries with various
query parameters.
The list feed makes some assumptions about how the data is laid out in the spreadsheet.
In particular, the list feed treats the first row of the worksheet as a header row; Spreadsheets dynamically creates XML
elements named after the contents of header-row cells. Users who want to provide Gdata feeds should not put any data
other than column headers in the first row of a worksheet.
The list feed contains all rows after the first row up to the first blank row. The first blank row terminates the data set. If
expected data isn't appearing in a feed, check the worksheet manually to see whether there's an unexpected blank row in
the middle of the data. In particular, if the second row of the spreadsheet is blank, then the list feed will contain no data.
Each Zend_Gdata_Spreadsheets_ListEntry contains an array, custom, which contains the data for that
row. You can extract and display this array:
$rowData = $listFeed->entries[1]->getCustom();
foreach($rowData as $customEntry) {
echo $customEntry->getColumnName() . " = " . $customEntry->getText();
}
An alternate version of this array, customByName, allows direct access to an entry's cells by name. This is convenient
when trying to access a specific header:
625
Zend_Gdata
$customEntry = $listFeed->entries[1]->getCustomByName('my_heading');
echo $customEntry->getColumnName() . " = " . $customEntry->getText();
Reverse-sort Rows
By default, rows in the feed appear in the same order as the corresponding rows in the GUI; that
is, they're in order by row number. To get rows in reverse order, set the reverse properties of the
Zend_Gdata_Spreadsheets_ListQuery object to true:
Note that if you want to order (or reverse sort) by a particular column, rather than by position in the worksheet, you can
set the orderby value of the Zend_Gdata_Spreadsheets_ListQuery object to column:<the header
of that column>.
Add a Row
Rows can be added to a spreadsheet by using the insertRow method of the Spreadsheet service.
$insertedListEntry = $spreadsheetService->insertRow($rowData,
$spreadsheetKey,
$worksheetId);
The $rowData parameter contains an array of column keys to data values. The method returns a
Zend_Gdata_Spreadsheets_SpreadsheetsEntry object which represents the inserted row.
Spreadsheets inserts the new row immediately after the last row that appears in the list-based feed, which is to say
immediately before the first entirely blank row.
Edit a Row
Once a Zend_Gdata_Spreadsheets_ListEntry object is fetched, its rows can be updated by using the
updateRow method of the Spreadsheet service.
626
Zend_Gdata
$updatedListEntry = $spreadsheetService->updateRow($oldListEntry,
$newRowData);
The $oldListEntry parameter contains the list entry to be updated. $newRowData contains an
array of column keys to data values, to be used as the new row data. The method returns a
Zend_Gdata_Spreadsheets_SpreadsheetsEntry object which represents the updated row.
Delete a Row
To delete a row, simply invoke deleteRow on the Zend_Gdata_Spreadsheets object with the existing entry
to be deleted:
$spreadsheetService->deleteRow($listEntry);
Alternatively, you can call the delete method of the entry itself:
$listEntry->delete();
Note that we don't recommend interacting with both a cell-based feed and a list-based feed for the same worksheet
at the same time.
foreach($cellFeed as $cellEntry) {
$row = $cellEntry->cell->getRow();
$col = $cellEntry->cell->getColumn();
$val = $cellEntry->cell->getText();
echo "$row, $col = $val\n";
}
627
Zend_Gdata
$updatedCell = $spreadsheetService->updateCell($row,
$col,
$inputValue,
$spreadsheetKey,
$worksheetId);
The new data is placed in the specified cell in the worksheet. If the specified cell contains data already, it will be
overwritten. Note: Use updateCell to change the data in a cell, even if the cell is empty.
This library implements version 2.0 of the Provisioning API. Access to your account via the Provisioning API must
be manually enabled for each domain using the Google Apps control panel. Only certain account types are able to
enable this feature.
For more information on the Google Apps Provisioning API, including instructions for enabling API access, refer to
the Provisioning API V2.0 Reference [http://code.google.com/apis/calendar/overview.html].
Authentication
The Provisioning API does not support authentication via AuthSub and anonymous access is not permitted.
All HTTP connections must be authenticated using ClientAuth authentication.
628
Zend_Gdata
$domain = "example.com";
$gdata = new Zend_Gdata_Gapps($client, $domain);
$domain = "example.com";
$query = new Zend_Gdata_Gapps_UserQuery($domain, $arg);
When using a service class factory method to create a query, the service class will automatically set the query's domain
to match its own domain. As a result, it is not necessary to specify the domain as part of the constructor arguments.
$domain = "example.com";
$gdata = new Zend_Gdata_Gapps($client, $domain);
$query = $gdata->newUserQuery($arg);
Users can also be created by instantiating UserEntry, providing a username, given name, family name, and password,
then calling insertUser() on a service object to upload the entry to the server.
$user = $gdata->newUserEntry();
$user->login = $gdata->newLogin();
$user->login->username = 'foo';
$user->login->password = '••••••••';
$user->name = $gdata->newName();
$user->name->givenName = 'Random';
$user->name->familyName = 'User';
$user = $gdata->insertUser($user);
The user's password should normally be provided as cleartext. Optionally, the password can be provided as an SHA-1
digest if login->passwordHashFunction is set to 'SHA-1'.
629
Zend_Gdata
$user = $gdata->retrieveUser('foo');
Users can also be retrieved by creating an instance of Zend_Gdata_Gapps_UserQuery, setting its username
property to equal the username of the user that is to be retrieved, and calling getUserEntry() on a service object
with that query.
$query = $gdata->newUserQuery('foo');
$user = $gdata->getUserEntry($query);
If the specified user cannot be located a ServiceException will be thrown with an error code of
Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST. ServiceExceptions will be covered in the section
called “Handling errors”.
$feed = $gdata->retrieveAllUsers();
This will create a Zend_Gdata_Gapps_UserFeed object which holds each user on the domain.
Alternatively, call getUserFeed() with no options. Keep in mind that on larger domains this feed may be paged
by the server. For more information on paging, see the section called “Working with Multi-page Feeds”.
$feed = $gdata->getUserFeed();
630
Zend_Gdata
$user = $gdata->retrieveUser('foo');
$user->name->givenName = 'Foo';
$user->name->familyName = 'Bar';
$user = $user->save();
$user = $gdata->retrieveUser('foo');
$user->login->password = '••••••••';
$user = $user->save();
Note that it is not possible to recover a password in this manner as stored passwords are not made available via the
Provisioning API for security reasons.
$user = $gdata->retrieveUser('foo');
$user->login->changePasswordAtNextLogin = true;
$user = $user->save();
$gdata->suspendUser('foo');
$gdata->restoreUser('foo');
$user = $gdata->retrieveUser('foo');
$user->login->suspended = true;
$user = $user->save();
631
Zend_Gdata
$user = $gdata->retrieveUser('foo');
$user->login->admin = true;
$user = $user->save();
And as expected, setting a user's login->admin property to FALSE revokes their administrative rights.
$user = $gdata->retrieveUser('foo');
$user->delete();
If you do not have access to a UserEntry object for an account, use the deleteUser() convenience method.
$gdata->deleteUser('foo');
Creating a nickname
Nicknames can be created by calling the createNickname() convenience method:
$gdata->createNickname('foo', 'bar');
Nicknames can also be created by instantiating NicknameEntry, providing the nickname with a name and an owner,
then calling insertNickname() on a service object to upload the entry to the server.
$nickname = $gdata->newNicknameEntry();
$nickname->login = $gdata->newLogin('foo');
$nickname->nickname = $gdata->newNickname('bar');
$nickname = $gdata->insertNickname($nickname);
Retrieving a nickname
Nicknames can be retrieved by calling the retrieveNickname() convenience method. This will return NULL if
a user is not found.
$nickname = $gdata->retrieveNickname('bar');
632
Zend_Gdata
$query = $gdata->newNicknameQuery('bar');
$nickname = $gdata->getNicknameEntry($query);
As with users, if no corresponding nickname is found a ServiceException will be thrown with an error code of
Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST. Again, these will be discussed in the section
called “Handling errors”.
$feed = $gdata->retrieveNicknames('foo');
This will create a Zend_Gdata_Gapps_NicknameFeed object which holds each nickname associated with the
specified user.
Alternatively, create a new Zend_Gdata_Gapps_NicknameQuery, set its username property to the desired user,
and submit the query by calling getNicknameFeed() on a service object.
$query = $gdata->newNicknameQuery();
$query->setUsername('foo');
$feed = $gdata->getNicknameFeed($query);
$feed = $gdata->retrieveAllNicknames();
This will create a Zend_Gdata_Gapps_NicknameFeed object which holds each nickname on the domain.
633
Zend_Gdata
$feed = $gdata->getNicknameFeed();
Deleting a nickname
Deleting a nickname to which you already hold a NicknameEntry for is a simple as calling delete() on that entry.
$nickname = $gdata->retrieveNickname('bar');
$nickname->delete();
For nicknames which you do not hold a NicknameEntry for, use the deleteNickname() convenience method.
$gdata->deleteNickname('bar');
$gdata->createEmailList('friends');
Email lists can also be created by instantiating EmailListEntry, providing a name for the list, then calling
insertEmailList() on a service object to upload the entry to the server.
$list = $gdata->newEmailListEntry();
$list->emailList = $gdata->newEmailList('friends');
$list = $gdata->insertEmailList($list);
$feed = $gdata->retrieveEmailLists('baz@somewhere.com');
634
Zend_Gdata
This will create a Zend_Gdata_Gapps_EmailListFeed object which holds each email list associated with the
specified recipient.
Alternatively, create a new Zend_Gdata_Gapps_EmailListQuery, set its recipient property to the desired
email address, and submit the query by calling getEmailListFeed() on a service object.
$query = $gdata->newEmailListQuery();
$query->setRecipient('baz@somewhere.com');
$feed = $gdata->getEmailListFeed($query);
$feed = $gdata->retrieveAllEmailLists();
This will create a Zend_Gdata_Gapps_EmailListFeed object which holds each email list on the domain.
$feed = $gdata->getEmailListFeed();
$gdata->deleteEmailList('friends');
$gdata->addRecipientToEmailList('bar@somewhere.com', 'friends');
635
Zend_Gdata
$feed = $gdata->retrieveAllRecipients('friends');
Alternatively, construct a new EmailListRecipientQuery, set its emailListName property to match the desired email
list, and call getEmailListRecipientFeed() on a service object.
$query = $gdata->newEmailListRecipientQuery();
$query->setEmailListName('friends');
$feed = $gdata->getEmailListRecipientFeed($query);
This will create a Zend_Gdata_Gapps_EmailListRecipientFeed object which holds each recipient for the
selected email list.
$gdata->removeRecipientFromEmailList('baz@somewhere.com', 'friends');
Handling errors
In addition to the standard suite of exceptions thrown by Zend_Gdata, requests using the Provisioning API may also
throw a Zend_Gdata_Gapps_ServiceException. These exceptions indicate that a API specific error occurred
which prevents the request from completing.
Each ServiceException instance may hold one or more Error objects. Each of these objects contains an error code,
reason, and (optionally) the input which triggered the exception. A complete list of known error codes is provided in
the Zend Framework API documentation under Zend_Gdata_Gapps_Error. Additionally, the authoritative error
list is available online at Google Apps Provisioning API V2.0 Reference: Appendix D [http://code.google.com/apis/
apps/gdata_provisioning_api_v2.0_reference.html#appendix_d].
While the complete list of errors received is available within ServiceException as an array by calling getErrors(),
often it is convenient to know if one specific error occurred. For these cases the presence of an error can be determined
by calling hasError().
The following example demonstrates how to detect if a requested resource doesn't exist and handle the fault gracefully:
636
Zend_Gdata
try {
$user = $gdata->getUserEntry($query);
} catch (Zend_Gdata_Gapps_ServiceException $e) {
// Set the user to null if not found
if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
$user = null;
} else {
throw $e;
}
}
return $user;
}
There are two item feeds: snippets feed and customer items feeds. The snippets feed contains all Google Base data
and is available to anyone to query against without a need for authentication. The customer items feed is a customer-
specific subset of data and only a customer/owner can access this feed to insert, update, or delete their own data.
Queries are constructed the same way against both types of feeds.
See http://code.google.com/apis/base [http://code.google.com/apis/base/] for more information about the Google Base
API.
Before any transactions can occur, this connection needs to be made. Creating a connection to the base servers involves
two steps: creating an HTTP client and binding a Zend_Gdata_Gbase service instance to that client.
Authentication
The Google Base API allows access to both public and private base feeds. Public feeds do not require authentication,
but are read-only and offer reduced functionality. Private feeds offers the most complete functionality but requires an
authenticated connection to the base servers. There are three authentication schemes that are supported by Google Base:
• ClientAuth provides direct username/password authentication to the base servers. Since this scheme requires
that users provide your application with their password, this authentication is only recommended when other
authentication schemes are insufficient.
• AuthSub allows authentication to the base servers via a Google proxy server. This provides the same level of
convenience as ClientAuth but without the security risk, making this an ideal choice for web-based applications.
The Zend_Gdata library provides support for all three authentication schemes. The rest of this chapter will assume
that you are familiar the authentication schemes available and how to create an appropriate authenticated connection.
For more information, please see section the section called “Google Data Client Authentication”. or the Authentication
Overview in the Google Data API Developer's Guide [http://code.google.com/apis/gdata/auth.html].
637
Zend_Gdata
Once deciding on an authentication scheme, the next step is to create an instance of Zend_Gdata_Gbase . This
class takes in an instance of Zend_Http_Client as a single argument. This provides an interface for AuthSub
and ClientAuth authentication, as both of these creation of a special authenticated HTTP client. If no arguments are
provided, an unauthenticated instance of Zend_Http_Client will be automatically created.
The example below shows how to create a Base service class using ClientAuth authentication:
A Base service using AuthSub can be created in a similar, though slightly more lengthy fashion:
/*
* Retrieve the current URL so that the AuthSub server knows where to
* redirect the user after authentication is complete.
*/
function getCurrentUrl()
{
global $_SERVER;
if (isset($_SERVER['HTTPS']) &&
strtolower($_SERVER['HTTPS']) == 'on') {
$protocol = 'https://';
} else {
$protocol = 'http://';
}
$host = $_SERVER['HTTP_HOST'];
if ($_SERVER['HTTP_PORT'] != '' &&
(($protocol == 'http://' && $_SERVER['HTTP_PORT'] != '80') ||
($protocol == 'https://' && $_SERVER['HTTP_PORT'] != '443'))) {
$port = ':' . $_SERVER['HTTP_PORT'];
} else {
638
Zend_Gdata
$port = '';
}
return $protocol . $host . $port . $php_request_uri;
}
/**
* Obtain an AuthSub authenticated HTTP client, redirecting the user
* to the AuthSub server to login if necessary.
*/
function getAuthSubHttpClient()
{
global $_SESSION, $_GET;
$authSubUrl = Zend_Gdata_AuthSub::getAuthSubTokenUri($next,
$scope,
$secure,
$session);
header("HTTP/1.0 307 Temporary redirect");
exit();
}
639
Zend_Gdata
session_start();
Finally, an unauthenticated server can be created for use with snippets feeds:
Retrieve Items
You can query customer items feed or snippets feed to retrieve items. It involves two steps, sending a query and
iterating through the returned feed.
When retrieving items using the Base API, specially constructed query URLs are used to describe what events should be
returned. The Zend_Gdata_Gbase_ItemQuery and Zend_Gdata_Gbase_SnippetQuery classes simplify
this task by automatically constructing a query URL based on provided parameters.
A full list of these parameters is available at the Query parameters section [http://code.google.com/apis/base/items-
feed.html#QueParameters] of the Customer Items Feed documentation.
640
Zend_Gdata
A full list of these parameters is available at the Query parameters section [http://code.google.com/apis/base/snippets-
feed.html#Parameters] of the Snippets Feed documentation.
To iterate through all attributes of a given item, invoke getGbaseAttributes() and iterate through the results:
Or, you can look for specific attribute name and iterate through the results that match:
You can test a feed operation before it is actually executed by setting the dry-run flag ($dryRun) to TRUE. Once you
are sure that you want to submit the data, set it to FALSE to execute the operation.
Insert an Item
Items can be added by using the insertGbaseItem() method for the Base service:
// Add title
$title = "PHP Developer Handbook";
$newEntry->title = $service->newTitle(trim($title));
641
Zend_Gdata
$dryRun = true;
$createdEntry = $service->insertGbaseItem($newEntry, $dryRun);
Modify an Item
You can update each attribute element of an item as you iterate through them:
642
Zend_Gdata
After making the changes, either invoke save($dryRun) method on the Zend_Gdata_Gbase_ItemEntry
object or call updateGbaseItem($entry, $dryRun) method on the Zend_Gdata_Gbase object to save
the changes.
Delete an Item
You can remove an item by calling deleteGbaseItem() method:
$dryRun = false;
$service->deleteGbaseItem($entry, $dryRun);
$dryRun = false;
$entry->delete($dryRun);
Access to public albums and photos is not restricted by account, however, a user must be logged in for non-read-
only access.
For more information on the API, including instructions for enabling API access, refer to the Picasa Web Albums Data
API Overview [http://code.google.com/apis/picasaweb/overview.html].
Authentication
The API provides authentication via AuthSub (recommended) and ClientAuth. HTTP connections must be
authenticated for write support, but non-authenticated connections have read-only access.
643
Zend_Gdata
Before any transactions can occur, this connection needs to be made. Creating a connection to the Picasa servers
involves two steps: creating an HTTP client and binding a Zend_Gdata_Photos service instance to that client.
Authentication
The Google Picasa API allows access to both public and private photo feeds. Public feeds do not require authentication,
but are read-only and offer reduced functionality. Private feeds offers the most complete functionality but requires an
authenticated connection to the Picasa servers. There are three authentication schemes that are supported by Google
Picasa :
• ClientAuth provides direct username/password authentication to the Picasa servers. Since this scheme requires
that users provide your application with their password, this authentication is only recommended when other
authentication schemes are insufficient.
• AuthSub allows authentication to the Picasa servers via a Google proxy server. This provides the same level of
convenience as ClientAuth but without the security risk, making this an ideal choice for web-based applications.
The Zend_Gdata library provides support for both authentication schemes. The rest of this chapter will assume that
you are familiar the authentication schemes available and how to create an appropriate authenticated connection. For
more information, please see section the Authentication section of this manual or the Authentication Overview in the
Google Data API Developer's Guide [http://code.google.com/apis/gdata/auth.html].
Once deciding on an authentication scheme, the next step is to create an instance of Zend_Gdata_Photos. The
class constructor takes an instance of Zend_Http_Client as a single argument. This provides an interface for
AuthSub and ClientAuth authentication, as both of these require creation of a special authenticated HTTP client. If no
arguments are provided, an unauthenticated instance of Zend_Http_Client will be automatically created.
The example below shows how to create a service class using ClientAuth authentication:
A service instance using AuthSub can be created in a similar, though slightly more lengthy fashion:
session_start();
/**
* Returns the full URL of the current page, based upon env variables
*
* Env variables used:
644
Zend_Gdata
* $_SERVER['HTTPS'] = (on|off|)
* $_SERVER['HTTP_HOST'] = value of the Host: header
* $_SERVER['SERVER_PORT'] = port number (only used if not http/80,https/443)
* $_SERVER['REQUEST_URI'] = the URI after the method of the HTTP request
*
* @return string Current URL
*/
function getCurrentUrl()
{
global $_SERVER;
/**
* Filter php_self to avoid a security vulnerability.
*/
$php_request_uri = htmlentities(substr($_SERVER['REQUEST_URI'], 0,
strcspn($_SERVER['REQUEST_URI'], "\n\r")), ENT_QUOTES);
/**
* Returns the AuthSub URL which the user must visit to authenticate requests
* from this application.
*
* Uses getCurrentUrl() to get the next URL which the user will be redirected
* to after successfully authenticating with the Google service.
*
* @return string AuthSub URL
*/
function getAuthSubUrl()
{
$next = getCurrentUrl();
$scope = 'http://picasaweb.google.com/data';
$secure = false;
$session = true;
return Zend_Gdata_AuthSub::getAuthSubTokenUri($next, $scope, $secure,
$session);
}
/**
* Returns a HTTP client object with the appropriate headers for communicating
645
Zend_Gdata
/**
* Create a new instance of the service, redirecting the user
* to the AuthSub server if necessary.
*/
$service = new Zend_Gdata_Photos(getAuthSubHttpClient());
Finally, an unauthenticated server can be created for use with public feeds:
• User is used to specify the user whose data is being searched for, and is specified as a username. If no user is
provided, "default" will be used instead to indicate the currently authenticated user (if authenticated).
• Album is used to specify the album which is being searched for, and is specified as either an id, or an album name.
• Photo is used to specify the photo which is being searched for, and is specified as an id.
$service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Photos($client);
For each query, a number of parameters limiting the search can be requested, or specified, with get(Parameter) and
set(Parameter), respectively. They are as follows:
646
Zend_Gdata
• Projection sets the format of the data returned in the feed, as either "api" or "base". Normally, "api" is desired. The
default is "api".
• Type sets the type of element to be returned, as either "feed" or "entry". The default is "feed".
• Access sets the visibility of items to be returned, as "all", "public", or "private". The default is "all". Non-public
elements will only be returned if the query is searching for the authenticated user.
• Tag sets a tag filter for returned items. When a tag is set, only items tagged with this value will return.
• Kind sets the kind of elements to return. When kind is specified, only entries that match this value will be returned.
• ImgMax sets the maximum image size for entries returned. Only image entries smaller than this value will be
returned.
• Thumbsize sets the thumbsize of entries that are returned. Any retrieved entry will have a thumbsize equal to this
value.
• User sets the user whose data is being searched for. The default is "default".
• AlbumId sets the id of the album being searched for. This element only applies to album and photo queries. In the
case of photo queries, this specifies the album that contains the requested photo. The album id is mutually exclusive
with the album's name. Setting one unsets the other.
• AlbumName sets the name of the album being searched for. This element only applies to the album and photo queries.
In the case of photo queries, this specifies the album that contains the requested photo. The album name is mutually
exclusive with the album's id. Setting one unsets the other.
• PhotoId sets the id of the photo being searched for. This element only applies to photo queries.
Retrieving A User
The service supports retrieving a user feed and list of the user's content. If the requested user is also the authenticated
user, entries marked as "hidden" will also be returned.
The user feed can be accessed by passing the username to the getUserFeed method:
$service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Photos($client);
try {
$userFeed = $service->getUserFeed("sample.user");
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e->getMessage();
}
647
Zend_Gdata
$service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Photos($client);
try {
$userFeed = $service->getUserFeed(null, $query);
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e->getMessage();
}
Constructing a query also provides the ability to request a user entry object:
$service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Photos($client);
try {
$userEntry = $service->getUserEntry($query);
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e->getMessage();
}
Retrieving An Album
The service supports retrieving an album feed and a list of the album's content.
The album feed is accessed by constructing a query object and passing it to getAlbumFeed:
$service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Photos($client);
try {
$albumFeed = $service->getAlbumFeed($query);
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e->getMessage();
}
Alternatively, the query object can be given an album name with setAlbumName. Setting the album name is mutually
exclusive with setting the album id, and setting one will unset the other.
Constructing a query also provides the ability to request an album entry object:
648
Zend_Gdata
$service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Photos($client);
try {
$albumEntry = $service->getAlbumEntry($query);
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e->getMessage();
}
Retrieving A Photo
The service supports retrieving a photo feed and a list of associated comments and tags.
The photo feed is accessed by constructing a query object and passing it to getPhotoFeed:
$service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Photos($client);
try {
$photoFeed = $service->getPhotoFeed($query);
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e->getMessage();
}
Constructing a query also provides the ability to request a photo entry object:
$service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Photos($client);
try {
$photoEntry = $service->getPhotoEntry($query);
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e->getMessage();
649
Zend_Gdata
Retrieving A Comment
The service supports retrieving comments from a feed of a different type. By setting a query to return a kind of
"comment", a feed request can return comments associated with a specific user, album, or photo.
Performing an action on each of the comments on a given photo can be accomplished as follows:
$service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Photos($client);
try {
$photoFeed = $service->getPhotoFeed($query);
Retrieving A Tag
The service supports retrieving tags from a feed of a different type. By setting a query to return a kind of "tag", a feed
request can return tags associated with a specific photo.
Performing an action on each of the tags on a given photo can be accomplished as follows:
$service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Photos($client);
try {
$photoFeed = $service->getPhotoFeed($query);
650
Zend_Gdata
Creating Entries
The service has functions to create albums, photos, comments, and tags.
Creating An Album
The service supports creating a new album for an authenticated user:
$service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Photos($client);
$service->insertAlbumEntry($entry);
Creating A Photo
The service supports creating a new photo for an authenticated user:
$service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Photos($client);
$fd = $service->newMediaFileSource($photo["tmp_name"]);
$fd->setContentType($photo["type"]);
$albumEntry = $service->getAlbumEntry($albumQuery);
$service->insertPhotoEntry($entry, $albumEntry);
Creating A Comment
The service supports creating a new comment for a photo:
651
Zend_Gdata
$service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Photos($client);
$photoEntry = $service->getPhotoEntry($photoQuery);
$service->insertCommentEntry($entry, $photoEntry);
Creating A Tag
The service supports creating a new tag for a photo:
$service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Photos($client);
$photoEntry = $service->getPhotoEntry($photoQuery);
$service->insertTagEntry($entry, $photoEntry);
Deleting Entries
The service has functions to delete albums, photos, comments, and tags.
Deleting An Album
The service supports deleting an album for an authenticated user:
$service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Photos($client);
652
Zend_Gdata
$entry = $service->getAlbumEntry($albumQuery);
$service->deleteAlbumEntry($entry, true);
Deleting A Photo
The service supports deleting a photo for an authenticated user:
$service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Photos($client);
$entry = $service->getPhotoEntry($photoQuery);
$service->deletePhotoEntry($entry, true);
Deleting A Comment
The service supports deleting a comment for an authenticated user:
$service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Photos($client);
$entry = $service->getCommentEntry($path);
$service->deleteCommentEntry($entry, true);
Deleting A Tag
The service supports deleting a tag for an authenticated user:
653
Zend_Gdata
$service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Photos($client);
$photoFeed = $service->getPhotoFeed($query);
$service->deleteTagEntry($tagEntry, true);
For more information on the YouTube Data API, please refer to the official PHP Developer's Guide [http://
code.google.com/apis/youtube/developers_guide_php.html] on code.google.com.
654
Zend_Gdata
Authentication
The YouTube Data API allows read-only access to public data, which does not require authentication.
For any write requests, a user needs to authenticate either using ClientLogin or AuthSub authentication.
Please refer to the Authentication section in the PHP Developer's Guide [http://code.google.com/apis/youtube/
developers_guide_php.html#Authentication] for more detail.
655
Zend_Gdata
echo $videoEntry->getVideoDescription();
echo "\n\n\n";
}
For more details on the different query parameters, please refer to the Reference
Guide [http://code.google.com/apis/youtube/reference.html#Searching_for_videos]. The available helper
functions in Zend_Gdata_YouTube_VideoQuery [http://framework.zend.com/apidoc/core/Zend_Gdata/
Zend_Gdata_YouTube_VideoQuery.html] for each of these parameters are described in more detail in the PHP
Developer's Guide [http://code.google.com/apis/youtube/developers_guide_php.html#SearchingVideos].
There are also query parameters to specify the time period over which the standard feed is computed.
656
Zend_Gdata
Alternatively, you could just retrieve the feed using the URL:
657
Zend_Gdata
Comments can also be retrieved for a video if you have a copy of the Zend_Gdata_YouTube_VideoEntry [http://
framework.zend.com/apidoc/core/Zend_Gdata/Zend_Gdata_YouTube_VideoEntry.html] object:
$feedUrl = $playlistEntry->getPlaylistVideoFeedUrl();
$playlistVideoFeed = $yt->getPlaylistVideoFeed($feedUrl);
658
Zend_Gdata
In order to upload a video directly, you must first construct a new Zend_Gdata_YouTube_VideoEntry [http://
framework.zend.com/apidoc/core/Zend_Gdata/Zend_Gdata_YouTube_VideoEntry.html] object and specify some
required meta-data The following example shows uploading the Quicktime video "mytestmovie.mov" to YouTube
with the following properties:
659
Zend_Gdata
$filesource = $yt->newMediaFileSource('mytestmovie.mov');
$filesource->setContentType('video/quicktime');
$filesource->setSlug('mytestmovie.mov');
$myVideoEntry->setMediaSource($filesource);
try {
$newEntry = $yt->insertEntry($myVideoEntry,
$uploadUrl,
'Zend_Gdata_YouTube_VideoEntry');
660
Zend_Gdata
To upload a video as private, simply use: $myVideoEntry->setVideoPrivate(); prior to performing the upload.
$videoEntry->isVideoPrivate() can be used to check whether a video entry is private or not.
Browser-based upload
Browser-based uploading is performed almost identically to direct uploading, except that
you do not attach a Zend_Gdata_App_MediaFileSource [http://framework.zend.com/apidoc/core/
Zend_Gdata/Zend_Gdata_App_MediaFileSource.html] object to the Zend_Gdata_YouTube_VideoEntry [http://
framework.zend.com/apidoc/core/Zend_Gdata/Zend_Gdata_YouTube_VideoEntry.html] you are constructing.
Instead you simply submit all of your video's meta-data to receive back a token element which can be used to construct
an HTML upload form.
$tokenHandlerUrl = 'http://gdata.youtube.com/action/GetUploadToken';
$tokenArray = $yt->getFormUploadToken($myVideoEntry, $tokenHandlerUrl);
$tokenValue = $tokenArray['token'];
$postUrl = $tokenArray['url'];
The above code prints out a link and a token that is used to construct an HTML form to display in the user's browser.
A simple example form is shown below with $tokenValue representing the content of the returned token element,
as shown being retrieved from $myVideoEntry above. In order for the user to be redirected to your website after
submitting the form, make sure to append a $nextUrl parameter to the $postUrl above, which functions in the same
way as the $next parameter of an AuthSub link. The only difference is that here, instead of a single-use token, a status
and an id variable are returned in the URL.
661
Zend_Gdata
'</form>';
try {
$control = $videoEntry->getControl();
} catch (Zend_Gdata_App_Exception $e) {
echo $e->getMessage();
}
Other Functions
In addition to the functionality described above, the YouTube API contains many other functions that allow you to
modify video meta-data, delete video entries and use the full range of community features on the site. Some of the
community features that can be modified through the API include: ratings, comments, playlists, subscriptions, user
profiles, contacts and messages.
Please refer to the full documentation available in the PHP Developer's Guide [http://code.google.com/apis/youtube/
developers_guide_php.html] on code.google.com.
try {
$client =
662
Zend_Gdata
Zend_Gdata_ClientLogin::getHttpClient($username, $password);
} catch(Zend_Gdata_App_Exception $ex) {
// Report the exception to the user
die($ex->getMessage());
}
• Zend_Gdata_App_AuthException indicates that the user's account credentials were not valid.
• Zend_Gdata_App_BadMethodCallException indicates that a method was called for a service that does
not support the method. For example, the CodeSearch service does not support post().
• Zend_Gdata_App_HttpException indicates that an HTTP request was not successful. Provides the ability
to get the full Zend_Http_Response object to determine the exact cause of the failure in cases where $e-
>getMessage() does not provide enough details.
You can use these exception subclasses to handle specific exceptions differently. See the API documentation for
information on which exception subclasses are thrown by which methods in Zend_Gdata.
try {
$client = Zend_Gdata_ClientLogin::getHttpClient($username,
$password,
$service);
} catch(Zend_Gdata_App_AuthException $authEx) {
// The user's credentials were incorrect.
// It would be appropriate to give the user a second try.
...
} catch(Zend_Gdata_App_HttpException $httpEx) {
// Google Data servers cannot be contacted.
die($httpEx->getMessage);}
663
664
Capítulo 25. Zend_Http
Introduction
Zend_Http_Client provides an easy interface for preforming Hyper-Text Transfer Protocol (HTTP) requests.
Zend_Http_Client supports most simple features expected from an HTTP client, as well as some more complex
features such as HTTP authentication and file uploads. Successful requests (and most unsuccessful ones too) return
a Zend_Http_Response object, which provides access to the response's headers and body (see the section called
“Zend_Http_Response”).
Using Zend_Http_Client
The class constructor optionally accepts a URL as its first parameter (can be either a string or a Zend_Uri_Http object),
and an array or Zend_Config object containing configuration options. Both can be left out, and set later using the
setUri() and setConfig() methods.
// You can also use a Zend_Config object to set the client's configuration
$config = new Zend_Config_Ini('httpclient.ini, 'secure');
$client->setConfig($config);
Nota
Zend_Http_Client uses Zend_Uri_Http to validate URLs. This means that some special characters like the
pipe symbol ('|') or the caret symbol ('^') will not be accepted in the URL by default. This can be modified by
setting the 'allow_unwise' option of Zend_Uri to 'true'. See the section called “Allowing "Unwise" characters
in URIs” for more information.
Configuration Parameters
The constructor and setConfig() method accept an associative array of configuration parameters, or a Zend_Config
object. Setting these parameters is optional, as they all have default values.
665
Zend_Http
The request() method takes one optional parameter - the request method. This can be either GET, POST, PUT, HEAD,
DELETE, TRACE, OPTIONS or CONNECT as defined by the HTTP protocol 1. For convenience, these are all defined
as class constants: Zend_Http_Client::GET, Zend_Http_Client::POST and so on.
1
See RFC 2616 - http://www.w3.org/Protocols/rfc2616/rfc2616.html.
666
Zend_Http
If no method is specified, the method set by the last setMethod() call is used. If setMethod() was never called, the
default request method is GET (see the above example).
While GET parameters can be sent with every request method, POST parameters are only sent in the body of POST
requests. Adding POST parameters to a request is very similar to adding GET parameters, and can be done with the
setParameterPost() method, which is similar to the setParameterGet() method in structure.
667
Zend_Http
Note that when sending POST requests, you can set both GET and POST parameters. On the other hand, while setting
POST parameters for a non-POST request will not trigger and error, it is useless. Unless the request is a POST request,
POST parameters are simply ignored.
According to the HTTP/1.1 RFC, HTTP 301 and 302 responses should be treated by the client by resending the same
request to the specified location - using the same request method. However, most clients to not implement this and
always use a GET request when redirecting. By default, Zend_Http_Client does the same - when redirecting on a 301
or 302 response, all GET and POST parameters are reset, and a GET request is sent to the new location. This behavior
can be changed by setting the 'strictredirects' configuration parameter to boolean TRUE:
Ejemplo 25.6. Forcing RFC 2616 Strict Redirections on 301 and 302 Responses
// Strict Redirections
$client->setConfig(array('strictredirects' => true));
// Non-strict Redirections
$client->setConfig(array('strictredirects' => false));
You can always get the number of redirections done after sending a request using the getRedirectionsCount() method.
668
Zend_Http
$cookie = Zend_Http_Cookie::fromString('flavor=chocolate%20chips');
$client->setCookie($cookie);
For more information about Zend_Http_Cookie objects, see the section called “Zend_Http_Cookie and
Zend_Http_CookieJar”.
Zend_Http_Client also provides the means for cookie stickiness - that is having the client internally store all sent and
received cookies, and resend them automatically on subsequent requests. This is useful, for example when you need
to log in to a remote site first and receive and authentication or session ID cookie before sending further requests.
For more information about the Zend_Http_CookieJar class, see the section called “The Zend_Http_CookieJar Class:
Instantiation”.
669
Zend_Http
setHeader() can also be easily used to set multiple headers in one call, by providing an array of headers as a single
parameter:
File Uploads
You can upload files through HTTP using the setFileUpload method. This method takes a file name as the first
parameter, a form name as the second parameter, and data as a third optional parameter. If the third data parameter is
null, the first file name parameter is considered to be a real file on disk, and Zend_Http_Client will try to read this file
and upload it. If the data parameter is not null, the first file name parameter will be sent as the file name, but no actual
file needs to exist on the disk. The second form name parameter is always required, and is equivalent to the "name"
attribute of an >input< tag, if the file was to be uploaded through an HTML form. A fourth optional parameter provides
the file's content-type. If not specified, and Zend_Http_Client reads the file from the disk, the mime_content_type
function will be used to guess the file's content type, if it is available. In any case, the default MIME type will be
application/octet-stream.
In the first example, the $text variable is uploaded and will be available as $_FILES['upload'] on the server side.
In the second example, the existing file /tmp/Backup.tar.gz is uploaded to the server and will be available as
$_FILES['bufile']. The content type will be guesses automatically if possible - and if not, the content type will be set
to 'application/octet-stream'.
Uploading files
When uploading files, the HTTP request content-type is automatically set to multipart/form-data. Keep in
mind that you must send a POST or PUT request in order to upload files. Most servers will ignore the requests
body on other request methods.
670
Zend_Http
$xml = '<book>' .
' <title>Islands in the Stream</title>' .
' <author>Ernest Hemingway</author>' .
' <year>1970</year>' .
'</book>';
$client->setRawData($xml, 'text/xml')->request('POST');
The data should be available on the server side through PHP's $HTTP_RAW_POST_DATA variable or through the
php://input stream.
HTTP Authentication
Currently, Zend_Http_Client only supports basic HTTP authentication. This feature is utilized using the setAuth()
method, or by specifying a username and a password in the URI. The setAuth() method takes 3 parameters: The user
name, the password and an optional authentication type parameter. As mentioned, currently only basic authentication
is supported (digest authentication support is planned).
671
Zend_Http
When performing several requests to the same host, it is highly recommended to enable the 'keepalive' configuration
flag. This way, if the server supports keep-alive connections, the connection to the server will only be closed once
all requests are done and the Client object is destroyed. This prevents the overhead of opening and closing TCP
connections to the server.
When you perform several requests with the same client, but want to make sure all the request-specific parameters are
cleared, you should use the resetParameters() method. This ensures that GET and POST parameters, request body and
request-specific headers are reset and are not reused in the next request.
Resetting parameters
Note that non-request specific headers are not reset when the resetParameters method is used. As a matter of
fact, only the 'Content-length' and 'Content-type' headers are reset. This allows you to set-and-forget headers
like 'Accept-language' and 'Accept-encoding'
Another feature designed specifically for consecutive requests is the Cookie Jar object. Cookie Jars allow you to
automatically save cookies set by the server in the first request, and send them on consecutive requests transparently.
This allows, for example, going through an authentication request before sending the actual data fetching request.
If your application requires one authentication request per user, and consecutive requests might be performed in more
than one script in your application, it might be a good idea to store the Cookie Jar object in the user's session. This
way, you will only need to authenticate the user once every session.
$client->setCookieJar($_SESSION['cookiejar']);
} else {
// If we don't, authenticate and store cookies
$client->setCookieJar();
$client->setUri('http://www.example.com/login.php');
$client->setParameterPost(array(
'user' => 'shahar',
'pass' => 'somesecret'
));
$client->request(Zend_Http_Client::POST);
// Now, clear parameters and set the URI to the original one
// (note that the cookies that were set by the server are now
// stored in the jar)
$client->resetParameters();
$client->setUri('http://www.example.com/fetchdata.php');
}
$response = $client->request(Zend_Http_Client::GET);
672
Zend_Http
• Zend_Http_Client_Adapter_Socket (default)
• Zend_Http_Client_Adapter_Proxy
• Zend_Http_Client_Adapter_Curl
• Zend_Http_Client_Adapter_Test
The Zend_Http_Client object's adapter connection adapter is set using the 'adapter' configuration option.
When instantiating the client object, you can set the 'adapter' configuration option to a string containing the
adapter's name (eg. 'Zend_Http_Client_Adapter_Socket') or to a variable holding an adapter object (eg. new
Zend_Http_Client_Adapter_Test). You can also set the adapter later, using the Zend_Http_Client-
>setConfig() method.
The Socket adapter allows several extra configuration options that can be set using Zend_Http_Client-
>setConfig() or passed to the client constructor.
673
Zend_Http
It is recommended to use persistent TCP connections only if you connect to the same server very frequently,
and are sure that the server is capable of handling a large number of concurrent connections. In any case you
are encouraged to benchmark the effect of persistent connections on both the client speed and server load
before using this option.
Additionally, when using persistent connections it is recommended to enable Keep-Alive HTTP requests as
described in the section called “Configuration Parameters” - otherwise persistent connections might have
little or no effect.
While the default SSL settings should work for most applications, you might need to change them if the server
you are connecting to requires special client setup. If so, you should read the sections about SSL transport
layers and options here [http://www.php.net/manual/en/transports.php#transports.inet].
The result of the example above will be similar to opening a TCP connection using the following PHP command:
fsockopen('tls://www.example.com', 443)
You can access the stream context using the following methods of Zend_Http_Client_Adapter_Socket:
• setStreamContext($context) Sets the stream context to be used by the adapter. Can accept either a stream
context resource created using the stream_context_create() [http://php.net/manual/en/function.stream-
context-create.php] PHP function, or an array of stream context options, in the same format provided to this function.
Providing an array will create a new stream context using these options, and set it.
• getStreamContext() Get the stream context of the adapter. If no stream context was set, will create a default
stream context and return it. You can then set or get the value of different context options using regular PHP stream
context functions.
Ejemplo 25.16. Setting stream context options for the Socket adapter
674
Zend_Http
// Array of options
$options = array(
'socket' => array(
// Bind local socket side to a specific interface
'bindto' => '10.1.2.3:50505'
),
'ssl' => array(
// Verify server side certificate,
// do not accept invalid or self-signed SSL certificates
'verify_peer' => true,
'allow_self_signed' => false,
// Method 3: get the default stream context and set the options on it
$context = $adapter->getStreamContext();
stream_context_set_option($context, $options);
// If everything went well, you can now access the context again
$opts = stream_context_get_options($adapter->getStreamContext());
echo $opts['ssl']['peer_certificate'];
Nota
Note that you must set any stream context options before using the adapter to preform actual requests.
If no context is set before preforming HTTP requests with the Socket adapter, a default stream context
will be created. This context resource could be accessed after preforming any requests using the
getStreamContext() method.
675
Zend_Http
Using the Proxy adapter requires several additional configuration parameters to be set, in addition to the default
'adapter' option:
proxy_host should always be set - if it is not set, the client will fall back to a direct connection using
Zend_Http_Client_Adapter_Socket. proxy_port defaults to '8080' - if your proxy listens on a different port
you must set this one as well.
proxy_user and proxy_pass are only required if your proxy server requires you to authenticate. Providing these will
add a 'Proxy-Authentication' header to the request. If your proxy does not require authentication, you can leave these
two options out.
proxy_auth sets the proxy authentication type, if your proxy server requires authentication. Possibly values are
similar to the ones accepted by the Zend_Http_Client::setAuth() method. Currently, only basic authentication
(Zend_Http_Client::AUTH_BASIC) is supported.
// Continue working...
As mentioned, if proxy_host is not set or is set to a blank string, the connection will fall back to a regular direct
connection. This allows you to easily write your application in a way that allows a proxy to be used optionally,
according to a configuration parameter.
Nota
Since the proxy adapter inherits from Zend_Http_Client_Adapter_Socket, you can use the stream
context access method (see the section called “Customizing and accessing the Socket adapter stream context”)
to set stream context options on Proxy connections as demonstrated above.
676
Zend_Http
$config = array(
'adapter' => 'Zend_Http_Client_Adapter_Curl',
'curloptions' => array(CURLOPT_FOLLOWLOCATION => true),
);
$client = new Zend_Http_Client($uri, $config);
By default the cURL adapter is configured to behave exactly like the Socket Adapter and it also accepts the same
configuration parameters as the Socket and Proxy adapters. You can also change the cURL options by either specifying
the 'curloptions' key in the constructor of the adapter or by calling setCurlOption($name, $value). The
$name key corresponds to the CURL_* constants of the cURL extension. You can get access to the Curl handle by
calling $adapter->getHandle();
$putFileSize = filesize("filepath");
$putFileHandle = fopen("filepath", "r");
For this reason, the Zend_Http_Client_Adapter_Test adapter is provided. You can write your application
to use Zend_Http_Client, and just for testing purposes, for example in your unit testing suite, you can replace
the default adapter with a Test adapter (a mock object), allowing you to run tests without actually performing server
connections.
677
Zend_Http
$response = $client->request('GET');
// .. continue parsing $response..
The above example shows how you can preset your HTTP client to return the response you need. Then, you can
continue testing your own code, without being dependent on a network connection, the server's response, etc. In this
case, the test would continue to check how the application parses the XML in the response body.
Sometimes, a single method call to an object can result in that object performing multiple HTTP transactions. In this
case, it's not possible to use setResponse() alone because there's no opportunity to set the next response(s) your program
might need before returning to the caller.
678
Zend_Http
The setResponse() method clears any responses in the Zend_Http_Client_Adapter_Test's buffer and sets the
first response that will be returned. The addResponse() method will add successive responses.
The responses will be replayed in the order that they were added. If more requests are made than the number of
responses stored, the responses will cycle again in order.
In the example above, the adapter is configured to test your object's behavior when it encounters a 302 redirect.
Depending on your application, following a redirect may or may not be desired behavior. In our example, we expect
that the redirect will be followed and we configure the test adapter to help us test this. The initial 302 response is set
up with the setResponse() method and the 200 response to be returned next is added with the addResponse() method.
After configuring the test adapter, inject the HTTP client containing the adapter into your object under test and test
its behavior.
If you need the adapter to fail on demand you can use setNextRequestWillFail($flag). The method will
cause the next call to connect() to throw an Zend_Http_Client_Adapter_Exception exception. This
can be useful when your application caches content from an external site (in case the site goes down) and you want
to test this feature.
try {
// This call will result in a Zend_Http_Client_Adapter_Exception
$client->request();
} catch (Zend_Http_Client_Adapter_Exception $e) {
// ...
}
// Further requests will work as expected until you call setNextRequestWillFail(true) again
679
Zend_Http
In order to do so, you must create your own adapter class that implements the
Zend_Http_Client_Adapter_Interface interface. The following example shows the skeleton of a user-
implemented adapter class. All the public functions defined in this example must be defined in your adapter as well:
class MyApp_Http_Client_Adapter_BananaProtocol
implements Zend_Http_Client_Adapter_Interface
{
/**
* Set the configuration array for the adapter
*
* @param array $config
*/
public function setConfig($config = array())
{
// This rarely changes - you should usually copy the
// implementation in Zend_Http_Client_Adapter_Socket.
}
/**
* Connect to the remote server
*
* @param string $host
* @param int $port
* @param boolean $secure
*/
public function connect($host, $port = 80, $secure = false)
{
// Set up the connection to the remote server
}
/**
* Send request to the remote server
*
* @param string $method
* @param Zend_Uri_Http $url
* @param string $http_ver
* @param array $headers
* @param string $body
* @return string Request as text
*/
public function write($method,
$url,
$http_ver = '1.1',
$headers = array(),
$body = '')
{
680
Zend_Http
/**
* Read response from server
*
* @return string
*/
public function read()
{
// Read response from remote server and return it as a string
}
/**
* Close the connection to the server
*
*/
public function close()
{
// Close the connection to the remote server - called last.
}
}
• Through the constructor, using the following syntax: new Zend_Http_Cookie(string $name, string
$value, string $domain, [int $expires, [string $path, [boolean $secure]]]);
681
Zend_Http
• $expires: Cookie expiration time, as UNIX time stamp (optional, defaults to null). If not set, cookie will be
treated as a 'session cookie' with no expiration time.
• $secure: Boolean, Whether the cookie is to be sent over secure (HTTPS) connections only (optional, defaults
to boolean FALSE)
• By calling the fromString() static method, with a cookie string as represented in the 'Set-Cookie' HTTP response
header or 'Cookie' HTTP request header. In this case, the cookie value must already be encoded. When the cookie
string does not contain a 'domain' part, you must provide a reference URI according to which the cookie's domain
and path will be set.
// You can also take the HTTP response Set-Cookie header and use it.
// This cookie is similar to the previous one, only it will not expire, and
// will only be sent over secure connections
$cookie = Zend_Http_Cookie::fromString('foo=bar; domain=.example.com; ' .
'path=/path; secure');
Nota
When instantiating a cookie object using the Zend_Http_Cookie::fromString() method, the cookie value
is expected to be URL encoded, as cookie strings should be. However, when using the constructor, the cookie
value string is expected to be the real, decoded value.
A cookie object can be transferred back into a string, using the __toString() magic method. This method will produce
a HTTP request "Cookie" header string, showing the cookie's name and value, and terminated by a semicolon (';'). The
value will be URL encoded, as expected in a Cookie header:
682
Zend_Http
• int getExpiryTime(): Get the cookie's expiration time, as UNIX time stamp. If the cookie has no expiration
time set, will return NULL.
• boolean isSecure(): Check whether the cookie is set to be sent over secure connections only. Generally
speaking, if true the cookie should only be sent over HTTPS.
• boolean isExpired(int $time = null): Check whether the cookie is expired or not. If the cookie
has no expiration time, will always return true. If $time is provided, it will override the current time stamp as the
time to check the cookie against.
• boolean isSessionCookie(): Check whether the cookie is a "session cookie" - that is a cookie with no
expiration time, which is meant to expire when the session ends.
683
Zend_Http
• mixed $uri: A Zend_Uri_Http object with a domain name and path to be checked. Optionally, a string
representing a valid HTTP URL can be passed instead. The cookie will match if the URL's scheme (HTTP or
HTTPS), domain and path all match.
• boolean $matchSessionCookies: Whether session cookies should be matched or not. Defaults to true. If
set to false, cookies with no expiration time will never match.
• int $now: Time (represented as UNIX time stamp) to check a cookie against for expiration. If not specified, will
default to the current time.
$cookie->match('https://www.example.com/somedir/foo.php');
// Will return true
$cookie->match('http://www.example.com/somedir/foo.php');
// Will return false, because the connection is not secure
$cookie->match('https://otherexample.com/somedir/foo.php');
// Will return false, because the domain is wrong
684
Zend_Http
$cookie->match('https://example.com/foo.php');
// Will return false, because the path is wrong
$cookie->match('https://www.example.com/somedir/foo.php', false);
// Will return false, because session cookies are not matched
$cookie->match('https://sub.domain.example.com/somedir/otherdir/foo.php');
// Will return true
// Create another cookie object - now, not secure, with expiration time
// in two hours
$cookie = Zend_Http_Cookie::fromString('foo=two+words; ' +
'domain=www.example.com; ' +
'expires='
. date(DATE_COOKIE, time() + 7200));
$cookie->match('http://www.example.com/');
// Will return true
$cookie->match('https://www.example.com/');
// Will return true - non secure cookies can go over secure connections
// as well!
$cookie->match('http://subdomain.example.com/');
// Will return false, because the domain is wrong
If you still wish to manually instantiate a CookieJar object, you can do so by calling "new Zend_Http_CookieJar()"
directly - the constructor method does not take any parameters. Another way to instantiate a CookieJar
object is to use the static Zend_Http_CookieJar::fromResponse() method. This method takes two parameters: a
Zend_Http_Response object, and a reference URI, as either a string or a Zend_Uri_Http object. This method
will return a new Zend_Http_CookieJar object, already containing the cookies set by the passed HTTP response.
The reference URI will be used to set the cookie's domain and path, if they are not defined in the Set-Cookie headers.
685
Zend_Http
It is important to note that each one of these methods takes a special parameter, which sets the return type of the
method. This parameter can have 3 values:
• Zend_Http_CookieJar->getAllCookies($ret_as): Get all cookies from the jar. $ret_as specifies the
return type as described above. If not specified, $ret_type defaults to COOKIE_OBJECT.
• Zend_Http_CookieJar->getMatchingCookies($uri[, $matchSessionCookies[,
$ret_as[, $now]]]): Get all cookies from the jar that match a specified scenario, that is a URI and expiration
time.
• $uri is either a Zend_Uri_Http object or a string specifying the connection type (secure or non-secure),
domain and path to match against.
• $matchSessionCookies is a boolean telling whether to match session cookies or not. Session cookies are
cookies that have no specified expiration time. Defaults to true.
• $ret_as specifies the return type as described above. If not specified, defaults to COOKIE_OBJECT.
• $now is an integer representing the UNIX time stamp to consider as "now" - that is any cookies who are set to
expire before this time will not be matched. If not specified, defaults to the current time.
You can read more about cookie matching here: the section called “Zend_Http_Cookie: Matching against a
scenario”.
686
Zend_Http
Zend_Http_Response
Introduction
Zend_Http_Response provides easy access to an HTTP responses message, as well as a set of static methods
for parsing HTTP response messages. Usually, Zend_Http_Response is used as an object returned by a
Zend_Http_Client request.
In most cases, a Zend_Http_Response object will be instantiated using the factory() method, which reads a string
containing an HTTP response message, and returns a new Zend_Http_Response object:
$str = '';
$sock = fsockopen('www.example.com', 80);
$req = "GET / HTTP/1.1\r\n" .
"Host: www.example.com\r\n" .
"Connection: close\r\n" .
"\r\n";
fwrite($sock, $req);
while ($buff = fread($sock, 1024))
$str .= $sock;
$response = Zend_Http_Response::factory($str);
You can also use the contractor method to create a new response object, by specifying all the parameters of the response:
• $headers: An associative array of HTTP response headers (eg. 'Host' => 'example.com')
• $message: The HTTP response message (eg 'OK', 'Internal Server Error'). If not specified, the message will be
set according to the response code
• Boolean isSuccessful(): Whether the request was successful or not. Returns TRUE for HTTP 1xx and
2xx response codes
• Boolean isError(): Whether the response code implies an error or not. Returns TRUE for HTTP 4xx (client
errors) and 5xx (server errors) response codes
687
Zend_Http
• Boolean isRedirect(): Whether the response is a redirection response or not. Returns TRUE for HTTP 3xx
response codes
if ($response->isError()) {
echo "Error transmitting data.\n"
echo "Server reply was: " . $response->getStatus() .
" " . $response->getMessage() . "\n";
}
// .. process the response here...
Accessor Methods
The main goal of the response object is to provide easy access to various response parameters.
• int getStatus(): Get the HTTP response status code (eg. 200, 504, etc.)
• string getMessage(): Get the HTTP response status message (eg. "Not Found", "Authorization Required")
• string getRawBody(): Get the raw, possibly encoded HTTP response body. If the body was decoded using
GZIP encoding for example, it will not be decoded.
• array getHeaders(): Get the HTTP response headers as an associative array (eg. 'Content-type' => 'text/html')
• string getHeadersAsString($status_line = true, $br = "\n"): Get the entire set of headers
as a string. If $status_line is true (default), the first status line (eg. "HTTP/1.1 200 OK") will also be returned. Lines
are broken with the $br parameter (Can be, for example, "<br />")
• string asString($br = "\n"): Get the entire response message as a string. Lines are broken with the
$br parameter (Can be, for example, "<br />"). You can also use the magic method __toString() when casting the
object as a string. It will then proxy to asString()
if ($response->getStatus() == 200) {
echo "The request returned the following information:<br />";
echo $response->getBody();
} else {
echo "An error occurred while fetching data:<br />";
echo $response->getStatus() . ": " . $response->getMessage();
}
688
Zend_Http
$ctype = $response->getHeader('Content-type');
if (is_array($ctype)) $ctype = $ctype[0];
$body = $response->getBody();
if ($ctype == 'text/html' || $ctype == 'text/xml') {
$body = htmlentities($body);
}
echo $body;
Apart from parser methods, the class also includes a set of decoders for common HTTP response transfer encodings:
689
690
Capítulo 26. Zend_InfoCard
Introduction
The Zend_InfoCard component implements relying-party support for Information Cards. Information Cards are
used for identity management on the internet and authentication of users to web sites. The web sites that the user
ultimately authenticates to are called relying-parties.
Detailed information about information cards and their importance to the internet identity metasystem can be found
on the IdentityBlog [http://www.identityblog.com/].
In the example above, the requiredClaims <param> tag is used to identify pieces of information known as claims
(i.e. person's first name, last name) which the web site (a.k.a "relying party") needs in order a user to authenticate using
an information card. For your reference, the full URI (for instance the givenname claim) is as follows: http://
schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname
When the above HTML is activated by a user (clicks on it), the browser will bring up a card selection program which
not only shows them which information cards meet the requirements of the site, but also allows them to select which
information card to use if multiple meet the criteria. This information card is transmitted as an XML document to the
specified POST URL and is ready to be processed by the Zend_InfoCard component.
Note, Information cards can only be HTTP POSTed to SSL-encrypted URLs. Please consult your web server's
documentation on how to set up SSL encryption.
691
Zend_InfoCard
<?php
if (isset($_POST['xmlToken'])) {
$adapter->addCertificatePair('/usr/local/Zend/apache2/conf/server.key',
'/usr/local/Zend/apache2/conf/server.crt');
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($adapter);
switch ($result->getCode()) {
case Zend_Auth_Result::SUCCESS:
$claims = $result->getIdentity();
print "Given Name: {$claims->givenname}<br />";
print "Surname: {$claims->surname}<br />";
print "Email Address: {$claims->emailaddress}<br />";
print "PPI: {$claims->getCardID()}<br />";
break;
case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:
print "The Credential you provided did not pass validation";
break;
default:
case Zend_Auth_Result::FAILURE:
print "There was an error processing your credentials.";
break;
}
if (count($result->getMessages()) > 0) {
print "<pre>";
var_dump($result->getMessages());
print "</pre>";
}
}
?>
<hr />
<div id="login" style="font-family: arial; font-size: 2em;">
<p>Simple Login Demo</p>
<form method="post">
<input type="submit" value="Login" />
<object type="application/x-informationCard" name="xmlToken">
<param name="tokenType"
value="urn:oasis:names:tc:SAML:1.0:assertion" />
<param name="requiredClaims"
value="http://.../claims/givenname
http://.../claims/surname
http://.../claims/emailaddress
http://.../claims/privatepersonalidentifier" />
</object>
</form>
</div>
692
Zend_InfoCard
In the example above, we first create an instance of the Zend_Auth_Adapter_InfoCard and pass the XML data
posted by the card selector into it. Once an instance has been created you must then provide at least one SSL certificate
public/private key pair used by the web server that received the HTTP POST. These files are used to validate the
destination of the information posted to the server and are a requirement when using Information Cards.
Once the adapter has been configured, you can then use the standard Zend_Auth facilities to validate the provided
information card token and authenticate the user by examining the identity provided by the getIdentity() method.
<?php
if (isset($_POST['xmlToken'])) {
$infocard = new Zend_InfoCard();
$infocard->addCertificatePair('/usr/local/Zend/apache2/conf/server.key',
'/usr/local/Zend/apache2/conf/server.crt');
$claims = $infocard->process($_POST['xmlToken']);
if($claims->isValid()) {
print "Given Name: {$claims->givenname}<br />";
print "Surname: {$claims->surname}<br />";
print "Email Address: {$claims->emailaddress}<br />";
print "PPI: {$claims->getCardID()}<br />";
} else {
print "Error Validating identity: {$claims->getErrorMsg()}";
}
}
?>
<hr />
<div id="login" style="font-family: arial; font-size: 2em;">
<p>Simple Login Demo</p>
<form method="post">
<input type="submit" value="Login" />
<object type="application/x-informationCard" name="xmlToken">
<param name="tokenType"
value="urn:oasis:names:tc:SAML:1.0:assertion" />
<param name="requiredClaims"
value="http://.../claims/givenname
http://.../claims/surname
http://.../claims/emailaddress
http://.../claims/privatepersonalidentifier" />
</object>
</form>
</div>
In the example above, we use the Zend_InfoCard component independently to validate the token provided by the
user. As was the case with the Zend_Auth_Adapter_InfoCard, we create an instance of Zend_InfoCard
and then set one or more SSL certificate public/private key pairs used by the web server. Once configured, we can use
the process() method to process the information card and return the results.
693
Zend_InfoCard
In most cases you will never need to use the getClaim() method. However, if your requiredClaims mandate
that you request claims from multiple different sources/namespaces then you will need to extract them explicitly using
this method (simply pass it the full URI of the claim to retrieve its value from within the information card). Generally
speaking however, the Zend_InfoCard component will set the default URI for claims to be the one used the most
frequently within the information card itself and the simplified property-access method can be used.
As part of the validation process, it is the developer's responsibility to examine the issuing source of the claims
contained within the information card and to decide if that source is a trusted source of information. To do so, the
getIssuer() method is provided within the Zend_InfoCard_Claims object which returns the URI of the
issuer of the information card claims.
An example of how to attach an information card to an existing traditional-authentication account is shown below:
// ...
public function submitinfocardAction()
{
if (!isset($_REQUEST['xmlToken'])) {
throw new ZBlog_Exception('Expected an encrypted token ' .
'but was not provided');
}
try {
$claims = $infoCard->process($request['xmlToken']);
} catch(Zend_InfoCard_Exception $e) {
// TODO Error processing your request
throw $e;
}
if ($claims->isValid()) {
$db = ZBlog_Data::getAdapter();
694
Zend_InfoCard
$ppi = $db->quote($claims->getCardID());
$fullname = $db->quote("{$claims->givenname} {$claims->surname}");
try {
$db->query($query);
} catch(Exception $e) {
// TODO Failed to store in DB
}
$this->view->render();
return;
} else {
throw new
ZBlog_Exception("Infomation card failed security checks");
}
}
The Zend_InfoCard adapter is used as a callback mechanism within the component to perform various tasks,
such as storing and retrieving Assertion IDs for information cards when they are processed by the component. While
storing the assertion IDs of submitted information cards is not necessary, failing to do so opens up the possibility of
the authentication scheme being compromised through a replay attack.
To prevent this, one must implement the Zend_InfoCard_Adapter_Interface and set an instance of this
interface prior to calling either the process() (standalone) or authenticate() method as a Zend_Auth
adapter. To set this interface, the setAdapter() method should be used. In the example below, we set a
Zend_InfoCard adapter and use it in our application:
695
Zend_InfoCard
{
/* Delete a given assertion by URI/ID */
}
}
$claims = $infoCard->process($_POST['xmlToken']);
696
Capítulo 27. Zend_Json
Introducción
Zend_Json ofrece métodos convenientes para la serialización desde PHP nativo a JSON y la decodificación JSON
a PHP nativo. Para más información sobre JSON, visite el sitio del proyecto JSON [http://www.json.org/].
JSON, JavaScript Object Notation, puede ser utilizado para intercambio de datos entre JavaScript y otros lenguajes.
Dado que JSON puede ser evaluado directamente por JavaScript, es más eficiente y ligero que el formato XML para
intercambiar datos con clientes JavaScript.
Adicionalmente, Zend_Json provee una forma útil para convertir cualquier string arbitrario con formato XML en
un string con formato JSON. Este rasgo integral permite a desarrolladores PHP transformar los datos codificados
de la empresa en formato XML a formato JSON antes de enviarlos a las aplicaciones del cliente Ajax basadas en
navegadores web. Provee una manera fácil de hacer conversiones dinámicas de datos del código en el servidor evitando
así el innecesario parsing de XML de las aplicaciones en el navegador. Ofrece una agradable función útil que facilita
las técnicas de procesamiento de datos para aplicaciones específicas.
Uso Básico
El uso de Zend_Json consiste en utilizar los dos métodos públicos estáticos disponibles:
Zend_Json::encode() y Zend_Json::decode().
// Recuperar un valor:
$phpNative = Zend_Json::decode($encodedValue);
JSON no permite referencias a objetos, de manera que debe tenerse cuidado de no codificar
objetos con referencias recursivas. Si tiene problemas con la recursión, Zend_Json::encode() y
Zend_Json_Encoder::encode() permiten un segundo parámetro opcional para comprobar si hay recursión; si
un objeto es serializado dos veces, se emitirá una excepción.
La decodificación de objetos JSON plantea una dificultad adicional, sin embargo, ya que los objetos Javascript se
corresponden más estrechamente a un array asociativo de PHP. Algunos sugieren que debe pasarse un identificador
de clase, y una instancia del objeto de esa clase debe crearse y alimentarla con datos de pares clave/valor del objeto
JSON; otros consideran que esto podría plantear un considerable riesgo de seguridad.
Por defecto, Zend_Json decodificará objetos JSON como arrays asociativos. Sin embargo, si desea retornar un
objeto, puede especificar esto:
697
Zend_Json
Por lo tanto, cualquiera de los objetos decodificados son devueltos como objetos StdClass con propiedades
correspondientea a pares clave/valor en la notación JSON.
La recomendación de Zend Framework es que el desarrollador debe decidir cómo decodificar objetos JSON. Si debe
crearse un objeto de un determinado tipo, puede ser creado en el código del desarrollador y alimentado con datos de
los valores decodificados utilizando Zend_Json.
Codificador/Decodificador Interno
Zend_Json tiene dos modos diferentes dependiendo de si ext/json está habilitada o no en su instalación PHP. Si ext/
json está instalado por defecto, las funciones json_encode() y json_decode() se utilizan para la codificación
y decodificación JSON. Si ext/json no está instalado, una implementación de Zend Framework en código PHP es
utilizada para la codificación/decodificación. Esto es considerablemente más lento que usando la extensión de PHP,
pero se comporta exactamente igual.
También algunas veces puede querer utilizar el codificador/decodificador interno incluso si tiene ext/json instalado.
Puede hacer esto llamando a:
Zend_Json::$useBuiltinEncoderDecoder = true:
Expresiones JSON
Javascript hace uso intenso de las funciones anónimas de llamadas de retorno, que pueden guardarse en variables
del objeto JSON. Aunque solo funcionan si no regresaron dentro comillas dobles, que es lo que hace naturalmente
Zend_Json. Con la Expression de apoyo para Zend_Json este apoyo puede codificar objetos JSON con callbacks
validos de javascript. Esto funciona tanto con json_encode() como con el codificador interno.
Un callback javascript se representa usando el objero Zend_Json_Expr. Este implementa el patrón del
objeto valor y es inmutable. Se puede establecer la expresión de javascript como el primer argumento del
constructor. Por defecto Zend_Json::encode no codifica callbacks javascript, usted tiene que pasar la opción
'enableJsonExprFinder' = true dentro de la función encode. Si se habilita, la expresión de apoyo trabaja
para todas las expresiones anidadas en grandes estructuras de objetos. Un ejemplo de uso se vería así:
$data = array(
'onClick' => new Zend_Json_Expr('function() {'
. 'alert("Yo soy un callback válido de javascript '
. 'creado por Zend_Json"); }'),
'other' => 'sin expresión',
);
$jsonObjectWithExpression = Zend_Json::encode(
$data,
false,
array('enableJsonExprFinder' => true)
698
Zend_Json
);
Zend_Json incluye una función estática llamada Zend_Json::fromXml(). Esta función generará JSON desde
una determinada entrada XML. Esta función toma cualquier string XML arbitrario como un parámetro de entrada.
También puede tomar opcionalmente parámetros booleanos de entrada que instruyan a la lógica de conversión de
ignorar o no los atributos XML durante el proceso de conversión. Si este parámetro opcional de entrada no está dado,
entonces el comportamiento por defecto es ignorar los atributos XML. La llamada a esta función se hace como se
muestra a continuación:
Zend_Json::fromXml() función que hace la conversión del parámetro de entrada formateado como un string
XML y devuelve el string de salida equivalente formateado a JSON. En caso de cualquier entrada con formato XML
erróneo o un error en la lógica de conversión, esta función arrojará una excepción. La conversión lógica también
usa técnicas recursivas para recorrer el árbol XML. Soporta una recursión de hasta 25 niveles de profundidad. Más
allá de esa profundidad, arrojará una Zend_Json_Exception. Hay varios archivos XML con diversos grados de
complejidad provistas en el directorio de tests de Zend Framework. Se pueden utilizar para probar la funcionalidad
de la característica xml2json.
El siguiente es un ejemplo simple que muestra tanto el string de entrada XML pasado a y al string JSON de salida
devuelto como resultado de la función Zend_Json::fromXml(). Este ejemplo utilizó el parámetro de la función
opcional como para no ignorar los atributos XML durante la conversión. Por lo tanto, puede notar que el string
resultante JSON incluye una representación de los atributos XML presentes en el string de entrada XML.
<book id="2">
<title>PHP Hacks</title>
<author><first>Jack</first><last>Herrington</last></author>
<publisher>O'Reilly</publisher>
</book>
<book id="3">
<title>Podcasting Hacks</title>
<author><first>Jack</first><last>Herrington</last></author>
<publisher>O'Reilly</publisher>
699
Zend_Json
</book>
</books>
{
"books" : {
"book" : [ {
"@attributes" : {
"id" : "1"
},
"title" : "Code Generation in Action",
"author" : {
"first" : "Jack", "last" : "Herrington"
},
"publisher" : "Manning"
}, {
"@attributes" : {
"id" : "2"
},
"title" : "PHP Hacks", "author" : {
"first" : "Jack", "last" : "Herrington"
},
"publisher" : "O'Reilly"
}, {
"@attributes" : {
"id" : "3"
},
"title" : "Podcasting Hacks", "author" : {
"first" : "Jack", "last" : "Herrington"
},
"publisher" : "O'Reilly"
}
]}
}
Más detalles sobre esta característica xml2json pueden encontrarse en la propuesta original. Eche un vistazo a la
Zend_xml2json proposal [http://tinyurl.com/2tfa8z].
JSON-RPC es un protocolo liviano de Remote Procedure Call que utiliza JSON para envolver sus mensajes. Esta
implementación JSON-RPC sigue la API PHP de SoapServer [http://us.php.net/manual/en/function.soap-soapserver-
construct.php]. Esto significa que, en una situación típica, simplemente:
700
Zend_Json
Zend_Json_Server utiliza the section called “Zend_Server_Reflection” para realizar reflexión sobre cualquiera
de las clases o funciones agregadas, y utiliza esa información para construir tanto la SMD y hacer cumplir el método de
llamado de firmas. Como tal, es imperativo que cualquier de las funciones agregadas y/o los métodos de clase tengan
mínimamente una plena documentación de PHP docblocks:
Zend_Json_Server escucha por solicitudes POST únicamente en este momento; afortunadamente, la mayoría de
las implementaciones del cliente JSON-RPC en los medios en el momento de escribir esto, sólo requieren a POST
como es. Esto hace que sea fácil de utilizar el mismo punto final del servidor para manejar a ambas peticiones así
como para entregar el servicio SMD, como se muestra en el siguiente ejemplo.
/**
* Calculator - clase de ejemplo para exponer via JSON-RPC
*/
class Calculator
{
/**
* Devuelve la suma de dos variables
*
* @param int $x
* @param int $y
* @return int
*/
public function add($x, $y)
{
return $x + $y;
}
/**
* Devuelve la diferencia de dos variables
*
* @param int $x
* @param int $y
* @return int
*/
public function subtract($x, $y)
{
return $x - $y;
}
/**
* Devuelve el producto de dos variables
701
Zend_Json
*
* @param int $x
* @param int $y
* @return int
*/
public function multiply($x, $y)
{
return $x * $y;
}
/**
* Devuelve la división de dos variables
*
* @param int $x
* @param int $y
* @return float
*/
public function divide($x, $y)
{
return $x / $y;
}
}
Nótese que cada método tiene un docblock con entradas indicando cada parámetro y su tipo, así como una entrada para
el valor de retorno. Esto es absolutamente crítico cuando se usa Zend_Json_Server -- o cualquier otro componente
del servidor en Zend Framework, por esa cuestión.
// Manejar el requerimiento:
$server->handle();
Sin embargo, esto no soluciona el problema de devolución de un SMD para que el cliente JSON-RPC pueda
autodescubrir los métodos. Esto puede lograrse determinando el método del requerimiento HTTP, y luego
especificando algún servidor de metadatos:
if ('GET' == $_SERVER['REQUEST_METHOD']) {
// Indica el punto final de la URL, y la versión en uso de JSON-RPC:
$server->setTarget('/json-rpc.php')
->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
// Capturar el SMD
$smd = $server->getServiceMap();
702
Zend_Json
header('Content-Type: application/json');
echo $smd;
return;
}
$server->handle();
Si utiliza el servidor JSON-RPC con Dojo toolkit, también necesitará establecer un flag de compatibilidad especial
para garantizar que los dos interoperen correctamente:
if ('GET' == $_SERVER['REQUEST_METHOD']) {
$server->setTarget('/json-rpc.php')
->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
$smd = $server->getServiceMap();
header('Content-Type: application/json');
echo $smd;
return;
}
$server->handle();
Detalles Avanzados
Aunque la mayor funcionalidad de Zend_Json_Server se puntualiza en Ejemplo 27.1, “Uso de
Zend_Json_Server”, hay más funcionalidad avanzada disponible.
Zend_Json_Server
Zend_Json_Server es la clase núcleo en la propuesta JSON-RPC; que maneja todas las peticiones y como
respuesta devuelve un conjunto de datos. Tiene los siguientes métodos:
• setClass($class): Especifica una clase u objeto para agregar al servidor; todos los métodos públicos de ese
item serán expuestos como métodos JSON-RPC.
703
Zend_Json
Zend_Json_Server_Request
El medio ambiente de una solicitud JSON-RPC está encapsulado en el objeto Zend_Json_Server_Request. Este
objeto le permite establecer porciones necesarias de la solicitud JSON-RPC, incluida el ID de la solicitud, parámetros
y especificaciones de la versión JSON-RPC. Tiene la capacidad de cargarse a sí mismo via JSON o un conjunto de
opciones, y puede mostrase a si mismo como JSON vía el método toJson().
• setOptions(array $options): Especifica la configuración del objeto. $options puede contener claves
que concuerden con cualuier método 'set': setParams(), setMethod(), setId(), y setVersion().
• addParam($value, $key = null): Agrega un parámetro para usar con el método de llamada. Los
parámetros pueden ser sólo los valores, o pueden incluir opcionalmente el nombre del parámetro.
• setParams(array $params): Establece todos los parámetros a la vez; sobrescribe cualquiera de los
parámetros existentes.
• setId($name): Establecer el identificador de solicitud(utilizado por el cliente para igualar las solicitudes de
respuestas).
704
Zend_Json
Zend_Json_Server_Response
La respuesta del conjunto de datos JSON-RPC es encapsulada en el objeto Zend_Json_Server_Response. Este
objeto le permite ajustar el valor de retorno de la solicitud, siendo la respuesta un error o no, el identificador de solicitud,
con que versión de especificación esta conformada la respuesta de JSON-RPC, y, opcionalmente el mapa de servicio.
• setId($name): Establecer el identificador de solicitud (de manera que la respuesta del cliente pueda coincidir
con la solicitud original).
• setVersion($version): Establecer la versión JSON-RPC con la que deba estar conformada la respuesta.
• toJson(): Serializar la respuesta a JSON. Si la respuesta es una respuesta de error, serializar el objeto error.
Una versión específica de HTTP está disponible a través de Zend_Json_Server_Response_Http. Esta clase
enviará las cabeceras HTTP apropiadas así como serializará la respuesta como JSON.
Zend_Json_Server_Error
JSON-RPC tiene un formato especial para informar condiciones de error. Todos los errores necesitan proporcionar,
mínimamente, un mensaje de error y un código de error; opcionalmente, pueden proporcionar datos adicionales, tales
como un backtrace.
Los códigos de error derivan de los recomendados por el proyecto XML-RPC EPI [http://xmlrpc-epi.sourceforge.net/
specs/rfc.fault_codes.php]. Zend_Json_Server apropiadamente asigna el código sobre la base de la condición de
error. Para las excepciones de la aplicación, se utiliza el código '-32000'.
• setCode($code): Establece el código de error; si el código de error no está en el rango de aceptación de XML-
RPC, -32000 será asignado.
705
Zend_Json
• setData($data): Establecer el conjunto de datos auxiliares para calificar más adelante el error, tal como un
backtrace.
• toArray(): Mandar el error a un array. El array contendrá las claves 'code', 'message', y 'data'.
Zend_Json_Server_Smd
SMD quiere decir Service Mapping Description, un esquema JSON que define cómo un cliente puede interactuar con
un servicio web en particular. En el momento de escribir esto, la especificación [http://groups.google.com/group/json-
schema/web/service-mapping-description-proposal] todavía no ha sido ratificada oficialmente, pero ya está en uso en
Dojo toolkit así como en otros clientes consumidores de JSON-RPC.
En su aspecto más básico, un SMD indica el método de transporte (POST, GET, TCP/IP, etc), el tipo de envoltura de la
solicitud (generalmente se basa en el protocolo del servidor), el objetivo URL del proveedor del servicio, y un mapa de
los servicios disponibles. En el caso de JSON-RPC, el servicio de mapa es una lista de los métodos disponibles, en el
que cada método documenta los parámetros disponibles y sus tipos, así como los tipos de valores esperados a devolver.
Zend_Json_Server_Smd Proporciona un objeto orientado para construir servicios de mapas. Básicamente, pasa
los metadatos describiendo el servicio usando mutators, y especifica los servicios (métodos y funciones).
Zend_Json_Server Realmente todo esto sucede entre bambalinas para usted, utilizando reflexión sobre las clases
y funciones agregadas; debe crear su propio servicio de mapas sólo si necesita brindar funcionalidad personalizada
que la introspección de clase y función no puede ofrecer.
• setOptions(array $options): Establecer un objeto SMD desde un array de opciones. Todos los mutators
(métodos comenzando con 'set') se pueden usar como claves.
• setContentType($type): Establecer el tipo de contenido que deben utilizar las solicitudes (por defecto, es
'application/json»).
706
Zend_Json
• getContentType(): Conseguir el tipo del contenido actual para las solicitudes al servicio.
• setId($id): Normalmente, este es el punto final de la URL del servicio (igual que el objetivo).
• getId(): Recuperar el ID del servicio (normalmente el punto final de la URL del servicio).
• isDojoCompatible(): Devuelve el valor del flag de compatibilidad de Dojo (falso, por defecto).
• addService($service): Añade un servicio al mapa. Puede ser un array de información a pasar al constructor
de Zend_Json_Server_Smd_Service, o una instancia de esa clase.
• setOptions(array $options): Establecer el estado del objeto dede un array. Cualquier mutator (métodos
comenzando con 'set') puede ser utilizado como una clave y establecerlo mediante este método.
• setName($name): Establecer el nombre del servicio (típicamente, la función o el nombre del método).
• setTarget($target): Establecer el punto final de la URL del servicio (típicamente, este será el mismo que
el SMD en general, al cual el servicio está agregado).
707
Zend_Json
• addParams(array $params): Agregar varios parámetros a la vez; cada param debería ser un array asociativo
conteniendo mínimamente la clave 'type' describiendo el tipo de parámetro y, opcionalmente la clave 'order';
cualquiera de las otras claves serán pasados como $options a addOption().
708
Capítulo 28. Zend_Layout
Introducción
Zend_Layout implementa un patrón clásico "Vista en dos etapas" (Two Step View) permitiendo a los
desarrolladores colocar el contenido de la aplicación dentro de otra vista, usualmente representando la plantilla del
sitio. Tales plantillas son a menudo denominadas layouts por otros proyectos, y Zend Framework ha adoptado este
término por consistencia.
• Automatizar la selección y renderizado de layouts cuando se usan con los componentes MVC de Zend Framework.
• Permitir configuraciones, incluyendo el nombre del layout, resolución (inflexión) del script layout, y ruta del script
layout.
• Permitir deshabilitar layouts, cambiar el script de diseño y otras condiciones; permitir estas acciones dentro de los
controladores y scripts de vista.
• Seguir normas de resolución similares (inflexión) como el ViewRenderer, pero permitiendo también el uso de
normas distintas
Layout scripts
In both cases, however, you'll need to create a layout script. Layout scripts simply utilize Zend_View (or whatever
view implementation you are using). Layout variables are registered with a Zend_Layout placeholder, and may be
accessed via the placeholder helper or by fetching them as object properties of the layout object via the layout helper.
As an example:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Site</title>
</head>
<body>
<?php
// fetch 'content' key using layout helper:
709
Zend_Layout
echo $this->layout()->content;
Because Zend_Layout utilizes Zend_View for rendering, you can also use any view helpers registered, and also
have access to any previously assigned view variables. Particularly useful are the various placeholder helpers, as they
allow you to retrieve content for areas such as the <head> section, navigation, etc.:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php echo $this->headTitle() ?>
<?php echo $this->headScript() ?>
<?php echo $this->headStyle() ?>
</head>
<body>
<?php echo $this->render('header.phtml') ?>
Zend_Layout::startMvc() creates an instance of Zend_Layout with any optional configuration you provide
it. It then registers a front controller plugin that renders the layout with any application content once the dispatch loop
is done, and registers an action helper to allow access to the layout object from your action controllers. Additionally,
you may at any time grab the layout instance from within a view script using the layout view helper.
First, let's look at how to initialize Zend_Layout for use with the MVC:
// In your bootstrap:
710
Zend_Layout
Zend_Layout::startMvc();
startMvc() can take an optional array of options or Zend_Config object to customize the instance; these options
are detailed in the section called “Zend_Layout Configuration Options”.
In an action controller, you may then access the layout instance as an action helper:
In your view scripts, you can then access the layout object via the layout view helper. This view helper is slightly
different than others in that it takes no arguments, and returns an object instead of a string value. This allows you to
immediately call methods on the layout object:
At any time, you can fetch the Zend_Layout instance registered with the MVC via the getMvcInstance()
static method:
Finally, Zend_Layout's front controller plugin has one important feature in addition to rendering the layout: it
retrieves all named segments from the response object and assigns them as layout variables, assigning the 'default'
segment to the variable 'content'. This allows you to access your application content and render it in your view scripts.
As an example, let's say your code first hits FooController::indexAction(), which renders some content
to the default response segment, and then forwards to NavController::menuAction(), which renders content
to the 'nav' response segment. Finally, you forward to CommentController::fetchAction() and fetch some
comments, but render those to the default response segment as well (which appends content to that segment). Your
view script could then render each separately:
<body>
<!-- renders /nav/menu -->
<div id="nav"><?php echo $this->layout()->nav ?></div>
711
Zend_Layout
</body>
This feature is particularly useful when used in conjunction with the ActionStack action helper and plugin, which you
can use to setup a stack of actions through which to loop, and thus create widgetized pages.
When used as a standalone component, simply instantiate the layout object, use the various accessors to set state, set
variables as object properties, and render the layout:
Sample Layout
Sometimes a picture is worth a thousand words. The following is a sample layout script showing how it might all
come together.
712
Zend_Layout
713
Zend_Layout
The actual order of elements may vary, depending on the CSS you've setup; for instance, if you're using absolute
positioning, you may be able to have the navigation displayed later in the document, but still show up at the top; the
same could be said for the sidebar or header. The actual mechanics of pulling the content remain the same, however.
• layout: the layout to use. Uses the current inflector to resolve the name provided to the appropriate layout view script.
By default, this value is 'layout' and resolves to 'layout.phtml'. Accessors are setLayout() and getLayout().
• layoutPath: the base path to layout view scripts. Accessors are setLayoutPath() and getLayoutPath().
• contentKey: the layout variable used for default content (when used with the MVC). Default value is 'content'.
Accessors are setContentKey() and getContentKey().
• mvcSuccessfulActionOnly: when using the MVC, if an action throws an exception and this flag is true,
the layout will not be rendered (this is to prevent double-rendering of the layout when the ErrorHandler
plugin is in use). By default, the flat is true. Accessors are setMvcSuccessfulActionOnly() and
getMvcSuccessfulActionOnly().
• view: the view object to use when rendering. When used with the MVC, Zend_Layout will attempt to use the
view object registered with the ViewRenderer if no view object has been passed to it explicitly. Accessors are
setView() and getView().
• helperClass: the action helper class to use when using Zend_Layout with the MVC components. By default,
this is Zend_Layout_Controller_Action_Helper_Layout. Accessors are setHelperClass() and
getHelperClass().
• pluginClass: the front controller plugin class to use when using Zend_Layout with the MVC components. By
default, this is Zend_Layout_Controller_Plugin_Layout. Accessors are setPluginClass() and
getPluginClass().
• inflector: the inflector to use when resolving layout names to layout view script paths; see the Zend_Layout
inflector documentation for more details. Accessors are setInflector() and getInflector().
Ejemplos
The following examples assume the following $options array and $config object:
$options = array(
'layout' => 'foo',
'layoutPath' => '/path/to/layouts',
'contentKey' => 'CONTENT', // ignored when MVC not used
);
714
Zend_Layout
/**
[layout]
layout = "foo"
layoutPath = "/path/to/layouts"
contentKey = "CONTENT"
*/
$config = new Zend_Config_Ini('/path/to/layout.ini', 'layout');
// Using constructor:
$layout = new Zend_Layout($options);
// Using startMvc():
$layout = Zend_Layout::startMvc($options);
// Using constructor:
$layout = new Zend_Layout($config);
// Using startMvc():
$layout = Zend_Layout::startMvc($config);
Note, however, that certain options, such as pluginClass and helperClass, will have no affect when passed
using this method; they need to be passed to the constructor or startMvc() method.
715
Zend_Layout
$layout->setLayout('foo')
->setLayoutPath('/path/to/layouts')
->setContentKey('CONTENT');
• Custom view objects. Zend_Layout allows you to utilize any class that implements Zend_View_Interface.
• Custom front controller plugins. Zend_Layout ships with a standard front controller plugin that automates
rendering of layouts prior to returning the response. You can substitute your own plugin.
• Custom action helpers. Zend_Layout ships with a standard action helper that should be suitable for most needs
as it is a dumb proxy to the layout object itself.
• Custom layout script path resolution. Zend_Layout allows you to use your own inflector for layout script path
resolution, or simply to modify the attached inflector to specify your own inflection rules.
If you need to use a custom Zend_View implementation that does not support these helpers, you will need
to find a way to get the layout variables to the view. This can be done by either extending the Zend_Layout
object and altering the render() method to pass variables to the view, or creating your own plugin class
that passes them prior to rendering the layout.
Alternately, if your view implementation supports any sort of plugin capability, you can access the variables
via the 'Zend_Layout' placeholder, using the placeholder helper:
716
Zend_Layout
desire to write your own, you can specify the name of the plugin class to load by passing the pluginClass option
to the startMvc() method.
Any plugin class you write for this purpose will need to extend Zend_Controller_Plugin_Abstract, and
should accept a layout object instance as an argument to the constructor. Otherwise, the details of your implementation
are up to you.
Should you feel the need to write custom functionality, simply write an action helper class extending
Zend_Controller_Action_Helper_Abstract and pass the class name as the helperClass option to the
startMvc() method. Details of the implementation are up to you.
You have three options for modifying inflection: modify the inflection target and/or view suffix via Zend_Layout
accessors, modify the inflector rules and target of the inflector associated with the Zend_Layout instance, or create
your own inflector instance and pass it to Zend_Layout::setInflector().
Let's say you want the layout script to end in the suffix 'html', and that you want to separate MixedCase and camelCased
words with underscores instead of dashes, and not lowercase the name. Additionally, you want it to look in a 'layouts'
subdirectory for the script.
717
Zend_Layout
$layout->getInflector()->setTarget('layouts/:script.:suffix')
->setStaticRule('suffix', 'html')
->setFilterRule(array('Word_CamelCaseToUnderscore'));
718
Capítulo 29. Zend_Ldap
Introduction
Zend_Ldap is a class for performing LDAP operations including but not limited to binding, searching and modifying
entries in an LDAP directory.
Theory of operation
This component currently consists of the main Zend_Ldap class, that conceptually represents a binding to a single
LDAP server and allows for executing operations against a LDAP server such as OpenLDAP or ActiveDirectory (AD)
servers. The parameters for binding may be provided explicitly or in the form of an options array. Zend_Ldap_Node
provides an object-oriented interface for single LDAP nodes and can be used to form a basis for an active-record-like
interface for a LDAP-based domain model.
The component provides several helper classes to perform operations on LDAP entries (Zend_Ldap_Attribute)
such as setting and retrieving attributes (date values, passwords, boolean values, ...), to create and modify LDAP filter
strings (Zend_Ldap_Filter) and to manipulate LDAP distinguished names (DN) (Zend_Ldap_Dn).
Additionally the component abstracts LDAP schema browsing for OpenLDAP and ActiveDirectoy servers
Zend_Ldap_Node_Schema and server information retrieval for OpenLDAP-, ActiveDirectory- and Novell
eDirectory servers (Zend_Ldap_Node_RootDse).
Using the Zend_Ldap class depends on the type of LDAP server and is best summarized with some simple examples.
If you are using OpenLDAP, a simple example looks like the following (note that the bindRequiresDn option is
important if you are not using AD):
$options = array(
'host' => 's0.foo.net',
'username' => 'CN=user1,DC=foo,DC=net',
'password' => 'pass1',
'bindRequiresDn' => true,
'accountDomainName' => 'foo.net',
'baseDn' => 'OU=Sales,DC=foo,DC=net',
);
$ldap = new Zend_Ldap($options);
$acctname = $ldap->getCanonicalAccountName('abaker',
Zend_Ldap::ACCTNAME_FORM_DN);
echo "$acctname\n";
$options = array(
'host' => 'dc1.w.net',
'useStartTls' => true,
'username' => 'user1@w.net',
'password' => 'pass1',
'accountDomainName' => 'w.net',
'accountDomainNameShort' => 'W',
'baseDn' => 'CN=Users,DC=w,DC=net',
);
719
Zend_Ldap
Note that we use the getCanonicalAccountName() method to retrieve the account DN here only because that
is what exercises the most of what little code is currently present in this class.
This behavior is critical to Zend_Auth_Adapter_Ldap, which passes the username supplied by the user directly
to bind().
The following example illustrates how the non-DN username 'abaker' can be used with bind():
$options = array(
'host' => 's0.foo.net',
'username' => 'CN=user1,DC=foo,DC=net',
'password' => 'pass1',
'bindRequiresDn' => true,
'accountDomainName' => 'foo.net',
'baseDn' => 'OU=Sales,DC=foo,DC=net',
);
$ldap = new Zend_Ldap($options);
$ldap->bind('abaker', 'moonbike55');
$acctname = $ldap->getCanonicalAccountName('abaker',
Zend_Ldap::ACCTNAME_FORM_DN);
echo "$acctname\n";
The bind() call in this example sees that the username 'abaker' is not in DN form, finds bindRequiresDn is TRUE,
uses 'CN=user1,DC=foo,DC=net' and 'pass1' to bind, retrieves the DN for 'abaker', unbinds and then rebinds with
the newly discovered 'CN=Alice Baker,OU=Sales,DC=foo,DC=net'.
720
Zend_Ldap
The default canonicalization depends on what account domain name options were
supplied. If accountDomainNameShort was supplied, the default accountCanonicalForm value is
ACCTNAME_FORM_BACKSLASH. Otherwise, if accountDomainName was supplied, the default is
ACCTNAME_FORM_PRINCIPAL.
Account name canonicalization ensures that the string used to identify an account is consistent regardless of what was
supplied to bind(). For example, if the user supplies an account name of abaker@example.com or just abaker
and the accountCanonicalForm is set to 3, the resulting canonicalized name would be EXAMPLE\abaker.
Consider the following example that illustrates the technique required to implement multi-domain authentication and
failover:
$acctname = 'W\\user2';
$password = 'pass2';
$multiOptions = array(
'server1' => array(
'host' => 's0.foo.net',
'username' => 'CN=user1,DC=foo,DC=net',
'password' => 'pass1',
'bindRequiresDn' => true,
'accountDomainName' => 'foo.net',
'accountDomainNameShort' => 'FOO',
'accountCanonicalForm' => 4, // ACCT_FORM_PRINCIPAL
'baseDn' => 'OU=Sales,DC=foo,DC=net',
),
'server2' => array(
'host' => 'dc1.w.net',
'useSsl' => true,
'username' => 'user1@w.net',
'password' => 'pass1',
'accountDomainName' => 'w.net',
'accountDomainNameShort' => 'W',
'accountCanonicalForm' => 4, // ACCT_FORM_PRINCIPAL
'baseDn' => 'CN=Users,DC=w,DC=net',
),
);
$ldap->setOptions($options);
try {
721
Zend_Ldap
$ldap->bind($acctname, $password);
$acctname = $ldap->getCanonicalAccountName($acctname);
echo "SUCCESS: authenticated $acctname\n";
return;
} catch (Zend_Ldap_Exception $zle) {
echo ' ' . $zle->getMessage() . "\n";
if ($zle->getCode() === Zend_Ldap_Exception::LDAP_X_DOMAIN_MISMATCH) {
continue;
}
}
}
If the bind fails for any reason, the next set of server options is tried.
The getCanonicalAccountName() call gets the canonical account name that the application would presumably
use to associate data with such as preferences. The accountCanonicalForm = 4 in all server options ensures that the
canonical form is consistent regardless of which server was ultimately used.
The special LDAP_X_DOMAIN_MISMATCH exception occurs when an account name with a domain component was
supplied (e.g., abaker@foo.net or FOO\abaker and not just abaker) but the domain component did not match
either domain in the currently selected server options. This exception indicates that the server is not an authority for
the account. In this case, the bind will not be performed, thereby eliminating unnecessary communication with the
server. Note that the continue instruction has no effect in this example, but in practice for error handling and debugging
purposes, you will probably want to check for LDAP_X_DOMAIN_MISMATCH as well as LDAP_NO_SUCH_OBJECT
and LDAP_INVALID_CREDENTIALS.
The above code is very similar to code used within Zend_Auth_Adapter_Ldap. In fact, we recommend that you
simply use that authentication adapter for multi-domain + failover LDAP based authentication (or copy the code).
API overview
Configuration / options
The Zend_Ldap component accepts an array of options either supplied to the constructor or through the
setOptions() method. The permitted options are as follows:
722
Zend_Ldap
Name Description
useSsl Whether or not the LDAP client should use SSL encrypted
transport. The useSsl and useStartTls options are mutually
exclusive.
username The default credentials username. Some servers require
that this be in DN form. This must be given in DN form if
the LDAP server requires a DN to bind and binding should
be possible with simple usernames.
password The default credentials password (used only with
username above).
bindRequiresDn If TRUE, this instructs Zend_Ldap to retrieve the DN for
the account used to bind if the username is not already in
DN form. The default value is FALSE.
baseDn The default base DN used for searching (e.g., for
accounts). This option is required for most account related
operations and should indicate the DN under which
accounts are located.
accountCanonicalForm A small integer indicating the form to which account
names should be canonicalized. See the Account Name
Canonicalization section below.
accountDomainName The FQDN domain for which the target LDAP server is
an authority (e.g., example.com).
accountDomainNameShort The 'short' domain for which the target LDAP server is
an authority. This is usually used to specify the NetBIOS
domain name for Windows networks but may also be used
by non-AD servers.
accountFilterFormat The LDAP search filter used to search for accounts.
This string is a sprintf() [http://php.net/sprintf]
style expression that must contain one '%s' to
accommodate the username. The default value is
'(&(objectClass=user)(sAMAccountName=%s))' unless
bindRequiresDn is set to TRUE, in which case the default
is '(&(objectClass=posixAccount)(uid=%s))'. Users of
custom schemas may need to change this option.
allowEmptyPassword Some LDAP servers can be configured to accept an empty
string password as an anonymous bind. This behavior
is almost always undesirable. For this reason, empty
passwords are explicitly disallowed. Set this value to
TRUE to allow an empty string password to be submitted
during the bind.
optReferrals If set to TRUE, this option indicates to the LDAP client that
referrals should be followed. The default value is FALSE.
tryUsernameSplit If set to FALSE, this option indicates that the given
username should not be split at the first @ or \ character
to separate the username from the domain during the
binding-procedure. This allows the user to use usernames
that contain an @ or \ character that do not inherit
some domain-information, e.g. using email-addresses for
binding. The default value is TRUE.
723
Zend_Ldap
API Reference
Nota
Method names in italics are static methods.
Zend_Ldap
Zend_Ldap is the base interface into a LDAP server. It provides connection and binding methods as well as methods
to operate on the LDAP tree.
724
Zend_Ldap
Method Description
Zend_Ldap disconnect() Disconnects the Zend_Ldap instance from the LDAP
server.
Zend_Ldap connect(string $host, integer Connects the Zend_Ldap instance to the given LDAP
$port, boolean $useSsl, boolean server. All parameters are optional and will be taken
$useStartTls) from the LDAP connection and binding parameters
passed to the instance via the construtor or via
Zend_Ldap::setOptions() when set to NULL.
Zend_Ldap bind(string $username, string Authenticates $username with $password at the
$password) LDAP server. If both paramaters are omitted the binding
will be carried out with the credentials given in the
connection and binding parameters. If no credentials
are given in the connection and binding parameters
an anonymous bind will be performed. Note that
this requires anonymous binds to be allowed on the
LDAP server. An empty string '' can be passed as
$password together with a username if, and only
if, allowEmptyPassword is set to TRUE in the
connection and binding parameters.
Zend_Ldap_Collection search(string| Searches the LDAP tree with the given $filter and the
Zend_Ldap_Filter_Abstract $filter, given search parameters.
string|Zend_Ldap_Dn $basedn, integer
$scope, array $attributes, string string| The filter string
$sort, string $collectionClass) Zend_Ldap_Filter_Abstract to be used in
$filter the search, e.g.
(objectClass=posixAccount
725
Zend_Ldap
Method Description
by its DN. The
default value is
Zend_Ldap::SEARCH_SCOPE_S
726
Zend_Ldap
Method Description
Zend_Ldap_Collection_Iter
on instantiation.
integer count(string| Counts the elements returned by the given search
Zend_Ldap_Filter_Abstract $filter, parameters. See Zend_Ldap::search() for a
string|Zend_Ldap_Dn $basedn, integer detailed description of the method parameters.
$scope)
integer countChildren(string| Counts the direct descendants (children) of the entry
Zend_Ldap_Dn $dn) identified by the given $dn.
boolean exists(string|Zend_Ldap_Dn Checks whether the entry identified by the given $dn
$dn) exists.
array searchEntries(string| Performs a search operation and returns the result as
Zend_Ldap_Filter_Abstract $filter, an PHP array. This is essentially the same method as
string|Zend_Ldap_Dn $basedn, integer Zend_Ldap::search() except for the return type.
$scope, array $attributes, string See Zend_Ldap::search() for a detailed description
$sort) of the method parameters.
array getEntry(string|Zend_Ldap_Dn Retrieves the LDAP entry identified by $dn
$dn, array $attributes, boolean with the attributes specified in $attributes. If
$throwOnNotFound) $attributes is ommitted, all attributes (array())
are included in the result. $throwOnNotFound is
FALSE by default, so the method will return NULL if
the specified entry cannot be found. If set to TRUE, a
Zend_Ldap_Exception will be thrown instead.
void prepareLdapEntryArray(array & Prepare an array for the use in LDAP modification
$entry) operations. This method does not need to be called
by the end-user as it's implicitly called on every data
modification method.
Zend_Ldap add(string|Zend_Ldap_Dn $dn, Adds the entry identified by $dn with its
array $entry) attributes $entry to the LDAP tree. Throws a
Zend_Ldap_Exception if the entry could not be
added.
Zend_Ldap update(string|Zend_Ldap_Dn Updates the entry identified by $dn with its
$dn, array $entry) attributes $entry to the LDAP tree. Throws a
Zend_Ldap_Exception if the entry could not be
modified.
Zend_Ldap save(string|Zend_Ldap_Dn Saves the entry identified by $dn with its
$dn, array $entry) attributes $entry to the LDAP tree. Throws a
Zend_Ldap_Exception if the entry could not be
saved. This method decides by querying the LDAP tree if
the entry will be added or updated.
Zend_Ldap delete(string|Zend_Ldap_Dn Deletes the entry identified by $dn from the LDAP tree.
$dn, boolean $recursively) Throws a Zend_Ldap_Exception if the entry could
not be deleted. $recursively is FALSE by default. If
set to TRUE the deletion will be carried out recursively and
will effectively delete a complete subtree. Deletion will
fail if $recursively is FALSE and the entry $dn is
not a leaf entry.
Zend_Ldap moveToSubtree(string| Moves the entry identified by $from to a location below
Zend_Ldap_Dn $from, string| $to keeping its RDN unchanged. $recursively
727
Zend_Ldap
Method Description
Zend_Ldap_Dn $to, boolean $recursively, specifies if the operation will be carried out recursively
boolean $alwaysEmulate) (FALSE by default) so that the entry $from and all
its descendants will be moved. Moving will fail if
$recursively is FALSE and the entry $from is not
a leaf entry. $alwaysEmulate controls whether the
ext/ldap function ldap_rename() should be used if
available. This can only work for leaf entries and for
servers and for ext/ldap supporting this function. Set to
TRUE to always use an emulated rename operation.
Nota
All move-operations are carried out by copying
and then deleting the corresponding entries in the
LDAP tree. These operations are not atomic so
that failures during the operation will result in an
inconsistent state on the LDAP server. The same
is true for all recursive operations. They also are
by no means atomic. Please keep this in mind.
Zend_Ldap move(string|Zend_Ldap_Dn This is an alias for Zend_Ldap::rename().
$from, string|Zend_Ldap_Dn $to, boolean
$recursively, boolean $alwaysEmulate)
Zend_Ldap rename(string|Zend_Ldap_Dn Renames the entry identified by $from to $to.
$from, string|Zend_Ldap_Dn $to, boolean $recursively specifies if the operation will be carried
$recursively, boolean $alwaysEmulate) out recursively (FALSE by default) so that the entry
$from and all its descendants will be moved. Moving will
fail if $recursively is FALSE and the entry $from
is not a leaf entry. $alwaysEmulate controls whether
the ext/ldap function ldap_rename() should be used
if available. This can only work for leaf entries and for
servers and for ext/ldap supporting this function. Set to
TRUE to always use an emulated rename operation.
Zend_Ldap copyToSubtree(string| Copies the entry identified by $from to a location below
Zend_Ldap_Dn $from, string| $to keeping its RDN unchanged. $recursively
Zend_Ldap_Dn $to, boolean $recursively) specifies if the operation will be carried out recursively
(FALSE by default) so that the entry $from and all
its descendants will be copied. Copying will fail if
$recursively is FALSE and the entry $from is not
a leaf entry.
Zend_Ldap copy(string|Zend_Ldap_Dn Copies the entry identified by $from to $to.
$from, string|Zend_Ldap_Dn $to, boolean $recursively specifies if the operation will be carried
$recursively) out recursively (FALSE by default) so that the entry
$from and all its descendants will be copied. Copying
will fail if $recursively is FALSE and the entry
$from is not a leaf entry.
Zend_Ldap_Node getNode(string| Returns the entry $dn wrapped in a Zend_Ldap_Node.
Zend_Ldap_Dn $dn)
Zend_Ldap_Node getBaseNode() Returns the entry for the base DN $baseDn wrapped in
a Zend_Ldap_Node.
728
Zend_Ldap
Method Description
Zend_Ldap_Node_RootDse getRootDse() Returns the RootDSE for the current server.
Zend_Ldap_Node_Schema getSchema() Returns the LDAP schema for the current server.
Zend_Ldap_Collection
Zend_Ldap_Collection implements Iterator to allow for item traversal using foreach() and
Countable to be able to respond to count(). With its protected _createEntry() method it provides a simple
extension point for developers needing custom result objects.
Method Description
Constructor. The constrcutor must be provided by a
__construct(Zend_Ldap_Collection_Iterator_Interface
$iterator) Zend_Ldap_Collection_Iterator_Interface
which does the real result iteration.
Zend_Ldap_Collection_Iterator_Default
is the default implementation for iterating ext/ldap results.
boolean close() Closes the internal iterator. This is also called in the
destructor.
array toArray() Returns all entries as an array.
array getFirst() Returns the first entry in the collection or NULL if the
collection is empty.
Zend_Ldap_Attribute
Zend_Ldap_Attribute is a helper class providing only static methods to manipulate arrays suitable to the
structure used in Zend_Ldap data modification methods and to the data format required by the LDAP server. PHP
data types are converted the following way:
object and array The value will be converted to a string by using serialize().
others All other data types (namely non-stream resources) will be ommitted.
others All other strings won't be automatically converted and are passed as they are.
729
Zend_Ldap
Method Description
void setAttribute(array &$data, string Sets the attribute $attribName in $data to the value
$attribName, mixed $value, boolean $value. If $append is TRUE (FALSE by default)
$append) $value will be appended to the attribute. $value can
be a scalar value or an array of scalar values. Conversion
will take place.
array|mixed getAttribute(array $data, Returns the attribute $attribName from $data. If
string $attribName, integer|null $index is NULL (default) an array will be returned
$index) containing all the values for the given attribute. An empty
array will be returned if the attribute does not exist
in the given array. If an integer index is specified the
corresponding value at the given index will be returned.
If the index is out of bounds, NULL will be returned.
Conversion will take place.
boolean attributeHasValue(array & Checks if the attribute $attribName in $data has
$data, string $attribName, mixed|array the value(s) given in $value. The method returns TRUE
$value) only if all values in $value are present in the attribute.
Comparison is done strictly (respecting the data type).
void Removes all duplicates from the attribute $attribName
removeDuplicatesFromAttribute(array & in $data.
$data, string $attribName)
void removeFromAttribute(array &$data, Removes the value(s) given in $value from the attribute
string $attribName, mixed|array $value) $attribName in $data.
string|null convertToLdapValue(mixed Converts a PHP data type into its LDAP representation.
$value) See introduction for details.
mixed convertFromLdapValue(string Converts an LDAP value into its PHP data type. See
$value) introduction for details.
string|null Converts a timestamp into its LDAP date/time
convertToLdapDateTimeValue(integer representation. If $utc is TRUE (FALSE by default) the
$value, boolean $utc) resulting LDAP date/time string will be in UTC, otherwise
a local date/time string will be returned.
integer|null Converts LDAP date/time representation into a
convertFromLdapDateTimeValue(string timestamp. The method returns NULL if $value can not
$value) be converted back into a PHP timestamp.
void setPassword(array &$data, string Sets a LDAP password for the attribute
$password, string $hashType, string $attribName in $data. $attribName defaults to
$attribName) 'userPassword' which is the standard password
attribute. The password hash can be specified
with $hashType. The default value here is
Zend_Ldap_Attribute::PASSWORD_HASH_MD5
with
Zend_Ldap_Attribute::PASSWORD_HASH_SHA
as the other possibilty.
string createPassword(string Creates a LDAP password. The password hash can be
$password, string $hashType) specified with $hashType. The default value here is
Zend_Ldap_Attribute::PASSWORD_HASH_MD5
with
730
Zend_Ldap
Method Description
Zend_Ldap_Attribute::PASSWORD_HASH_SHA
as the other possibilty.
void setDateTimeAttribute(array & Sets the attribute $attribName in $data to the
$data, string $attribName, integer| date/time value $value. If $append is TRUE
array $value, boolean $utc, boolean (FALSE by default) $value will be appended to
$append) the attribute. $value can be an integer value or an
array of integers. Date-time-conversion according to
Zend_Ldap_Attribute::convertToLdapDateTimeValue()
will take place.
array|integer Returns the date/time attribute $attribName
getDateTimeAttribute(array $data, from $data. If $index is NULL
string $attribName, integer|null (default) an array will be returned
$index) containing all the date/time values
for the given attribute. An empty
array will be returned if the
attribute does not exist in the
given array. If an integer index
is specified the corresponding date/
time value at the given index will
be returned. If the index is out
of bounds, NULL will be returned.
Date-time-conversion according to
Zend_Ldap_Attribute::convertFromLdapDateTimeValue()
will take place.
Zend_Ldap_Dn
Zend_Ldap_Dn provides an object-oriented interface to manipulating LDAP distinguished names (DN). The
parameter $caseFold that is used in several methods determines the way DN attributes are handled regarding their
case. Allowed values for this paraneter are:
The class implements ArrayAccess to allow indexer-access to the different parts of the DN.
The ArrayAccess-methods proxy to Zend_Ldap_Dn::get($offset, 1, null) for
offsetGet(integer $offset), to Zend_Ldap_Dn::set($offset, $value) for offsetSet() and
to Zend_Ldap_Dn::remove($offset, 1) for offsetUnset(). offsetExists() simply checks if the
index is within the bounds.
731
Zend_Ldap
Method Description
Zend_Ldap_Dn fromString(string $dn, Creates a Zend_Ldap_Dn instance from a string.
string|null $caseFold)
Zend_Ldap_Dn fromArray(array $dn, Creates a Zend_Ldap_Dn instance from an array. The
string|null $caseFold) array must conform to the array structure detailed under
Zend_Ldap_Dn::implodeDn().
array getRdn(string|null $caseFold) Gets the RDN of the current DN. The return value is an
array with the RDN attribute names its keys and the RDN
attribute values.
string getRdnString(string|null Gets the RDN of the current DN. The return value is a
$caseFold) string.
Zend_Ldap_Dn getParentDn(integer Gets the DN of the current DN's ancestor $levelUp
$levelUp) levels up the tree. $levelUp defaults to 1.
array get(integer $index, integer Returns a slice of the current DN determined by $index
$length, string|null $caseFold) and $length. $index starts with 0 on the DN part from
the left.
Zend_Ldap_Dn set(integer $index, array Replaces a DN part in the current DN. This operation
$value) manipulates the current instance.
Zend_Ldap_Dn remove(integer $index, Removes a DN part from the current DN. This operation
integer $length) manipulates the current instance. $length defaults to 1
Zend_Ldap_Dn append(array $value) Appends a DN part to the current DN. This operation
manipulates the current instance.
Zend_Ldap_Dn prepend(array $value) Prepends a DN part to the current DN. This operation
manipulates the current instance.
Zend_Ldap_Dn insert(integer $index, Inserts a DN part after the index $index to the current
array $value) DN. This operation manipulates the current instance.
void setCaseFold(string|null Sets the case-folding option to the current DN instance.
$caseFold) If $caseFold is null the default case-folding setting
(Zend_Ldap_Dn::ATTR_CASEFOLD_NONE by
default or set via
Zend_Ldap_Dn::setDefaultCaseFold() will
be set for the current instance.
string toString(string|null $caseFold) Returns DN as a string.
array toArray(string|null $caseFold) Returns DN as an array.
string __toString() Returns DN as a string - proxies to
Zend_Ldap_Dn::toString(null).
void setDefaultCaseFold(string Sets the default case-folding option used by all instances
$caseFold) on creation by default. Already existing instances are not
affected by this setting.
array escapeValue(string|array Escapes a DN value according to RFC 2253.
$values)
array unescapeValue(string|array Undoes the conversion done by
$values) Zend_Ldap_Dn::escapeValue().
array explodeDn(string $dn, array Explodes the DN $dn into an array containing all parts
&$keys, array &$vals, string|null of the given DN. $keys optinally receive DN keys (e.g.
$caseFold)
732
Zend_Ldap
Method Description
CN, OU, DC, ...). $vals optionally receive DN values.
The resulting array will be of type
array(
array("cn" => "name1", "uid" => "user"),
array("cn" => "name2"),
array("dc" => "example"),
array("dc" => "org")
)
for a DN of
cn=name1+uid=user,cn=name2,dc=example,dc=org.
boolean checkDn(string $dn, array Checks if a given DN $dn is malformed. If $keys or
&$keys, array &$vals, string|null $keys and $vals are given, these arrays will be filled
$caseFold) with the appropriate DN keys and values.
string implodeRdn(array $part, string| Returns a DN part in the form $attribute=$value
null $caseFold)
string implodeDn(array $dnArray, Implodes an array in the form delivered by
string|null $caseFold, string Zend_Ldap_Dn::explodeDn() to a DN string.
$separator) $separator defaults to ',' but some LDAP servers
also understand ';'. $dnArray must of type
array(
array("cn" => "name1", "uid" => "user"),
array("cn" => "name2"),
array("dc" => "example"),
array("dc" => "org")
)
boolean isChildOf(string|Zend_Ldap_Dn Checks if given $childDn is beneath $parentDn
$childDn, string|Zend_Ldap_Dn subtree.
$parentDn)
Zend_Ldap_Filter
Tabla 29.7. Zend_Ldap_Filter API
Method Description
Zend_Ldap_Filter equals(string $attr, Creates an 'equals' filter: (attr=value).
string $value)
Zend_Ldap_Filter begins(string $attr, Creates an 'begins with' filter: (attr=value*).
string $value)
Zend_Ldap_Filter ends(string $attr, Creates an 'ends with' filter: (attr=*value).
string $value)
Zend_Ldap_Filter contains(string Creates an 'contains' filter: (attr=*value*).
$attr, string $value)
Zend_Ldap_Filter greater(string $attr, Creates an 'greater' filter: (attr>value).
string $value)
733
Zend_Ldap
Method Description
Zend_Ldap_Filter greaterOrEqual(string Creates an 'greater or equal' filter: (attr>=value).
$attr, string $value)
Zend_Ldap_Filter less(string $attr, Creates an 'less' filter: (attr<value).
string $value)
Zend_Ldap_Filter lessOrEqual(string Creates an 'less or equal' filter: (attr<=value).
$attr, string $value)
Zend_Ldap_Filter approx(string $attr, Creates an 'approx' filter: (attr~=value).
string $value)
Zend_Ldap_Filter any(string $attr) Creates an 'any' filter: (attr=*).
Zend_Ldap_Filter string(string Creates a simple custom string filter. The user is
$filter) responsible for all value-escaping as the filter is used as is.
Zend_Ldap_Filter mask(string $mask, Creates a filter from a string mask. All $value
string $value,...) parameters will be escaped and substituted into $mask by
using sprintf() [http://php.net/sprintf]
Zend_Ldap_Filter Creates an 'and' filter from all arguments given.
andFilter(Zend_Ldap_Filter_Abstract
$filter,...)
Zend_Ldap_Filter Creates an 'or' filter from all arguments given.
orFilter(Zend_Ldap_Filter_Abstract
$filter,...)
__construct(string $attr, string Constructor. Creates an arbitrary filter according to
$value, string $filtertype, string|null the parameters supplied. The resulting filter will be
$prepend, string|null $append) a concatenation $attr . $filtertype .
$prepend . $value . $append. Normally this
constructor is not needed as all filters can be created by
using the apprpriate factory methods.
string toString() Returns a string representation of the filter.
string __toString() Returns a string representation of the filter. Proxies to
Zend_Ldap_Filter::toString().
Zend_Ldap_Filter_Abstract negate() Negates the current filter.
Zend_Ldap_Filter_Abstract Creates an 'and' filter from the current filter and all filters
addAnd(Zend_Ldap_Filter_Abstract passed in as the arguments.
$filter,...)
Zend_Ldap_Filter_Abstract Creates an 'or' filter from the current filter and all filters
addOr(Zend_Ldap_Filter_Abstract passed in as the arguments.
$filter,...)
string|array escapeValue(string|array Escapes the given $values according to RFC 2254 so
$values) that they can be safely used in LDAP filters. If a single
string is given, a string is returned - otherwise an array is
returned. Any control characters with an ASCII code < 32
as well as the characters with special meaning in LDAP
filters "*", "(", ")", and "\" (the backslash) are converted
into the representation of a backslash followed by two hex
digits representing the hexadecimal value of the character.
string|array unescapeValue(string| Undoes the conversion done by
array $values) Zend_Ldap_Filter::escapeValue(). Converts
734
Zend_Ldap
Method Description
any sequences of a backslash followed by two hex digits
into the corresponding character.
Zend_Ldap_Node
Zend_Ldap_Node includes the magic propery accessors __set(), __get(),
__unset() and __isset() to access the attributes by their name.
They proxy to Zend_Ldap_Node::setAttribute(), Zend_Ldap_Node::getAttribute(),
Zend_Ldap_Node::deleteAttribute() and Zend_Ldap_Node::existsAttribute() respectively.
Furthermore the class implements ArrayAccess for array-style-access to the attributes. Zend_Ldap_Node also
implements Iterator and RecursiveIterato to allow for recursive tree-traversal.
735
Zend_Ldap
Method Description
Zend_Ldap_Node update(Zend_Ldap $ldap) Sends all pending changes to the LDAP server. If $ldap
is omitted the current LDAP connection is used. If the
current node is detached from a LDAP connection a
Zend_Ldap_Exception will be thrown. If $ldap is
provided the current node will be attached to the given
LDAP connection.
Zend_Ldap_Dn getCurrentDn() Gets the current DN of the current node as a
Zend_Ldap_Dn. This does not reflect possible rename-
operations.
Zend_Ldap_Dn getDn() Gets the original DN of the current node as a
Zend_Ldap_Dn. This reflects possible rename-operations.
string getDnString(string $caseFold) Gets the original DN of the current node as a string. This
reflects possible rename-operations.
array getDnArray(string $caseFold) Gets the original DN of the current node as an array. This
reflects possible rename-operations.
string getRdnString(string $caseFold) Gets the RDN of the current node as a string. This reflects
possible rename-operations.
array getRdnArray(string $caseFold) Gets the RDN of the current node as an array. This reflects
possible rename-operations.
Zend_Ldap_Node setDn(Zend_Ldap_Dn| Sets the new DN for this node effectively moving the node
string|array $newDn) once Zend_Ldap_Node::update() is called.
Zend_Ldap_Node move(Zend_Ldap_Dn| This is an alias for Zend_Ldap_Node::setDn().
string|array $newDn)
Zend_Ldap_Node rename(Zend_Ldap_Dn| This is an alias for Zend_Ldap_Node::setDn().
string|array $newDn)
array getObjectClass() Returns the objectClass of the node.
Zend_Ldap_Node setObjectClass(array| Sets the objectClass attribute.
string $value)
Zend_Ldap_Node Appends to the objectClass attribute.
appendObjectClass(array|string $value)
string toLdif(array $options) Returns a LDIF representation of the current
node. $options will be passed to the
Zend_Ldap_Ldif_Encoder.
array getChangedData() Gets changed node data. The array
contains all changed attributes. This format
can be used in Zend_Ldap::add() and
Zend_Ldap::update().
array getChanges() Returns all changes made.
string toString() Returns the DN of the current node - proxies to
Zend_Ldap_Dn::getDnString().
string __toString() Casts to string representation - proxies to
Zend_Ldap_Dn::toString().
array toArray(boolean Returns an array representation of the
$includeSystemAttributes) current node. If $includeSystemAttributes
is FALSE (defaults to TRUE) the system
736
Zend_Ldap
Method Description
specific attributes are stripped from the array.
Unlike Zend_Ldap_Node::getAttributes() the
resulting array contains the DN with key 'dn'.
string toJson(boolean Returns a JSON representation of the current node using
$includeSystemAttributes) Zend_Ldap_Node::toArray().
array getData(boolean Returns the node's attributes. The array contains all
$includeSystemAttributes) attributes in its internal format (no conversion).
boolean existsAttribute(string $name, Checks whether a given attribute exists. If
boolean $emptyExists) $emptyExists is FALSE empty attributes (containing
only array()) are treated as non-existent returning FALSE.
If $emptyExists is true empty attributes are treated as
existent returning TRUE. In this case teh method returns
FALSE only if the attribute name is missing in the key-
collection.
boolean attributeHasValue(string Checks if the given value(s) exist in the attribute. The
$name, mixed|array $value) method returns TRUE only if all values in $value
are present in the attribute. Comparison is done strictly
(respecting the data type).
integer count() Returns the number of attributes in the node. Implements
Countable.
mixed getAttribute(string $name, Gets a LDAP attribute. Data conversion is applied using
integer|null $index) Zend_Ldap_Attribute::getAttribute().
array getAttributes(boolean Gets all attributes of node. If
$includeSystemAttributes) $includeSystemAttributes is FALSE (defaults
to TRUE) the system specific attributes are stripped from
the array.
Zend_Ldap_Node setAttribute(string Sets a LDAP attribute. Data conversion is applied using
$name, mixed $value) Zend_Ldap_Attribute::setAttribute().
Zend_Ldap_Node Appends to a LDAP attribute. Data conversion is applied
appendToAttribute(string $name, mixed using
$value) Zend_Ldap_Attribute::setAttribute().
array|integer Gets a LDAP date/time attribute.
getDateTimeAttribute(string $name, Data conversion is applied using
integer|null $index) Zend_Ldap_Attribute::getDateTimeAttribute().
Zend_Ldap_Node Sets a LDAP date/time attribute.
setDateTimeAttribute(string $name, Data conversion is applied using
integer|array $value, boolean $utc) Zend_Ldap_Attribute::setDateTimeAttribute().
Zend_Ldap_Node Appends to a LDAP date/time attribute.
appendToDateTimeAttribute(string Data conversion is applied using
$name, integer|array $value, boolean Zend_Ldap_Attribute::setDateTimeAttribute().
$utc)
Zend_Ldap_Node Sets a LDAP password on $attribName
setPasswordAttribute(string $password, (defaults to 'userPassword') to $password
string $hashType, string $attribName) with the hash type $hashType (defaults to
Zend_Ldap_Attribute::PASSWORD_HASH_MD5).
Zend_Ldap_Node deleteAttribute(string Deletes a LDAP attribute.
$name)
737
Zend_Ldap
Method Description
void Removes duplicate values from a LDAP attribute.
removeDuplicatesFromAttribute(string
$name)
void removeFromAttribute(string Removes the given values from a LDAP attribute.
$attribName, mixed|array $value)
boolean exists(Zend_Ldap $ldap) Checks if the current node exists on the given LDAP
server (current server is used if NULL is passed).
Zend_Ldap_Node reload(Zend_Ldap $ldap) Reloads the current node's attributes from the given LDAP
server (current server is used if NULL is passed).
Zend_Ldap_Node_Collection Searches the nodes's subtree with the given
searchSubtree(string| $filter and the given search parameters. See
Zend_Ldap_Filter_Abstract $filter, Zend_Ldap::search() for details on the parameters
integer $scope, string $sort) $scope and $sort.
integer countSubtree(string| Count the nodes's subtree items matching the
Zend_Ldap_Filter_Abstract $filter, given $filter and the given search scope. See
integer $scope) Zend_Ldap::search() for details on the $scope
parameter.
integer countChildren() Count the nodes's children.
Zend_Ldap_Node_Collection Searches the nodes's children matching the given
searchChildren(string| $filter. See Zend_Ldap::search() for details on
Zend_Ldap_Filter_Abstract $filter, the $sort parameter.
string $sort)
boolean hasChildren() Returns whether the current node has children.
Zend_Ldap_Node_ChildrenIterator Returns all children of the current node.
getChildren()
Zend_Ldap_Node getParent(Zend_Ldap Returns the parent of the current node using the LDAP
$ldap) connection $ldap (uses the current LDAP connection if
omitted).
Zend_Ldap_Node_RootDse
The following methods are available on all vendor-specific subclasses.
Zend_Ldap_Node_RootDse includes the magic propery accessors __get() and __isset() to access
the attributes by their name. They proxy to Zend_Ldap_Node_RootDse::getAttribute() and
Zend_Ldap_Node_RootDse::existsAttribute() respectively. __set() and __unset() are also
implemented but they throw a BadMethodCallException as modifications are not allowed on RootDSE nodes.
Furthermore the class implements ArrayAccess for array-style-access to the attributes. offsetSet() and
offsetUnset() also throw a BadMethodCallException due ro obvious reasons.
738
Zend_Ldap
Method Description
array getRdnArray(string $caseFold) Gets the RDN of the current node as an array.
array getObjectClass() Returns the objectClass of the node.
string toString() Returns the DN of the current node - proxies to
Zend_Ldap_Dn::getDnString().
string __toString() Casts to string representation - proxies to
Zend_Ldap_Dn::toString().
array toArray(boolean Returns an array representation of the current
$includeSystemAttributes) node. If $includeSystemAttributes is
FALSE (defaults to TRUE) the system specific
attributes are stripped from the array. Unlike
Zend_Ldap_Node_RootDse::getAttributes()
the resulting array contains the DN with key 'dn'.
string toJson(boolean Returns a JSON representation of the current node using
$includeSystemAttributes) Zend_Ldap_Node_RootDse::toArray().
array getData(boolean Returns the node's attributes. The array contains all
$includeSystemAttributes) attributes in its internal format (no conversion).
boolean existsAttribute(string $name, Checks whether a given attribute exists. If
boolean $emptyExists) $emptyExists is FALSE empty attributes (containing
only array()) are treated as non-existent returning FALSE.
If $emptyExists is true empty attributes are treated as
existent returning TRUE. In this case teh method returns
FALSE only if the attribute name is missing in the key-
collection.
boolean attributeHasValue(string Checks if the given value(s) exist in the attribute. The
$name, mixed|array $value) method returns TRUE only if all values in $value
are present in the attribute. Comparison is done strictly
(respecting the data type).
integer count() Returns the number of attributes in the node. Implements
Countable.
mixed getAttribute(string $name, Gets a LDAP attribute. Data conversion is applied using
integer|null $index) Zend_Ldap_Attribute::getAttribute().
array getAttributes(boolean Gets all attributes of node. If
$includeSystemAttributes) $includeSystemAttributes is FALSE (defaults
to TRUE) the system specific attributes are stripped from
the array.
array|integer Gets a LDAP date/time attribute.
getDateTimeAttribute(string $name, Data conversion is applied using
integer|null $index) Zend_Ldap_Attribute::getDateTimeAttribute().
Zend_Ldap_Node_RootDse Reloads the current node's attributes from the given LDAP
reload(Zend_Ldap $ldap) server.
Zend_Ldap_Node_RootDse Factory method to create the RootDSE.
create(Zend_Ldap $ldap)
array getNamingContexts() Gets the namingContexts.
string|null getSubschemaSubentry() Gets the subschemaSubentry.
739
Zend_Ldap
Method Description
boolean supportsVersion(string|int| Determines if the LDAP version is supported.
array $versions)
boolean supportsSaslMechanism(string| Determines if the sasl mechanism is supported.
array $mechlist)
integer getServerType() Gets the server type. Returns
for unknown
Zend_Ldap_Node_RootDse::SERVER_TYPE_GENERIC
LDAP servers
for OpenLDAP
Zend_Ldap_Node_RootDse::SERVER_TYPE_OPENLDAP
servers
for Microsoft
Zend_Ldap_Node_RootDse::SERVER_TYPE_ACTIVEDIRECTORY
ActiveDirectory
servers
For Novell
Zend_Ldap_Node_RootDse::SERVER_TYPE_EDIRECTORY
eDirectory servers
Zend_Ldap_Dn getSchemaDn() Returns the schema DN.
OpenLDAP
Nota
Refer to LDAP Operational Attributes and Objects [http://www.zytrax.com/books/ldap/ch3/#operational]for
information on the attributes of OpenLDAP RootDSE.
Method Description
integer getServerType() Gets the server type. Returns
Zend_Ldap_Node_RootDse::SERVER_TYPE_OPENLDAP
string|null getConfigContext() Gets the configContext.
string|null getMonitorContext() Gets the monitorContext.
boolean supportsControl(string|array Determines if the control is supported.
$oids)
boolean supportsExtension(string|array Determines if the extension is supported.
$oids)
boolean supportsFeature(string|array Determines if the feature is supported.
$oids)
ActiveDirectory
740
Zend_Ldap
Nota
Refer to RootDSE [http://msdn.microsoft.com/en-us/library/ms684291(VS.85).aspx]for information on the
attributes of Microsoft ActiveDirectory RootDSE.
Method Description
integer getServerType() Gets the server type. Returns
Zend_Ldap_Node_RootDse::SERVER_TYPE_ACTIVEDIRECTORY
string|null Gets the configurationNamingContext.
getConfigurationNamingContext()
string|null getCurrentTime() Gets the currentTime.
string|null getDefaultNamingContext() Gets the defaultNamingContext.
string|null getDnsHostName() Gets the dnsHostName.
string|null Gets the domainControllerFunctionality.
getDomainControllerFunctionality()
string|null getDomainFunctionality() Gets the domainFunctionality.
string|null getDsServiceName() Gets the dsServiceName.
string|null getForestFunctionality() Gets the forestFunctionality.
string|null getHighestCommittedUSN() Gets the highestCommittedUSN.
string|null getIsGlobalCatalogReady() Gets the isGlobalCatalogReady.
string|null getIsSynchronized() Gets the isSynchronized.
string|null getLdapServiceName() Gets the ldapServiceName.
string|null Gets the rootDomainNamingContext.
getRootDomainNamingContext()
string|null getSchemaNamingContext() Gets the schemaNamingContext.
string|null getServerName() Gets the serverName.
boolean supportsCapability(string| Determines if the capability is supported.
array $oids)
boolean supportsControl(string|array Determines if the control is supported.
$oids)
boolean supportsPolicy(string|array Determines if the version is supported.
$policies)
eDirectory
Nota
Refer to Getting Information about the LDAP Server [http://www.novell.com/documentation/edir88/
edir88/index.html?page=/documentation/edir88/edir88/data/ah59jqq.html]for information on the attributes
of Novell eDirectory RootDSE.
741
Zend_Ldap
Zend_Ldap_Node_Schema
The following methods are available on all vendor-specific subclasses.
Zend_Ldap_Node_Schema includes the magic propery accessors __get() and __isset() to access
the attributes by their name. They proxy to Zend_Ldap_Node_Schema::getAttribute() and
Zend_Ldap_Node_Schema::existsAttribute() respectively. __set() and __unset() are also
implemented but they throw a BadMethodCallException as modifications are not allowed on RootDSE nodes.
Furthermore the class implements ArrayAccess for array-style-access to the attributes. offsetSet() and
offsetUnset() also throw a BadMethodCallException due ro obvious reasons.
742
Zend_Ldap
Method Description
false (defaults to true) the system specific
attributes are stripped from the array. Unlike
Zend_Ldap_Node_Schema::getAttributes()
the resulting array contains the DN with key 'dn'.
string toJson(boolean Returns a JSON representation of the current node using
$includeSystemAttributes) Zend_Ldap_Node_Schema::toArray().
array getData(boolean Returns the node's attributes. The array contains all
$includeSystemAttributes) attributes in its internal format (no conversion).
boolean existsAttribute(string $name, Checks whether a given attribute exists. If
boolean $emptyExists) $emptyExists is false empty attributes (containing
only array()) are treated as non-existent returning false.
If $emptyExists is true empty attributes are treated as
existent returning true. In this case teh method returns
false only if the attribute name is missing in the key-
collection.
boolean attributeHasValue(string Checks if the given value(s) exist in the attribute. The
$name, mixed|array $value) method returns true only if all values in $value
are present in the attribute. Comparison is done strictly
(respecting the data type).
integer count() Returns the number of attributes in the node. Implements
Countable.
mixed getAttribute(string $name, Gets a LDAP attribute. Data conversion is applied using
integer|null $index) Zend_Ldap_Attribute::getAttribute().
array getAttributes(boolean Gets all attributes of node. If
$includeSystemAttributes) $includeSystemAttributes is false (defaults
to true) the system specific attributes are stripped from
the array.
array|integer Gets a LDAP date/time attribute.
getDateTimeAttribute(string $name, Data conversion is applied using
integer|null $index) Zend_Ldap_Attribute::getDateTimeAttribute().
Zend_Ldap_Node_Schema reload(Zend_Ldap Reloads the current node's attributes from the given LDAP
$ldap) server.
Zend_Ldap_Node_Schema create(Zend_Ldap Factory method to create the Schema node.
$ldap)
array getAttributeTypes() Gets the attribute types as an array of .
array getObjectClasses() Gets the object classes as an array of
Zend_Ldap_Node_Schema_ObjectClass_Interface.
743
Zend_Ldap
Method Description
string getDescription() Gets the attribute description
for structural
Zend_Ldap_Node_Schema::OBJECTCLASS_TYPE_STRUCTURAL
classes
for auxiliary
Zend_Ldap_Node_Schema::OBJECTCLASS_TYPE_AUXILIARY
classes
array getParentClasses() Returns the parent objectClasses of this class. This
includes structural, abstract and auxiliary objectClasses.
Classes representing attribute types and object classes extend Zend_Ldap_Node_Schema_Item which provides
some core methods to access arbitrary attributes on the underlying LDAP node. Zend_Ldap_Node_Schema_Item
includes the magic propery accessors __get() and __isset() to access the attributes by their name. Furthermore
the class implements ArrayAccess for array-style-access to the attributes. offsetSet() and offsetUnset()
throw a BadMethodCallException as modifications are not allowed on schema information nodes.
OpenLDAP
Additionally the common methods above apply to instances of Zend_Ldap_Node_Schema_OpenLDAP.
744
Zend_Ldap
Method Description
array getMatchingRuleUse() Gets the matching rule use.
ActiveDirectory
Zend_Ldif_Encoder
Tabla 29.22. Zend_Ldif_Encoder API
Method Description
array decode(string $string) Decodes the string $string into an array of LDIF items.
string encode(scalar|array| Encode $value into a LDIF representation. $options
Zend_Ldap_Node $value, array $options) is an array that may contain the following keys:
745
Zend_Ldap
Method Description
'wrap' The line-length. 78 by default to conform
to the LDIF specification.
Usage Scenarios
Authentication scenarios
OpenLDAP
ActiveDirectory
746
Zend_Ldap
'cn=Hugo Müller,ou=People,dc=my,dc=local');
747
Zend_Ldap
Extended operations
Copy and move entries in the LDAP
Ejemplo 29.8. Copy a LDAP entry recursively with all its descendants
Ejemplo 29.9. Move a LDAP entry recursively with all its descendants to a different subtree
Tools
Creation and modification of DN strings
748
Zend_Ldap
// (&(name=value)(name=value*)(name=*value))
$f5 = Zend_Ldap_Filter::andFilter($f2, $f3, $f4);
// (|(name=value)(name=value*)(name=*value))
$f6 = Zend_Ldap_Filter::orFilter($f2, $f3, $f4);
Extended operations
Copy and move nodes in the LDAP
Tree traversal
Ejemplo 29.12. Traverse LDAP tree recursively
749
Zend_Ldap
var_dump($n);
}
• OpenLDAP [http://www.zytrax.com/books/ldap/ch3/#operational]
Schema Browsing
Ejemplo 29.14. Getting hands on the server schema
OpenLDAP
ActiveDirectory
Schema browsing on ActiveDirectory servers
Due to restrictions on Microsoft ActiveDirectory servers regarding the number of entries returned by generic
search routines and due to the structure of the ActiveDirectory schema repository, schema browsing is
currently not available for Microsoft ActiveDirectory servers.
750
Zend_Ldap
$data = array(
'dn' => 'uid=rogasawara,ou=###,o=Airius',
'objectclass' => array('top',
'person',
'organizationalPerson',
'inetOrgPerson'),
'uid' => array('rogasawara'),
'mail' => array('rogasawara@airius.co.jp'),
'givenname;lang-ja' => array('####'),
'sn;lang-ja' => array('###'),
'cn;lang-ja' => array('### ####'),
'title;lang-ja' => array('### ##'),
'preferredlanguage' => array('ja'),
'givenname' => array('####'),
'sn' => array('###'),
'cn' => array('### ####'),
'title' => array('### ##'),
'givenname;lang-ja;phonetic' => array('####'),
'sn;lang-ja;phonetic' => array('#####'),
'cn;lang-ja;phonetic' => array('##### ####'),
'title;lang-ja;phonetic' => array('###### ####'),
'givenname;lang-en' => array('Rodney'),
'sn;lang-en' => array('Ogasawara'),
'cn;lang-en' => array('Rodney Ogasawara'),
'title;lang-en' => array('Sales, Director'),
);
$ldif = Zend_Ldap_Ldif_Encoder::encode($data, array('sort' => false,
'version' => null));
/*
$ldif contains:
dn:: dWlkPXJvZ2FzYXdhcmEsb3U95Za25qWt6YOoLG89QWlyaXVz
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
uid: rogasawara
mail: rogasawara@airius.co.jp
givenname;lang-ja:: 44Ot44OJ44OL44O8
sn;lang-ja:: 5bCP56yg5Y6f
cn;lang-ja:: 5bCP56yg5Y6fIOODreODieODi+ODvA==
title;lang-ja:: 5Za25qWt6YOoIOmDqOmVtw==
preferredlanguage: ja
givenname:: 44Ot44OJ44OL44O8
sn:: 5bCP56yg5Y6f
cn:: 5bCP56yg5Y6fIOODreODieODi+ODvA==
title:: 5Za25qWt6YOoIOmDqOmVtw==
givenname;lang-ja;phonetic:: 44KN44Gp44Gr44O8
sn;lang-ja;phonetic:: 44GK44GM44GV44KP44KJ
cn;lang-ja;phonetic:: 44GK44GM44GV44KP44KJIOOCjeOBqeOBq+ODvA==
title;lang-ja;phonetic:: 44GI44GE44GO44KH44GG44G2IOOBtuOBoeOCh+OBhg==
givenname;lang-en: Rodney
sn;lang-en: Ogasawara
cn;lang-en: Rodney Ogasawara
title;lang-en: Sales, Director
751
Zend_Ldap
*/
752
Zend_Ldap
753
754
Capítulo 30. Zend_Loader
Cargando archivos y clases dinámicamente
La clase Zend_Loader incluye métodos para ayudar a cargar archivos dinámicamente.
Cargando Archivos
El método estático Zend_Loader::loadFile() carga un archivo PHP. El archivo cargado puede contener
cualquier código PHP. El método se comporta como un envoltorio para la función PHP include() [http://php.net/
include]. Este método devuelve un booleano false en caso de fallo, por ejemplo, si el archivo especificado no existe.
El argumento $filename especifica el archivo que se va a cargar, el cual no debe contener ninguna información de
rutas. Una verificación de seguridad es efectuada sobre $filename. El archivo $filename sólo puede contener
caracteres alfanuméricos, guiones ("-"), barras bajas ("_"), o puntos ("."). No hay ninguna restricción en el argumento
$dirs.
El parámetro $dirs especifica en qué carpetas buscar el archivo. Si el valor es NULL, sólo se buscará en el
include_path ; si el valor es un string o un array, se buscará en la carpeta o carpetas especificadas , seguidas del
include_path.
Cargando Clases
El método estático Zend_Loader::loadClass($class, $dirs) carga un archivo PHP y comprueba la
existencia de la clase.
Zend_Loader::loadClass('Container_Tree',
array(
'/home/production/mylib',
'/home/production/myapp'
)
);
755
Zend_Loader
La cadena que especifica la clase es convertida a una ruta relativa sustituyendo las barras bajas (_) por el separador
de carpeta de su Sistema Operativo, y añadiendo '.php'. En el ejemplo de arriba, 'Container_Tree' se convierte en
'Container\\Tree.php' en Windows.
Si $dirs es una cadena o un array, Zend_Loader::loadClass() busca las carpetas en el orden suministrado.
El primer archivo encontrado es cargado. Si el archivo no existe en el $dirs especificado, entonces se busca en el
include_path del entorno PHP.
Zend_Loader::loadFile() se usa para cargar, así que el nombre de la clase puede contener únicamente
caracteres alfanuméricos, guiones ('-'), barras bajas ('_'), y puntos ('.').
if (Zend_Loader::isReadable($filename)) {
// hace algo con $filename
}
El argumento $filename especifica el nombre de archivo que comprobar. Puede contener información de la ruta.
Este método envuelve la función PHP is_readable() [http://php.net/is_readable]. La función PHP no busca en
include_path, mientras que Zend_Loader::isReadable() sí.
Usando el Autoloader
La clase Zend_Loader contiene un método que se puede registrar con PHP SPL autoloader.
Zend_Loader::autoload() es el método callback. Por comodidad, Zend_Loader permite a la función
registerAutoload() registrar su método autoload(). Si la extensión spl_autoload no está presente en
el entorno PHP, entonces el método registerAutoload() lanza una Zend_Exception.
Zend_Loader::registerAutoload();
Ejemplo 30.5. Ejemplo de registro del método de callback autoload desde una clase extendida
Debido a la semántica de referencia de funciones estáticas en PHP, se debe implementar código tanto para la
clase loadClass() como autoload(), y autoload() debe llamar a self::loadClass(). Si su método
autoload() delega en su padre la llamada a self::loadClass(), entonces llamará al método con ese nombre
en la clase padre, no la subclase.
756
Zend_Loader
Zend_Loader::registerAutoload('My_Loader');
Se puede eliminar un callback de autoload. registerAutoload() tiene un segundo parámetro opcional, que es
true por defecto. Si este parámetro es false, el callback de autoload será borrado de la pila de autoload SPL.
The Autoloader
Zend_Loader_Autoloader introduces a comprehensive autoloading solution for Zend Framework. It has been
designed with several goals in mind:
• Provide a true namespace autoloader. (Previous incarnations intercepted all userland namespaces.)
• Allow registering arbitrary callbacks as autoloaders, and manage them as a stack. (At the time of this writing,
this overcomes some issues with spl_autoload, which does not allow re-registering a callback that utilizes an
instance method.)
Zend_Loader_Autoloader implements a singleton, making it unversally accessible. This provides the ability to
register additional autoloaders from anywhere in your code as necessary.
$autoloader = Zend_Loader_Autoloader::getInstance();
By default, the autoloader is configured to match the "Zend_" and "ZendX_" namespaces. If you have your own library
code that uses your own namespace, you may register it with the autoloader using the registerNamespace()
method. For instance, if your library code is prefixed with "My_", you could do so as follows:
$autoloader->registerNamespace('My_');
757
Zend_Loader
Namespace Prefixes
You'll note that the previous example uses "My_" and not "My". This is because
Zend_Loader_Autoloader is intended as a general purpose autoloader, and does not make the
assumption that a given class prefix namespace includes an underscore. If your class namespace does include
one, you should include it when registering your namespace.
You can also register arbitrary autoloader callbacks, optionally with a specific namespace (or group of namespaces).
Zend_Loader_Autoloader will attempt to match these first before using its internal autoloading mechanism.
As an example, you may want to utilize one or more eZcomponents components with your Zend Framework
application. To use its autoloading capabilities, push it onto the autoloader stack using pushAutoloader():
This tells the autoloader to use the eZcomponents autoloader for classes beginning with "ezc".
You can use the unshiftAutoloader() method to add the autoloader to the beginning of the autoloader chain.
By default, Zend_Loader_Autoloader does no error suppression when using its internal autoloader, which
utilizes Zend_Loader::loadClass(). Most of the time, this is exactly what you want. However, there may be
cases where you want to suppress them. You can do this using suppressNotFoundWarnings():
$autoloader->suppressNotFoundWarnings(true);
Finally, there may be times when you want the autoloader to load any namespace. For instance, PEAR libraries do not
share a common namespace, making specifying individual namespaces difficult when many PEAR components are in
use. You can use the setFallbackAutoloader() method to have the autoloader act as a catch-all:
$autoloader->setFallbackAutoloader(true);
• During development, you want to track the latest version of Zend Framework you have installed, so that you can
ensure the application works when you upgrade between versions.
When pushing to Quality Assurance, however, you need to have slightly more stability, so you want to use the latest
installed revision of a specific minor version.
Finally, when you push to production, you want to pin to a specific installed version, to ensure no breakage occurs
if or when you add new versions of Zend Framework to you server.
The autoloader allows you to do this with the method setZfPath(). This method takes two arguments, a path to a
set of Zend Framework installations, and a version to use. Once invoked, it prepends a path to the include_path
pointing to the appropriate Zend Framework installation library.
The directory you specify as your path should have a tree such as the following:
758
Zend_Loader
ZendFramework/
|-- 1.9.2/
| |-- library/
|-- ZendFramework-1.9.1-minimal/
| |-- library/
|-- 1.8.4PL1/
| |-- library/
|-- 1.8.4/
| |-- library/
|-- ZendFramework-1.8.3/
| |-- library/
|-- 1.7.8/
| |-- library/
|-- 1.7.7/
| |-- library/
|-- 1.7.6/
| |-- library/
Note that each subdirectory should contain the directory library, which contains the actual Zend Framework library
code. The individual subdirectory names may be version numbers, or simply be the untarred contents of a standard
Zend Framework distribution tarball/zipfile.
Now, let's address the use cases. In the first use case, in development, we want to track the latest source install. We
can do that by passing "latest" as the version:
$autoloader->setZfPath($path, 'latest');
In the example from above, this will map to the directory ZendFramework/1.9.2/library/; you can verify
this by checking the return value of getZfPath().
In the second situation, for quality assurance, let's say we want to pin to the 1.8 minor release, using the latest install
you have for that release. You can do so as follows:
$autoloader->setZfPath($path, '1.8');
In the final case, for production, we'll pin to a specific version -- 1.7.7, since that was what was available when Quality
Assurance tested prior to our release.
$autoloader->setZfPath($path, '1.7.7');
You can also specify these values in the configuration file you use with Zend_Application. To do so, you'd
specify the following information:
[production]
autoloaderZfPath = "path/to/ZendFramework"
autoloaderZfVersion = "1.7.7"
[qa]
759
Zend_Loader
autoloaderZfVersion = "1.8"
[development]
autoloaderZfVersion = "latest"
Note the different environment sections, and the different version specified in each environment; these factors will
allow Zend_Application to configure the autoloader appropriately.
Performance implications
For best performance, either do not use this feature, or specify a specific Zend Framework version (i.e., not
"latest", a major revision such as "1", or a minor revision such as "1.8"). Otherwise, the autoloader will need
to scan the provided path for directories matching the criteria -- a somewhat expensive operation to perform
on each request.
interface Zend_Loader_Autoloader_Interface
{
public function autoload($class);
}
When using this interface, you can simply pass a class instance to Zend_Loader_Autoloader's
pushAutoloader() and unshiftAutoloader() methods:
$autoloader->pushAutoloader($foo, 'Foo_');
Autoloader Reference
Below, please find a guide to the methods available in Zend_Loader_Autoloader.
760
Zend_Loader
761
Zend_Loader
762
Zend_Loader
Resource Autoloaders
Resource autoloaders are intended to manage namespaced library code that follow Zend Framework coding standard
guidelines, but which do not have a 1:1 mapping between the class name and the directory structure. Their primary
purpose is to facilitate autoloading application resource code, such as application-specific models, forms, and ACLs.
Resource autoloaders register with the autoloader on instantiation, with the namespace to which they are associated.
This allows you to easily namespace code in specific directories, and still reap the benefits of autoloading.
path/to/some/directory/
acls/
Site.php
forms/
Login.php
models/
User.php
Within this directory, all code is prefixed with the namespace "My_". Within the "acls" subdirectory, the component
prefix "Acl_" is added, giving a final class name of "My_Acl_Site". Similarly, the "forms" subdirectory maps to
"Form_", giving "My_Form_Login". The "models" subdirectory has no component namespace, giving "My_User".
You can use a resource autoloader to autoload these classes. To instantiate the resource autoloader, you are required
to pass at the minimum the base path and namespace for the resources it will be responsible for:
Base namespace
In Zend_Loader_Autoloader, you are expected to provide the trailing underscore
("_") in your namespace if your autoloader will use it to match the namespace.
Zend_Loader_Autoloader_Resource makes the assumption that all code you are autoloading will
use an underscore separator between namespaces, components, and classes. As a result, you do not need to
use the trailing underscore when registering a resource autoloader.
Now that we have setup the base resource autoloader, we can add some components to it to autoload. This is done using
the addResourceType() method, which accepts three arguments: a resource "type", used internally as a reference
name; the subdirectory path underneath the base path in which these resources live; and the component namespace to
append to the base namespace. As an example, let's add each of our resource types.
763
Zend_Loader
Alternately, you could pass these as an array to addResourceTypes(); the following is equivalent to the above:
$resourceLoader->addResourceTypes(array(
'acl' => array(
'path' => 'acls/',
'namespace' => 'Acl',
),
'form' => array(
'path' => 'forms/',
'namespace' => 'Form',
),
'model' => array(
'path' => 'models/',
),
));
Finally, you can specify all of this when instantiating the object, by simply specifying a "resourceTypes" key in the
options passed and a structure like that above:
764
Zend_Loader
As an example, if you have a module with the prefix of "Blog_", and attempted to instantiate the class
"Blog_Form_Entry", it would look in the resource directory's "forms/" subdirectory for a file named "Entry.php".
Loading Plugins
A number of Zend Framework components are pluggable, and allow loading of dynamic functionality by specifying
a class prefix and path to class files that are not necessarily on the include_path or do not necessarily follow
traditional naming conventions. Zend_Loader_PluginLoader provides common functionality for this process.
The basic usage of the PluginLoader follows Zend Framework naming conventions of one class per file, using the
underscore as a directory separator when resolving paths. It allows passing an optional class prefix to prepend when
determining if a particular plugin class is loaded. Additionally, paths are searched in LIFO order. Due to the LIFO
search and the class prefixes, this allows you to define namespaces for your plugins, and thus override plugins from
paths registered earlier.
application/
modules/
foo/
views/
helpers/
FormLabel.php
FormSubmit.php
bar/
views/
helpers/
FormSubmit.php
library/
Zend/
View/
Helper/
FormLabel.php
FormSubmit.php
FormText.php
Now, let's create a plugin loader to address the various view helper repositories available:
765
Zend_Loader
We can then load a given view helper using just the portion of the class name following the prefixes as defined when
adding the paths:
Nota
In some cases, you may use the same prefix for multiple paths. Zend_Loader_PluginLoader actually
registers an array of paths for each given prefix; the last one registered will be the first one checked. This is
particularly useful if you are utilizing incubator components.
Zend_Loader_PluginLoader also optionally allows you to share plugins across plugin-aware objects, without
needing to utilize a singleton instance. It does so via a static registry. Indicate the registry name at instantiation as the
second parameter to the constructor:
Other components that instantiate the PluginLoader using the same registry name will then have access to already
loaded paths and plugins.
766
Zend_Loader
• getPaths($prefix = null) returns all paths as prefix / path pairs if no $prefix is provided, or just the
paths registered for a given prefix if a $prefix is present.
• clearPaths($prefix = null) will clear all registered paths by default, or only those associated with a
given prefix, if the $prefix is provided and present in the stack.
Another common use case for the PluginLoader is to determine fully qualified plugin class names of loaded classes;
getClassName() provides this functionality. Typically, this would be used in conjunction with isLoaded():
if ($loader->isLoaded('Adapter')) {
$class = $loader->getClassName('Adapter');
$adapter = call_user_func(array($class, 'getInstance'));
}
Zend_Loader_PluginLoader offers an opt-in feature for just this situation, a class file include cache. When
enabled, it will create a file that contains all successful includes which you can then call from your bootstrap. Using
this strategy, you can greatly improve the performance of your production servers.
Obviously, the path and filename will vary based on your needs. This code should come as early as possible, to ensure
that plugin-based components can make use of it.
During development, you may wish to disable the cache. One method for doing so is to use a configuration key for
determining whether or not the plugin loader should cache.
767
Zend_Loader
include_once $classFileIncCache;
}
if ($config->enablePluginLoaderCache) {
Zend_Loader_PluginLoader::setIncludeFileCache($classFileIncCache);
}
This technique allows you to keep your modifications to your configuration file rather than code.
768
Capítulo 31. Zend_Locale
Introduction
Zend_Locale is the Frameworks answer to the question, "How can the same application be used around the whole
world?" Most people will say, "That's easy. Let's translate all our output to several languages." However, using simple
translation tables to map phrases from one language to another is not sufficient. Different regions will have different
conventions for first names, surnames, salutory titles, formatting of numbers, dates, times, currencies, etc.
When you are using Zend_Locale you will not have this limitations, because Zend_Locale is not related
to or coupled with PHP's setlocale().
What is Localization
Localization means that an application (or homepage) can be used from different users which speak different languages.
But as you already have expected Localization means more than only translating strings. It includes
• Zend_Locale - Backend support of locales available for localization support within other Zend Framework
components.
• Zend_Locale_Data - Retrieve localized standard strings as country names, language names and more from the
CLDR [http://unicode.org/cldr/] .
769
Zend_Locale
What is a Locale?
Each computer user makes use of Locales, even when they don't know it. Applications lacking localization support,
normally have implicit support for one particular locale (the locale of the author). When a class or function makes use
of localization, we say it is locale-aware. How does the code know which localization the user is expecting?
A locale string or object identifying a supported locale gives Zend_Locale and its subclasses access to information
about the language and region expected by the user. Correct formatting, normalization, and conversions are made
based on this information.
Nota
Be aware that there exist not only locales with 2 characters as most people think. Also there are languages
and regions which are not only abbreviated with 2 characters. Therefor you should NOT strip the region and
language yourself, but use Zend_Locale when you want to strip language or region from a locale string.
Otherwise you could have unexpected behaviour within your code when you do this yourself.
A user from USA would expect the language English and the region USA, yielding the locale identifier "en_US".
A user in Germany would expect the language German and the region Germany, yielding the locale identifier
"de_DE". See the list of pre-defined locale and region combinations [http://unicode.org/cldr/data/diff/supplemental/
languages_and_territories.html], if you need to select a specific locale within Zend Framework.
A German user in America might expect the language German and the region USA, but these non-standard mixes are
not supported directly as recognized "locales". Instead, if an invalid combination is used, then it will automatically
be truncated by dropping the region code. For example, "de_IS" would be truncated to "de", and "xh_RU" would
be truncated to "xh", because neither of these combinations are valid. Additionally, if the base language code is not
supported (e.g. "zz_US") or does not exist, then a default "root" locale will be used. The "root" locale has default
definitions for internationally recognized representations of dates, times, numbers, currencies, etc. The truncation
process depends on the requested information, since some combinations of language and region might be valid for one
type of data (e.g. dates), but not for another (e.g. currency format).
Beware of historical changes, as Zend Framework components do not know about or attempt to track the numerous
timezone changes made over many years by many regions. For example, we can see a historical list [http://
www.statoids.com/tus.html] showing dozens of changes made by governments to when and if a particular region
observes Daylight Savings Time, and even which timezone a particular geographic area belongs. Thus, when
performing date math, the math performed by Zend Framework components will not adjust for these changes, but
instead will give the correct time for the timezone using current, modern rules for DST and timezone assignment for
geographic regions.
770
Zend_Locale
Zend_Locale(Zend_Locale::ENVIRONMENT) is used, then preference will be given to using the host server's
environment configuration, as described below.
The search algorithm used by Zend_Locale for automatic selection of a locale uses three sources of information:
1. const Zend_Locale::BROWSER - The user's Web browser provides information with each request, which is
published by PHP in the global variable HTTP_ACCEPT_LANGUAGE. If no matching locale can be found, then
preference is given to ENVIRONMENT and lastly FRAMEWORK.
2. const Zend_Locale::ENVIRONMENT - PHP publishes the host server's locale via the PHP internal function
setlocale(). If no matching locale can be found, then preference is given to FRAMEWORK and lastly BROWSER.
3. const Zend_Locale::FRAMEWORK - When Zend Framework has a standardized way of specifying component
defaults (planned, but not yet available), then using this constant during instantiation will give preference to
choosing a locale based on these defaults. If no matching locale can be found, then preference is given to
ENVIRONMENT and lastly BROWSER.
1. 'browser' - Zend_Locale should work with the information which is provided by the user's Web browser. It
is published by PHP in the global variable HTTP_ACCEPT_LANGUAGE.
If a user provides more than one locale within his browser, Zend_Locale will use the first found locale. If the user
does not provide a locale or the script is being called from the command line the automatic locale 'environment'
will automatically be used and returned.
2. 'environment' - Zend_Locale should work with the information which is provided by the host server. It is
published by PHP via the internal function setlocale().
If a environment provides more than one locale, Zend_Locale will use the first found locale. If the host does not
provide a locale the automatic locale 'browser' will automatically be used and returned.
3. 'auto' - Zend_Locale should automatically detect any locale which can be worked with. It will first search
for a users locale and then, if not successful, search for the host locale.
771
Zend_Locale
If no locale can be detected, it will throw an exception and tell you that the automatic detection has been failed.
In such cases Zend_Locale will normally throw an exception with a message that the automatic detection of any
locale was not successful. You have two options to handle such a situation. Either through setting a new locale per
hand, or defining a default locale.
But this has one big negative effect. You will have to set your locale object within every class using Zend_Locale.
This could become very unhandy if you are using multiple classes.
Since Zend Framework Release 1.5 there is a much better way to handle this. You can set a default locale which the
static setDefault() method. Of course, every unknown or not full qualified locale will also throw an exception.
setDefault() should be the first call before you initiate any class using Zend_Locale. See the following
example for details:
In the case that no locale can be detected, automatically the locale de will be used. Otherwise, the detected locale
will be used.
772
Zend_Locale
ZF Locale-Aware Classes
In the Zend Framework, locale-aware classes rely on Zend_Locale to automatically select a locale, as explained
above. For example, in a Zend Framework web application, constructing a date using Zend_Date without specifying
a locale results in an object with a locale based on information provided by the current user's web browser.
To override this default behavior, and force locale-aware Zend Framework components to use specific locales,
regardless of the origin of your website visitors, explicitly specify a locale as the third argument to the constructor.
If you know many objects should all use the same default locale, explicitly specify the default locale to avoid the
overhead of each object determining the default locale.
773
Zend_Locale
echo $date->getDate();
Zend_Locale_Format::setOptions(array $options)
The 'precision' option of a value is used to truncate or stretch extra digits. A value of '-1' disables modification of the
number of digits in the fractional part of the value. The 'locale' option helps when parsing numbers and dates using
separators and month names. The date format 'format_type' option selects between CLDR/ISO date format specifier
tokens and PHP's date() tokens. The 'fix_date' option enables or disables heuristics that attempt to correct invalid dates.
The 'number_format' option specifies a default number format for use with toNumber() (see the section called
“Number localization” ).
The 'date_format' option can be used to specify a default date format string, but beware of using getDate(),
checkdateFormat() and getTime() after using setOptions() with a 'date_format'. To use these four methods with the
default date format for a locale, use array('date_format' => null, 'locale' => $locale) for their options.
For working with the standard definitions of a locale the option Zend_Locale_Format::STANDARD can be used.
Setting the option Zend_Locale_Format::STANDARD for date_format uses the standard definitions from
the actual set locale. Setting it for number_format uses the standard number format for this locale. And setting it for
locale uses the standard locale for this environment or browser.
When no cache is set, then Zend_Locale will automatically set a cache itself. Sometimes it is wished to prevent
that a cache is set, even if this degrades performance. In this case the static disableCache(true) method should
be used. It does not only disable the actual set cache, without erasing it, but also prevents that a cache is automatically
generated when no cache is set.
774
Zend_Locale
Using Zend_Locale
Zend_Locale also provides localized information about locales for each locale, including localized names for other
locales, days of the week, month names, etc.
Equality
Zend_Locale also provides a convenience function to compare two locales. All locale-aware classes should provide
a similar equality check.
Default locales
The method getDefault() returns an array of relevant locales using information from the user's web browser
(if available), information from the environment of the host server, and Zend Framework settings. As with the
constructor for Zend_Locale, the first parameter selects a preference of which information to consider (BROWSER,
ENVIRONMENT, or FRAMEWORK) first. The second parameter toggles between returning all matching locales or only
775
Zend_Locale
the first/best match. Locale-aware components normally use only the first locale. A quality rating is included, when
available.
To obtain only the default locales relevant to the BROWSER, ENVIRONMENT, or FRAMEWORK , use the corresponding
method:
• getEnvironment()
• getBrowser()
• getLocale()
// Actual locale
print $locale->toString();
// new locale
$locale->setLocale('aa_DJ');
print $locale->toString();
776
Zend_Locale
The requested list of information is always returned as named array. If you want to give more than one value to a
explicit type where you wish to receive values from, you have to give an array instead of multiple values.
print_r ($list);
// example key -> value pairs...
// [de] -> Deutsch
// [en] -> Englisch
// use one of the returned key as value for the getTranslation() method
// of another language
print Zend_Locale::getTranslation('de', 'language', 'zh');
// returns the translation for the language 'de' in chinese
You can receive this informations for all languages. But not all of the informations are completely available for all
languages. Some of these types are also available through an own function for simplicity. See this list for detailed
informations.
Tabla 31.1. Details for getTranslationList($type = null, $locale = null, $value = null)
Type Description
Language Returns a localized list of all languages. The language part
of the locale is returned as key and the translation as value
Script Returns a localized list of all scripts. The script is returned
as key and the translation as value
Territory Returns a localized list of all territories. This contains
countries, continents and territories. To get only territories
and continents use '1' as value. To get only countries use
'2' as value. The country part of the locale is used as key
where applicable. In the other case the official ISO code
for this territory is used. The translated territory is returned
as value. When you omit the value you will get a list with
both.
Variant Returns a localized list of known variants of scripts. The
variant is returned as key and the translation as value
Key Returns a localized list of known keys. This keys are
generic values used in translation. These are normally
calendar, collation and currency. The key is returned as
array key and the translation as value
777
Zend_Locale
Type Description
Type Returns a localized list of known types of keys. These are
variants of types of calendar representations and types of
collations. When you use 'collation' as value you will get
all types of collations returned. When you use 'calendar'
as value you will get all types of calendars returned. When
you omit the value you will get a list all both returned. The
type is used as key and the translation as value
Layout Returns a list of rules which describes how to format
special text parts
Characters Returns a list of allowed characters within this locale
Delimiters Returns a list of allowed quoting characters for this locale
Measurement Returns a list of known measurement values. This list is
depreciated
Months Returns a list of all month representations within this
locale. There are several different representations which
are all returned as sub array. If you omit the value you
will get a list of all months from the 'gregorian' calendar
returned. You can give any known calendar as value to
get a list of months from this calendar returned. Use
Zend_Date for simplicity
Month Returns a localized list of all month names for this locale.
If you omit the value you will get the normally used
gregorian full name of the months where each month
number is used as key and the translated month is returned
as value. You can get the months for different calendars
and formats if you give an array as value. The first array
entry has to be the calendar, the second the used context
and the third the width to return. Use Zend_Date for
simplicity
Days Returns a list of all day representations within this locale.
There are several different representations which are all
returned as sub array. If you omit the value you will get a
list of all days from the 'gregorian' calendar returned. You
can give any known calendar as value to get a list of days
from this calendar returned. Use Zend_Date for simplicity
Day Returns a localized list of all day names for this locale.
If you omit the value you will get the normally used
gregorian full name of the days where the english day
abbreviation is used as key and the translated day is
returned as value. You can get the days for different
calendars and formats if you give an array as value. The
first array entry has to be the calendar, the second the used
context and the third the width to return. Use Zend_Date
for simplicity
Week Returns a list of values used for proper week calculations
within a locale. Use Zend_Date for simplicity
Quarters Returns a list of all quarter representations within this
locale. There are several different representations which
778
Zend_Locale
Type Description
are all returned as sub array. If you omit the value you
will get a list of all quarters from the 'gregorian' calendar
returned. You can give any known calendar as value to get
a list of quarters from this calendar returned
Quarter Returns a localized list of all quarter names for this locale.
If you omit the value you will get the normally used
gregorian full name of the quarters where each quarter
number is used as key and the translated quarter is returned
as value. You can get the quarters for different calendars
and formats if you give an array as value. The first array
entry has to be the calendar, the second the used context
and the third the width to return
Eras Returns a list of all era representations within this locale.
If you omit the value you will get a list of all eras from
the 'gregorian' calendar returned. You can give any known
calendar as value to get a list of eras from this calendar
returned
Era Returns a localized list of all era names for this locale.
If you omit the value you will get the normally used
gregorian full name of the eras where each era number is
used as key and the translated era is returned as value. You
can get the eras for different calendars and formats if you
give an array as value. The first array entry has to be the
calendar and the second the width to return
Date Returns a localized list of all date formats for this locale.
The name of the dateformat is used as key and the format
itself as value.If you omit the value you will get the date
formats for the gregorian calendar returned. You can get
the date formats for different calendars if you give the
wished calendar as string. Use Zend_Date for simplicity
Time Returns a localized list of all time formats for this locale.
The name of the timeformat is used as key and the format
itself as value. If you omit the value you will get the time
formats for the gregorian calendar returned. You can get
the time formats for different calendars if you give the
wished calendar as string. Use Zend_Date for simplicity
DateTime Returns a localized list of all known date-time formats for
this locale. The name of the date-time format is used as key
and the format itself as value. If you omit the value you
will get the date-time formats for the gregorian calendar
returned. You can get the date-time formats for different
calendars if you give the wished calendar as string. Use
Zend_Date for simplicity
DateItem Returns a list of default formats for given date or time
items
DateInterval Returns a list of date or time formats which are used
when you want to display intervals. The list is a
multidimentional array where the first dimension is the
779
Zend_Locale
Type Description
interval format, and the second dimension is the token
with the greatest difference.
Field Returns a localized list of date fields which can be used to
display calendars or date strings like 'month' or 'year' in a
wished language. If you omit the value you will get this
list for the gregorian calendar returned. You can get the
list for different calendars if you give the wished calendar
as string
Relative Returns a localized list of relative dates which can be
used to display textual relative dates like 'yesterday' or
'tomorrow' in a wished language. If you omit the value
you will get this list for the gregorian calendar returned.
You can get the list for different calendars if you give the
wished calendar as string
Symbols Returns a localized list of characters used for number
representations
NameToCurrency Returns a localized list of names for currencies. The
currency is used as key and the translated name as value.
Use Zend_Currency for simplicity
CurrencyToName Returns a list of currencies for localized names. The
translated name is used as key and the currency as value.
Use Zend_Currency for simplicity
CurrencySymbol Returns a list of known localized currency symbols for
currencies. The currency is used as key and the symbol as
value. Use Zend_Currency for simplicity
Question Returns a list of localized strings for acceptance ('yes') and
negotation ('no'). Use Zend_Locale's getQuestion method
for simplicity
CurrencyFraction Returns a list of fractions for currency values. The
currency is used as key and the fraction as integer value.
Use Zend_Currency for simplicity
CurrencyRounding Returns a list of how to round which currency. The
currency is used as key and the rounding as integer value.
Use Zend_Currency for simplicity
CurrencyToRegion Returns a list of currencies which are known to be used
within a region. The ISO3166 value ('region') is used as
array key and the ISO4217 value ('currency') as array
value. Use Zend_Currency for simplicity
RegionToCurrency Returns a list of regions where a currency is used . The
ISO4217 value ('currency') is used as array key and the
ISO3166 value ('region') as array value. When a currency
is used in several regions these regions are separated with
a whitespace. Use Zend_Currency for simplicity
RegionToTerritory Returns a list of territories with the countries or sub
territories which are included within that territory. The
ISO territory code ('territory') is used as array key and the
ISO3166 value ('region') as array value. When a territory
780
Zend_Locale
Type Description
contains several regions these regions are separated with
a whitespace
TerritoryToRegion Returns a list of regions and the territories where these
regions are located. The ISO3166 code ('region') is used
as array key and the ISO territory code ('territory') as array
value. When a region is located in several territories these
territories are separated with a whitespace
ScriptToLanguage Returns a list of scripts which are used within a language.
The language code is used as array key and the script code
as array value. When a language contains several scripts
these scripts are separated with a whitespace
LanguageToScript Returns a list of languages which are using a script. The
script code is used as array key and the language code as
array value. When a script is used in several languages
these languages are separated with a whitespace
TerritoryToLanguage Returns a list of countries which are using a language. The
country code is used as array key and the language code as
array value. When a language is used in several countries
these countries are separated with a whitespace
LanguageToTerritory Returns a list of countries and the languages spoken within
these countries. The country code is used as array key and
the language code as array value. When a territory is using
several languages these languages are separated with a
whitespace
TimezoneToWindows Returns a list of windows timezones and the related ISO
timezone. The windows timezone is used as array key and
the ISO timezone as array value
WindowsToTimezone Returns a list of ISO timezones and the related windows
timezone. The ISO timezone is used as array key and the
windows timezone as array value
TerritoryToTimezone Returns a list of regions or territories and the related ISO
timezone. The ISO timezone is used as array key and the
territory code as array value
TimezoneToTerritory Returns a list of timezones and the related region or
territory code. The region or territory code is used as array
key and the ISO timezone as array value
CityToTimezone Returns a localized list of cities which can be used as
translation for a related timezone. Not for all timezones is
a translation available, but for a user is the real city written
in his languages more accurate than the ISO name of this
timezone. The ISO timezone is used as array key and the
translated city as array value
TimezoneToCity Returns a list of timezones for localized city names. The
localized city is used as array key and the ISO timezone
name as array value
781
Zend_Locale
Type Description
PhoneToTerritory Returns a list of phone codes which are known to be used
within a territory. The territory (region) is used as array
key and the telephone code as array value
TerritoryToPhone Returns a list of territories where a phone is used . The
phone code is used as array key and the territory (region)
as array value. When a phone code is used in several
territories these territories are separated with a whitespace
NumericToTerritory Returns a list of 3 digit number codes for territories. The
territory (region) is used as array key and the 3 digit
number code as array value
TerritoryToNumeric Returns a list of territories with their 3 digit number code.
The 3 digit number code is used as array key and the
territory (region) as array value
Alpha3ToTerritory Returns a list of 3 sign character codes for territories.
The territory (region) is used as array key and the 3 sign
character code as array value
TerritoryToAlpha3 Returns a list of territories with their 3 sign character code.
The 3 sign character code is used as array key and the
territory (region) as array value
PostalToTerritory Returns a list of territories with a regex for postal codes
which are included within that territory. The ISO territory
code ('territory') is used as array key and the regex as array
value.
NumberingSystem Returns a list of scripts with the notation for digits used
within the script
FallbackToChar Returns a list of replacement characters for often used
unicode characters. This can be used to replace "©" with
"(C)" for example
CharToFallback Returns a list of unicode characters for often used
replacement characters. This can be used to replace "(C)"
with "©" for example
LocaleUpgrade Returns a list of locale dependencies which can be used to
upgrade a language to a full qualified locale
Unit Returns a list of localized calendar units. This can be
used to translate the strings "day", "month" and so on
automatically
If you are in need of a single translated value, you can use the getTranslation() method. It returns always a
string but it accepts some different types than the getTranslationList() method. Also value is the same as
before with one difference. You have to give the detail you want to get returned as additional value.
Nota
Because you have almost always give a value as detail this parameter has to be given as first parameter. This
differs from the getTranslationList() method.
782
Zend_Locale
Tabla 31.2. Details for getTranslation($value = null, $type = null, $locale = null)
Type Description
Language Returns a translation for a language. To select the wished
translation you must give the language code as value
Script Returns a translation for a script. To select the wished
translation you must give the script code as value
Territory or Country Returns a translation for a territory. This can be countries,
continents and territories. To select the wished variant you
must give the territory code as value
Variant Returns a translation for a script variant. To select the
wished variant you must give the variant code as value
Key Returns translation for a known keys. This keys are
generic values used in translation. These are normally
calendar, collation and currency. To select the wished key
you must give the key code as value
DefaultCalendar Returns the default calendar for the given locale. For
most locales this will be 'gregorian'. Use Zend_Date for
simplicity
MonthContext Returns the default context for months which is used
within the given calendar. If you omit the value the
'gregorian' calendar will be used. Use Zend_Date for
simplicity
DefaultMonth Returns the default format for months which is used within
the given calendar. If you omit the value the 'gregorian'
calendar will be used. Use Zend_Date for simplicity
Month Returns a translation for a month. You have to give the
number of the month as integer value. It has to be between
1 and 12. If you want to receive data for other calendars,
contexts or formats, then you must give an array instead
of an integer with the expected values. The array has to
look like this: array( 'calendar', 'context',
'format', 'month number'). If you give only an
integer then the default values are the 'gregorian' calendar,
the context 'format' and the format 'wide'. Use Zend_Date
for simplicity
DayContext Returns the default context for ´days which is used within
the given calendar. If you omit the value the 'gregorian'
calendar will be used. Use Zend_Date for simplicity
DefaultDay Returns the default format for days which is used within
the given calendar. If you omit the value the 'gregorian'
calendar will be used. Use Zend_Date for simplicity
Day Returns a translation for a day. You have to give the
english abbreviation of the day as string value ('sun', 'mon',
etc.). If you want to receive data for other calendars,
contexts or format, then you must give an array instead
of an integer with the expected values. The array has to
look like this: array('calendar', 'context',
'format', 'day abbreviation'). If you give
783
Zend_Locale
Type Description
only an string then the default values are the 'gregorian'
calendar, the context 'format' and the format 'wide'. Use
Zend_Date for simplicity
Quarter Returns a translation for a quarter. You have to give the
number of the quarter as integer and it has to be between
1 and 4. If you want to receive data for other calendars,
contexts or formats, then you must give an array instead
of an integer with the expected values. The array has to
look like this: array('calendar', 'context',
'format', 'quarter number'). If you give
only an string then the default values are the 'gregorian'
calendar, the context 'format' and the format 'wide'
Am Returns a translation for 'AM' in a expected locale. If
you want to receive data for other calendars an string
with the expected calendar. If you omit the value then
the 'gregorian' calendar will be used. Use Zend_Date for
simplicity
Pm Returns a translation for 'PM' in a expected locale. If
you want to receive data for other calendars an string
with the expected calendar. If you omit the value then
the 'gregorian' calendar will be used. Use Zend_Date for
simplicity
Era Returns a translation for an era within a locale. You
have to give the era number as string or integer. If
you want to receive data for other calendars or formats,
then you must give an array instead of the era number
with the expected values. The array has to look like
this: array('calendar', 'format', 'era
number'). If you give only an string then the default
values are the 'gregorian' calendar and the 'abbr' format
DefaultDate Returns the default date format which is used within
the given calendar. If you omit the value the 'gregorian'
calendar will be used. Use Zend_Date for simplicity
Date Returns the date format for an given calendar or format
within a locale. If you omit the value then the 'gregorian'
calendar will be used with the 'medium' format. If you
give a string then the 'gregorian' calendar will be used with
the given format. Or you can also give an array which
will have to look like this: array('calendar',
'format'). Use Zend_Date for simplicity
DefaultTime Returns the default time format which is used within
the given calendar. If you omit the value the 'gregorian'
calendar will be used. Use Zend_Date for simplicity
Time Returns the time format for an given calendar or format
within a locale. If you omit the value then the 'gregorian'
calendar will be used with the 'medium' format. If you
give a string then the 'gregorian' calendar will be used with
the given format. Or you can also give an array which
784
Zend_Locale
Type Description
will have to look like this: array('calendar',
'format'). Use Zend_Date for simplicity
DateTime Returns the datetime format for the given locale which
indicates how to display date with times in the same
string within the given calendar. If you omit the value
the 'gregorian' calendar will be used. Use Zend_Date for
simplicity
DateItem Returns the default format for a given date or time item
DateInterval Returns the interval format for a given date or time
format. The first value is the calendar format, normally
'gregorian'. The second value is the interval format and
the third value the token with the greatest difference.
For example: array('gregorian', 'yMMMM', 'y') returns the
interval format for the date format 'yMMMM' where 'y'
has the greatest difference.
Field Returns a translated date field which can be used to
display calendars or date strings like 'month' or 'year' in
a wished language. You must give the field which has to
be returned as string. In this case the 'gregorian' calendar
will be used. You can get the field for other calendar
formats if you give an array which has to look like this:
array('calendar', 'date field')
Relative Returns a translated date which is relative to today which
can include date strings like 'yesterday' or 'tomorrow'
in a wished language. You have to give the number of
days relative to tomorrow to receive the expected string.
Yesterday would be '-1', tomorrow '1' and so on. This will
use the 'gregorian' calendar. If you want to get relative
dates for other calendars you will have to give an array
which has to look like this: array('calendar',
'relative days'). Use Zend_Date for simplicity
DecimalNumber Returns the format for decimal numbers within a given
locale. Use Zend_Locale_Format for simplicity
ScientificNumber Returns the format for scientific numbers within a given
locale
PercentNumber Returns the format for percentage numbers within a given
locale
CurrencyNumber Returns the format for displaying currency numbers
within a given locale. Use Zend_Currency for simplicity
NameToCurrency Returns the translated name for a given currency.
The currency has to be given in ISO format which
is for example 'EUR' for the currency 'euro'. Use
Zend_Currency for simplicity
CurrencyToName Returns a currency for a given localized name. Use
Zend_Currency for simplicity
785
Zend_Locale
Type Description
CurrencySymbol Returns the used symbol for a currency within a given
locale. Not for all currencies exists a symbol. Use
Zend_Currency for simplicity
Question Returns a localized string for acceptance ('yes') and
negotation ('no'). You have to give either 'yes' or 'no' as
value to receive the expected string. Use Zend_Locale's
getQuestion method for simplicity
CurrencyFraction Returns the fraction to use for a given currency. You must
give the currency as ISO value. Use Zend_Currency for
simplicity
CurrencyRounding Returns how to round a given currency. You must give
the currency as ISO value. If you omit the currency
then the 'DEFAULT' rounding will be returned. Use
Zend_Currency for simplicity
CurrencyToRegion Returns the currency for a given region. The region code
has to be given as ISO3166 string for example 'AT' for
austria. Use Zend_Currency for simplicity
RegionToCurrency Returns the regions where a currency is used. The
currency has to be given as ISO4217 code for example
'EUR' for euro. When a currency is used in multiple
regions, these regions are separated with a whitespace
character. Use Zend_Currency for simplicity
RegionToTerritory Returns the regions for a given territory. The territory
has to be given as ISO4217 string for example '001' for
world. The regions within this territory are separated with
a whitespace character
TerritoryToRegion Returns the territories where a given region is located.
The region has to be given in ISO3166 string for
example 'AT' for austria. When a region is located in
multiple territories then these territories are separated with
a whitespace character
ScriptToLanguage Returns the scripts which are used within a given
language. The language has to be given as ISO language
code for example 'en' for english. When multiple scripts
are used within a language then these scripts are separated
with a whitespace character
LanguageToScript Returns the languages which are used within a given
script. The script has to be given as ISO script code
for example 'Latn' for latin. When a script is used in
multiple languages then these languages are separated
with a whitespace character
TerritoryToLanguage Returns the territories where a given language is used.
The language has to be given as ISO language code for
example 'en' for english. When multiple territories exist
where this language is used then these territories are
separated with a whitespace character
LanguageToTerritory Returns the languages which are used within a given
territory. The territory has to be given as ISO3166 code
786
Zend_Locale
Type Description
for example 'IT' for italia. When a language is used in
multiple territories then these territories are separated with
a whitespace character
TimezoneToWindows Returns a ISO timezone for a given windows timezone
WindowsToTimezone Returns a windows timezone for a given ISO timezone
TerritoryToTimezone Returns the territory for a given ISO timezone
TimezoneToTerritory Returns the ISO timezone for a given territory
CityToTimezone Returns the localized city for a given ISO timezone. Not
for all timezones does a city translation exist
TimezoneToCity Returns the ISO timezone for a given localized city name.
Not for all cities does a timezone exist
PhoneToTerritory Returns the telephone code for a given territory (region).
The territory code has to be given as ISO3166 string for
example 'AT' for austria
TerritoryToPhone Returns the territory (region) where a telephone code is
used. The telephone code has to be given as plain integer
code for example '43' for +43. When a telephone code is
used in multiple territories (regions), these territories are
separated with a whitespace character
NumericToTerritory Returns the 3 digit number code for a given territory
(region). The territory code has to be given as ISO3166
string for example 'AT' for austria
TerritoryToNumeric Returns the territory (region) for a 3 digit number code.
The 3 digit number code has to be given as plain integer
code for example '43'
Alpha3ToTerritory Returns the 3 sign character code for a given territory
(region). The territory code has to be given as ISO3166
string for example 'AT' for austria
TerritoryToAlpha3 Returns the territory (region) for a 3 sign character code
PostalToTerritory Returns the a regex for postal codes for a given territory.
The territory has to be given as ISO4217 string for
example '001' for world
NumberingSystem Returns a scripts with the notation for digits used within
this script
FallbackToChar Returns a replacement character for a often used unicode
character. This can be used to replace "©" with "(C)" for
example
CharToFallback Returns a unicode character for a often used replacement
character. This can be used to replace "(C)" with "©" for
example
LocaleUpgrade Returns a locale dependencies for a given language which
can be used to upgrade this language to a full qualified
locale
Unit Returns a localized calendar unit. This can be used
to translate the strings "day", "month" and so on
787
Zend_Locale
Type Description
automatically. The first parameter has to be the type, and
the second parameter has to be the count
Nota
With Zend Framework 1.5 several old types have been renamed. This has to be done because of several new
types, some misspelling and to increase the usability. See this table for a list of old to new types:
The example below demonstrates how to obtain the names of things in different languages.
The next example shows how to find the name of a language in another language, when the two letter iso country
code is not known.
$code2name = Zend_Locale::getLanguageTranslationList('en_US');
$name2code = array_flip($code2name);
$frenchCode = $name2code['French'];
echo Zend_Locale::getLanguageTranslation($frenchCode, 'de_AT');
// output is the German name of the French language
788
Zend_Locale
To generate a list of all languages known by Zend_Locale, with each language name shown in its
own language, try the example below in a web page. Similarly, getCountryTranslationList() and
getCountryTranslation() could be used to create a table mapping your native language names for regions to
the names of the regions shown in another language. Use a try .. catch block to handle exceptions that occur
when using a locale that does not exist. Not all languages are also locales. In the example, below exceptions are ignored
to prevent early termination.
$list = Zend_Locale::getLanguageTranslationList('auto');
• yes and no: A generic string representation for yes and no responses. This will contain the first and most generic
response from yesarray and noarray.
yesarray and noarray: An array with all known yes and no responses. Several languages have more than just two
responses. In general this is the full string and its abbreviation.
yesexpr and noexpr: An generated regex which allows you to handle user response, and search for yes or no.
All of this informations are of course localized and depend on the set locale. See the following example for the
informations you can receive:
- - - Output - - -
Array
(
[yes] => ja
[no] => nein
[yesarray] => Array
789
Zend_Locale
(
[0] => ja
[1] => j
)
Nota
Until 1.0.3 yesabbr from the underlaying locale data was also available. Since 1.5 this information is no longer
standalone available, but you will find the information from it within yesarray.
$localelist = Zend_Locale::getLocaleList();
Nota
Note that the locales are returned as key of the array you will receive. The value is always a boolean true.
Detecting locales
When you want to detect if a given input, regardless of its source, is a locale you should use the static isLocale()
method. The first parameter of this method is the string which you want to check.
$input = 'to_RU';
if (Zend_Locale::isLocale($input)) {
print "'{$input}' is a locale";
} else {
print "Sorry... the given input is no locale";
}
As you can see, the output of this method is always a boolean. There is only one reason you could get an exception
when calling this method. When your system does not provide any locale and Zend Framework is not able to detect it
automatically. Normally this shows that there is a problem with your OS in combination with PHP's setlocale().
790
Zend_Locale
You should also note that any given locale string will automatically be degraded if the region part does not exist for
this locale. In our previous example the language 'to' does not exist in the region 'RU', but you will still get true
returned as Zend_Locale can work with the given input.
Still it's sometimes usefull to prevent this automatic degrading, and this is where the second parameter of
isLocale() comes in place. The strict parameter defaults to FALSE and can be used to prevent degrading when
set to TRUE.
$input = 'to_RU';
if (Zend_Locale::isLocale($input, true)) {
print "'{$input}' is a locale";
} else {
print "Sorry... the given input is no locale";
}
Now that you are able to detect if a given string is a locale you could add locale aware behaviour to your own classes.
But you will soon detect that this will always leads to the same 15 lines of code. Something like the following example:
With Zend Framework 1.8 we added a static findLocale() method which returns you a locale string which you
can work with. It processes the following tasks:
• Detects the locale from browser when the previous detections failed
• Detects the locale from environment when the previous detections failed
• Detects the locale from framework when the previous detections failed
791
Zend_Locale
The following example shows how these checks and the above code can be simplified with one single call:
$locale = Zend_Locale::findLocale($inputstring);
The exact same string containing a number or a date might mean different things to people with different customs and
conventions. Disambiguation of numbers and dates requires rules about how to interpret these strings and normalize
the values into a standardized data structure. Thus, all methods in Zend_Locale_Format require a locale in order
to parse the input data.
792
Zend_Locale
Also, the precision of the resulting decimal representation can be rounded to a desired length with getNumber()
with the option 'precision'. If no precision is given, no rounding occurs. Use only PHP integers to specify the
precision.
If the resulting decimal representation should be truncated to a desired length instead of rounded the option
'number_format' can be used instead. Define the length of the decimal representation with the desired length
of zeros. The result will then not be rounded. So if the defined precision within number_format is zero the value
"1.6" will return "1", not "2. See the example nearby:
$number = Zend_Locale_Format::getNumber('13.524,678',
array('number_format' => '#.00',
'locale' => $locale)
);
print $number; // will return 13524.67
Number localization
toNumber($value, array $options = array()) can localize numbers to the following supported locales
. This function will return a localized string of the given number in a conventional format for a specific locale. The
'number_format' option explicitly specifies a non-default number format for use with toNumber().
Unlimited length
toNumber() can localize numbers with unlimited length. It is not related to integer or float limitations.
The same way as within getNumber(), toNumber() handles precision. If no precision is given, the complete
localized number will be returned.
793
Zend_Locale
Using the option 'number_format' a self defined format for generating a number can be defined. The format itself has
to be given in CLDR format as described below. The locale is used to get separation, precision and other number
formatting signs from it. German for example defines ',' as precision separation and in English the '.' sign is used.
$number = Zend_Locale_Format::toNumber(13547.3,
array('number_format' => '#,##0.00',
'locale' => 'de')
);
Number testing
isNumber($value, array $options = array()) checks if a given string is a number and returns true
or false.
794
Zend_Locale
795
Zend_Locale
796
Zend_Locale
unsupported numeral system will result in an exception, to avoid accidentally performing an incorrect conversion due
to a spelling error. All characters in the input, which are not numerals for the selected numeral system, are copied
to the output with no conversion provided for unit separator characters. Zend_Locale* components rely on the
data provided by CLDR (see their list of scripts grouped by language [http://unicode.org/cldr/data/diff/supplemental/
languages_and_scripts.html?sortby=date]).
In CLDR and hereafter, the Europena/Latin numerals will be referred to as "Latin" or by the assigned 4-letter code
"Latn". Also, the CLDR refers to this numeral systems as "scripts".
Suppose a web form collected a numeric input expressed using Eastern Arabic digits "####". Most software and PHP
functions expect input using Arabic numerals. Fortunately, converting this input to its equivalent Latin numerals "100"
requires little effort using convertNumerals($inputNumeralString, $sourceNumeralSystem,
$destNumeralSystem) , which returns the $input with numerals in the script $sourceNumeralSystem
converted to the script $destNumeralSystem.
Ejemplo 31.39. Converting numerals from Eastern Arabic scripts to European/Latin scripts
Similarly, any of the supported numeral systems may be converted to any other supported numeral system.
Ejemplo 31.40. Converting numerals from Latin script to Eastern Arabic script
$latinScript = '123';
$arabicScript = Zend_Locale_Format::convertNumerals($latinScript,
'Latn',
'Arab');
Ejemplo 31.41. Getting 4 letter CLDR script code using a native-language name of the script
797
Zend_Locale
If setOption(array('fix_date' => true)) is set the getDate() method adds a key 'fixed' with a whole
number value indicating if the input date string required "fixing" by rearranging the day, month, or year in the input
to fit the format used.
For those needing to specify explicitly the format of the date string, the following format token specifiers are supported.
If an invalid format specifier is used, such as the PHP 'i' specifier when in ISO format mode, then an error will be
thrown by the methods in Zend_Locale_Format that support user-defined formats.
These specifiers (below) are a small subset of the full "ISO" set supported by Zend_Date's toString(). If you
need to use PHP date() compatible format specifiers, then first call setOptions(array('format_type'
=> 'php')). And if you want to convert only one special format string from PHP date() compatible format to
"ISO" format use convertPhpToIsoFormat(). Currently, the only practical difference relates to the specifier
for minutes ('m' using the ISO default, and 'i' using the PHP date format).
798
Zend_Locale
$dateString = Zend_Locale_Format::getDate('13.04.2006',
array('date_format' =>
'dd.MM.yyyy')
);
print_r($dateString); // outputs:
Array
(
[format] => dd.MM.yyyy
[day] => 13
[month] => 4
[year] => 2006
)
print_r($date); // outputs:
Array
(
[format] => dd.MM.yyyy
[day] => 13
[month] => 4
[year] => 2006
[fixed] => 4
)
Since getDate() is "locale-aware", specifying the $locale is sufficient for date strings adhering to that locale's
format. The option 'fix_date' uses simple tests to determine if the day or month is not valid, and then applies
heuristics to try and correct any detected problems. Note the use of 'Zend_Locale_Format::STANDARD' as the
799
Zend_Locale
value for 'date_format' to prevent the use of a class-wide default date format set using setOptions(). This
forces getDate to use the default date format for $locale.
print_r ($date);
A complete date and time is returned when the input contains both a date and time in the expected format.
print_r ($date);
If a specific format is desired, specify the $format argument, without giving a $locale. Only single-letter codes
(H, m, s, y, M, d), and MMMM and EEEE are supported in the $format.
$date = Zend_Locale_Format::getDate('13200504T551422',
array('date_format' =>
'ddyyyyMM ssmmHH')
);
print_r ($date);
800
Zend_Locale
The option 'fix_date' uses simple tests to determine if the day or month is not valid, and then applies heuristics to
try and correct any detected problems. getDate() automatically detects and corrects some kinds of problems with
input, such as misplacing the year:
$date = Zend_Locale_Format::getDate('41.10.20',
array('date_format' => 'ddMMyy',
'fix_date' => true)
);
Testing Dates
Use checkDateFormat($inputString, array('date_format' => $format, $locale)) to
check if a given string contains all expected date parts. The checkDateFormat() method uses getDate(), but
without the option 'fixdate' to avoid returning true when the input fails to conform to the date format. If errors
are detected in the input, such as swapped values for months and days, the option 'fixdate' method will apply
heuristics to "correct" dates before determining their validity.
801
Zend_Locale
// using the default date format for 'de_AT', is this a valid date?
if (Zend_Locale_Format::checkDateFormat('13.Apr.2006',
array('date_format' =>
Zend_Locale_Format::STANDARD,
$locale)
) {
print "date";
} else {
print "not a date";
}
Normalizing a Time
Normally, a time will be returned with a date, if the input contains both. If the proper format is not known, but the
locale relevant to the user input is known, then getTime() should be used, because it uses the default time format
for the selected locale.
Testing Times
Use checkDateFormat() to check if a given string contains a proper time. The usage is exact the same as with
checking Dates, only date_format should contain the parts which you expect to have.
Supported locales
Zend_Locale provides information on several locales. The following table shows all languages and their related
locales, sorted by language:
802
Zend_Locale
803
Zend_Locale
804
Zend_Locale
805
Zend_Locale
806
Zend_Locale
807
Zend_Locale
808
Zend_Locale
809
Zend_Locale
810
Zend_Locale
811
Zend_Locale
812
Zend_Locale
813
814
Capítulo 32. Zend_Log
Overview
Zend_Log is a component for general purpose logging. It supports multiple log backends, formatting messages sent
to the log, and filtering messages from being logged. These functions are divided into the following objects:
• A Log (instance of Zend_Log) is the object that your application uses the most. You can have as many Log objects
as you like; they do not interact. A Log object must contain at least one Writer, and can optionally contain one or
more Filters.
• A Filter (implements Zend_Log_Filter_Interface) blocks log data from being saved. A filter may be
applied to an individual Writer, or to a Log where it is applied before all Writers. In either case, filters may be
chained.
• A Formatter (implements Zend_Log_Formatter_Interface) can format the log data before it is written by
a Writer. Each Writer has exactly one Formatter.
Creating a Log
To get started logging, instantiate a Writer and then pass it to a Log instance:
$logger->addWriter($writer);
It is important to note that the Log must have at least one Writer. You can add any number of Writers using the Log's
addWriter() method.
Logging Messages
To log a message, call the log() method of a Log instance and pass it the message with a corresponding priority:
The first parameter of the log() method is a string message and the second parameter is an integer priority.
The priority must be one of the priorities recognized by the Log instance. This is explained in the next section.
A shortcut is also available. Instead of calling the log() method, you can call a method by the same name as the
priority:
815
Zend_Log
Destroying a Log
If the Log object is no longer needed, set the variable containing it to NULL to destroy it. This will automatically call
the shutdown() instance method of each attached Writer before the Log object is destroyed:
$logger = null;
Explicitly destroying the log in this way is optional and is performed automatically at PHP shutdown.
These priorities are always available, and a convenience method of the same name is available for each one.
The priorities are not arbitrary. They come from the BSD syslog protocol, which is described in RFC-3164 [http://
tools.ietf.org/html/rfc3164]. The names and corresponding priority numbers are also compatible with another PHP
logging system, PEAR Log [http://pear.php.net/package/log], which perhaps promotes interoperability between it and
Zend_Log.
Priority numbers descend in order of importance. EMERG (0) is the most important priority. DEBUG (7) is the least
important priority of the built-in priorities. You may define priorities of lower importance than DEBUG. When selecting
the priority for your log message, be aware of this priority hierarchy and choose appropriately.
$logger->addPriority('FOO', 8);
The snippet above creates a new priority, FOO, whose value is 8. The new priority is then available for logging:
816
Zend_Log
The creation of the event array is completely transparent. However, knowledge of the event array is required for
adding an item that does not exist in the default set above.
To add a new item to every future event, call the setEventItem() method giving a key and a value:
$logger->setEventItem('pid', getmypid());
The example above sets a new item named pid and populates it with the PID of the current process. Once a new item
has been set, it is available automatically to all writers along with all of the other data event data during logging. An
item can be overwritten at any time by calling the setEventItem() method again.
Setting a new event item with setEventItem() causes the new item to be sent to all writers of the logger. However,
this does not guarantee that the writers actually record the item. This is because the writers won't know what to do
with it unless a formatter object is informed of the new item. Please see the section on Formatters to learn more.
Writers
A Writer is an object that inherits from Zend_Log_Writer_Abstract. A Writer's responsibility is to record log
data to a storage backend.
Writing to Streams
Zend_Log_Writer_Stream sends log data to a PHP stream [http://www.php.net/stream].
To write log data to the PHP output buffer, use the URL php://output. Alternatively, you can send log data
directly to a stream like STDERR (php://stderr).
$logger->info('Informational message');
$logger->info('Informational message');
By default, the stream opens in the append mode ("a"). To open it with a different mode, the
Zend_Log_Writer_Stream constructor accepts an optional second parameter for the mode.
817
Zend_Log
$logger->info('Informational message');
You cannot specify the mode for existing stream resources. Doing so causes a Zend_Log_Exception to be thrown.
Writing to Databases
Zend_Log_Writer_Db writes log information to a database table using Zend_Db. The constructor of
Zend_Log_Writer_Db receives a Zend_Db_Adapter instance, a table name, and a mapping of database
columns to event data items:
$logger->info('Informational message');
The example above writes a single row of log data to the database table named log_table_name table. The database
column named lvl receives the priority number and the column named msg receives the log message.
Writing to Firebug
Zend_Log_Writer_Firebug sends log data to the Firebug [http://www.getfirebug.com/] Console [http://
getfirebug.com/logging.html].
818
Zend_Log
All data is sent via the Zend_Wildfire_Channel_HttpHeaders component which uses HTTP headers to
ensure the page content is not disturbed. Debugging AJAX requests that require clean JSON and XML responses is
possible with this approach.
Requirements:
// Place this in your bootstrap file before dispatching your front controller
$writer = new Zend_Log_Writer_Firebug();
$logger = new Zend_Log($writer);
$logger->addPriority('FOO', 8);
$writer->setPriorityStyle(8, 'TRACE');
$logger->foo('Foo Message');
The default style for user-defined priorities can be set with the setDefaultPriorityStyle() method.
819
Zend_Log
$writer->setDefaultPriorityStyle('TRACE');
The LOG, INFO, WARN, ERROR and TRACE styles require no special formatting.
Exception Logging
To log a Zend_Exception simply pass the exception object to the logger. It does not matter which priority or style
you have set as the exception is automatically recognized.
Tabla Logging
You can also log data and format it in a table style. Columns are automatically recognized and the first row of data
automatically becomes the header.
$writer->setPriorityStyle(8, 'TABLE');
$logger->addPriority('TABLE', 8);
820
Zend_Log
Writing to Email
Zend_Log_Writer_Mail writes log entries in an email message by using Zend_Mail. The
Zend_Log_Writer_Mail constructor takes a Zend_Mail object, and an optional Zend_Layout object.
The primary use case for Zend_Log_Writer_Mail is notifying developers, systems administrators, or any
concerned parties of errors that might be occurring with PHP-based scripts. Zend_Log_Writer_Mail was born
out of the idea that if something is broken, a human being needs to be alerted of it immediately so they can take
corrective action.
// Set subject text for use; summary of number of errors is appended to the
// subject line before sending the message.
$writer->setSubjectPrependText('Errors with script foo.php');
One email is sent containing all log entries at or above the filter level. For example, if warning-level entries an up are
to be emailed, and two warnings and five errors occur, the resulting email will contain a total of seven log entries.
Zend_Layout Usage
A Zend_Layout instance may be used to generate the HTML portion of a multipart email. If a Zend_Layout
instance is in use, Zend_Log_Writer_Mail assumes that it is being used to render HTML and sets the body
HTML for the message as the Zend_Layout-rendered value.
When using Zend_Log_Writer_Mail with a Zend_Layout instance, you have the option to set a custom
formatter by using the setLayoutFormatter() method. If no Zend_Layout-specific entry formatter was
specified, the formatter currently in use will be used. Full usage of Zend_Layout with a custom formatter is outlined
below.
821
Zend_Log
->addTo('project_developers@example.org');
// Note that a subject line is not being set on the Zend_Mail instance!
Caveats
Sending log entries via email can be dangerous. If error conditions are being improperly handled by your script, or if
you're misusing the error levels, you might find yourself in a situation where you are sending hundreds or thousands
of emails to the recipients depending on the frequency of your errors.
At this time, Zend_Log_Writer_Mail does not provide any mechanism for throttling or otherwise batching up
the messages. Such functionality should be implemented by the consumer if necessary.
Again, Zend_Log_Writer_Mail's primary goal is to proactively notify a human being of error conditions. If those
errors are being handled in a timely fashion, and safeguards are being put in place to prevent those circumstances in
the future, then email-based notification of errors can be a valuable tool.
822
Zend_Log
One useful case for Zend_Log_Writer_Syslog is for aggregating logs from clustered machines via the system
log functionality. Many systems allow remote logging of system events, which allows system administrators to monitor
a cluster of machines from a single log file.
By default, all syslog messages generated are prefixed with the string "Zend_Log". You may specify a different
"application" name by which to identify such log messages by either passing the application name to the constructor
or the application accessor:
The system log also allows you to identify the "facility," or application type, logging the message; many system loggers
will actually generate different log files per facility, which again aids administrators monitoring server activity.
You may specify the log facility either in the constructor or via an accessor. It should be one of the openlog()
constants defined on the openlog() manual page [http://php.net/openlog].
When logging, you may continue to use the default Zend_Log priority constants; internally, they are mapped to the
appropriate syslog() priority constants.
// goes nowhere
$logger->info('Informational message');
$logger->info('Informational message');
var_dump($mock->events[0]);
823
Zend_Log
// Array
// (
// [timestamp] => 2007-04-06T07:16:37-07:00
// [message] => Informational message
// [priority] => 6
// [priorityName] => INFO
// )
To clear the events logged by the mock, simply set $mock->events = array().
Compositing Writers
There is no composite Writer object. However, a Log instance can write to any number of Writers. To do this, use
the addWriter() method:
Formatters
A Formatter is an object that is responsible for taking an event array describing a log event and outputting a string
with a formatted log line.
Some Writers are not line-oriented and cannot use a Formatter. An example is the Database Writer, which inserts the
event items directly into database columns. For Writers that cannot support a Formatter, an exception is thrown if you
attempt to set a Formatter.
Simple Formatting
Zend_Log_Formatter_Simple is the default formatter. It is configured automatically when you specify no
formatter. The default configuration is equivalent to the following:
A formatter is set on an individual Writer object using the Writer's setFormatter() method:
824
Zend_Log
$logger->addWriter($writer);
$logger->info('there');
The constructor of Zend_Log_Formatter_Simple accepts a single parameter: the format string. This string
contains keys surrounded by percent signs (e.g. %message%). The format string may contain any key from
the event data array. You can retrieve the default keys by using the DEFAULT_FORMAT constant from
Zend_Log_Formatter_Simple.
Formatting to XML
Zend_Log_Formatter_Xml formats log data into XML strings. By default, it automatically logs all items in the
event data array:
$logger->info('informational message');
The code above outputs the following XML (space added for clarity):
<logEntry>
<timestamp>2007-04-06T07:24:37-07:00</timestamp>
<message>informational message</message>
<priority>6</priority>
<priorityName>INFO</priorityName>
</logEntry>
It's possible to customize the root element as well as specify a mapping of XML elements to the items in the event
data array. The constructor of Zend_Log_Formatter_Xml accepts a string with the name of the root element as
the first parameter and an associative array with the element mapping as the second parameter:
$logger->info('informational message');
The code above changes the root element from its default of logEntry to log. It also maps the element msg to the
event data item message. This results in the following output:
825
Zend_Log
<log>
<msg>informational message</msg>
<level>INFO</level>
</log>
Filters
A Filter object blocks a message from being written to the log.
// blocked
$logger->info('Informational message');
// logged
$logger->emerg('Emergency message');
When you add one or more Filters to the Log object, the message must pass through all of the Filters before any
Writers receives it.
826
Zend_Log
$logger->emerg('Emergency message');
827
828
Capítulo 33. Zend_Mail
Introduction
Getting started
Zend_Mail provides generalized functionality to compose and send both text and MIME-compliant multipart e-mail
messages. Mail can be sent with Zend_Mail via the default Zend_Mail_Transport_Sendmail transport or
via Zend_Mail_Transport_Smtp.
Minimum definitions
In order to send an e-mail with Zend_Mail you have to specify at least one recipient, a sender (e.g., with
setFrom()), and a message body (text and/or HTML).
For most mail attributes there are "get" methods to read the information stored in the mail object. For further details,
please refer to the API documentation. A special one is getRecipients(). It returns an array with all recipient e-
mail addresses that were added prior to the method call.
For security reasons, Zend_Mail filters all header fields to prevent header injection with newline (\n) characters.
Double quotation is changed to single quotation and angle brackets to square brackets in the name of sender and
recipients. If the marks are in email address, the marks will be removed.
You also can use most methods of the Zend_Mail object with a convenient fluent interface.
829
Zend_Mail
new transport instance can then act as the default Zend_Mail transport, or it can be passed to the send() method
of Zend_Mail.
Therefore if you want to use BCC on a windows server, use the SMTP transport for sending!
830
Zend_Mail
// Create transport
$config = array('name' => 'sender.example.com');
$transport = new Zend_Mail_Transport_Smtp('mail.example.com', $config);
If you wish to have a separate connection for each mail delivery, you will need to create and destroy your transport
before and after each send() method is called. Or alternatively, you can manipulate the connection between each
delivery by accessing the transport's protocol object.
// Create transport
$transport = new Zend_Mail_Transport_Smtp();
$transport->setConnection($protocol);
$protocol->quit();
$protocol->disconnect();
831
Zend_Mail
Additional transports
Additional transports can be written by implementing Zend_Mail_Transport_Interface.
HTML E-Mail
To send an e-mail in HTML format, set the body using the method setBodyHTML() instead of setBodyText().
The MIME content type will automatically be set to text/html then. If you use both HTML and Text bodies, a
multipart/alternative MIME message will automatically be generated:
Attachments
Files can be attached to an e-mail using the createAttachment() method. The default behavior of Zend_Mail
is to assume the attachment is a binary object (application/octet-stream), that it should be transferred with base64
encoding, and that it is handled as an attachment. These assumptions can be overridden by passing more parameters
to createAttachment():
832
Zend_Mail
$mail->createAttachment($someBinaryString);
$mail->createAttachment($myImage,
'image/gif',
Zend_Mime::DISPOSITION_INLINE,
Zend_Mime::ENCODING_8BIT);
If you want more control over the MIME part generated for this attachment you can use the return
value of createAttachment() to modify its attributes. The createAttachment() method returns a
Zend_Mime_Part object:
$at = $mail->createAttachment($myImage);
$at->type = 'image/gif';
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding = Zend_Mime::ENCODING_8BIT;
$at->filename = 'test.gif';
$mail->send();
$mail->addAttachment($at);
$mail->send();
Adding Recipients
Recipients can be added in three ways:
Additional parameter
addTo() and addCc() accept a second optional parameter that is used as a human-readable name of the
recipient for the header. Double quotation is changed to single quotation and angle brackets to square brackets
in the parameter.
833
Zend_Mail
Additional Headers
Arbitrary mail headers can be set by using the addHeader() method. It requires two parameters containing the
name and the value of the header field. A third optional parameter determines if the header should have only one or
multiple values:
To set the Reply-To: header there exists the function setReplyTo($email, $name=null), because it requires
additional special escaping of the different parts (email and name).
Character Sets
Zend_Mail does not check for the correct character set of the mail parts. When instantiating Zend_Mail, a charset
for the e-mail itself may be given. It defaults to iso-8859-1. The application has to make sure that all parts added
to that mail object have their content encoded in the correct character set. When creating a new mail part, a different
charset can be given for each part.
Encoding
Text and HTML message bodies are encoded with the quotedprintable mechanism by default. Message headers are
also encoded with the quotedprintable mechanism if you do not specify base64 in setHeaderEncoding(). All
other attachments are encoded via base64 if no other encoding is given in the addAttachment() call or assigned
to the MIME part object later. 7Bit and 8Bit encoding currently only pass on the binary content data.
Header Encoding, especially the encoding of the subject, is a tricky topic. Zend_Mime currently implements
its own algorithm to encode quoted printable headers according to RFC-2045. This due to the problems of
834
Zend_Mail
iconv_mime_encode and mb_encode_mimeheader with regards to certain charsets. This algorithm only
breaks the header at spaces, which might lead to headers that far exceed the suggested length of 76 chars. For this
cases it is suggested to switch to BASE64 header encoding as the following example describes:
// By default Zend_Mime::ENCODING_QUOTEDPRINTABLE
$mail = new Zend_Mail('UTF-8');
Zend_Mail_Transport_Smtp encodes lines starting with one dot or two dots so that the mail does not violate
the SMTP protocol.
SMTP Authentication
Zend_Mail supports the use of SMTP authentication, which can be enabled be passing the 'auth' parameter to
the configuration array in the Zend_Mail_Transport_Smtp constructor. The available built-in authentication
methods are PLAIN, LOGIN and CRAM-MD5 which all expect a 'username' and 'password' value in the configuration
array.
Authentication types
The authentication type is case-insensitive but has no punctuation. E.g. to use CRAM-MD5 you would pass
'auth' => 'crammd5' in the Zend_Mail_Transport_Smtp constructor.
835
Zend_Mail
If you want to read from a Mbox file you only need to give the filename to the constructor of
Zend_Mail_Storage_Mbox:
836
Zend_Mail
For both storages SSL and TLS are supported. If you use SSL the default port changes as given in the RFC.
// use SSL on different port (default is 995 for Pop3 and 993 for Imap)
$mail = new Zend_Mail_Storage_Pop3(array('host' => 'example.com',
'user' => 'test',
'password' => 'test',
'ssl' => 'SSL'));
// use TLS
$mail = new Zend_Mail_Storage_Pop3(array('host' => 'example.com',
'user' => 'test',
'password' => 'test',
'ssl' => 'TLS'));
837
Zend_Mail
$message = $mail->getMessage($messageNum);
Array access is also supported, but this access method won't supported any additional parameters that could be added
to getMessage(). As long as you don't mind, and can live with the default values, you may use:
$message = $mail[$messageNum];
To count the messages in the storage, you can either use the method countMessages() or use array access:
// method
$maxMessage = $mail->countMessages();
// array access
$maxMessage = count($mail);
To remove a mail, you use the method removeMessage() or again array access:
// method
$mail->removeMessage($messageNum);
// array access
unset($mail[$messageNum]);
// check if CC isset:
if( isset($message->cc) ) { // or $message->headerExists('cc');
838
Zend_Mail
$cc = $message->cc;
}
If you have multiple headers with the same name- i.e. the Received headers- you might want an array instead of a
string. In this case, use the getHeader() method.
The method getHeaders() returns all headers as array with the lower-cased name as key and the value as and array
for multiple headers or as string for single headers.
If you don't have a multipart message, fetching the content is easily done via getContent(). Unlike the headers,
the content is only fetched when needed (aka late-fetch).
Checking for multipart messages is done with the method isMultipart(). If you have multipart message you
can get an instance of Zend_Mail_Part with the method getPart(). Zend_Mail_Part is the base class
of Zend_Mail_Message, so you have the same methods: getHeader(), getHeaders(), getContent(),
getPart(), isMultipart and the properties for headers.
839
Zend_Mail
Zend_Mail_Part also implements RecursiveIterator, which makes it easy to scan through all parts. And
for easy output, it also implements the magic method __toString(), which returns the content.
840
Zend_Mail
// ...
// check for other flags
// ...
default:
echo $flag . '(unknown flag) ';
}
}
As IMAP allows user or client defined flags, you could get flags that don't have a constant in Zend_Mail_Storage.
Instead, they are returned as strings and can be checked the same way with hasFlag().
Using folders
All storages, except Pop3, support folders, also called mailboxes. The interface implemented by all storages supporting
folders is called Zend_Mail_Storage_Folder_Interface. Also all of these classes have an additional
optional parameter called folder, which is the folder selected after login, in the constructor.
For the local storages you need to use separate classes called Zend_Mail_Storage_Folder_Mbox or
Zend_Mail_Storage_Folder_Maildir. Both need one parameter called dirname with the name of the base
dir. The format for maildir is as defined in maildir++ (with a dot as default delimiter), Mbox is a directory hierarchy
with Mbox files. If you don't have a Mbox file called INBOX in your Mbox base dir you need to set another folder
in the constructor.
Zend_Mail_Storage_Imap already supports folders by default. Ejemplos for opening these storages:
841
Zend_Mail
With the method getFolders($root = null) you can get the folder hierarchy starting with the root folder or the given
folder. It's returned as an instance of Zend_Mail_Storage_Folder, which implements RecursiveIterator
and all children are also instances of Zend_Mail_Storage_Folder. Each of these instances has a local and a
global name returned by the methods getLocalName() and getGlobalName(). The global name is the absolute
name from the root folder (including delimiters), the local name is the name in the parent folder.
If you use the iterator, the key of the current element is the local name. The global name is also returned by the magic
method __toString(). Some folders may not be selectable, which means they can't store messages and selecting
them results in an error. This can be checked with the method isSelectable(). So it's very easy to output the
whole tree in a view:
842
Zend_Mail
The current selected folder is returned by the method getSelectedFolder(). Changing the folder is done with
the method selectFolder(), which needs the global name as parameter. If you want to avoid to write delimiters
you can also use the properties of a Zend_Mail_Storage_Folder instance:
Advanced Use
Using NOOP
If you're using a remote storage and have some long tasks you might need to keep the connection alive via noop:
Caching instances
Zend_Mail_Storage_Mbox, Zend_Mail_Storage_Folder_Mbox, Zend_Mail_Storage_Maildir
and Zend_Mail_Storage_Folder_Maildir implement the magic methods __sleep() and __wakeup(),
which means they are serializable. This avoids parsing the files or directory tree more than once. The disadvantage
is that your Mbox or Maildir storage should not change. Some easy checks may be done, like reparsing the current
Mbox file if the modification time changes, or reparsing the folder structure if a folder has vanished (which still results
in an error, but you can search for another folder afterwards). It's better if you have something like a signal file for
changes and check it before using the cached instance.
843
Zend_Mail
// do stuff ...
$cache->set($cache_id, $mail);
If you need additional protocol features, you can extend the protocol class and use it in the constructor of the main
class. As an example, assume we need to knock different ports before we can connect to POP3.
844
Zend_Mail
$port = $this->port;
}
parent::connect($host, $port);
}
}
// initialize parent
parent::__construct($protocol);
}
}
As you see, we always assume we're connected, logged in and, if supported, a folder is selected in the constructor
of the main class. Thus if you assign your own protocol class, you always need to make sure that's done or the next
method will fail if the server doesn't allow it in the current state.
There are three methods used for quotas: getQuota(), setQuota() and checkQuota():
845
Zend_Mail
$quota = $mail->checkQuota(true);
echo 'You are ', $quota['over_quota'] ? 'over quota' : 'not over quota', "\n";
echo 'You have ',
$quota['count'],
' of ',
$quota['quota']['count'],
' messages and use ';
echo $quota['size'], ' of ', $quota['quota']['size'], ' octets';
If you want to specify your own quota instead of using the one specified in the maildirsize file you can do with
setQuota():
To add your own quota checks use single letters as keys, and they will be preserved (but obviously not checked).
It's also possible to extend Zend_Mail_Storage_Writable_Maildir to define your own quota only if the
maildirsize file is missing (which can happen in Maildir++):
846
Capítulo 34. Zend_Measure
Introduction
Zend_Measure_* classes provide a generic and easy way for working with measurements. Using
Zend_Measure_* classes, you can convert measurements into different units of the same type. They can be added,
subtracted and compared against each other. From a given input made in the user's native language, the unit of
measurement can be automatically extracted. Numerous units of measurement are supported.
Zend_Measure_* includes support for many different units of measurement. The units of measurement all have
a unified notation: Zend_Measure_<TYPE>::NAME_OF_UNIT, where <TYPE> corresponds to a well-known
physical or numerical property. . Every unit of measurement consists of a conversion factor and a display unit. A
detailed list can be found in the chapter Types of measurements .
Creation of Measurements
When creating a measurement object, Zend_Measure_* methods expect the input/original measurement data value
as the first parameter. This can be a numeric argument , a String without units, or a localized string
with unit(s) specified. The second parameter defines the type of the measurement. Both parameters are mandatory.
The language may optionally be specified as the third parameter.
847
Zend_Measure
$measurement = 1234.7;
$unit = new Zend_Measure_Length((integer)$measurement,
Zend_Measure_Length::STANDARD);
echo $unit;
// outputs '1234 m' (meters)
$mystring = "10010100111010111010100001011011101010001";
$unit = new Zend_Measure_Number($mystring, Zend_Measure_Number::BINARY);
echo $unit;
Usually, Zend_Measure_* can automatically extract the desired measurement embedded in an arbitrary string.
Only the first identifiable number denoted using standard European/Latin digits (0,1,2,3,4,5,6,7,8,9) will be used for
measurement creation. If there are more numerals later in the string, the rest of these numerals will be ignored.
848
Zend_Measure
Since Zend Framework 1.7.0 Zend_Measure does also support the usage of an application wide locale. You can
simply set a Zend_Locale instance to the registry like shown below. With this notation you can forget about setting
the locale manually with each instance when you want to use the same locale multiple times.
Outputting measurements
Measurements can be output in a number of different ways.
Automatic output
Outputting values
Automatic output
Zend_Measure supports outputting of strings automatically.
849
Zend_Measure
echo $unit;
Measurement output
Output can be achieved simply by using echo [http://php.net/echo] or print [http://php.net/print] .
Outputting values
The value of a measurement can be output using getValue().
echo $unit->getValue();
The getValue() method accepts an optional parameter 'round' which allows to define a precision for the generated
output. The standard precision is '2'.
echo $unit->getType();
Manipulating Measurements
Parsing and normalization of input, combined with output to localized notations makes data accessible to users in
different locales. Many additional methods exist in Zend_Measure_* components to manipulate and work with
this data, after it has been normalized.
850
Zend_Measure
• Convert
• Compare to boolean
• Compare to greater/smaller
Convert
Probably the most important feature is the conversion into different units of measurement. The conversion of a unit
can be done any number of times using the method convertTo(). Units of measurement can only be converted to
other units of the same type (class). Therefore, it is not possible to convert (e.g.) a length into a weight, which would
might encourage poor programming practices and allow errors to propagate without exceptions.
The convertTo method accepts an optional parameter. With this parameter you can define an precision for the
returned output. The standard precision is '2'.
print "Kilo:".$unit->convertTo('KILOGRAM');
// Define objects
$unit = new Zend_Measure_Length(200, Zend_Measure_Length::CENTIMETER);
$unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
851
Zend_Measure
Automatic conversion
Adding one object to another will automatically convert it to the correct unit. It is not necessary to call
convertTo() before adding different units.
// Define objects
$unit = new Zend_Measure_Length(200, Zend_Measure_Length::CENTIMETER);
$unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
echo $sum;
Compare
Measurements can also be compared, but without automatic unit conversion. Thus, equals() returns TRUE, only
if both the value and the unit of measure are identical.
// Define measurements
$unit = new Zend_Measure_Length(100, Zend_Measure_Length::CENTIMETER);
$unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
if ($unit->equals($unit2)) {
print "Both measurements are identical";
} else {
print "These are different measurements";
}
// Define measurements
$unit = new Zend_Measure_Length(100, Zend_Measure_Length::CENTIMETER);
$unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
$unit2->setType(Zend_Measure_Length::CENTIMETER);
if ($unit->equals($unit2)) {
print "Both measurements are identical";
} else {
print "These are different measurements";
852
Zend_Measure
Compare
To determine if a measurement is less than or greater than another, use compare(), which returns 0, -1 or 1 depending
on the difference between the two objects. Identical measurements will return 0. Lesser ones will return a negative,
greater ones a positive value.
print "Equal:".$unit2->compare($unit);
print "Lesser:".$unit2->compare($unit3);
print "Greater:".$unit3->compare($unit2);
$unit->setValue(1.2);
echo $unit;
$unit->setValue(1.2, Zend_Measure_Length::KILOMETER);
echo $unit;
$unit->setValue("1.234,56", Zend_Measure_Length::MILLIMETER,$locale);
echo $unit;
$unit->setType(Zend_Measure_Length::KILOMETER);
echo $unit; // outputs "1000 km"
853
Zend_Measure
Types of measurements
All supported measurement types are listed below, each with an example of the standard usage for such measurements.
854
Zend_Measure
855
Zend_Measure
In the class BINARY both notations can be found, such as KILOBYTE = 1024 - binary conputer
conversion KIBIBYTE = 1024 - new notation KILO_BINARY_BYTE = 1024 - new, or the
notation, long format KILOBYTE_SI = 1000 - SI notation for kilo (1000). DVDs for example are
marked with the SI-notation, but almost all harddisks are marked in computer binary notation.
Roman numbers
For the roman number system digits greater 4000 are supported. In reality these digits are shown with a crossbeam on
top of the digit. As the crossbeam can not be shown within the computer, an underline has to be used instead of it.
$great = '_X';
$locale = new Zend_Locale('en');
$unit = new Zend_Measure_Number($great,Zend_Measure_Number::ROMAN, $locale);
856
Capítulo 35. Zend_Memory
Overview
Introduction
The Zend_Memory component is intended to manage data in an environment with limited memory.
Memory objects (memory containers) are generated by memory manager by request and transparently swapped/loaded
when it's necessary.
For example, if creating or loading a managed object would cause the total memory usage to exceed the limit you
specify, some managed objects are copied to cache storage outside of memory. In this way, the total memory used by
managed objects does not exceed the limit you need to enforce.
$backendOptions = array(
'cache_dir' => './tmp/' // Directory where to put the swapped memory blocks
);
$loadedFiles = array();
$loadedFiles[] = $memoryManager->create($data);
}
echo $loadedFiles[$index1]->value;
$loadedFiles[$index2]->value = $newValue;
$loadedFiles[$index3]->value[$charIndex] = '_';
Theory of Operation
Zend_Memory component operates with the following concepts:
• Memory manager
• Memory container
857
Zend_Memory
Memory manager
The memory manager generates memory objects (locked or movable) by request of user application and returns them
wrapped into a memory container object.
Memory container
The memory container has a virtual or actual value attribute of string type. This attribute contains the data value
specified at memory object creation time.
$memObject = $memoryManager->create($data);
echo $memObject->value;
$memObject->value = $newValue;
$memObject->value[$index] = '_';
echo ord($memObject->value[$index1]);
Nota
If you are using a PHP version earlier than 5.2, use the getRef() method instead of accessing the value property
directly.
Locked memory
Locked memory objects are always stored in memory. Data stored in locked memory are never swapped to the cache
backend.
Movable memory
Movable memory objects are transparently swapped and loaded to/from the cache backend by Zend_Memory when
it's necessary.
The memory manager doesn't swap objects with size less than the specified minimum, due to performance
considerations. See the section called “MinSize” for more details.
Memory Manager
Creating a Memory Manager
You can create new a memory manager (Zend_Memory_Manager object) using the
Zend_Memory::factory($backendName [, $backendOprions]) method.
858
Zend_Memory
The first argument $backendName is a string that names one of the backend implementations supported by
Zend_Cache.
$backendOptions = array(
'cache_dir' => './tmp/' // Directory where to put the swapped memory blocks
);
You may use the special name 'None' as a backend name, in addition to standard Zend_Cache backends.
$memoryManager = Zend_Memory::factory('None');
If you use 'None' as the backend name, then the memory manager never swaps memory blocks. This is useful if you
know that memory is not limited or the overall size of objects never reaches the memory limit.
$memObject = $memoryManager->create($data);
The $data argument is optional and used to initialize the object value. If the $data argument is omitted, the value
is an empty string.
$memObject = $memoryManager->createLocked($data);
The $data argument is optional and used to initialize the object value. If the $data argument is omitted, the value
is an empty string.
Destroying Objects
Memory objects are automatically destroyed and removed from memory when they go out of scope:
859
Zend_Memory
function foo()
{
global $memoryManager, $memList;
...
$memObject1 = $memoryManager->create($data1);
$memObject2 = $memoryManager->create($data2);
$memObject3 = $memoryManager->create($data3);
...
$memList[] = $memObject3;
...
...
// $memObject1 is destroyed here
// but $memObject3 object is still referenced by $memList
// and is not destroyed
}
If loading or creation of an object causes memory usage to exceed of this limit, then the memory manager swaps some
other objects.
You can retrieve or set the memory limit setting using the getMemoryLimit() and
setMemoryLimit($newLimit) methods:
The default value is two-thirds of the value of 'memory_limit' in php.ini or 'no limit' (-1) if 'memory_limit' is
not set in php.ini.
MinSize
MinSize is a minimal size of memory objects, which may be swapped by memory manager. The memory manager
does not swap objects that are smaller than this value. This reduces the number of swap/load operations.
You can retrieve or set the minimum size using the getMinSize() and setMinSize($newSize) methods:
860
Zend_Memory
Memory Objects
Movable
Create movable memory objects using the create([$data]) method of the memory manager:
$memObject = $memoryManager->create($data);
"Movable" means that such objects may be swapped and unloaded from memory and then loaded when application
code accesses the object.
Locked
Create locked memory objects using the createLocked([$data]) method of the memory manager:
$memObject = $memoryManager->createLocked($data);
"Locked" means that such objects are never swapped and unloaded from memory.
It's useful if an application or developer can decide, that some objects should never be swapped, based on performance
considerations.
Access to locked objects is faster, because the memory manager doesn't need to track changes for these objects.
The locked objects class (Zend_Memory_Container_Locked) guarantees virtually the same performance as
working with a string variable. The overhead is a single dereference to get the class property.
$memObject = $memoryManager->create($data);
echo $memObject->value;
$memObject->value = $newValue;
$memObject->value[$index] = '_';
echo ord($memObject->value[$index1]);
861
Zend_Memory
An alternative way to access memory object data is to use the getRef() method. This method must be used for PHP
versions before 5.2. It also may have to be used in some other cases for performance reasons.
getRef() method
Movable objects are loaded from the cache at this moment if the object is not already in memory. If the object is loaded
from the cache, this might cause swapping of other objects if the memory limit would be exceeded by having all the
managed objects in memory.
The getRef() method must be used to access memory object data for PHP versions before 5.2.
Tracking changes to data needs additional resources. The getRef() method returns reference to string, which is
changed directly by user application. So, it's a good idea to use the getRef() method for value data processing:
$memObject = $memoryManager->create($data);
$value = &$memObject->getRef();
touch() method
The touch() method should be used in common with getRef(). It signals that object value has been changed:
$memObject = $memoryManager->create($data);
...
$value = &$memObject->getRef();
$memObject->touch();
862
Zend_Memory
lock() method
The lock() methods locks object in memory. It should be used to prevent swapping of some objects you choose.
Normally, this is not necessary, because the memory manager uses an intelligent algorithm to choose candidates for
swapping. But if you exactly know, that at this part of code some objects should not be swapped, you may lock them.
Locking objects in memory also guarantees that reference returned by the getRef() method is valid until you unlock
the object:
$memObject1 = $memoryManager->create($data1);
$memObject2 = $memoryManager->create($data2);
...
$memObject1->lock();
$memObject2->lock();
$value1 = &$memObject1->getRef();
$value2 = &$memObject2->getRef();
$memObject1->touch();
$memObject1->unlock();
$memObject2->unlock();
unlock() method
unlock() method unlocks object when it's no longer necessary to be locked. See the example above.
isLocked() method
The isLocked() method can be used to check if object is locked. It returns TRUE if the object is locked, or FALSE
if it is not locked. This is always TRUE for "locked" objects, and may be either TRUE or FALSE for "movable" objects.
863
864
Capítulo 36. Zend_Mime
Zend_Mime
Introduction
Zend_Mime is a support class for handling multipart MIME messages. It is used by Zend_Mail and
Zend_Mime_Message and may be used by applications requiring MIME support.
• Zend_Mime::isPrintable(): Returns TRUE if the given string contains no unprintable characters, FALSE
otherwise.
• Zend_Mime::TYPE_OCTETSTREAM: 'application/octet-stream'
• Zend_Mime::TYPE_TEXT: 'text/plain'
• Zend_Mime::TYPE_HTML: 'text/html'
• Zend_Mime::ENCODING_7BIT: '7bit'
• Zend_Mime::ENCODING_8BIT: '8bit'
• Zend_Mime::ENCODING_QUOTEDPRINTABLE: 'quoted-printable'
• Zend_Mime::ENCODING_BASE64: 'base64'
• Zend_Mime::DISPOSITION_ATTACHMENT: 'attachment'
• Zend_Mime::DISPOSITION_INLINE: 'inline'
Instantiating Zend_Mime
When Instantiating a Zend_Mime Object, a MIME boundary is stored that is used for all subsequent non-static method
calls on that object. If the constructor is called with a string parameter, this value is used as a MIME boundary. If not,
a random MIME boundary is generated during construction time.
865
Zend_Mime
Zend_Mime_Message
Introduction
Zend_Mime_Message represents a MIME compliant message that can contain one or more separate Parts
(Represented as Zend_Mime_Part objects). With Zend_Mime_Message, MIME compliant multipart messages
can be generated from Zend_Mime_Part objects. Encoding and Boundary handling are handled transparently by
the class. Zend_Mime_Message objects can also be reconstructed from given strings (experimental). Used by
Zend_Mail.
Instantiation
There is no explicit constructor for Zend_Mime_Message.
An array with all Zend_Mime_Part objects in the Zend_Mime_Message is returned from the method -
>getParts(). The Zend_Mime_Part objects can then be changed since they are stored in the array as references.
If parts are added to the array or the sequence is changed, the array needs to be given back to the Zend_Mime_Part
object by calling ->setParts($partsArray).
The function ->isMultiPart() will return true if more than one part is registered with the
Zend_Mime_Message object and thus the object would generate a Multipart-Mime-Message when generating the
actual output.
Boundary handling
Zend_Mime_Message usually creates and uses its own Zend_Mime Object to generate a boundary. If you need
to define the boundary or want to change the behaviour of the Zend_Mime object used by Zend_Mime_Message,
you can instantiate the Zend_Mime object yourself and then register it to Zend_Mime_Message. Usually you will
not need to do this. ->setMime(Zend_Mime $mime) sets a special instance of Zend_Mime to be used by this
Zend_Mime_Message
->getMime() returns the instance of Zend_Mime that will be used to render the message when
generateMessage() is called.
866
Zend_Mime
Zend_Mime_Part
Introduction
This class represents a single part of a MIME message. It contains the actual content of the message part plus
information about its encoding, content type and original filename. It provides a method for generating a string from the
stored data. Zend_Mime_Part objects can be added to Zend_Mime_Message to assemble a complete multipart
message.
Instantiation
Zend_Mime_Part is instantiated with a string that represents the content of the new part. The type is assumed to
be OCTET-STREAM, encoding is 8Bit. After instantiating a Zend_Mime_Part, meta information can be set by
accessing its attributes directly:
getHeaders() returns the Mime-Headers for the MimePart as generated from the information in the publicly
accessible attributes. The attributes of the object need to be set correctly before this method is called.
• $charset has to be set to the actual charset of the content if it is a text type (Text or HTML).
• $id may be set to identify a content-id for inline images in a HTML mail.
• $filename contains the name the file will get when downloading it.
• $disposition defines if the file should be treated as an attachment or if it is used inside the (HTML-) mail
(inline).
• $location can be used as resource URI that has relation to the content.
867
868
Capítulo 37. Zend_Navigation
Introduction
Zend_Navigation is a component for managing trees of pointers to web pages. Simply put: It can be used for
creating menus, breadcrumbs, links, and sitemaps, or serve as a model for other navigation related purposes.
Pages
A page (Zend_Navigation_Page) in Zend_Navigation – in its most basic form – is an object that holds a
pointer to a web page. In addition to the pointer itself, the page object contains a number of other properties that are
typically relevant for navigation, such as label, title, etc.
Containers
A navigation container (Zend_Navigation_Container) is a container class for pages. It has methods for adding,
retrieving, deleting and iterating pages. It implements the SPL [http://php.net/spl] interfaces RecursiveIterator
and Countable, and can thus be iterated with SPL iterators such as RecursiveIteratorIterator.
Nota
Zend_Navigation_Page extends Zend_Navigation_Container, which means that a page can
have sub pages.
Read more about rendering navigational elements in the manual section on navigation helpers.
Pages
Zend_Navigation ships with two page types:
MVC pages are link to on-site web pages, and are defined using MVC parameters (action, controller, module,
route, params). URI pages are defined by a single property uri, which give you the full flexibility to link off-site
pages or do other things with the generated links (e.g. an URI that turns into <a href="#">foo<a>).
869
Zend_Navigation
Option keys are mapped to set methods. This means that the option order maps to the method setOrder(), and
reset_params maps to the method setResetParams(). If there is no setter method for the option, it will be
set as a custom property of the page.
870
Zend_Navigation
871
Zend_Navigation
Custom properties
All pages support setting and getting of custom properties by use of the magic methods __set($name,
$value), __get($name), __isset($name) and __unset($name). Custom properties may have
any value, and will be included in the array that is returned from $page->toArray(), which means that
pages can be serialized/deserialized successfully even if the pages contains properties that are not native in
the page class.
Both native and custom properties can be set using $page->set($name, $value) and retrieved using
$page->get($name), or by using magic methods.
echo $page->foo;
if ($page->meaning != 42) {
// action should be taken
}
Zend_Navigation_Page_Mvc
MVC pages are defined using MVC parameters known from the Zend_Controller component. An MVC page
will use Zend_Controller_Action_Helper_Url internally in the getHref() method to generate hrefs, and
the isActive() method will intersect the Zend_Controller_Request_Abstract params with the page's
params to determine if the page is active.
Nota
The three examples below assume a default MVC setup with the default route in place.
872
Zend_Navigation
The URI returned is relative to the baseUrl in Zend_Controller_Front. In the examples, the baseUrl
is '/' for simplicity.
// getHref() returns /
$page = new Zend_Navigation_Page_Mvc(array(
'action' => 'index',
'controller' => 'index'
));
/*
* Dispatched request:
* - module: default
* - controller: index
* - action: index
*/
$page1 = new Zend_Navigation_Page_Mvc(array(
'action' => 'index',
'controller' => 'index'
));
873
Zend_Navigation
/*
* Dispatched request:
* - module: blog
* - controller: post
* - action: view
* - id: 1337
*/
$page = new Zend_Navigation_Page_Mvc(array(
'action' => 'view',
'controller' => 'post',
'module' => 'blog'
));
// returns true, because request has the same module, controller and action
$page->isActive();
/*
* Dispatched request:
* - module: blog
* - controller: post
* - action: view
*/
$page = new Zend_Navigation_Page_Mvc(array(
'action' => 'view',
'controller' => 'post',
'module' => 'blog',
'params' => array('id' => null)
));
// returns false, because page requires the id param to be set in the request
$page->isActive(); // returns false
Nota
Note that when using the route property in a page, you should also specify the default params that the route
defines (module, controller, action, etc.), otherwise the isActive() method will not be able to determine
if the page is active. The reason for this is that there is currently no way to get the default params from
a Zend_Controller_Router_Route_Interface object, nor to retrieve the current route from a
Zend_Controller_Router_Interface object.
874
Zend_Navigation
// returns: /a/42
$page->getHref();
Zend_Navigation_Page_Uri
Pages of type Zend_Navigation_Page_Uri can be used to link to pages on other domains or sites, or to
implement custom logic for the page. URI pages are simple; in addition to the common page options, a URI page takes
only one option — uri. The uri will be returned when calling $page->getHref(), and may be a String or NULL.
Nota
Zend_Navigation_Page_Uri will not try to determine whether it should be active when calling
$page->isActive(). It merely returns what currently is set, so to make a URI page active you have to
manually call $page->setActive() or specifying active as a page option when constructing.
875
Zend_Navigation
// ...or
$page = Zend_Navigation_Page::factory(array(
'type' => 'My_Navigation_Page',
'label' => 'Property names are mapped to setters',
'foo' => 'bar',
876
Zend_Navigation
If type is given, the factory will assume the value to be the name of the class that should be created. If the value is
mvc or uri and MVC/URI page will be created.
$page = Zend_Navigation_Page::factory(array(
'label' => 'My MVC page',
'action' => 'index'
));
$page = Zend_Navigation_Page::factory(array(
'label' => 'Search blog',
'action' => 'index',
'controller' => 'search',
'module' => 'blog'
));
$page = Zend_Navigation_Page::factory(array(
'label' => 'Home',
'action' => 'index',
'controller' => 'index',
'module' => 'index',
'route' => 'home'
));
$page = Zend_Navigation_Page::factory(array(
'type' => 'mvc',
'label' => 'My MVC page'
));
$page = Zend_Navigation_Page::factory(array(
'label' => 'My URI page',
'uri' => 'http://www.example.com/'
));
$page = Zend_Navigation_Page::factory(array(
'label' => 'Search',
'uri' => 'http://www.example.com/search',
'active' => true
877
Zend_Navigation
));
$page = Zend_Navigation_Page::factory(array(
'label' => 'My URI page',
'uri' => '#'
));
$page = Zend_Navigation_Page::factory(array(
'type' => 'uri',
'label' => 'My URI page'
));
Ejemplo 37.9. Creating a custom page type using the page factory
To create a custom page type using the factory, use the option type to specify a class name to instantiate.
$page = Zend_Navigation_Page::factory(array(
'type' => 'My_Navigation_Page',
'label' => 'My custom page',
'foo_bar' => 'foo bar'
));
Containers
Containers have methods for adding, retrieving, deleting and iterating pages. Containers implement the SPL [http://
php.net/spl] interfaces RecursiveIterator and Countable, meaning that a container can be iterated using the
SPL RecursiveIteratorIterator class.
Creating containers
Zend_Navigation_Container is abstract, and can not be instantiated directly. Use Zend_Navigation if
you want to instantiate a container.
Zend_Navigation can be constructed entirely empty, or take an array or a Zend_Config object with pages
to put in the container. Each page in the given array/config will eventually be passed to the addPage() method
of the container class, which means that each element in the array/config can be an array or a config object, or a
Zend_Navigation_Page instance.
/*
* Create a container from an array
878
Zend_Navigation
*
* Each element in the array will be passed to
* Zend_Navigation_Page::factory() when constructing.
*/
$container = new Zend_Navigation(array(
array(
'label' => 'Page 1',
'id' => 'home-link',
'uri' => '/'
),
array(
'label' => 'Zend',
'uri' => 'http://www.zend-project.com/',
'order' => 100
),
array(
'label' => 'Page 2',
'controller' => 'page2',
'pages' => array(
array(
'label' => 'Page 2.1',
'action' => 'page2_1',
'controller' => 'page2',
'class' => 'special-one',
'title' => 'This element has a special class',
'active' => true
),
array(
'label' => 'Page 2.2',
'action' => 'page2_2',
'controller' => 'page2',
'class' => 'special-two',
'title' => 'This element has a special class too'
)
)
),
array(
'label' => 'Page 2 with params',
'action' => 'index',
'controller' => 'page2',
// specify a param or two
'params' => array(
'format' => 'json',
'foo' => 'bar'
)
),
array(
'label' => 'Page 2 with params and a route',
'action' => 'index',
'controller' => 'page2',
// specify a route name and a param for the route
'route' => 'nav-route-example',
'params' => array(
'format' => 'json'
879
Zend_Navigation
)
),
array(
'label' => 'Page 3',
'action' => 'index',
'controller' => 'index',
'module' => 'mymodule',
'reset_params' => false
),
array(
'label' => 'Page 4',
'uri' => '#',
'pages' => array(
array(
'label' => 'Page 4.1',
'uri' => '/page4',
'title' => 'Page 4 using uri',
'pages' => array(
array(
'label' => 'Page 4.1.1',
'title' => 'Page 4 using mvc params',
'action' => 'index',
'controller' => 'page4',
// let's say this page is active
'active' => '1'
)
)
)
)
),
array(
'label' => 'Page 0?',
'uri' => '/setting/the/order/option',
// setting order to -1 should make it appear first
'order' => -1
),
array(
'label' => 'Page 5',
'uri' => '/',
// this page should not be visible
'visible' => false,
'pages' => array(
array(
'label' => 'Page 5.1',
'uri' => '#',
'pages' => array(
array(
'label' => 'Page 5.1.1',
'uri' => '#',
'pages' => array(
array(
'label' => 'Page 5.1.2',
'uri' => '#',
// let's say this page is active
880
Zend_Navigation
881
Zend_Navigation
)
));
/* CONTENTS OF /path/to/navigation.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<nav>
<zend>
<label>Zend</label>
<uri>http://www.zend-project.com/</uri>
<order>100</order>
</zend>
<page1>
<label>Page 1</label>
<uri>page1</uri>
<pages>
<page1_1>
<label>Page 1.1</label>
<uri>page1/page1_1</uri>
</page1_1>
</pages>
</page1>
<page2>
<label>Page 2</label>
<uri>page2</uri>
<pages>
<page2_1>
<label>Page 2.1</label>
<uri>page2/page2_1</uri>
</page2_1>
<page2_2>
<label>Page 2.2</label>
<uri>page2/page2_2</uri>
<pages>
<page2_2_1>
<label>Page 2.2.1</label>
<uri>page2/page2_2/page2_2_1</uri>
</page2_2_1>
<page2_2_2>
<label>Page 2.2.2</label>
<uri>page2/page2_2/page2_2_2</uri>
<active>1</active>
882
Zend_Navigation
</page2_2_2>
</pages>
</page2_2>
<page2_3>
<label>Page 2.3</label>
<uri>page2/page2_3</uri>
<pages>
<page2_3_1>
<label>Page 2.3.1</label>
<uri>page2/page2_3/page2_3_1</uri>
</page2_3_1>
<page2_3_2>
<label>Page 2.3.2</label>
<uri>page2/page2_3/page2_3_2</uri>
<visible>0</visible>
<pages>
<page2_3_2_1>
<label>Page 2.3.2.1</label>
<uri>page2/page2_3/page2_3_2/1</uri>
<active>1</active>
</page2_3_2_1>
<page2_3_2_2>
<label>Page 2.3.2.2</label>
<uri>page2/page2_3/page2_3_2/2</uri>
<active>1</active>
<pages>
<page_2_3_2_2_1>
<label>Ignore</label>
<uri>#</uri>
<active>1</active>
</page_2_3_2_2_1>
</pages>
</page2_3_2_2>
</pages>
</page2_3_2>
<page2_3_3>
<label>Page 2.3.3</label>
<uri>page2/page2_3/page2_3_3</uri>
<resource>admin</resource>
<pages>
<page2_3_3_1>
<label>Page 2.3.3.1</label>
<uri>page2/page2_3/page2_3_3/1</uri>
<active>1</active>
883
Zend_Navigation
</page2_3_3_1>
<page2_3_3_2>
<label>Page 2.3.3.2</label>
<uri>page2/page2_3/page2_3_3/2</uri>
<resource>guest</resource>
<active>1</active>
</page2_3_3_2>
</pages>
</page2_3_3>
</pages>
</page2_3>
</pages>
</page2>
<page3>
<label>Page 3</label>
<uri>page3</uri>
<pages>
<page3_1>
<label>Page 3.1</label>
<uri>page3/page3_1</uri>
<resource>guest</resource>
</page3_1>
<page3_2>
<label>Page 3.2</label>
<uri>page3/page3_2</uri>
<resource>member</resource>
<pages>
<page3_2_1>
<label>Page 3.2.1</label>
<uri>page3/page3_2/page3_2_1</uri>
</page3_2_1>
<page3_2_2>
<label>Page 3.2.2</label>
<uri>page3/page3_2/page3_2_2</uri>
<resource>admin</resource>
</page3_2_2>
</pages>
</page3_2>
<page3_3>
<label>Page 3.3</label>
<uri>page3/page3_3</uri>
<resource>special</resource>
<pages>
884
Zend_Navigation
<page3_3_1>
<label>Page 3.3.1</label>
<uri>page3/page3_3/page3_3_1</uri>
<visible>0</visible>
</page3_3_1>
<page3_3_2>
<label>Page 3.3.2</label>
<uri>page3/page3_3/page3_3_2</uri>
<resource>admin</resource>
</page3_3_2>
</pages>
</page3_3>
</pages>
</page3>
<home>
<label>Home</label>
<order>-100</order>
<module>default</module>
<controller>index</controller>
<action>index</action>
</home>
</nav>
</config>
*/
Adding pages
Adding pages to a container can be done with the methods addPage(), addPages(), or setPages(). See
examples below for explanation.
// create container
$container = new Zend_Navigation();
885
Zend_Navigation
$pages = array(
array(
'label' => 'Save'
'action' => 'save',
),
array(
'label' => 'Delete',
'action' => 'delete'
)
);
Removing pages
Removing pages can be done with removePage() or removePages(). The first method accepts a an instance
of a page, or an integer. The integer corresponds to the order a page has. The latter method will remove all pages
in the container.
886
Zend_Navigation
Finding pages
Containers have finder methods for retrieving pages. They are findOneBy($property, $value),
findAllBy($property, $value), and findBy($property, $value, $all = false). Those
methods will recursively search the container for pages matching the given $page->$property == $value.
The first method, findOneBy(), will return a single page matching the property with the given value, or null if it
cannot be found. The second method will return all pages with a property matching the given value. The third method
will call one of the two former methods depending on the $all flag.
The finder methods can also be used magically by appending the property name to findBy, findOneBy,
or findAllBy, e.g. findOneByLabel('Home') to return the first matching page with label Home. Other
combinations are findByLabel(...), findOnyByTitle(...), findAllByController(...), etc.
Finder methods also work on custom properties, such as findByFoo('bar').
887
Zend_Navigation
Iterating containers
Zend_Navigation_Container implements RecursiveIteratorIterator, and can be iterated using any
Iterator class. To iterate a container recursively, use the RecursiveIteratorIterator class.
888
Zend_Navigation
/*
* Create a container from an array
*/
$container = new Zend_Navigation(array(
array(
'label' => 'Page 1',
'uri' => '#'
),
array(
'label' => 'Page 2',
'uri' => '#',
'pages' => array(
array(
'label' => 'Page 2.1',
'uri' => '#'
),
array(
'label' => 'Page 2.2',
'uri' => '#'
)
)
)
array(
'label' => 'Page 3',
'uri' => '#'
)
));
Other operations
The method hasPage(Zend_Navigation_Page $page) checks if the container has the given page. The
method hasPages() checks if there are any pages in the container, and is equivalent to count($container)
> 1.
The toArray() method converts the container and the pages in it to an array. This can be useful for serializing
and debugging.
889
Zend_Navigation
var_dump($container->toArray());
/* Output:
array(2) {
[0]=> array(15) {
["label"]=> string(6) "Page 1"
["id"]=> NULL
["class"]=> NULL
["title"]=> NULL
["target"]=> NULL
["rel"]=> array(0) {
}
["rev"]=> array(0) {
}
["order"]=> NULL
["resource"]=> NULL
["privilege"]=> NULL
["active"]=> bool(false)
["visible"]=> bool(true)
["type"]=> string(23) "Zend_Navigation_Page_Uri"
["pages"]=> array(0) {
}
["uri"]=> string(1) "#"
}
[1]=> array(15) {
["label"]=> string(6) "Page 2"
["id"]=> NULL
["class"]=> NULL
["title"]=> NULL
["target"]=> NULL
890
Zend_Navigation
["rel"]=> array(0) {
}
["rev"]=> array(0) {
}
["order"]=> NULL
["resource"]=> NULL
["privilege"]=> NULL
["active"]=> bool(false)
["visible"]=> bool(true)
["type"]=> string(23) "Zend_Navigation_Page_Uri"
["pages"]=> array(2) {
[0]=> array(15) {
["label"]=> string(8) "Page 2.1"
["id"]=> NULL
["class"]=> NULL
["title"]=> NULL
["target"]=> NULL
["rel"]=> array(0) {
}
["rev"]=> array(0) {
}
["order"]=> NULL
["resource"]=> NULL
["privilege"]=> NULL
["active"]=> bool(false)
["visible"]=> bool(true)
["type"]=> string(23) "Zend_Navigation_Page_Uri"
["pages"]=> array(0) {
}
["uri"]=> string(1) "#"
}
[1]=>
array(15) {
["label"]=> string(8) "Page 2.2"
["id"]=> NULL
["class"]=> NULL
["title"]=> NULL
["target"]=> NULL
["rel"]=> array(0) {
}
["rev"]=> array(0) {
}
["order"]=> NULL
["resource"]=> NULL
["privilege"]=> NULL
["active"]=> bool(false)
["visible"]=> bool(true)
["type"]=> string(23) "Zend_Navigation_Page_Uri"
["pages"]=> array(0) {
}
["uri"]=> string(1) "#"
}
}
["uri"]=> string(1) "#"
891
Zend_Navigation
}
}
*/
892
Capítulo 38. Zend_OpenId
Introduction
Zend_OpenId is a Zend Framework component that provides a simple API for building OpenID-enabled sites and
identity providers.
What is OpenID?
OpenID is a set of protocols for user-centric digital identities. These protocols allows users to create an identity online,
using an identity provider. This identity can be used on any site that supports OpenID. Using OpenID-enabled sites,
users do not need to remember traditional authentication tokens such as usernames and passwords for each site. All
OpenID-enabled sites accept a single OpenID identity. This identity is typically a URL. It may be the URL of the
user's personal page, blog or other resource that may provide additional information about them. That mean a user
needs just one identifier for all sites he or she uses. services. OpenID is an open, decentralized, and free user-centric
solution. Users may choose which OpenID provider to use, or even create their own personal identity server. No central
authority is required to approve or register OpenID-enabled sites or identity providers.
For more information about OpenID visit the OpenID official site [http://www.openid.net/].
1. Authentication is initiated by the end user, who passes their OpenID identifier to the OpenID consumer through
a User-Agent.
2. The OpenID consumer performs normalization and discovery on the user-supplied identifier. Through this process,
the consumer obtains the claimed identifier, the URL of the OpenID provider and an OpenID protocol version.
3. The OpenID consumer establishes an optional association with the provider using Diffie-Hellman keys. As a result,
both parties have a common "shared secret" that is used for signing and verification of the subsequent messages.
4. The OpenID consumer redirects the User-Agent to the URL of the OpenID provider with an OpenID authentication
request.
5. The OpenID provider checks if the User-Agent is already authenticated and, if not, offers to do so.
7. The OpenID provider checks if it is allowed to pass the user identity to the given consumer, and asks the user if
necessary.
893
Zend_OpenId
9. The OpenID Provider redirects the User-Agent back to the OpenID consumer with an "authentication approved"
or "failed" request.
10.The OpenID consumer verifies the information received from the provider by using the shared secret it got in step
3 or by sending an additional direct request to the OpenID provider.
Zend_OpenId Structure
Zend_OpenId consists of two sub-packages. The first one is Zend_OpenId_Consumer for developing OpenID-
enabled sites, and the second is Zend_OpenId_Provider for developing OpenID servers. They are completely
independent of each other and may be used separately.
The only common code used by these sub-packages are the OpenID Simple Registration Extension implemented by
Zend_OpenId_Extension_Sreg class and a set of utility functions implemented by the Zend_OpenId class.
Nota
Zend_OpenId takes advantage of the GMP extension [http://php.net/gmp], where available. Consider
enabling the GMP extension for enhanced performance when using Zend_OpenId.
Zend_OpenId_Consumer Basics
Zend_OpenId_Consumer can be used to implement OpenID authentication for web sites.
OpenID Authentication
From a web site developer's point of view, the OpenID authentication process consists of three steps:
The OpenID authentication protocol actually requires more steps, but many of them are encapsulated inside
Zend_OpenId_Consumer and are therefore transparent to the developer.
The end user initiates the OpenID authentication process by submitting his or her identification credentials with the
appropriate form. The following example shows a simple form that accepts an OpenID identifier. Note that the example
only demonstrates a login.
894
Zend_OpenId
<html><body>
<form method="post" action="example-1_2.php"><fieldset>
<legend>OpenID Login</legend>
<input type="text" name="openid_identifier">
<input type="submit" name="openid_action" value="login">
</fieldset></form></body></html>
This form passes the OpenID identity on submission to the following PHP script that performs the second step of
authentication. The PHP script need only call the Zend_OpenId_Consumer::login() method in this step. The
first argument of this method is an accepted OpenID identity, and the second is the URL of a script that handles the
third and last step of authentication.
An OpenID provider usually asks users for their password (if they weren't previously logged-in), whether the user
trusts this site and what information may be returned to the site. These interactions are not visible to the OpenID
consumer, so it can not obtain the user's password or other information that the user did not has not directed the OpenID
provider to share with it.
The third step of authentication is initiated by the response from the OpenID provider, after it has authenticated the
user's password. This response is passed indirectly, as an HTTP redirection using the end user's web browser. The
consumer must now simply check that this response is valid.
This check is performed using the Zend_OpenId_Consumer::verify method, which takes an array of the HTTP
request's arguments and checks that this response is properly signed by the OpenID provider. It may assign the claimed
OpenID identity that was entered by end user in the first step using a second, optional argument.
895
Zend_OpenId
<?php
$status = "";
if (isset($_POST['openid_action']) &&
$_POST['openid_action'] == "login" &&
!empty($_POST['openid_identifier'])) {
In addition, this code differentiates between cancelled and invalid authentication responses. The provider returns a
cancelled response if the identity provider is not aware of the supplied identity, the user is not logged in, or the user
doesn't trust the site. An invalid response indicates that the response is not conformant to the OpenID protocol or is
incorrectly signed.
Consumer Realm
When an OpenID-enabled site passes authentication requests to a provider, it identifies itself with a realm URL. This
URL may be considered a root of a trusted site. If the user trusts the realm URL, he or she should also trust matched
and subsequent URLs.
896
Zend_OpenId
By default, the realm URL is automatically set to the URL of the directory in which the login script resides. This
default value is useful for most, but not all, cases. Sometimes an entire domain, and not a directory should be trusted.
Or even a combination of several servers in one domain.
To override the default value, developers may pass the realm URL as a third argument to the
Zend_OpenId_Consumer::login method. In the following example, a single interaction asks for trusted access
to all php.net sites.
This example implements only the second step of authentication; the first and third steps are similar to the examples
above.
Immediate Check
In some cases, an application need only check if a user is already logged in to a trusted OpenID server without any
interaction with the user. The Zend_OpenId_Consumer::check method does precisely that. It is executed with
the same arguments as Zend_OpenId_Consumer::login, but it doesn't display any OpenID server pages to the
user. From the users point of view this process is transparent, and it appears as though they never left the site. The
third step succeeds if the user is already logged in and trusted by the site, otherwise it will fail.
This example implements only the second step of authentication; the first and third steps are similar to the examples
above.
Zend_OpenId_Consumer_Storage
There are three steps in the OpenID authentication procedure, and each step is performed by a separate HTTP request.
To store information between requests, Zend_OpenId_Consumer uses internal storage.
Developers do not necessarily have to be aware of this storage because by default Zend_OpenId_Consumer uses
file-based storage under the temporary directory- similar to PHP sessions. However, this storage may be not suitable
in all cases. Some developers may want to store information in a database, while others may need to use common
storage suitable for server farms. Fortunately, developers may easily replace the default storage with their own. To
specify a custom storage mechanism, one need only extend the Zend_OpenId_Consumer_Storage class and
pass this subclass to the Zend_OpenId_Consumer constructor in the first argument.
The following example demonstrates a simple storage mechanism that uses Zend_Db as its backend and exposes
three groups of functions. The first group contains functions for working with associations, while the second group
897
Zend_OpenId
caches discovery information, and the third group can be used to check whether a response is unique. This class can
easily be used with existing or new databases; if the required tables don't exist, it will create them.
898
Zend_OpenId
899
Zend_OpenId
$url = $res['url'];
$macFunc = $res['macFunc'];
$secret = base64_decode($res['secret']);
$expires = $res['expires'];
return true;
}
return false;
}
900
Zend_OpenId
$table = $this->_discovery_table;
$this->_db->query("delete from $table where id = '$id'");
return true;
}
$db = Zend_Db::factory('Pdo_Sqlite',
array('dbname'=>'/tmp/openid_consumer.db'));
$storage = new DbStorage($db);
$consumer = new Zend_OpenId_Consumer($storage);
This example doesn't list the OpenID authentication code itself, but this code would be the same as that for other
examples in this chapter. examples.
• nickname - any UTF-8 string that the end user uses as a nickname
• email - the email address of the user as specified in section 3.4.1 of RFC2822
• dob - the user's date of birth in the format 'YYYY-MM-DD'. Any values whose representation uses fewer than the
specified number of digits in this format should be zero-padded. In other words, the length of this value must always
be 10. If the end user does not want to reveal any particular part of this value (i.e., year, month or day), it must be
set to zero. For example, if the user wants to specify that his date of birth falls in 1980, but not specify the month
or day, the value returned should be '1980-00-00'.
• gender - the user's gender: "M" for male, "F" for female
• postcode - a UTF-8 string that conforms to the postal system of the user's country
901
Zend_OpenId
• timezone - an ASCII string from a TimeZone database. For example, "Europe/Paris" or "America/Los_Angeles".
An OpenID-enabled web site may ask for any combination of these fields. It may also strictly require some
information and allow users to provide or hide additional information. The following example instantiates the
Zend_OpenId_Extension_Sreg class, requiring a nickname and optionally requests an email and a fullname.
As you can see, the Zend_OpenId_Extension_Sreg constructor accepts an array of OpenID fields. This array
has the names of fields as indexes to a flag indicating whether the field is required; true means the field is required
and false means the field is optional. The Zend_OpenId_Consumer::login method accepts an extension or an
array of extensions as its fourth argument.
902
Zend_OpenId
If the Zend_OpenId_Extension_Sreg object was created without any arguments, the user code should
check for the existence of the required data itself. However, if the object is created with the same list of
required fields as on the second step, it will automatically check for the existence of required data. In this case,
Zend_OpenId_Consumer::verify will return false if any of the required fields are missing.
Zend_OpenId_Extension_Sreg uses version 1.0 by default, because the specification for version 1.1 is not
yet finalized. However, some libraries don't fully support version 1.0. For example, www.myopenid.com requires an
SREG namespace in requests which is only available in 1.1. To work with such a server, you must explicitly set the
version to 1.1 in the Zend_OpenId_Extension_Sreg constructor.
The second argument of the Zend_OpenId_Extension_Sreg constructor is a policy URL, that should be
provided to the user by the identity provider.
The big difference between this adapter and existing ones, is that it works on two HTTP requests and includes a
dispatch code to perform the second or third step of OpenID authentication.
<?php
class OpenIdAdapter implements Zend_Auth_Adapter_Interface {
private $_id = null;
903
Zend_OpenId
}
return new Zend_Auth_Result($ret, $id, array($msg));
}
}
$status = "";
$auth = Zend_Auth::getInstance();
if ((isset($_POST['openid_action']) &&
$_POST['openid_action'] == "login" &&
!empty($_POST['openid_identifier'])) ||
isset($_GET['openid_mode'])) {
$adapter = new OpenIdAdapter(@$_POST['openid_identifier']);
$result = $auth->authenticate($adapter);
if ($result->isValid()) {
Zend_OpenId::redirect(Zend_OpenId::selfURL());
} else {
$auth->clearIdentity();
foreach ($result->getMessages() as $message) {
$status .= "$message<br>\n";
}
}
} else if ($auth->hasIdentity()) {
if (isset($_POST['openid_action']) &&
$_POST['openid_action'] == "logout") {
$auth->clearIdentity();
} else {
$status = "You are logged in as " . $auth->getIdentity() . "<br>\n";
}
}
?>
<html><body>
<?php echo htmlspecialchars($status);?>
<form method="post"><fieldset>
<legend>OpenID Login</legend>
<input type="text" name="openid_identifier" value="">
<input type="submit" name="openid_action" value="login">
<input type="submit" name="openid_action" value="logout">
</fieldset></form></body></html>
With Zend_Auth the end-user's identity is saved in the session's data. It may be checked with
Zend_Auth::hasIdentity and Zend_Auth::getIdentity.
Zend_OpenId_Consumer doesn't provide any GUI capabilities but it performs HTTP redirections on
success of Zend_OpenId_Consumer::login and Zend_OpenId_Consumer::check. These redirections
may work incorrectly or not at all if some data was already sent to the web browser. To properly
perform HTTP redirection in MVC code the real Zend_Controller_Response_Http should be sent to
Zend_OpenId_Consumer::login or Zend_OpenId_Consumer::check as the last argument.
904
Zend_OpenId
Zend_OpenId_Provider
Zend_OpenId_Provider can be used to implement OpenID servers. This chapter provides examples that
demonstrate how to build a very basic server. However, for implementation of a production OpenID server (such as
www.myopenid.com [http://www.myopenid.com]) you may have to deal with more complex issues.
Quick Start
The following example includes code for creating a user account using Zend_OpenId_Provider::register.
The link element with rel="openid.server" points to our own server script. If you submit this identity to an
OpenID-enabled site, it will perform authentication on this server.
The code before the <html> tag is just a trick that automatically creates a user account. You won't need such code
when using real identities.
<?php
// Set up test identity
define("TEST_SERVER", Zend_OpenId::absoluteURL("example-8.php"));
define("TEST_ID", Zend_OpenId::selfURL());
define("TEST_PASSWORD", "123");
$server = new Zend_OpenId_Provider();
if (!$server->hasUser(TEST_ID)) {
$server->register(TEST_ID, TEST_PASSWORD);
}
?>
<html><head>
<link rel="openid.server" href="<?php echo TEST_SERVER;?>" />
</head><body>
<?php echo TEST_ID;?>
</body></html>
The following identity server script handles two kinds of requests from OpenID-enabled sites (for association and
authentication). Both of them are handled by the same method: Zend_OpenId_Provider::handle. The two
arguments to the Zend_OpenId_Provider constructor are URLs of login and trust pages, which ask for input
from the end user.
On success, the method Zend_OpenId_Provider::handle returns a string that should be passed back
to the OpenID-enabled site. On failure, it returns FALSE. This example will return an HTTP 403 response if
Zend_OpenId_Provider::handle fails. You will get this response if you open this script with a web browser,
because it sends a non-OpenID conforming request.
905
Zend_OpenId
echo 'Forbidden';
}
Nota
It is a good idea to use a secure connection (HTTPS) for these scripts- especially for the following interactive
scripts- to prevent password disclosure.
The following script implements a login screen for an identity server using Zend_OpenId_Provider and redirects
to this page when a required user has not yet logged in. On this page, a user will enter his password to login.
You should use the password "123" that was used in the identity script above.
On submit, the script calls Zend_OpenId_Provider::login with the accepted user's identity and password,
then redirects back to the main identity provider's script. On success, the Zend_OpenId_Provider::login
establishes a session between the user and the identity provider and stores the information about the user, who is now
logged in. All following requests from the same user won't require a login procedure- even if they come from another
OpenID enabled web site.
Nota
Note that this session is between end-user and identity provider only. OpenID enabled sites know nothing
about it.
<?php
$server = new Zend_OpenId_Provider();
906
Zend_OpenId
<td>Password:</td>
<td>
<input type="text"
name="openid_password"
value="">
</td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit"
name="openid_action"
value="login">
</td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>
The fact that the user is now logged in doesn't mean that the authentication must necessarily succeed. The user may
decide not to trust particular OpenID enabled sites. The following trust screen allows the end user to make that choice.
This choice may either be made only for current requests or forever. In the second case, information about trusted/
untrusted sites is stored in an internal database, and all following authentication requests from this site will be handled
automatically without user interaction.
<?php
$server = new Zend_OpenId_Provider();
if (isset($_POST['allow'])) {
if (isset($_POST['forever'])) {
$server->allowSite($server->getSiteRoot($_GET));
}
$server->respondToConsumer($_GET);
} else if (isset($_POST['deny'])) {
if (isset($_POST['forever'])) {
$server->denySite($server->getSiteRoot($_GET));
}
Zend_OpenId::redirect($_GET['openid_return_to'],
array('openid.mode'=>'cancel'));
}
}
?>
<html>
<body>
<p>A site identifying as
907
Zend_OpenId
Production OpenID servers usually support the Simple Registration Extension that allows consumers to request some
information about the user from the provider. In this case, the trust page can be extended to allow entering requested
fields or selecting a specific user profile.
Nota
The following example is not complete. It doesn't provide GUI code for the user, instead performing an
automatic login and trust relationship instead. This is done just to simplify the example; a production server
should include some code from previous examples.
define("TEST_ID", Zend_OpenId::absoluteURL("example-9-id.php"));
define("TEST_PASSWORD", "123");
908
Zend_OpenId
} else {
$ret = $server->handle();
if (is_string($ret)) {
echo $ret;
} else if ($ret !== true) {
header('HTTP/1.0 403 Forbidden');
echo 'Forbidden';
}
}
If you compare this example with previous examples split in to separate pages, you will see only the one difference
besides the dispatch code: unset($_GET['openid_action']). This call to unset is necessary to route the
next request to main handler.
<?php
define("TEST_SERVER", Zend_OpenId::absoluteURL("example-10.php"));
define("TEST_ID", Zend_OpenId::selfURL());
define("TEST_PASSWORD", "123");
$server = new Zend_OpenId_Provider();
if (!$server->hasUser(TEST_ID)) {
$server->register(TEST_ID, TEST_PASSWORD);
$server->login(TEST_ID, TEST_PASSWORD);
$sreg = new Zend_OpenId_Extension_Sreg(array(
'nickname' =>'test',
'email' => 'test@test.com'
));
$root = Zend_OpenId::absoluteURL(".");
Zend_OpenId::normalizeUrl($root);
$server->allowSite($root, $sreg);
$server->logout();
}
?>
<html>
<head>
<link rel="openid.server" href="<?php echo TEST_SERVER;?>" />
</head>
<body>
<?php echo TEST_ID;?>
</body>
</html>
You should now pass this identity to the OpenID-enabled web site (use the Simple Registration Extension example
from the previous section), and it should use the following OpenID server script.
This script is a variation of the script in the "Everything Together" example. It uses the same automatic login
mechanism, but doesn't contain any code for a trust page. The user already trusts the example scripts forever. This
909
Zend_OpenId
trust was established by calling the Zend_OpenId_Provider::allowSite() method in the identity script.
The same method associates the profile with the trusted URL. This profile will be returned automatically for a request
from the trusted URL.
To make Simple Registration Extension work, you must simply pass an instance of
Zend_OpenId_Extension_Sreg as the second argument to the Zend_OpenId_Provider::handle()
method.
define("TEST_ID", Zend_OpenId::absoluteURL("example-10-id.php"));
define("TEST_PASSWORD", "123");
Anything Else?
Building OpenID providers is much less common than building OpenID-enabled sites, so this manual doesn't cover
all Zend_OpenId_Provider features exhaustively, as was done for Zend_OpenId_Consumer.
• A set of methods to build an end-user GUI that allows users to register and manage their trusted sites and profiles
• An abstract storage layer to store information about users, their sites and their profiles. It also stores
associations between the provider and OpenID-enabled sites. This layer is very similar to that of the
Zend_OpenId_Consumer class. It also uses file storage by default, but may used with another backend.
• An abstract user-association layer that may associate a user's web browser with a logged-in identity
The Zend_OpenId_Provider class doesn't attempt to cover all possible features that can be implemented by
OpenID servers, e.g. digital certificates, but it can be extended easily using Zend_OpenId_Extensions or by
standard object-oriented extension.
910
Capítulo 39. Zend_Paginator
Introduction
Zend_Paginator is a flexible component for paginating collections of data and presenting that data to users.
• Do not force users to adhere to only one way of displaying data or rendering pagination controls
• Loosely couple Zend_Paginator to other Zend Framework components so that users who wish to use it
independently of Zend_View, Zend_Db, etc. can do so
Usage
Paginating data collections
In order to paginate items into pages, Zend_Paginator must have a generic way of accessing that data. For that
reason, all data access takes place through data source adapters. Several adapters ship with Zend Framework by default:
Nota
Instead of selecting every matching row of a given query, the DbSelect and DbTableSelect adapters retrieve
only the smallest amount of data necessary for displaying the current page.
Because of this, a second query is dynamically generated to determine the total number of matching rows.
However, it is possible to directly supply a count or count query yourself. See the setRowCount() method
in the DbSelect adapter for more information.
911
Zend_Paginator
For convenience, you may take advantage of the static factory() method for the adapters packaged with Zend
Framework:
$paginator = Zend_Paginator::factory($array);
Nota
In the case of the Null adapter, in lieu of a data collection you must supply an item count to its constructor.
Although the instance is technically usable in this state, in your controller action you'll need to tell the paginator what
page number the user requested. This allows him to advance through the paginated data.
$paginator->setCurrentPageNumber($page);
The simplest way to keep track of this value is through a URL. Although we recommend using a
Zend_Controller_Router_Interface-compatible router to handle this, it is not a requirement.
The following is an example route you might use in an INI configuration file:
routes.example.route = articles/:articleName/:page
routes.example.defaults.controller = articles
routes.example.defaults.action = view
routes.example.defaults.page = 1
routes.example.reqs.articleName = \w+
routes.example.reqs.page = \d+
With the above route (and using Zend Framework MVC components), you might set the current page number like this:
$paginator->setCurrentPageNumber($this->_getParam('page'));
There are other options available; see Configuration for more on them.
Finally, you'll need to assign the paginator instance to your view. If you're using Zend_View with the ViewRenderer
action helper, the following will work:
$this->view->paginator = $paginator;
To use the DbSelect and DbTableSelect adapters you don't have to retrieve the data upfront from the database. Both
adapters do the retrieval for you, aswell as the counting of the total pages. If additional work has to be done on the
database results the adapter getItems() method has to be extended in your application.
Additionally these adapters do not fetch all records from the database in order to count them. Instead, the adapters
manipulates the original query to produce the corresponding COUNT query. Paginator then executes that COUNT
query to get the number of rows. This does require an extra round-trip to the database, but this is many times faster
than fetching an entire result set and using count(). Especially with large collections of data.
912
Zend_Paginator
The database adapters will try and build the most efficient query that will execute on pretty much all modern databases.
However, depending on your database or even your own schema setup, there might be more efficient ways to get a
rowcount. For this scenario the database adapters allow you to set a custom COUNT query. For example, if you keep
track of the count of blog posts in a separate table, you could achieve a faster count query with the following setup:
This approach will probably not give you a huge performance gain on small collections and/or simple select queries.
However, with complex queries and large collections, a similar approach could give you a significant performance
boost.
<html>
<body>
<h1>Ejemplo</h1>
<?php if (count($this->paginator)): ?>
<ul>
<?php foreach ($this->paginator as $item): ?>
<li><?php echo $item; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Notice the view helper call near the end. PaginationControl accepts up to four parameters: the paginator instance, a
scrolling style, a view partial, and an array of additional parameters.
The second and third parameters are very important. Whereas the view partial is used to determine how the pagination
control should look, the scrolling style is used to control how it should behave. Say the view partial is in the style of
a search pagination control, like the one below:
913
Zend_Paginator
What happens when the user clicks the "next" link a few times? Well, any number of things could happen. The current
page number could stay in the middle as you click through (as it does on Yahoo!), or it could advance to the end of
the page range and then appear again on the left when the user clicks "next" one more time. The page numbers might
even expand and contract as the user advances (or "scrolls") through them (as they do on Google).
The fourth and final parameter is reserved for an optional associative array of additional variables that you want
available in your view partial (available via $this). For instance, these values could include extra URL parameters
for pagination links.
By setting the default view partial, default scrolling style, and view instance, you can eliminate the calls to
PaginationControl completely:
Zend_Paginator::setDefaultScrollingStyle('Sliding');
Zend_View_Helper_PaginationControl::setDefaultViewPartial(
'my_pagination_control.phtml'
);
$paginator->setView($view);
When all of these values are set, you can render the pagination control inside your view script with a simple echo
statement:
Nota
Of course, it's possible to use Zend_Paginator with other template engines. For example, with Smarty
you might do the following:
914
Zend_Paginator
$smarty->assign('pages', $paginator->getPages());
You could then access paginator values from a template like so:
{$pages->pageCount}
Search pagination:
<!--
See http://developer.yahoo.com/ypatterns/pattern.php?pattern=searchpagination
-->
Item pagination:
<!--
915
Zend_Paginator
See http://developer.yahoo.com/ypatterns/pattern.php?pattern=itempagination
-->
</div>
<?php endif; ?>
Dropdown pagination:
916
Zend_Paginator
<option value="<?php
echo $this->url(array('page' => $page));?>"<?php echo $selected ?>>
<?php echo $page; ?>
</option>
<?php endforeach; ?>
</select>
<?php endif; ?>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js">
</script>
<script type="text/javascript">
$('paginationControl').observe('change', function() {
window.location = this.options[this.selectedIndex].value;
})
</script>
Listing of properties
The following options are available to pagination control view partials:
Configuration
Zend_Paginator has several configuration methods that can be called:
917
Zend_Paginator
Advanced usage
Custom data source adapters
At some point you may run across a data type that is not covered by the packaged adapters. In this case, you will
need to write your own.
To do so, you must implement Zend_Paginator_Adapter_Interface. There are two methods required to
do this:
• count()
• getItems($offset, $itemCountPerPage)
Additionally, you'll want to implement a constructor that takes your data source as a parameter and stores it as a
protected or private property. How you wish to go about doing this specifically is up to you.
The getItems() method is only slightly more complicated. For this, your adapter is supplied with an offset and the
number of items to display per page. You must return the appropriate slice of data. For an array, that would be:
Take a look at the packaged adapters (all of which implement the Zend_Paginator_Adapter_Interface) for
ideas of how you might go about implementing your own.
918
Zend_Paginator
This method should calculate a lower and upper bound for page numbers within the range of so-called "local" pages
(that is, pages that are nearby the current page).
There's nothing special about this call; it's merely a convenience method to check the validity of the lower and upper
bound and return an array of the range to the paginator.
When you're ready to use your new scrolling style, you'll need to tell Zend_Paginator what directory to look in.
To do that, do the following:
$prefix = 'My_Paginator_ScrollingStyle';
$path = 'My/Paginator/ScrollingStyle/';
Zend_Paginator::addScrollingStylePrefixPath($prefix, $path);
Caching features
Zend_Paginator can be told to cache the data it has already passed on, preventing the adapter from fetching them
each time they are used. To tell paginator to automatically cache the adapter's data, just pass to its setCache()
method a Zend_Cache_Core instance.
$paginator = Zend_Paginator::factory($someData);
$fO = array('lifetime' => 3600, 'automatic_serialization' => true);
$bO = array('cache_dir'=>'/tmp');
$cache = Zend_cache::factory('Core', 'File', $fO, $bO);
Zend_Paginator::setCache($cache);
As far as Zend_Paginator has got a Zend_Cache_Core instance, data will be cached. Sometimes you would
like not to cache data even if you already passed a cache instance. You should then use setCacheEnable() for that.
$paginator = Zend_Paginator::factory($someData);
// $cache is a Zend_Cache_Core instance
Zend_Paginator::setCache($cache);
// ... later on the script
$paginator->setCacheEnable(false);
// cache is now disabled
When a cache is set, data are automatically stored in it and pulled out from it. It then can be useful to empty the
cache manually. You can get this done by calling clearPageItemCache($pageNumber). If you don't pass
any parameter, the whole cache will be empty. You can optionally pass a parameter representing the page number
to empty in the cache:
$paginator = Zend_Paginator::factory($someData);
Zend_Paginator::setCache($cache);
$items = $paginator->getCurrentItems();
// page 1 is now in cache
$page3Items = $paginator->getItemsByPage(3);
// page 3 is now in cache
919
Zend_Paginator
Changing the item count per page will empty the whole cache as it would have become invalid:
$paginator = Zend_Paginator::factory($someData);
Zend_Paginator::setCache($cache);
// fetch some items
$items = $paginator->getCurrentItems();
It is also possible to see the data in cache and ask for them directly. getPageItemCache() can be used for that:
$paginator = Zend_Paginator::factory($someData);
$paginator->setItemCountPerPage(3);
Zend_Paginator::setCache($cache);
Zend_Paginator_AdapterAggregate Interface
Depending on your application you might want to paginate objects, whose internal data-structure is equal
to existing adapters, but you don't want to break up your encapsulation to allow access to this data.
In other cases an object might be in a "has-an adapter" relationship, rather than the "is-an adapter"
relationsship that Zend_Paginator_Adapter_Abstract promotes. For this cases you can use the
Zend_Paginator_AdapterAggregate interface that behaves much like the IteratorAggregate interface
of the PHP SPL extension.
interface Zend_Paginator_AdapterAggregate
{
/**
* Return a fully configured Paginator Adapter from this method.
*
* @return Zend_Paginator_Adapter_Abstract
*/
public function getPaginatorAdapter();
}
The interface is fairly small and only expects you to return an instance of
Zend_Paginator_Adapter_Abstract. An Adapter Aggregate instance is then recognized by both
Zend_Paginator::factory and the constructor of Zend_Paginator and handled accordingly.
920
Capítulo 40. Zend_Pdf
Introducción
El componente Zend_Pdf es un motor para manipular documentos PDF (Portable Document Format). Puede cargar,
crear, modificar y guardar documentos. Por lo tanto, puede ayudar a cualquier aplicación PHP a crear dinámicamente
documentos PDF modificando los documentos existentes o generar unos nuevos desde cero. Zend_Pdf ofrece las
siguientes características:
• Manipular páginas desde dentro de un documento. Cambiar el orden de las páginas, añadir nuevas páginas, eliminar
páginas de un documento.
• Dibujo de texto utilizando alguno de las 14 fuentes estándar (incorporadas) o sus propias fuentes personalizadas
TrueType.
• Rotaciones.
• Dibujo de imágenes. 2
Para crear un nuevo documento, primero debe ser creado un nuevo objeto Zend_Pdf.
La clase Zend_Pdf también ofrece dos métodos estáticos para cargar un documento PDF. Estos métodos son
Zend_Pdf::load() y Zend_Pdf::parse(). Ambos retornan objetos Zend_Pdf como resultado o arrojan
una excepción si ocurre un error.
...
// Crear un nuevo documento PDF
$pdf1 = new Zend_Pdf();
921
Zend_Pdf
El formato de archivos PDF soporta la actualización incremental del documento. Así, cada vez que un documento es
actualizado, entonces se crea una nueva revisión del documento. El componente Zend_Pdf soporta la recuperación
de una revisión especificada.
Una revisión puede especificarse como un segundo parámetro a los métodos Zend_Pdf::load() y
Zend_Pdf::parse() o requerirlo llamando al método Zend_Pdf::rollback(). 3 call.
...
// Cargar la revisión anterior del documento PDF
$pdf1 = Zend_Pdf::load($fileName, 1);
...
// Cargar el documento PDF
$pdf = Zend_Pdf::load($fileName);
...
// Actualizar el documento PDF
$pdf->save($fileName, true);
// Save document as a new file
$pdf->save($newFileName);
...
3
El método Zend_Pdf::rollback() debe ser invocado antes de aplicar cualquier cambio al documento, de lo contrario el comportamiento
no está definido.
922
Zend_Pdf
Las páginas PDF o bien son cargadas desde una PDF ya existente o creadas usando la API Zend_Pdf.
Se pueden crear nuevas páginas instanciando directamente al objeto Zend_Pdf_Page o llamando al método
Zend_Pdf::newPage(), que devuelve un objeto Zend_Pdf_Page. Zend_Pdf::newPage() crea una
página que ya está agregada a un documento. Las páginas no agregadas no pueden ser utilizadas con múltiples
documentos PDF, pero son algo más eficientes. 4
• Zend_Pdf_Page::SIZE_A4
• Zend_Pdf_Page::SIZE_A4_LANDSCAPE
• Zend_Pdf_Page::SIZE_LETTER
• Zend_Pdf_Page::SIZE_LETTER_LANDSCAPE
Las páginas del documento se almacenados en el atributo público $pages de la clase Zend_Pdf. El atributo posee
un array de objetos Zend_Pdf_Page y define completamente las instancias y el orden de las páginas. Este array
puede manipularse como cualquie otro array PHP:
...
// Invertir el orden de las páginas.
$pdf->pages = array_reverse($pdf->pages);
...
// Agregar una nueva página.
$pdf->pages[] = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
// Agregar una nueva página.
$pdf->pages[] = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
...
Clonado de Páfinas.
La página PDF existente puede ser clonada creando un nuevo objeto Zend_Pdf_Page con una página existente
como parámetro:
4
Es una limitación de la versión actual de ZF. Será eliminada en futuras versiones. Pero las páginas no agregadas siempre dan mejor resultado
(más óptimo) para compartir páginas entre los documentos.
923
Zend_Pdf
...
// Almacenar la página plantilla en una variable
$template = $pdf->pages[$templatePageIndex];
...
// Agregar una nueva página.
$page1 = new Zend_Pdf_Page($template);
$pdf->pages[] = $page1;
...
...
Caution
Importante! La página clonada comparte algunos recursos de PDF con una página plantilla, la que puede ser
utilizada sólo en el mismo documento como una página plantilla. El documento modificado pueden guardarse
como uno nuevo.
Dibujo
Geometría
PDF utiliza la misma geometría que PostScript. Se inicia desde la parte inferior izquierda de la página y por defecto
se mide en puntos (1/72 de pulgada).
$width = $pdfPage->getWidth();
$height = $pdfPage->getHeight();
Colores
PDF tiene una poderosa capacidad de representación de colores. El módulo Zend_Pdf soporta la Escala de Grises,
y los espacios de color RGB y CMYK. Cualquiera de ellos puede ser usado en cualquier lugar, donde el objeto
Zend_Pdf_Color sea requerido. Las clases Zend_Pdf_Color_GrayScale, Zend_Pdf_Color_Rgb y
Zend_Pdf_Color_Cmyk proporcionan esta funcionalidad:
924
Zend_Pdf
// $r, $g, $b (float numbers). 0.0 (min intensity) - 1.0 (max intensity)
$color2 = new Zend_Pdf_Color_Rgb($r, $g, $b);
// $c, $m, $y, $k (float numbers). 0.0 (min intensity) - 1.0 (max intensity)
$color3 = new Zend_Pdf_Color_Cmyk($c, $m, $y, $k);
Dibujo de Formas
Todas las operaciones de dibujo se puede hacer en un contexto de página PDF.
/**
* Dibujar una línea desde x1,y1 hasta x2,y2.
*
* @param float $x1
* @param float $y1
* @param float $x2
* @param float $y2
* @return Zend_Pdf_Page
*/
public function drawLine($x1, $y1, $x2, $y2);
/**
* Dibujar un rectángulo.
*
* Rellenar los tipos:
* Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE - rellenar el rectángulo
* y delinearlo (por defecto)
* Zend_Pdf_Page::SHAPE_DRAW_STROKE - delinear el rectángulo
* Zend_Pdf_Page::SHAPE_DRAW_FILL - rellenar el rectángulo
*
* @param float $x1
* @param float $y1
* @param float $x2
* @param float $y2
* @param integer $fillType
* @return Zend_Pdf_Page
*/
public function drawRectangle($x1, $y1, $x2, $y2,
$fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE);
/**
* Dibujar un polígono.
925
Zend_Pdf
*
* Si $fillType es Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE o
* Zend_Pdf_Page::SHAPE_DRAW_FILL, entonces el polígono se cierra automáticamente.
* Véase la descripción detallada de estos métodos en la documentación de PDF
* (sección 4.4.2 Path painting Operators, Filling)
*
* @param array $x - array de float (la coordenada X de los vértices)
* @param array $y - array de float (la coordenada Y de los vértices)
* @param integer $fillType
* @param integer $fillMethod
* @return Zend_Pdf_Page
*/
public function drawPolygon($x, $y,
$fillType =
Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
$fillMethod =
Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING);
/**
* Dibujar un círculo centrado en X, y con un radio de radius.
*
* Los ángulos están especificados en radianes.
*
* Firmas del Método::
* drawCircle($x, $y, $radius);
* drawCircle($x, $y, $radius, $fillType);
* drawCircle($x, $y, $radius, $startAngle, $endAngle);
* drawCircle($x, $y, $radius, $startAngle, $endAngle, $fillType);
*
*
* No es un círculo de verdad, porque PDF sólo admite curvas cúbicss de Bezier,
* pero con muy buena aproximación.
* Se distingue de un verdadero círculo en un máximo de 0.00026 radios (en PI/8,
* 3*PI/8, 5*PI/8, 7*PI/8, 9*PI/8, 11*PI/8, 13*PI/8 y 15*PI/8 ángulos).
* A 0, PI/4, PI/2, 3*PI/4, PI, 5*PI/4, 3*PI/2 y 7*PI/4 es exactamente
* la tangente a un círculo.
*
* @param float $x
* @param float $y
* @param float $radius
* @param mixed $param4
* @param mixed $param5
* @param mixed $param6
* @return Zend_Pdf_Page
*/
public function drawCircle($x,
$y,
$radius,
$param4 = null,
$param5 = null,
$param6 = null);
926
Zend_Pdf
/**
* Dibujar una elipse dentro del rectángulo especificado.
*
* Firmas del método:
* drawEllipse($x1, $y1, $x2, $y2);
* drawEllipse($x1, $y1, $x2, $y2, $fillType);
* drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle);
* drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle, $fillType);
*
* Los ángulos se especifican en radianes
*
* @param float $x1
* @param float $y1
* @param float $x2
* @param float $y2
* @param mixed $param5
* @param mixed $param6
* @param mixed $param7
* @return Zend_Pdf_Page
*/
public function drawEllipse($x1,
$y1,
$x2,
$y2,
$param5 = null,
$param6 = null,
$param7 = null);
Dibujo de Texto
Las operaciones de dibujo de texto también existen en el contexto de una página PDF. Puede dibujar una sola línea de
texto en cualquier posición en la página mediante el suministro de las coordenadas X e Y de la base de referencia. La
fuente y tamaño actual de la letra se utilizan para operaciones de dibujo de texto (ver descripción detallada más abajo).
/**
* Dibujar una línea de texto en una posición específica.
*
* @param string $text
* @param float $x
* @param float $y
* @param string $charEncoding (opcional) Codificación de caracteres del texto
* fuente. El valor por defecto es la codificación actual y local.
* @throws Zend_Pdf_Exception
* @return Zend_Pdf_Page
*/
public function drawText($text, $x, $y, $charEncoding = '');
...
$pdfPage->drawText('Hello world!', 72, 720);
...
927
Zend_Pdf
Por defecto, los strings de texto se interpretan usando el método de codificación de la localización actual. Si tiene un
string que utiliza un método de codificación diferente (como un string UTF-8 a leer desde un archivo en disco, o un
string MacRoman obtenido a partir del legado de una base de datos), puede indicar la codificación de caracteres a llamar
en tiempo de dibujo y Zend_Pdf se encargará de la conversión. Puede proporcionar la fuente de cualquier método
de codificación de strings soportados por la función de PHP iconv() [http://www.php.net/manual/
function.iconv.php] :
...
// Leer del disco un string codificado en UTF-8
$unicodeString = fread($fp, 1024);
Uso de Fuentes
Zend_Pdf_Page::drawText() utiliza la fuente y el tamaño actual de la fuente de la página, que se establece
con el método Zend_Pdf_Page::setFont():
/**
* Establecer la fuente actual.
*
* @param Zend_Pdf_Resource_Font $font
* @param float $fontSize
* @return Zend_Pdf_Page
*/
public function setFont(Zend_Pdf_Resource_Font $font, $fontSize);
Los documentos PDF soportan fuentes PostScript Type 1 y TrueType, así como dos tipos especializados de PDF, Type
3 y fuentes compuestas. También hay 14 fuentes estándar Tipo 1 incorporadas para cada visor PDF: Courier (4 estilos),
Helvetica (4 estilos), Times (4 estilos), Symbol y Zapf Dingbats.
Zend_Pdf actualmente soporta el estándar de 14 fuentes PDF, así como sus propias fuentes
personalizadas TrueType. Los objetos Font se obtienen a través de una de los dos métodos
de fábrica: Zend_Pdf_Font::fontWithName($fontName) para las 14 fuentes estándar PDF o
Zend_Pdf_Font::fontWithPath($filePath) para fuentes personalizadas.
...
// Crear una fuente nueva
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
// Aplicar la fuente
$pdfPage->setFont($font, 36);
...
Los nombres de las 14 constantes para el tipo de letra estándar de PDF se definen en la clase Zend_Pdf_Font:
928
Zend_Pdf
• Zend_Pdf_Font::FONT_COURIER
• Zend_Pdf_Font::FONT_COURIER_BOLD
• Zend_Pdf_Font::FONT_COURIER_ITALIC
• Zend_Pdf_Font::FONT_COURIER_BOLD_ITALIC
• Zend_Pdf_Font::FONT_TIMES
• Zend_Pdf_Font::FONT_TIMES_BOLD
• Zend_Pdf_Font::FONT_TIMES_ITALIC
• Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC
• Zend_Pdf_Font::FONT_HELVETICA
• Zend_Pdf_Font::FONT_HELVETICA_BOLD
• Zend_Pdf_Font::FONT_HELVETICA_ITALIC
• Zend_Pdf_Font::FONT_HELVETICA_BOLD_ITALIC
• Zend_Pdf_Font::FONT_SYMBOL
• Zend_Pdf_Font::FONT_ZAPFDINGBATS
También puede utilizar cualquier fuente individual TrueType (que generalmente tiene una extensión '.ttf') o bien una
fuente OpenType (con la extensión '.otf') si contiene esquemas TrueType. Actualmente no están soportadas, pero está
previsto para una versión futura archivos de fuentes .dfont de Mac OS X y de Microsoft TrueType Collection(extensión
'.ttc').
Para utilizar una fuente TrueType, debe proporcionar toda la ruta del archivo a la fuente del programa. Si la fuente no
se puede leer por alguna razón, o si no es una fuente TrueType, el método de fábrica arrojará una excepción:
...
// Crear una nueva fuente
$goodDogCoolFont = Zend_Pdf_Font::fontWithPath('/path/to/GOODDC__.TTF');
// Aplicar la fuente
$pdfPage->setFont($goodDogCoolFont, 36);
...
Por defecto, las fuentes personalizadas serán incorporados en el documento PDF resultante. Esto permite que los
destinatarios vean la página como está previsto, incluso si no tienen los tipos de letra apropiados instalados en su
sistema. Si le preocupa el tamaño del archivo, puede pedir que la fuente del programa no sea integrada pasando una
opción 'do not embed' ("no incluir") al método de fábrica:
Ejemplo 40.10. Crear una fuente TrueType, pero no incluirla en el documento PDF.
929
Zend_Pdf
...
// Crear una nueva fuente
$goodDogCoolFont = Zend_Pdf_Font::fontWithPath('/path/to/GOODDC__.TTF',
Zend_Pdf_Font::EMBED_DONT_EMBED);
// Aplicar la fuente
$pdfPage->setFont($goodDogCoolFont, 36);
...
Si el programa no es de fuentes incrustadas, pero el destinatario del archivo PDF tiene instalada la fuente en su sistema,
va a ver el documento como estaba previsto. Si no tiene la fuente correcta instalada, la aplicación del visor de PDF
hará todo lo posible para sintetizar un sustituto.
Algunas fuentes tienen normas específicas de concesión de licencias que les impiden ser tenidas en cuenta en
documentos PDF. Así que no son capturados con la "guardia baja" por la presente, si intenta utilizar una fuente que
no puede ser incorporada, el método de fábrica lanzará una excepción.
Puede seguir utilizando esas fuentes, pero debe pasar el flag de no incluir como se ha descripto anteriormente, o
simplemente puede suprimir la excepción:
Ejemplo 40.11. No arrojar una excepción para las fuentes que no puedan ser incorporadas.
...
$font = Zend_Pdf_Font::fontWithPath(
'/path/to/unEmbeddableFont.ttf',
Zend_Pdf_Font::EMBED_SUPPRESS_EMBED_EXCEPTION
);
...
Esta técnica de supresión se prefiere si va a permitir a un usuario final a elegir sus propios tipos de letra. Las fuentes
que puedan ser embebidas en el documento PDF, lo harán, aquellos que no puedan, no.
Los de programas de fuentes pueden ser bastante grandes, algunas llegan a decenas de megabytes. Por defecto, todas
las fuentes incorporadas son comprimidas utilizando el esquema de compresión Flate, lo que resulta en un ahorro de
espacio del 50% en promedio. Si, por alguna razón, no desea comprimir la fuente del programa, se puede desactivar
con una opción:
...
$font = Zend_Pdf_Font::fontWithPath('/path/to/someReallyBigFont.ttf',
Zend_Pdf_Font::EMBED_DONT_COMPRESS);
...
Por último, en caso necesario, puede combinar las opciones de la integración mediante el operador binario OR:
...
$font = Zend_Pdf_Font::fontWithPath(
$someUserSelectedFontPath,
930
Zend_Pdf
(Zend_Pdf_Font::EMBED_SUPPRESS_EMBED_EXCEPTION |
Zend_Pdf_Font::EMBED_DONT_COMPRESS));
...
Zend_Pdf usa CP1252 (WinLatin1) para dibujar el texto con las fuentes estándar.
El texto todavía se puede proporcionar en cualquier otra codificación, que debe ser especificada si ésta es distinto de
una fuente local actual. Realmente, sólo se dibujarán caracteres WinLatin1.
...
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER);
$pdfPage->setFont($font, 36)
->drawText('Euro sign - €', 72, 720, 'UTF-8')
->drawText('Text with umlauts - à è ì', 72, 650, 'UTF-8');
...
Puede ser útil para las actualizaciones incrementales de un documento. Sin esta funcionalidad tiene que agregar y
posiblemente, incrustar una fuente en un documento cada vez que desee actualizarlo.
Los objetos Zend_Pdf y Zend_Pdf_Page proporcionan métodos especiales para extraer todas las fuentes
mencionadas en un documento o una página:
...
$pdf = Zend_Pdf::load($documentPath);
...
// Obtener todas las fuentes del documento
$fontList = $pdf->extractFonts();
$pdf->pages[] = ($page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4));
$yPosition = 700;
foreach ($fontList as $font) {
$page->setFont($font, 15);
$fontName = $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT,
'en',
'UTF-8');
$page->drawText($fontName . ': The quick brown fox jumps over the lazy dog',
100,
931
Zend_Pdf
$yPosition,
'UTF-8');
$yPosition -= 30;
}
...
// Obtener las fuentes referenciadas dentro de la primera página del documento
$firstPage = reset($pdf->pages);
$firstPageFonts = $firstPage->extractFonts();
...
...
$pdf = new Zend_Pdf();
...
$pdf->pages[] = ($page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4));
$font = Zend_Pdf_Font::fontWithPath($fontPath);
$page->setFont($font, $fontSize);
$page->drawText($text, $x, $y);
...
// Este nombre de fuente debe ser almacenado en algún lugar...
$fontName = $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT,
'en',
'UTF-8');
...
$pdf->save($docPath);
...
...
$pdf = Zend_Pdf::load($docPath);
...
$pdf->pages[] = ($page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4));
$page->setFont($font, $fontSize);
$page->drawText($text, $x, $y);
...
$pdf->save($docPath, true /* modo de actualización incremental */);
...
Las fuentes extraídas pueden ser utilizadas en el lugar de cualquier otra fuente con las siguientes limitaciones:
• La fuente extraída puede ser usada sólo en el contexto del documento del que se ha extraído.
• Posiblemente, el programa no extraiga realmente la fuente incrustada. Así que las fuentes extraídas no pueden
proporcionar métricas correctas y la fuente original tiene que ser utilizada para los cálculos de ancho de texto:
932
Zend_Pdf
...
$font = $pdf->extractFont($fontName);
$originalFont = Zend_Pdf_Font::fontWithPath($fontPath);
Dibujo de Imágenes
La clase Zend_Pdf_Page proporciona el método drawImage() para dibujar la imagen:
/**
* Dibujar una imagen en una posición específica de la página.
*
* @param Zend_Pdf_Resource_Image $image
* @param float $x1
* @param float $y1
* @param float $x2
* @param float $y2
* @return Zend_Pdf_Page
*/
public function drawImage(Zend_Pdf_Resource_Image $image, $x1, $y1, $x2, $y2);
...
// Cargar la imagen
$image = Zend_Pdf_Image::imageWithPath('my_image.jpg');
Importante! el soporte a JPEG requiere que se configure la extensión PHP GD. Importante! el soporte a PNG requiere
que se configure la extensión ZLIB para trabajar con imágenes canal Alfa.
933
Zend_Pdf
/**
* Establecer el patrón de líneas de guiones.
*
* El patrón es una array de números de punto flotante:
* array(on_length, off_length, on_length, off_length, ...)
* La fase está desplazada lateralmente desde el comienzo de la línea.
*
* @param array $pattern
* @param array $phase
* @return Zend_Pdf_Page
*/
public function setLineDashingPattern($pattern, $phase = 0);
Estilo Relleno
Los métodos Zend_Pdf_Page::drawRectangle(), Zend_Pdf_Page::drawPolygon(),
Zend_Pdf_Page::drawCircle() y Zend_Pdf_Page::drawEllipse() toman el argumento
$fillType como un parámetro opcional. Puede ser:
La tortuosa regla del número distinto de cero determina si un punto está dentro de un camino
de un rayo conceptual dibujado a partir de ese punto hasta el infinito en cualquier dirección y
luego de examinar los lugares en los que un segmento de la ruta atraviesa el rayo. A partir de la
cuenta de 0, la norma agrega 1 cada vez que un segmento de ruta atraviesa el rayo de izquierda
a derecha y resta 1 cada vez que un segmento cruza de derecha a izquierda. Después de contar
todos los cruces, si el resultado es 0, entonces el punto está fuera del camino; otra cosa es el
interior. Nota: El método que acabamos de describir no especifica qué hacer si un segmento de
ruta coincide con o es tangente al rayo elegido. Dado que la dirección de los rayos es arbitraria, la
regla simplemente elige un rayo que no encuentre problemas con las intersecciones. Por simples
caminos convexos, la regla del tortuoso número distinto de cero define el dentro y afuera como uno
lo espera intuitivamente. Los casos más interesantes son aquellos que involucran la complejidad o
934
Zend_Pdf
las rutas auto-intersectadas como las que se muestran en la Figura 4.10 (en un PDF de referencia).
Para un camino que consiste en una estrella de cinco puntas, dibujado con cinco segmentos
conectados de líneas rectas intersectándose entre sí, la regla considera que el interior será toda el
área delimitada por la estrella, incluido el pentágono en el centro. Para un camino compuesto por
dos círculos concéntricos, las áreas de ambos círculos cerrados se consideran que están adentro,
siempre que ambas se hayan dibujado en la misma dirección. Si los círculos son dibujados en
direcciones opuestas, sólo la forma de "doughnut" (rosquilla) formada entre ellos es el interior,
de acuerdo a la norma, el "agujero de la rosquilla" está afuera.
• Zend_Pdf_Page::FILL_METHOD_EVEN_ODD
Una alternativa al tortuoso número distinto de cero es la regla par-impar. Esta norma
determina la "interioridad" de un punto por el dibujo de un rayo desde ese punto en cualquier
dirección y simplemente contando el número de segmentos de ruta que atraviesan los rayos,
independientemente de la dirección. Si este número es impar, el punto está adentro, si es par, el
punto está afuera. Esto produce los mismos resultados que la regla del tortuoso número distinto
de cero para caminos con formas simples, pero produce resultados diferentes para formas más
complejas. La Figura 4.11 (en un PDF de referencia) muestra los efectos de la aplicación de la
regla par-impar a las rutas complejss. Para la estrella de cinco puntas, la regla considera que
los puntos del triángulo están dentro de la ruta, pero no el pentágono en el centro. Para los dos
círculos concéntricos, sólo la forma de la "rosquilla" entre los dos círculo está considerada adentro,
independientemente de las direcciones en las que se dibujen los círculos.
Transformaciones Lineales
Rotaciones.
La página PDF se puede rotar antes de aplicar cualquier operación de dibujo. Se puede hacer con el método
Zend_Pdf_Page::rotate():
/**
* Rotar la página.
*
* @param float $x - la coordenada X del punto de rotación
* @param float $y - la coordenada Y del punto de rotación
* @param float $angle - ángulo de rotación
* @return Zend_Pdf_Page
*/
public function rotate($x, $y, $angle);
/**
* Establecer la escala al sistema de coordenadas.
*
* @param float $xScale - factor de escala de la dimensión X
* @param float $yScale - factor de escala de la dimensión Y
* @return Zend_Pdf_Page
935
Zend_Pdf
*/
public function scale($xScale, $yScale);
/**
* Traducir sistema de coordenadas.
*
* @param float $xShift - desplazamiento de la coordenada X
* @param float $yShift - desplazamiento de la coordenada Y
* @return Zend_Pdf_Page
*/
public function translate($xShift, $yShift);
/**
* Traducir sistema de coordenadas.
*
* @param float $x - la coordenada X del eje del punto de sesgo
* @param float $y - la coordenada Y del eje del punto de sesgo
* @param float $xAngle - ángulo de sesgo en el eje X
* @param float $yAngle - ángulo de sesgo en el eje Y
* @return Zend_Pdf_Page
*/
public function skew($x, $y, $xAngle, $yAngle);
/**
* Salva el estado de los gráficos de esta página.
* Esta toma una instantánea del estilo aplicado actualmente, posición,
* área de recorte y cualquier rotación/traducción/escalado que ha sido
* aplicada.
*
* @return Zend_Pdf_Page
*/
public function saveGS();
/**
* Restablecer los gráficos que se guardaron con la última llamada a
936
Zend_Pdf
* saveGS().
*
* @return Zend_Pdf_Page
*/
public function restoreGS();
/**
* Intersectar el área actual de recorte con un rectángulo.
*
* @param float $x1
* @param float $y1
* @param float $x2
* @param float $y2
* @return Zend_Pdf_Page
*/
public function clipRectangle($x1, $y1, $x2, $y2);
/**
* Intersectar el área actual de recorte con un polígono.
*
* @param array $x - array de float (la coordenada X de los vértices)
* @param array $y - array de float (la coordenada Y de los vértices)
* @param integer $fillMethod
* @return Zend_Pdf_Page
*/
public function clipPolygon($x,
$y,
$fillMethod =
Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING);
/**
* Intersectar el área actual de recorte con un círculo.
*
* @param float $x
* @param float $y
* @param float $radius
* @param float $startAngle
* @param float $endAngle
* @return Zend_Pdf_Page
*/
public function clipCircle($x,
$y,
$radius,
$startAngle = null,
937
Zend_Pdf
$endAngle = null);
/**
* Intersectar el área actual de recorte con una elipse.
*
* Firmas del método:
* drawEllipse($x1, $y1, $x2, $y2);
* drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle);
*
* @todo process special cases with $x2-$x1 == 0 or $y2-$y1 == 0
*
* @param float $x1
* @param float $y1
* @param float $x2
* @param float $y2
* @param float $startAngle
* @param float $endAngle
* @return Zend_Pdf_Page
*/
public function clipEllipse($x1,
$y1,
$x2,
$y2,
$startAngle = null,
$endAngle = null);
Estilos
La clase Zend_Pdf_Style proporciona la funcionalidad de los estilos.
Los estilos se pueden utilizar para almacenar un conjunto de parámetros de estado del gráfico y aplicarlo a un página
PDF por una operación:
/**
* Establecer el estilo a utilizar para futuras operaciones de dibujo sobre esta página
*
* @param Zend_Pdf_Style $style
* @return Zend_Pdf_Page
*/
public function setStyle(Zend_Pdf_Style $style);
/**
* Regresar el estilo aplicado a la página.
*
* @return Zend_Pdf_Style|null
*/
public function getStyle();
La clase Zend_Pdf_Style proporciona un conjunto de métodos para obtener o configurar diferentes parámetros
de estado de los gráficos:
938
Zend_Pdf
/**
* Establecer el color de la línea.
*
* @param Zend_Pdf_Color $color
* @return Zend_Pdf_Page
*/
public function setLineColor(Zend_Pdf_Color $color);
/**
* Obtener el color de la línea.
*
* @return Zend_Pdf_Color|null
*/
public function getLineColor();
/**
* Establecer el ancho de la línea.
*
* @param float $width
* @return Zend_Pdf_Page
*/
public function setLineWidth($width);
/**
* Obtener el ancho de la línea.
*
* @return float
*/
public function getLineWidth();
/**
* Establecer el patrón de la línea de guiones
*
* @param array $pattern
* @param float $phase
* @return Zend_Pdf_Page
*/
public function setLineDashingPattern($pattern, $phase = 0);
/**
* Obtener el patrón de la línea de guiones
*
* @return array
*/
public function getLineDashingPattern();
/**
* Obtener la fase de la línea de guiones
*
939
Zend_Pdf
* @return float
*/
public function getLineDashingPhase();
/**
* Establecer el color de relleno.
*
* @param Zend_Pdf_Color $color
* @return Zend_Pdf_Page
*/
public function setFillColor(Zend_Pdf_Color $color);
/**
* Obtener el color de relleno.
*
* @return Zend_Pdf_Color|null
*/
public function getFillColor();
/**
* Establecer la fuente actual.
*
* @param Zend_Pdf_Resource_Font $font
* @param float $fontSize
* @return Zend_Pdf_Page
*/
public function setFont(Zend_Pdf_Resource_Font $font, $fontSize);
/**
* Modificar el tamaño de la fuente actual.
*
* @param float $fontSize
* @return Zend_Pdf_Page
*/
public function setFontSize($fontSize);
/**
* Obtener la fuente actual.
*
* @return Zend_Pdf_Resource_Font $font
*/
public function getFont();
/**
* Obtener el tamaño de la fuente actual.
*
* @return float $fontSize
*/
public function getFontSize();
940
Zend_Pdf
Transparencia
El módulo Zend_Pdf soporta el manejo de la transparencia.
/**
* Establecer la transparencia.
*
* $alpha == 0 - transparente
* $alpha == 1 - opaco
*
* Modos de transparencia soportados por PDF:
* Normal (por defecto), Multiply, Screen, Overlay, Darken, Lighten,
* ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion
*
* @param float $alpha
* @param string $mode
* @throws Zend_Pdf_Exception
* @return Zend_Pdf_Page
*/
public function setAlpha($alpha, $mode = 'Normal');
Interactive Features
Destinations
A destination defines a particular view of a document, consisting of the following items:
Destinations may be associated with outline items (Document Outline (bookmarks)), annotations (Annotations), or
actions (Actions). In each case, the destination specifies the view of the document to be presented when the outline item
or annotation is opened or the action is performed. In addition, the optional document open action can be specified.
Zend_Pdf_Destination_Zoom
Display the specified page, with the coordinates (left, top) positioned at the upper-left corner of the window and the
contents of the page magnified by the factor zoom.
941
Zend_Pdf
Where:
NULL, specified for $left, $top or $zoom parameter means "current viewer application value".
• FloatgetLeftEdge();
• setLeftEdge(float $left);
• FloatgetTopEdge();
• setTopEdge(float $top);
• FloatgetZoomFactor();
• setZoomFactor(float $zoom);
Zend_Pdf_Destination_Fit
Display the specified page, with the coordinates (left, top) positioned at the upper-left corner of the window and
the contents of the page magnified by the factor zoom. Display the specified page, with its contents magnified just
enough to fit the entire page within the window both horizontally and vertically. If the required horizontal and vertical
magnification factors are different, use the smaller of the two, centering the page within the window in the other
dimension.
Zend_Pdf_Destination_FitHorizontally
Display the specified page, with the vertical coordinate top positioned at the top edge of the window and the contents
of the page magnified just enough to fit the entire width of the page within the window.
Where:
• FloatgetTopEdge();
• setTopEdge(float $top);
942
Zend_Pdf
Zend_Pdf_Destination_FitVertically
Display the specified page, with the horizontal coordinate left positioned at the left edge of the window and the contents
of the page magnified just enough to fit the entire height of the page within the window.
Where:
• FloatgetLeftEdge();
• setLeftEdge(float $left);
Zend_Pdf_Destination_FitRectangle
Display the specified page, with its contents magnified just enough to fit the rectangle specified by the coordinates
left, bottom, right, and top entirely within the window both horizontally and vertically. If the required horizontal and
vertical magnification factors are different, use the smaller of the two, centering the rectangle within the window in
the other dimension.
Where:
• FloatgetLeftEdge();
• setLeftEdge(float $left);
• FloatgetBottomEdge();
• setBottomEdge(float $bottom);
• FloatgetRightEdge();
• setRightEdge(float $right);
• FloatgetTopEdge();
• setTopEdge(float $top);
943
Zend_Pdf
Zend_Pdf_Destination_FitBoundingBox
Display the specified page, with its contents magnified just enough to fit its bounding box entirely within the window
both horizontally and vertically. If the required horizontal and vertical magnification factors are different, use the
smaller of the two, centering the bounding box within the window in the other dimension.
Zend_Pdf_Destination_FitBoundingBoxHorizontally
Display the specified page, with the vertical coordinate top positioned at the top edge of the window and the contents
of the page magnified just enough to fit the entire width of its bounding box within the window.
Where
• FloatgetTopEdge();
• setTopEdge(float $top);
Zend_Pdf_Destination_FitBoundingBoxVertically
Display the specified page, with the horizontal coordinate left positioned at the left edge of the window and the contents
of the page magnified just enough to fit the entire height of its bounding box within the window.
Where
• FloatgetLeftEdge();
• setLeftEdge(float $left);
Zend_Pdf_Destination_Named
All destinations listed above are "Explicit Destinations".
In addition to this, PDF document may contain a dictionary of such destinations which may be used to reference from
outside the PDF (e.g. 'http://www.mycompany.com/document.pdf#chapter3').
944
Zend_Pdf
Zend_Pdf_Destination_Named objects allow to refer destinations from the document named destinations
dictionary.
StringgetName();
Each destination object (including named destinations) can be resolved using the
resolveDestination($destination) method. It returns corresponding Zend_Pdf_Page object, if
destination target is found, or NULL otherwise.
Nota
Unresolvable named destinations are automatically removed from a document while document saving.
$pdf->pages[] = $page1;
$pdf->pages[] = $page2;
$destination1 = Zend_Pdf_Destination_Fit::create($page2);
$destination2 = Zend_Pdf_Destination_Fit::create($page3);
945
Zend_Pdf
$pdf->setNamedDestination('Page2', $destination1);
$pdf->setNamedDestination('Page3', $destination2);
// Returns $destination2
$destination = $pdf->getNamedDestination('Page3');
// Returns $destination1
$pdf->resolveDestination(Zend_Pdf_Destination_Named::create('Page2'));
Actions
Instead of simply jumping to a destination in the document, an annotation or outline item can specify an action for
the viewer application to perform, such as launching an application, playing a sound, or changing an annotation's
appearance state.
946
Zend_Pdf
• Zend_Pdf_Action_Rendition - control the playing of multimedia content (begin, stop, pause, or resume a
playing rendition).
Actions chaining
Actions objects can be chained using Zend_Pdf_Action::$next public property.
It's an array of Zend_Pdf_Action objects, which also may have their sub-actions.
Zend_Pdf_Action class supports RecursiveIterator interface, so child actions may be iterated recursively:
$pdf->pages[] = $page1;
$pdf->pages[] = $page2;
$action1 = Zend_Pdf_Action_GoTo::create(
Zend_Pdf_Destination_Fit::create($page2));
$action2 = Zend_Pdf_Action_GoTo::create(
Zend_Pdf_Destination_Fit::create($page3));
$action3 = Zend_Pdf_Action_GoTo::create(
Zend_Pdf_Destination_Named::create('Capítulo1'));
$action4 = Zend_Pdf_Action_GoTo::create(
Zend_Pdf_Destination_Named::create('Capítulo5'));
$action2->next[] = $action3;
$action2->next[] = $action4;
$action1->next[] = $action2;
947
Zend_Pdf
Zend_Pdf_Target Zend_Pdf::getOpenAction() method returns current document open action (or null
if open action is not set).
Zend_Pdf class provides public property $outlines which is an array of Zend_Pdf_Outline objects.
$pdf = Zend_Pdf::load($path);
$pdf->save($path, true);
948
Zend_Pdf
• Zend_Pdf_Target getTarget() - get outline target (action or explicit or named destination object).
• setOptions(array $options) - set outline options. The following options are recognized: 'title', 'open',
'color', 'italic', 'bold', and 'target'.
• Zend_Pdf_Outline::create(array $options)
Each outline object may have child outline items listed in Zend_Pdf_Outline::$childOutlines public
property. It's an array of Zend_Pdf_Outline objects, so outlines are organized in a tree.
Zend_Pdf_Outline class implements RecursiveArray interface, so child outlines may be recursively iterated using
RecursiveIteratorIterator:
$pdf = Zend_Pdf::load($path);
949
Zend_Pdf
$pdf->save($path, true);
Nota
All outline items with unresolved destinations (or destinations of GoTo actions) are updated while document
saving by setting their targets to null. So document will not be corrupted by removing pages referenced by
outlines.
Annotations
An annotation associates an object such as a note, sound, or movie with a location on a page of a PDF document, or
provides a way to interact with the user by means of the mouse and keyboard.
A link annotation represents either a hypertext link to a destination elsewhere in the document or an action to be
performed.
A text annotation represents a "sticky note" attached to a point in the PDF document.
• setLeft(float $left)
• float getLeft()
• setRight(float $right)
• float getRight()
• setTop(float $top)
• float getTop()
• setBottom(float $bottom)
• float getBottom()
• setText(string $text)
950
Zend_Pdf
• string getText()
Text annotation property is a text to be displayed for the annotation or, if this type of annotation does not display text,
an alternate description of the annotation's contents in human-readable form.
• setDestination(Zend_Pdf_Target|string $target)
• Zend_Pdf_Target getDestination()
Históricamente, esta información se almacena usando una estructura especial de Información. Esta estructura está
disponible para lectura y la escritura como una array asociativo utilizando propiedades públicas properties de
objetos Zend_Pdf:
$pdf = Zend_Pdf::load($pdfPath);
Las siguientes claves están definidas por v1.4 PDF (Acrobat 5) estándar:
• Creator - string, opcional, si el documento se convirtió desde otro formato a PDF, el nombre de la aplicación (por
ejemplo, Adobe FrameMaker ®) que creó el documento original a partir del cual se convirtió.
• Producer - string, opcional, si el documento se convirtió desde otro formato a PDF, el nombre de la aplicación (por
ejemplo, Acrobat Distiller), que lo convirtió a PDF.
• CreationDate - string, opcional, la fecha y la hora en que el documento fue creado, en la forma siguiente:
"D:YYYYMMDDHHmmSSOHH'mm'", en la que:
• YYYY es el año.
• MM es el mes.
• DD es el día (01–31).
• HH es la hora (00–23).
• mm es el minuto (00–59).
951
Zend_Pdf
• SS es el segundo (00–59).
• O es la relación de la hora local a la hora universal (UT), identificado por uno de los caracteres +, -, o Z (véase
más adelante).
Por ejemplo, el 23 de diciembre de 1998, a las 7:52 PM, hora estándar del Pacífico de EE.UU., está representado
por el string "D:199812231952-08'00'".
• ModDate - string, opcional, la fecha y la hora en que el documento fue modificado mas recientemente, de la misma
forma que CreationDate.
• Trapped - booleano, opcional, indica si el documento ha sido modificado para incluir la captura de información.
• false - El documento aún no ha sido capturado; cualquier captura todavía debe ser hecha.
• null - O bien se desconoce si el documento ha sido capturado o que lo ha sido en parte pero no completamente
capturado, y alguna captura adicional puede ser necesaria.
Desde PDF v 1.6 los metadatos se pueden almacenar en el documento XML especial adjunto al PDF (XMP - Extensible
Metadata Platform [http://www.adobe.com/products/xmp/]).
Este documento XML se pueden recuperar y adjuntar al PDF con los métodos Zend_Pdf::getMetadata() y
Zend_Pdf::setMetadata($metadata):
$pdf = Zend_Pdf::load($pdfPath);
$metadata = $pdf->getMetadata();
$metadataDOM = new DOMDocument();
$metadataDOM->loadXML($metadata);
$titleNode = $xpath->query('/rdf:RDF/rdf:Description/pdf:Title')->item(0);
$title = $titleNode->nodeValue;
...
952
Zend_Pdf
$pdf->save($pdfPath);
Las propiedades comunes del documento se duplican en la estructura de Info y el documento Metadata (si se presentan).
Ahora es responsabilidad del usuario la aplicación para mantenerlos sincronizados.
También está el archivo test.pdf, que puede ser usado con esta demo con fines de prueba.
/**
* @package Zend_Pdf
* @subpackage demo
*/
if (!isset($argv[1])) {
echo "USAGE: php demo.php <pdf_file> [<output_pdf_file>]\n";
exit;
}
try {
$pdf = Zend_Pdf::load($argv[1]);
} catch (Zend_Pdf_Exception $e) {
if ($e->getMessage() == 'Can not open \'' . $argv[1] .
'\' file for reading.') {
// Crear un nuevo archivo PDF si no existe
$pdf = new Zend_Pdf();
if (!isset($argv[2])) {
// forzar una reescritura completa del archivo (en vez de actualizar)
$argv[2] = $argv[1];
}
} else {
// Arrojar una excepción si no es la excepción "Can't open file"
throw $e;
}
}
//------------------------------------------------------------------------
// Invertir el orden de las páginas
$pdf->pages = array_reverse($pdf->pages);
953
Zend_Pdf
$fontH = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD);
$style->setFont($fontH, 32);
try {
// Crear un nuevo objeto imagen
$imageFile = dirname(__FILE__) . '/stamp.jpg';
$stampImage = Zend_Pdf_Image::imageWithPath($imageFile);
} catch (Zend_Pdf_Exception $e) {
// Ejemplo de operación con excepciones de carga de imágenes.
if ($e->getMessage() != 'Extensión de imagen no está instalada.' &&
$e->getMessage() != 'El soporte a JPG no está configurado correctamente.') {
throw $e;
}
$stampImage = null;
}
$page->saveGS();
$page->clipCircle(550, -10, 50);
if ($stampImage != null) {
$page->drawImage($stampImage, 500, -60, 600, 40);
}
$page->restoreGS();
954
Zend_Pdf
// Dibujar un rectángulo
$page2->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
->setLineDashingPattern(array(3, 2, 3, 4), 1.6)
->drawRectangle(60, 400, 400, 350);
// Dibujar un círculo
$page2->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
->drawCircle(85, 375, 25);
// Dibujar sectores
$page2->drawCircle(200, 375, 25, 2*M_PI/3, -M_PI/6)
->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
->drawCircle(200, 375, 25, M_PI/6, 2*M_PI/3)
->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
->drawCircle(200, 375, 25, -M_PI/6, M_PI/6);
// Dibujar un rectángulo
$page2->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
->setLineDashingPattern(array(3, 2, 3, 4), 1.6)
955
Zend_Pdf
// Dibujar un círculo
$page2->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
->drawCircle(25, 25, 25);
// Dibujar sectores
$page2->drawCircle(140, 25, 25, 2*M_PI/3, -M_PI/6)
->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
->drawCircle(140, 25, 25, M_PI/6, 2*M_PI/3)
->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
->drawCircle(140, 25, 25, -M_PI/6, M_PI/6);
$page2->restoreGS();
// Dibujar un rectángulo
$page2->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
->setLineDashingPattern(array(3, 2, 3, 4), 1.6)
->drawRectangle(0, 50, 340, 0);
// Dibujar un círculo
956
Zend_Pdf
$page2->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
->drawCircle(25, 25, 25);
// Dibujar sectores
$page2->drawCircle(140, 25, 25, 2*M_PI/3, -M_PI/6)
->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
->drawCircle(140, 25, 25, M_PI/6, 2*M_PI/3)
->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
->drawCircle(140, 25, 25, -M_PI/6, M_PI/6);
$page2->restoreGS();
//------------------------------------------------------------------------
if (isset($argv[2])) {
$pdf->save($argv[2]);
} else {
$pdf->save($argv[1], true /* update */);
}
957
958
Capítulo 41. Zend_ProgressBar
Zend_ProgressBar
Introduction
Zend_ProgressBar is a component to create and update progressbars in different environments. It consists of a
single backend, which outputs the progress through one of the multiple adapters. On every update, it takes an absolute
value and optionally a status message, and then calls the adapter with some precalculated values like percentage and
estimated time left.
while (!feof($fp)) {
// Do something
$progressBar->update($currentByteCount);
}
$progressBar->finish();
In the first step, an instance of Zend_ProgressBar is created, with a specific adapter, a min-value of 0 and a max-
value of the total filesize. Then a file is processed and in every loop the progressbar is updated with the current byte
count. At the end of the loop, the progressbar status is set to finished.
You can also call the update() method of Zend_ProgressBar without arguments, which just recalculates ETA
and notifies the adapter. This is useful when there is no data update but you want the progressbar to be updated.
Persistent progress
If you want the progressbar to be persistent over multiple requests, you can give the name of a session namespace
as fourth argument to the constructor. In that case, the progressbar will not notify the adapter within the constructor,
but only when you call update() or finish(). Also the current value, the status text and the start time for ETA
calculation will be fetched in the next request run again.
Standard adapters
Zend_ProgressBar comes with the following three adapters:
959
Zend_ProgressBar
Zend_ProgressBar_Adapter_Console
Zend_ProgressBar_Adapter_Console is a text-based adapter for terminals. It can automatically detect
terminal widths but supports custom widths as well. You can define which elements are displayed with the progressbar
and as well customize the order of them. You can also define the style of the progressbar itself.
You can set the adapter options either via the set* methods or give an array or a Zend_Config instance with
options as first parameter to the constructor. The available options are:
• outputStream: A different output-stream, if you don't want to stream to STDOUT. Can be any other stream like
php://stderr or a path to a file.
• elements: Either NULL for default or an array with at least one of the following constants of
Zend_Console_ProgressBar as value:
• ELEMENT_ETA: The automatic calculated ETA. This element is firstly displayed after five seconds, because in
this time, it is not able to calculate accurate results.
• barIndicatorChar: A string which is used for the indicator in the progressbar. This one can be empty.
Zend_ProgressBar_Adapter_JsPush
Zend_ProgressBar_Adapter_JsPush is an adapter which let's you update a progressbar in a browser via
Javascript Push. This means that no second connection is required to gather the status about a running process, but
that the process itself sends its status directly to the browser.
You can set the adapter options either via the set* methods or give an array or a Zend_Config instance with
options as first parameter to the constructor. The available options are:
• updateMethodName: The javascript method which should be called on every update. Default value is
Zend_ProgressBar_Update.
• finishMethodName: The javascript method which should be called after finish status was set. Default value is
NULL, which means nothing is done.
960
Zend_ProgressBar
The usage of this adapter is quite simple. First you create a progressbar in your browser, either with JavaScript
or previously created with plain HTML. Then you define the update method and optionally the finish method in
JavaScript, both taking a json object as single argument. Then you call a webpage with the long-running process in a
hidden iframe or object tag. While the process is running, the adapter will call the update method on every update
with a json object, containing the following parameters:
<div id="zend-progressbar-container">
<div id="zend-progressbar-done"></div>
</div>
#long-running-process {
position: absolute;
left: -100px;
top: -100px;
width: 1px;
height: 1px;
}
#zend-progressbar-container {
width: 100px;
height: 30px;
#zend-progressbar-done {
width: 0;
height: 30px;
background-color: #000000;
}
961
Zend_ProgressBar
function Zend_ProgressBar_Update(data)
{
document.getElementById('zend-progressbar-done').style.width = data.percent + '%';
}
This will create a simple container with a black border and a block which indicates the current process. You should
not hide the iframe or object by display: none;, as some browsers like Safari 2 will not load the actual
content then.
Instead of creating your custom progressbar, you may want to use one of the available JavaScript libraries like Dojo,
jQuery etc. For example, there are:
• Dojo: http://dojotoolkit.org/book/dojo-book-0-9/part-2-dijit/user-assistance-and-feedback/progress-bar
• jQuery: http://t.wits.sg/2008/06/20/jquery-progress-bar-11/
• MooTools: http://davidwalsh.name/dw-content/progress-bar.php
• Prototype: http://livepipe.net/control/progressbar
Interval of updates
You should take care of not sending too many updates, as every update has a min-size of 1kb. This is a
requirement for the Safari browser to actually render and execute the function call. Internet Explorer has a
similar limitation of 256 bytes.
Zend_ProgressBar_Adapter_JsPull
Zend_ProgressBar_Adapter_JsPull is the opposite of jsPush, as it requires to pull for new updates, instead
of pushing updates out to the browsers. Generally you should use the adapter with the persistence option of the
Zend_ProgressBar. On notify, the adapter sends a JSON string to the browser, which looks exactly like the JSON
string which is send by the jsPush adapter. The only difference is, that it contains an additional parameter, finished,
which is either FALSE when update() is called or TRUE, when finish() is called.
You can set the adapter options either via the set* methods or give an array or a Zend_Config instance with
options as first parameter to the constructor. The available options are:
• exitAfterSend: Exits the current request after the data were send to the browser. Default is TRUE.
962
Capítulo 42. Zend_Queue
Introduction
Zend_Queue provides a factory function to create specific queue client objects.
A message queue is a method for distributed processing. For example, a Job Broker application may accept multiple
applications for jobs from a variety of sources.
You could create a queue "/queue/applications" that would have a sender and a receiver. The sender would
be any available source that could connect to your message service or indirectly to an application (web) that could
connect to the message service.
<resume>
<name>John Smith</name>
<location>
<city>San Francisco</city>
<state>California</state>
<zip>00001</zip>
</location>
<skills>
<programming>PHP</programming>
<programming>Perl</programming>
</skills>
</resume>
The recipient or consumer of the queue would pick up the message and process the resume.
There are many messaging patterns that can be applied to queues to abstract the flow of control from
the code and provide metrics, transformations, and monitoring of messages queues. A good book on
messaging patterns is Enterprise Integration Patterns: Designing, Building, and Deploying Messaging Solutions
(Addison-Wesley Signature Series) [http://www.amazon.com/Enterprise-Integration-Patterns-Designing-Addison-
Wesley/dp/0321200683] (ISBN-10 0321127420; ISBN-13 978-0321127426).
Ejemplo usage
The below example of Zend_Queue shows a variety of features, including queue creation, queue retrieval, message
retrieval, message deletion, and sending messages.
963
Zend_Queue
Framework
The Zend_Queue is a proxy that hides the details of the queue services. The queue services are represented
by Zend_Queue_Adapter_<service>. For example, Zend_Queue_Adapter_Db is a queue that will use
database tables to store and retrieve messages.
$options = array(
'name' => 'queue1',
'driverOptions' => array(
'host' => '127.0.0.1',
'port' => '3306',
'username' => 'queue',
'password' => 'queue',
'dbname' => 'queue',
'type' => 'pdo_mysql'
)
);
The Zend_Queue constructor will create a Zend_Queue_Adapter_Db and initialize the adapter with the
configuration settings.
964
Zend_Queue
The accepted configuration settings for each adapter are provided in the adapter notes.
$messages = $queue->receive(5);
foreach ($messages as $i => $message) {
echo "$i) Message => ", $message->body, "\n";
}
Introduction
Zend_Queue is a proxy class that represents an adapter.
The send(), count($queue), and receive() methods are employed by each adapter to interact with queues.
send() is not available in all adapters; the Zend_Queue_Adapter_Null does not support send().
receive() is not available in all adapters; the Zend_Queue_Adapter_Null does not support receive().
Adapters
Zend_Queue supports all queues implementing the interface Zend_Queue_Adapter_AdapterInterface.
The following Message Queue services are supported:
965
Zend_Queue
Limitations
Message transaction handling is not supported.
• $options['name'] = '/temp/queue1';
This is the name of the queue that you wish to start using. (Required)
• $options['driverOptions']['host'] = 'host.domain.tld';
$options['driverOptions']['host'] = '127.0.0.1';
• $options['driverOptions']['port'] = 61613;
• $options['driverOptions']['username'] = 'username';
Optional for some messaging servers. Read the manual for your messaging server.
• $options['driverOptions']['password'] = 'password';
Optional for some messaging servers. Read the manual for your messaging server.
• $options['driverOptions']['timeout_sec'] = 2;
$options['driverOptions']['timeout_usec'] = 0;
This is the amount of time that Zend_Queue_Adapter_Activemq will wait for read activity on a socket before
returning no messages.
Db - Zend_Queue_Adapter_Db
Driver options are checked for a few required options such as type, host, username, password, and
dbname. You may pass along additional parameters for Zend_DB::factory() as parameters in
966
Zend_Queue
$options['driverOptions']. An example of an additional option not listed here, but could be passed would
be port.
$options = array(
'driverOptions' => array(
'host' => 'db1.domain.tld',
'username' => 'my_username',
'password' => 'my_password',
'dbname' => 'messaging',
'type' => 'pdo_mysql',
'port' => 3306, // optional parameter.
),
'options' => array(
// use Zend_Db_Select for update, not all databases can support this
// feature.
Zend_Db_Select::FOR_UPDATE => true
)
);
• $options['name'] = 'queue1';
This is the name of the queue that you wish to start using. (Required)
• $options['driverOptions']['type'] = 'Pdo';
type is the adapter you wish to have Zend_Db::factory() use. This is the first parameter for the
Zend_Db::factory() class method call.
• $options['driverOptions']['host'] = 'host.domain.tld';
$options['driverOptions']['host'] = '127.0.0.1';
• $options['driverOptions']['username'] = 'username';
• $options['driverOptions']['password'] = 'password';
• $options['driverOptions']['dbname'] = 'dbname';
The database name that you have created the required tables for. See the notes section below.
MemcacheQ - Zend_Queue_Adapter_Memcacheq
• $options['name'] = 'queue1';
This is the name of the queue that you wish to start using. (Required)
• $options['driverOptions']['host'] = 'host.domain.tld';
$options['driverOptions']['host'] = '127.0.0.1;'
967
Zend_Queue
• $options['driverOptions']['port'] = 22201;
The hostname and port corresponding to the Zend Platform Job Queue daemon you will use. (Required)
• $options['daemonOptions']['password'] = '1234';
The password required for accessing the Zend Platform Job Queue daemon. (Required)
Array - Zend_Queue_Adapter_Array
• $options['name'] = 'queue1';
This is the name of the queue that you wish to start using. (Required)
Apache ActiveMQ
Visibility duration for Zend_Queue_Adapter_Activemq is not available.
While Apache's ActiveMQ will support multiple subscriptions, the Zend_Queue does not. You must create a new
Zend_Queue object for each individual subscription.
• /queue/
• /topic/
• /temp-queue/
• /temp-topic/
968
Zend_Queue
Zend_Db
The database CREATE TABLE ( ... ) SQL statement can be found in Zend/Queue/Adapter/Db/queue.sql.
MemcacheQ
Memcache can be downloaded from http://www.danga.com/memcached/.
• deleteMessage() - Messages are deleted upon reception from the queue. Calling this function would have no
effect. Calling this function will throw an error.
• count() or count($adapter) - MemcacheQ does not support a method for counting the number of items in
a queue. Calling this function will throw an error.
The following is a list of methods where this adapter's behavior diverges from the standard offerings:
• create() - Zend Platform does not have the concept of discrete queues; instead, it allows administrators to provide
scripts for processing jobs. Since adding new scripts is restricted to the administration interface, this method simply
throws an exception indicating the action is forbidden.
• isExists() - Just like create(), since Job Queue does not have a notion of named queues, this method throws
an exception when invoked.
• delete() - similar to create(), deletion of JQ scripts is not possible except via the admin interface; this method
raises an exception.
• getQueues() - Zend Platform does not allow introspection into the attached job handling scripts via the API.
This method throws an exception.
• count() - returns the total number of jobs currently active in the Job Queue.
• send() - this method is perhaps the one method that diverges most from other adapters. The $message argument
may be one of three possible types, and will operate differently based on the value passed:
• string - the name of a script registered with Job Queue to invoke. If passed in this way, no arguments are provided
to the script.
• array - an array of values with which to configure a ZendApi_Job object. These may include the following:
• priority - the job priority to use when registering with the queue.
• predecessor - the ID of a job on which this one depends, and which must be executed before this one
may begin.
969
Zend_Queue
• preserved - whether or not to retain the job within the Job Queue history. By default, off; pass a true value
to retain it.
• user_variables - an associative array of all variables you wish to have in scope during job execution
(similar to named arguments).
• interval - how often, in seconds, the job should run. By default, this is set to 0, indicating it should run
once, and once only.
• end_time - an expiry time, past which the job should not run. If the job was set to run only once, and
end_time has passed, then the job will not be executed. If the job was set to run on an interval, it will not
execute again once end_time has passed.
• schedule_time - a UNIX timestamp indicating when to run the job; by default, 0, indicating the job should
run as soon as possible.
• application_id - the application identifier of the job. By default, this is null, indicating that one will be
automatically assigned by the queue, if the queue was assigned an application ID.
As noted, only the script argument is required; all others are simply available to allow passing more fine-
grained detail on how and when to run the job.
• ZendApi_Job - finally, you may simply pass a ZendApi_Job instance, and it will be passed along to
Platform's Job Queue.
• receive() - retrieves a list of active jobs from Job Queue. Each job in the returned set will be an instance of
Zend_Queue_Message_PlatformJob.
• deleteMessage() - since this adapter only works with Job Queue, this method expects the provided $message
to be a Zend_Queue_Message_PlatformJob instance, and will throw an exception otherwise.
Array (local)
The Array queue is a PHP array() in local memory. The Zend_Queue_Adapter_Array is good for unit testing.
Customizing Zend_Queue
Creating your own adapter
Zend_Queue will accept any adapter that implements Zend_Queue_Adapter_AdapterAbstract.
You can create your own adapter by extending one of the existing adapters, or the abstract class
Zend_Queue_Adapter_AdapterAbstract. I suggest reviewing Zend_Queue_Adapter_Array as this
adapter is the easiest to conceptualize.
970
Zend_Queue
* Custom_DbForUpdate uses the SELECT ... FOR UPDATE to find it's rows.
* this is more likely to produce the wanted rows than the existing code.
*
* However, not all databases have SELECT ... FOR UPDATE as a feature.
*
* Note: this was later converted to be an option for Zend_Queue_Adapter_Db
*
* This code still serves as a good example.
*/
}
$options = array(
'name' => 'queue1',
'driverOptions' => array(
'host' => '127.0.0.1',
'port' => '3306',
'username' => 'queue',
'password' => 'queue',
'dbname' => 'queue',
'type' => 'pdo_mysql'
)
);
or
$options = array(
'name' => 'queue1',
'namespace' => 'Custom',
'driverOptions' => array(
'host' => '127.0.0.1',
'port' => '3306',
'username' => 'queue',
'password' => 'queue',
'dbname' => 'queue',
'type' => 'pdo_mysql'
)
);
$queue = Zend_Queue('DbForUpdate', $config); // loads Custom_DbForUpdate
971
Zend_Queue
class Zend_Queue_Message
{
protected $_data = array();
}
You can extend the existing messaging class. See the example code in tests/Zend/Queue/Custom/
Message.php.
Stomp
Zend_Queue_Stomp provides a basic client to communicate with Stomp [http://stomp.codehaus.org/] compatible
servers. Some servers, such as Apache ActiveMQ and RabbitMQ, will allow you to communicate by other methods,
such as HTTP, and XMPP.
• Zend_Queue_Stomp_Client. This class provides the basic functions to send() and receive()
Zend_Queue_Stomp_Frames to and from a Stomp compatible server.
972
Capítulo 43. Zend_Reflection
Introduction
Zend_Reflection is a drop-in extension to PHP's own Reflection API [http://php.net/reflection], providing several
additional features:
• DocBlocks gain a Reflection class, allowing introspection of docblocks. This provides the ability to determine what
annotation tags have been defined as well as to retrieve their values, and the ability to retrieve the short and long
descriptions.
• Files gain a Reflection class, allowing introspection of PHP files. This provides the ability to determine what
functions and classes are defined in a given file, as well as to introspect them.
• Ability to override any Reflection class with your own variant, for the entire reflection tree you create.
In general, Zend_Reflection works just like the standard Reflection API, but provides a few additional methods
for retrieving artifacts not defined in the Reflection API.
Zend_Reflection Ejemplos
Ejemplo 43.1. Performing reflection on a file
$r = new Zend_Reflection_File($filename);
printf(
"===> The %s file\n".
" has %d lines\n",
$r->getFileName(),
$r->getEndLine()
);
$classes = $r->getClasses();
echo " It has " . count($classes) . ":\n";
foreach ($classes as $class) {
echo " " . $class->getName() . "\n";
}
$functions = $r->getFunctions();
echo " It has " . count($functions) . ":\n";
foreach ($functions as $function) {
echo " " . $function->getName() . "\n";
}
973
Zend_Reflection
$r = new Zend_Reflection_Class($class);
printf(
"The class level docblock has the short description: %s\n".
"The class level docblock has the long description:\n%s\n",
$r->getDocblock()->getShortDescription(),
$r->getDocblock()->getLongDescription(),
);
printf(
"The method '%s' has a return type of %s",
$r->getName(),
$r->getReturn()
);
printf(
"The short description: %s\n".
"The long description:\n%s\n",
$r->getDocblock()->getShortDescription(),
$r->getDocblock()->getLongDescription(),
);
974
Zend_Reflection
Zend_Reflection Reference
The various classes in Zend_Reflection mimic the API of PHP's Reflection API [http://php.net/reflection] - with
one important difference. PHP's Reflection API does not provide introspection into docblock annotation tags, nor into
parameter variable types or return types.
Zend_Reflection analyzes method docblock annotations to determine parameter variable types and the return
type. Specifically, the @param and @return annotations are used. However, you can also check for any other
annotation tags, as well as the standard "short" and "long" descriptions.
Each reflection object in Zend_Reflection overrides the getDocblock() method to return an instance of
Zend_Reflection_Docblock. This class provides introspection into the docblocks and annotation tags.
Zend_Reflection_File is a new reflection class that allows introspection of PHP files. With it, you can retrieve
the classes, functions, and global PHP code contained in the file.
Finally, the various methods that return other reflection objects allow a second parameter, the name of the reflection
class to use for the returned reflection object.
Zend_Reflection_Docblock
Zend_Reflection_Docblock is the heart of Zend_Reflection's value-add over PHP's Reflection API. It
provides the following methods:
• getStartLine(): returns the starting position of the docblock within the defining file.
• getShortDescription(): get the short, one-line description (usually the first line of the docblock).
• getTag($name): Retrieve the given annotation tag reflection object, or a boolean FALSE if it's not present.
• getTags($filter): Retrieve all tags, or all tags matching the given $filter string. The tags returned will
be an array of Zend_Reflection_Docblock_Tag objects.
Zend_Reflection_Docblock_Tag
Zend_Reflection_Docblock_Tag provides reflection for individual annotation tags. Most tags consist of only
a name and a description. In the case of some special tags, the class provides a factory method for retrieving an instance
of the appropriate class.
• factory($tagDocblockLine): instantiate the appropriate tag reflection class and return it.
975
Zend_Reflection
Zend_Reflection_Docblock_Tag_Param
Zend_Reflection_Docblock_Tag_Param is a specialized version of
Zend_Reflection_Docblock_Tag. The @param annotation tag description consists of the parameter type,
variable name, and variable description. It adds the following methods to Zend_Reflection_Docblock_Tag:
Zend_Reflection_Docblock_Tag_Return
Like Zend_Reflection_Docblock_Tag_Param, Zend_Reflection_Docblock_Tag_Return is a
specialized version of Zend_Reflection_Docblock_Tag. The @return annotation tag description consists
of the return type and variable description. It adds the following method to Zend_Reflection_Docblock_Tag:
Zend_Reflection_File
Zend_Reflection_File provides introspection into PHP files. With it, you can introspect the classes, functions,
and bare PHP code defined in a file. It defines the following methods:
Zend_Reflection_Class
Zend_Reflection_Class extends ReflectionClass, and follows its API. It adds one additional method,
getDeclaringFile(), which may be used to retrieve the Zend_Reflection_File reflection object for the
defining file.
Additionally, the following methods add an additional argument for specifying the reflection class to use when fetching
a reflection object:
• getDeclaringFile($reflectionClass = 'Zend_Reflection_File')
• getDocblock($reflectionClass = 'Zend_Reflection_Docblock')
976
Zend_Reflection
• getInterfaces($reflectionClass = 'Zend_Reflection_Class')
• getMethod($reflectionClass = 'Zend_Reflection_Method')
• getParentClass($reflectionClass = 'Zend_Reflection_Class')
Zend_Reflection_Extension
Zend_Reflection_Extension extends ReflectionExtension, and follows its API. It overrides the
following methods to add an additional argument for specifying the reflection class to use when fetching a reflection
object:
Zend_Reflection_Function
Zend_Reflection_Function adds a method for retrieving the function return type, as well as overrides several
methods to allow specifying the reflection class to use for returned reflection objects.
Zend_Reflection_Method
Zend_Reflection_Method mirrors Zend_Reflection_Function, and only overrides one additional
method:
Zend_Reflection_Parameter
Zend_Reflection_Parameter adds a method for retrieving the parameter type, as well as overrides methods
to allow specifying the reflection class to use on returned reflection objects.
977
Zend_Reflection
Zend_Reflection_Property
Zend_Reflection_Property overrides a single method in order to allow specifying the returned reflection
object class:
978
Capítulo 44. Zend_Registry
Using the Registry
A registry is a container for storing objects and values in the application space. By storing the value in a registry, the
same object is always available throughout your application. This mechanism is an alternative to using global storage.
The typical method to use registries with Zend Framework is through static methods in the Zend_Registry class.
Alternatively, the registry can be used as an array object, so you can access elements stored within it with a convenient
array-like interface.
Zend_Registry::set('index', $value);
The value returned can be an object, an array, or a scalar. You can change the value stored in a specific entry of the
registry by calling the set() method to set the entry to a new value.
The index can be a scalar (NULL, string, or number), like an ordinary array.
$value = Zend_Registry::get('index');
The getInstance() method returns the singleton registry object. This registry object is iterable, making all values
stored in the registry easily accessible.
$registry = Zend_Registry::getInstance();
The registry instance you access through the static methods is simply one such instance. It is for convenience that it
is stored statically, so that it is accessible from anywhere in an application.
979
Zend_Registry
Use the traditional new operator to instantiate Zend_Registry. Instantiating Zend_Registry using its
constructor also makes initializing the entries in the registry simple by taking an associative array as an argument.
Once such a Zend_Registry object is instantiated, you can use it by calling any array object method or by setting
it as the singleton instance for Zend_Registry with the static method setInstance().
Zend_Registry::setInstance($registry);
The setInstance() method throws a Zend_Exception if the static registry has already been initialized.
$registry = Zend_Registry::getInstance();
$registry['index'] = $value;
var_dump( $registry['index'] );
Nota
You must set the ArrayObject::ARRAY_AS_PROPS option before the static registry has been accessed
for the first time.
980
Zend_Registry
Zend_Registry::setInstance($registry);
$registry->tree = 'apple';
.
.
.
$registry->index = $value;
var_dump($registry->index);
if (Zend_Registry::isRegistered($index)) {
$value = Zend_Registry::get($index);
}
To find out if a particular index in a registry array or object has a value, use the isset() function as you would
with an ordinary array.
$registry = Zend_Registry::getInstance();
Nota
The class must be a subclass of Zend_Registry.
981
Zend_Registry
Zend_Registry::setClassName('My_Registry');
Zend_Registry::set('index', $value);
The registry throws a Zend_Exception if you attempt to set the classname after the registry has been accessed for
the first time. It is therefore recommended that you specify the class name for your static registry in your application
bootstrap.
You might use this method, for example, if you want to use setInstance() or setClassName() after the
singleton registry object has been initialized. Unsetting the singleton instance allows you to use these methods even
after the singleton registry object has been set. Using Zend_Registry in this manner is not recommended for typical
applications and environments.
Zend_Registry::set('index', $value);
Zend_Registry::_unsetInstance();
Zend_Registry::set('index', $value);
982
Capítulo 45. Zend_Rest
Introduction
REST Web Services use service-specific XML formats. These ad-hoc standards mean that the manner for accessing a
REST web service is different for each service. REST web services typically use URL parameters (GET data) or path
information for requesting data and POST data for sending data.
Zend Framework provides both Client and Server capabilities, which, when used together allow for a much more
"local" interface experience via virtual object property access. The Server component features automatic exposition
of functions and classes using a meaningful and simple XML format. When accessing these services using the Client,
it is possible to easily retrieve the return data from the remote call. Should you wish to use the client with a non-
Zend_Rest_Server based service, it will still provide easier data access.
Zend_Rest_Client
Introduction
Using the Zend_Rest_Client is very similar to using SoapClient objects (SOAP web service extension [http://
www.php.net/soap]). You can simply call the REST service procedures as Zend_Rest_Client methods. Specify
the service's full address in the Zend_Rest_Client constructor.
/**
* Connect to framework.zend.com server and retrieve a greeting
*/
$client = new Zend_Rest_Client('http://framework.zend.com/rest');
Differences in calling
Zend_Rest_Client attempts to make remote methods look as much like native methods as possible, the
only difference being that you must follow the method call with one of either get(), post(), put() or
delete(). This call may be made via method chaining or in separate method calls:
$client->sayHello('Davey', 'Day');
echo $client->get();
Responses
All requests made using Zend_Rest_Client return a Zend_Rest_Client_Response object. This object has
many properties that make it easier to access the results.
When the service is based on Zend_Rest_Server, Zend_Rest_Client can make several assumptions about
the response, including response status (success or failure) and return type.
983
Zend_Rest
if ($result->isSuccess()) {
echo $result; // "Hello Davey, Good Day"
}
In the example above, you can see that we use the request result as an object, to call isSuccess(), and then because
of __toString(), we can simply echo the object to get the result. Zend_Rest_Client_Response will allow
you to echo any scalar value. For complex types, you can use either array or object notation.
If however, you wish to query a service not using Zend_Rest_Server the Zend_Rest_Client_Response
object will behave more like a SimpleXMLElement. However, to make things easier, it will automatically query
the XML using XPath if the property is not a direct descendant of the document root element. Additionally, if you
access a property as a method, you will receive the PHP value for the object, or an array of PHP value results.
984
Zend_Rest
<rank>60635</rank>
</weblog>
<inboundblogs>44</inboundblogs>
<inboundlinks>218</inboundlinks>
</result>
</document>
</tapi>
Here we are accessing the firstname and lastname properties. Even though these are not top-level elements,
they are automatically returned when accessed by name.
Multiple items
If multiple items are found when accessing a value by name, an array of SimpleXMLElements will be
returned; accessing via method notation will return an array of PHP values.
Request Arguments
Unless you are making a request to a Zend_Rest_Server based service, chances are you will need to send multiple
arguments with your request. This is done by calling a method with the name of the argument, passing in the value as
the first (and only) argument. Each of these method calls returns the object itself, allowing for chaining, or "fluent"
usage. The first call, or the first argument if you pass in more than one argument, is always assumed to be the method
when calling a Zend_Rest_Server service.
$client->arg('value1');
$client->arg2('value2');
$client->get();
// or
$client->arg('value1')->arg2('value2')->get();
Both of the methods in the example above, will result in the following get args: ?
method=arg&arg1=value1&arg=value1&arg2=value2
You will notice that the first call of $client->arg('value1'); resulted in both
method=arg&arg1=value1 and arg=value1; this is so that Zend_Rest_Server can understand the request
properly, rather than requiring pre-existing knowledge of the service.
Strictness of Zend_Rest_Client
Any REST service that is strict about the arguments it receives will likely fail using Zend_Rest_Client,
because of the behavior described above. This is not a common practice and should not cause problems.
Zend_Rest_Server
Introduction
Zend_Rest_Server is intended as a fully-featured REST server.
985
Zend_Rest
/**
* Say Hello
*
* @param string $who
* @param string $when
* @return string
*/
function sayHello($who, $when)
{
return "Hello $who, Good $when";
}
Numeric index
Numeric arguments use a 1-based index.
To call sayHello from the example above, you can use either:
?method=sayHello&who=Davey&when=Day
or:
?method=sayHello&arg1=Davey&arg2=Day
986
Zend_Rest
/**
* Say Hello
*
* @param string $who
* @param string $when
* @return array
*/
function sayHello($who, $when)
{
return array('msg' => "An Error Occurred", 'status' => false);
}
/**
* Say Hello
*
* @param string $who
* @param string $when
* @return SimpleXMLElement
*/
function sayHello($who, $when)
{
$xml ='<?xml version="1.0" encoding="ISO-8859-1"?>
<mysite>
<value>Hey $who! Hope you\'re having a good $when</value>
<code>200</code>
</mysite>';
$xml = simplexml_load_string($xml);
return $xml;
}
$server->handle();
The response from the service will be returned without modification to the client.
987
988
Capítulo 46. Zend_Search_Lucene
Overview
Introduction
Zend_Search_Lucene is a general purpose text search engine written entirely in PHP 5. Since it stores its index on
the filesystem and does not require a database server, it can add search capabilities to almost any PHP-driven website.
Zend_Search_Lucene supports the following features:
• Many powerful query types: phrase queries, boolean queries, wildcard queries, proximity queries, range queries
and many others.
Zend_Search_Lucene was derived from the Apache Lucene project. The currently (starting from ZF 1.6)
supported Lucene index format versions are 1.4 - 2.3. For more information on Lucene, visit http://lucene.apache.org/
java/docs/.
Previous Zend_Search_Lucene implementations support the Lucene 1.4 (1.9) - 2.1 index formats.
Starting from Zend Framework 1.5 any index created using pre-2.1 index format is automatically
upgraded to Lucene 2.1 format after the Zend_Search_Lucene update and will not be compatible with
Zend_Search_Lucene implementations included into Zend Framework 1.0.x.
A document is represented by the Zend_Search_Lucene_Document class, and this objects of this class contain
instances of Zend_Search_Lucene_Field that represent the fields on the document.
It is important to note that any information can be added to the index. Application-specific information or metadata
can be stored in the document fields, and later retrieved with the document during search.
It is the responsibility of your application to control the indexer. This means that data can be indexed from any source
that is accessible by your application. For example, this could be the filesystem, a database, an HTML form, etc.
Zend_Search_Lucene_Field class provides several static methods to create fields with different characteristics:
// Field is not tokenized, but is indexed and stored within the index.
// Stored fields can be retrived from the index.
$doc->addField(Zend_Search_Lucene_Field::Keyword('doctype',
'autogenerated'));
989
Zend_Search_Lucene
$doc->addField(Zend_Search_Lucene_Field::UnIndexed('created',
time()));
Encoding may differ for different documents as well as for different fields within one document:
If encoding parameter is omitted, then the current locale is used at processing time. For example:
setlocale(LC_ALL, 'de_DE.iso-8859-1');
...
$doc->addField(Zend_Search_Lucene_Field::UnStored('contents', $contents));
Fields are always stored and returned from the index in UTF-8 encoding. Any required conversion to UTF-8 happens
automatically.
Text analyzers (see below) may also convert text to some other encodings. Actually, the default analyzer converts text
to 'ASCII//TRANSLIT' encoding. Be careful, however; this translation may depend on current locale.
Java Lucene uses the 'contents' field as a default field to search. Zend_Search_Lucene searches through all fields
by default, but the behavior is configurable. See the "Default search field" chapter for details.
• UnIndexed fields are not searchable, but they are returned with search hits. Database timestamps, primary keys,
file system paths, and other external identifiers are good candidates for UnIndexed fields.
990
Zend_Search_Lucene
• Binary fields are not tokenized or indexed, but are stored for retrieval with search hits. They can be used to store
any data encoded as a binary string, such as an image icon.
• Text fields are stored, indexed, and tokenized. Text fields are appropriate for storing information like subjects and
titles that need to be searchable as well as returned with search results.
• UnStored fields are tokenized and indexed, but not stored in the index. Large amounts of text are best indexed
using this type of field. Storing data creates a larger index on disk, so if you need to search but not redisplay the data,
use an UnStored field. UnStored fields are practical when using a Zend_Search_Lucene index in combination
with a relational database. You can index large data fields with UnStored fields for searching, and retrieve them
from your relational database by using a separate field as an identifier.
HTML documents
Zend_Search_Lucene offers a HTML parsing feature. Documents can be created directly from a HTML file or
string:
$doc = Zend_Search_Lucene_Document_Html::loadHTMLFile($filename);
$index->addDocument($doc);
...
$doc = Zend_Search_Lucene_Document_Html::loadHTML($htmlString);
$index->addDocument($doc);
Zend_Search_Lucene_Document_Html class recognizes document title, body and document header meta tags.
The 'title' field is actually the /html/head/title value. It's stored within the index, tokenized and available for search.
The 'body' field is the actual body content of the HTML file or string. It doesn't include scripts, comments or attributes.
The third parameter of loadHTML() and loadHTMLFile() methods optionally specifies source HTML document
encoding. It's used if encoding is not specified using Content-type HTTP-EQUIV meta tag.
Other document header meta tags produce additional document fields. The field 'name' is taken from 'name' attribute,
and the 'content' attribute populates the field 'value'. Both are tokenized, indexed and stored, so documents may be
searched by their meta tags (for example, by keywords).
991
Zend_Search_Lucene
Parsed documents may be augmented by the programmer with any other field:
$doc = Zend_Search_Lucene_Document_Html::loadHTML($htmlString);
$doc->addField(Zend_Search_Lucene_Field::UnIndexed('created',
time()));
$doc->addField(Zend_Search_Lucene_Field::UnIndexed('updated',
time()));
$doc->addField(Zend_Search_Lucene_Field::Text('annotation',
'Document annotation text'));
$index->addDocument($doc);
Document links are not included in the generated document, but may
be retrieved with the Zend_Search_Lucene_Document_Html::getLinks() and
Zend_Search_Lucene_Document_Html::getHeaderLinks() methods:
$doc = Zend_Search_Lucene_Document_Html::loadHTML($htmlString);
$linksArray = $doc->getLinks();
$headerLinksArray = $doc->getHeaderLinks();
Starting from Zend Framework 1.6 it's also possible to exclude links with rel attribute set to 'nofollow'.
Use Zend_Search_Lucene_Document_Html::setExcludeNoFollowLinks($true) to turn on this
option.
$doc = Zend_Search_Lucene_Document_Docx::loadDocxFile($filename);
$index->addDocument($doc);
Zend_Search_Lucene_Document_Docx class recognizes document meta data and document text. Meta data
consists, depending on document contents, of filename, title, subject, creator, keywords, description, lastModifiedBy,
revision, modified, created.
992
Zend_Search_Lucene
The 'lastModifiedBy' field is the username who has last modified the actual document.
The 'modified' field is the actual document last modified date / time.
The 'body' field is the actual body content of the Word 2007 document. It only includes normal text, comments and
revisions are not included.
Parsed documents may be augmented by the programmer with any other field:
$doc = Zend_Search_Lucene_Document_Docx::loadDocxFile($filename);
$doc->addField(Zend_Search_Lucene_Field::UnIndexed(
'indexTime',
time())
);
$doc->addField(Zend_Search_Lucene_Field::Text(
'annotation',
'Document annotation text')
);
$index->addDocument($doc);
$doc = Zend_Search_Lucene_Document_Pptx::loadPptxFile($filename);
$index->addDocument($doc);
Zend_Search_Lucene_Document_Pptx class recognizes document meta data and document text. Meta data
consists, depending on document contents, of filename, title, subject, creator, keywords, description, lastModifiedBy,
revision, modified, created.
993
Zend_Search_Lucene
The 'lastModifiedBy' field is the username who has last modified the actual document.
The 'modified' field is the actual document last modified date / time.
The 'body' field is the actual content of all slides and slide notes in the Powerpoint 2007 document.
Parsed documents may be augmented by the programmer with any other field:
$doc = Zend_Search_Lucene_Document_Pptx::loadPptxFile($filename);
$doc->addField(Zend_Search_Lucene_Field::UnIndexed(
'indexTime',
time()));
$doc->addField(Zend_Search_Lucene_Field::Text(
'annotation',
'Document annotation text'));
$index->addDocument($doc);
$doc = Zend_Search_Lucene_Document_Xlsx::loadXlsxFile($filename);
$index->addDocument($doc);
Zend_Search_Lucene_Document_Xlsx class recognizes document meta data and document text. Meta data
consists, depending on document contents, of filename, title, subject, creator, keywords, description, lastModifiedBy,
revision, modified, created.
The 'lastModifiedBy' field is the username who has last modified the actual document.
994
Zend_Search_Lucene
The 'modified' field is the actual document last modified date / time.
The 'body' field is the actual content of all cells in all worksheets of the Excel 2007 document.
Parsed documents may be augmented by the programmer with any other field:
$doc = Zend_Search_Lucene_Document_Xlsx::loadXlsxFile($filename);
$doc->addField(Zend_Search_Lucene_Field::UnIndexed(
'indexTime',
time()));
$doc->addField(Zend_Search_Lucene_Field::Text(
'annotation',
'Document annotation text'));
$index->addDocument($doc);
Building Indexes
Creating a New Index
Index creation and updating capabilities are implemented within the Zend_Search_Lucene component, as well
as the Java Lucene project. You can use either of these options to create indexes that Zend_Search_Lucene can
search.
The PHP code listing below provides an example of how to index a file using Zend_Search_Lucene indexing API:
// Create index
$index = Zend_Search_Lucene::create('/data/my-index');
Updating Index
The same procedure is used to update an existing index. The only difference is that the open() method is called instead
of the create() method:
995
Zend_Search_Lucene
Updating Documents
The Lucene index file format doesn't support document updating. Documents should be removed and re-added to the
index to effectively update them.
Zend_Search_Lucene::delete() method operates with an internal index document id. It can be retrieved
from a query hit by 'id' property:
$removePath = ...;
$hits = $index->find('path:' . $removePath);
foreach ($hits as $hit) {
$index->delete($hit->id);
}
Zend_Search_Lucene::maxDoc() returns one greater than the largest possible document number. It's
actually the overall number of the documents in the index including deleted documents, so it has a synonym:
Zend_Search_Lucene::count().
$indexSize = $index->count();
$documents = $index->numDocs();
Index optimization removes deleted documents and squeezes documents' IDs in to a smaller range. A document's
internal id may therefore change during index optimization.
996
Zend_Search_Lucene
Index optimization
A Lucene index consists of many segments. Each segment is a completely independent set of data.
Lucene index segment files can't be updated by design. A segment update needs full segment reorganization. See
Lucene index file formats for details (http://lucene.apache.org/java/2_3_0/fileformats.html) 1. New documents are
added to the index by creating new segment.
Increasing number of segments reduces quality of the index, but index optimization restores it. Optimization essentially
merges several segments into a new one. This process also doesn't update segments. It generates one new large segment
and updates segment list ('segments' file).
Full index optimization can be trigger by calling the Zend_Search_Lucene::optimize() method. It merges
all index segments into one new segment:
// Optimize index.
$index->optimize();
Automatic optimization is an iterative process managed by several index options. It merges very small segments into
larger ones, then merges these larger segments into even larger segments and so on.
997
Zend_Search_Lucene
values, more RAM is used during indexing, and while searches on unoptimized indices are slower, indexing is faster.
Thus larger values (> 10) are best for batch index creation, and smaller values (< 10) for indices that are interactively
maintained.
MergeFactor is a good estimation for average number of segments merged by one auto-optimization pass. Too large
values produce large number of segments while they are not merged into new one. It may be a cause of "failed to open
stream: Too many open files" error message. This limitation is system dependent.
Lucene Java and Luke (Lucene Index Toolbox - http://www.getopt.org/luke/) can also be used to optimize
an index. Latest Luke release (v0.8) is based on Lucene v2.3 and compatible with current implementation
of Zend_Search_Lucene component (Zend Framework 1.6). Earlier versions of Zend_Search_Lucene
implementations need another versions of Java Lucene tools to be compatible:
• Zend Framework 1.5 - Java Lucene 2.1 (Luke tool v0.7.1 - http://www.getopt.org/luke/luke-0.7.1/)
• Zend Framework 1.0 - Java Lucene 1.4 - 2.1 (Luke tool v0.6 - http://www.getopt.org/luke/luke-0.6/)
Permissions
By default, index files are available for reading and writing by everyone.
Limitations
Index size
Index size is limited by 2GB for 32-bit platforms.
Supported Filesystems
Zend_Search_Lucene uses flock() to provide concurrent searching, index updating and optimization.
998
Zend_Search_Lucene
Searching an Index
Building Queries
There are two ways to search the index. The first method uses query parser to construct a query from a string. The
second is to programmatically create your own queries through the Zend_Search_Lucene API.
Before choosing to use the provided query parser, please consider the following:
1. If you are programmatically creating a query string and then parsing it with the query parser then you should
consider building your queries directly with the query API. Generally speaking, the query parser is designed for
human-entered text, not for program-generated text.
2. Untokenized fields are best added directly to queries and not through the query parser. If a field's values are
generated programmatically by the application, then the query clauses for this field should also be constructed
programmatically. An analyzer, which the query parser uses, is designed to convert human-entered text to terms.
Program-generated values, like dates, keywords, etc., should be added with the query API.
3. In a query form, fields that are general text should use the query parser. All others, such as date ranges, keywords,
etc., are better added directly through the query API. A field with a limited set of values that can be specified with
a pull-down menu should not be added to a query string that is subsequently parsed but instead should be added
as a TermQuery clause.
4. Boolean queries allow the programmer to logically combine two or more queries into new one. Thus it's the best
way to add additional criteria to a search defined by a query string.
Both ways use the same API method to search through the index:
$index = Zend_Search_Lucene::open('/data/my_index');
$index->find($query);
The Zend_Search_Lucene::find() method determines the input type automatically and uses the query parser
to construct an appropriate Zend_Search_Lucene_Search_Query object from an input of type string.
It is important to note that the query parser uses the standard analyzer to tokenize separate parts of query string. Thus
all transformations which are applied to indexed text are also applied to query strings.
The standard analyzer may transform the query string to lower case for case-insensitivity, remove stop-words, and
stem among other transformations.
The API method doesn't transform or filter input terms in any way. It's therefore more suitable for computer generated
or untokenized fields.
Query Parsing
Zend_Search_Lucene_Search_QueryParser::parse() method may be used to parse query strings into
query objects.
This query object may be used in query construction API methods to combine user entered queries with
programmatically generated queries.
Actually, in some cases it's the only way to search for values within untokenized fields:
999
Zend_Search_Lucene
$userQuery = Zend_Search_Lucene_Search_QueryParser::parse($queryStr);
$hits = $index->find($query);
$userQuery = Zend_Search_Lucene_Search_QueryParser::parse($queryStr,
'iso-8859-5');
It's also possible to specify the default query string encoding with
Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding() method:
Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('iso-8859-5');
...
$userQuery = Zend_Search_Lucene_Search_QueryParser::parse($queryStr);
Search Results
The search result is an array of Zend_Search_Lucene_Search_QueryHit objects. Each of these has two
properties: $hit->id is a document number within the index and $hit->score is a score of the hit in a search
result. The results are ordered by score (descending from highest score).
$index = Zend_Search_Lucene::open('/data/my_index');
$hits = $index->find($query);
1000
Zend_Search_Lucene
$index = Zend_Search_Lucene::open('/data/my_index');
$hits = $index->find($query);
foreach ($hits as $hit) {
// return Zend_Search_Lucene_Document object for this hit
echo $document = $hit->getDocument();
// same as getFieldValue()
echo $document->title;
}
The fields available from the Zend_Search_Lucene_Document object are determined at the time of indexing.
The document fields are either indexed, or index and stored, in the document by the indexing application (e.g.
LuceneIndexCreation.jar).
Note that the document identity ('path' in our example) is also stored in the index and must be retrieved from it.
Zend_Search_Lucene gives the possibility to limit result set size with getResultSetLimit() and
setResultSetLimit() methods:
$currentResultSetLimit = Zend_Search_Lucene::getResultSetLimit();
Zend_Search_Lucene::setResultSetLimit($newLimit);
It doesn't give the 'best N' results, but only the 'first N' 2.
Results Scoring
Zend_Search_Lucene uses the same scoring algorithms as Java Lucene. All hits in the search result are ordered
by score by default. Hits with greater score come first, and documents having higher scores should match the query
more precisely than documents having lower scores.
Roughly speaking, search hits that contain the searched term or phrase more frequently will have a higher score.
2
Returned hits are still ordered by score or by the specified order, if given.
1001
Zend_Search_Lucene
A hit's score can be retrieved by accessing the score property of the hit:
$hits = $index->find($query);
The Zend_Search_Lucene_Search_Similarity class is used to calculate the score for each hit. See
Extensibility. Scoring Algorithms section for details.
A name of stored field by which to sort result should be passed as the $sortField parameter.
$sortType may be omitted or take the following enumerated values: SORT_REGULAR (compare items normally-
default value), SORT_NUMERIC (compare items numerically), SORT_STRING (compare items as strings).
$sortOrder may be omitted or take the following enumerated values: SORT_ASC (sort in ascending order- default
value), SORT_DESC (sort in descending order).
Ejemplos:
Please use caution when using a non-default search order; the query needs to retrieve documents completely from an
index, which may dramatically reduce search performance.
The first one is utilizing Zend_Search_Lucene_Document_Html class (see HTML documents section for
details) using the following methods:
/**
1002
Zend_Search_Lucene
/**
* Highlight text using specified View helper or callback function.
*
* @param string|array $words Words to highlight. Words could be organized
using the array or string.
* @param callback $callback Callback method, used to transform
(highlighting) text.
* @param array $params Array of additionall callback parameters passed
through into it (first non-optional parameter
is an HTML fragment for highlighting)
* @return string
* @throws Zend_Search_Lucene_Exception
*/
public function highlightExtended($words, $callback, $params = array())
To customize highlighting behavior use highlightExtended() method with specified callback, which
takes one or more parameters3, or extend Zend_Search_Lucene_Document_Html class and redefine
applyColour($stringToHighlight, $colour) method used as a default highlighting callback. 4
Nota
Highlighting is performed in terms of current analyzer. So all forms of the word(s) recognized by analyzer
are highlighted.
E.g. if current analyzer is case insensitive and we request to highlight 'text' word, then 'text', 'Text', 'TEXT'
and other case combinations will be highlighted.
In the same way, if current analyzer supports stemming and we request to highlight 'indexed', then 'index',
'indexing', 'indices' and other word forms will be highlighted.
On the other hand, if word is skipped by corrent analyzer (e.g. if short words filter is applied to the analyzer),
then nothing will be highlighted.
1003
Zend_Search_Lucene
$query = Zend_Search_Lucene_Search_QueryParser::parse($queryStr);
$highlightedHTML = $query->highlightMatches($sourceHTML);
Optional second parameter is a default HTML document encoding. It's used if encoding is not specified using Content-
type HTTP-EQUIV meta tag.
interface Zend_Search_Lucene_Search_Highlighter_Interface
{
/**
* Set document for highlighting.
*
* @param Zend_Search_Lucene_Document_Html $document
*/
public function setDocument(Zend_Search_Lucene_Document_Html $document);
/**
* Get document for highlighting.
*
* @return Zend_Search_Lucene_Document_Html $document
*/
public function getDocument();
/**
* Highlight specified words (method is invoked once per subquery)
*
* @param string|array $words Words to highlight. They could be
organized using the array or string.
*/
public function highlight($words);
}
Highlighter highlight() method is invoked once per subquery, so it has an ability to differentiate highlighting
for them.
Actually, default highlighter does this walking through predefined color table. So you can implement your own
highlighter or just extend the default and redefine color table.
Query Language
Java Lucene and Zend_Search_Lucene provide quite powerful query languages.
1004
Zend_Search_Lucene
These languages are mostly the same with some minor differences, which are mentioned below.
Full Java Lucene query language syntax documentation can be found here [http://lucene.apache.org/java/2_3_0/
queryparsersyntax.html].
Terms
A query is broken up into terms and operators. There are three types of terms: Single Terms, Phrases, and Subqueries.
Multiple terms can be combined together with boolean operators to form complex queries (see below).
Fields
Lucene supports fields of data. When performing a search you can either specify a field, or use the default field. The
field names depend on indexed data and default field is defined by current settings.
The first and most significant difference from Java Lucene is that terms are searched through all fields by default.
There are two static methods in the Zend_Search_Lucene class which allow the developer to configure these
settings:
$defaultSearchField = Zend_Search_Lucene::getDefaultSearchField();
...
Zend_Search_Lucene::setDefaultSearchField('contents');
The NULL value indicated that the search is performed across all fields. It's the default setting.
You can search specific fields by typing the field name followed by a colon ":" followed by the term you are looking for.
As an example, let's assume a Lucene index contains two fields- title and text- with text as the default field. If you
want to find the document entitled "The Right Way" which contains the text "don't go this way", you can enter:
or
Because "text" is the default field, the field indicator is not required.
Note: The field is only valid for the term, phrase or subquery that it directly precedes, so the query
title:Do it right
Will only find "Do" in the title field. It will find "it" and "right" in the default field (if the default field is set) or in
all indexed fields (if the default field is set to NULL).
1005
Zend_Search_Lucene
Wildcards
Lucene supports single and multiple character wildcard searches within single terms (but not within phrase queries).
The single character wildcard search looks for string that match the term with the "?" replaced by any single character.
For example, to search for "text" or "test" you can use the search:
te?t
Multiple character wildcard searches look for 0 or more characters when matching strings against terms. For example,
to search for test, tests or tester, you can use the search:
test*
You can use "?", "*" or both at any place of the term:
*wr?t*
Starting from ZF 1.7.7 wildcard patterns need some non-wildcard prefix. Default prefix length is 3 (like in Java
Lucene). So "*", "te?t", "*wr?t*" terms will cause an exception5.
Term Modifiers
Lucene supports modifying query terms to provide a wide range of searching options.
"~" modifier can be used to specify proximity search for phrases or fuzzy search for individual terms.
Range Searches
Range queries allow the developer or user to match documents whose field(s) values are between the lower and upper
bound specified by the range query. Range Queries can be inclusive or exclusive of the upper and lower bounds.
Sorting is performed lexicographically.
mod_date:[20020101 TO 20030101]
This will find documents whose mod_date fields have values between 20020101 and 20030101, inclusive. Note that
Range Queries are not reserved for date fields. You could also use range queries with non-date fields:
title:{Aida TO Carmen}
5
Please note, that it's not a Zend_Search_Lucene_Search_QueryParserException, but a Zend_Search_Lucene_Exception.
It's thrown during query rewrite (execution) operation.
1006
Zend_Search_Lucene
This will find all documents whose titles would be sorted between Aida and Carmen, but not including Aida and
Carmen.
Inclusive range queries are denoted by square brackets. Exclusive range queries are denoted by curly brackets.
If field is not specified then Zend_Search_Lucene searches for specified interval through all fields by default.
{Aida TO Carmen}
Fuzzy Searches
Zend_Search_Lucene as well as Java Lucene supports fuzzy searches based on the Levenshtein Distance, or Edit
Distance algorithm. To do a fuzzy search use the tilde, "~", symbol at the end of a Single word Term. For example to
search for a term similar in spelling to "roam" use the fuzzy search:
roam~
This search will find terms like foam and roams. Additional (optional) parameter can specify the required similarity.
The value is between 0 and 1, with a value closer to 1 only terms with a higher similarity will be matched. For example:
roam~0.8
Proximity Searches
Lucene supports finding words from a phrase that are within a specified word distance in a string. To do a proximity
search use the tilde, "~", symbol at the end of the phrase. For example to search for a "Zend" and "Framework" within
10 words of each other in a document use the search:
"Zend Framework"~10
Boosting a Term
Java Lucene and Zend_Search_Lucene provide the relevance level of matching documents based on the terms
found. To boost the relevance of a term use the caret, "^", symbol with a boost factor (a number) at the end of the term
you are searching. The higher the boost factor, the more relevant the term will be.
Boosting allows you to control the relevance of a document by boosting individual terms. For example, if you are
searching for
1007
Zend_Search_Lucene
PHP framework
and you want the term "PHP" to be more relevant boost it using the ^ symbol along with the boost factor next to the
term. You would type:
PHP^4 framework
This will make documents with the term PHP appear more relevant. You can also boost phrase terms and subqueries
as in the example:
By default, the boost factor is 1. Although the boost factor must be positive, it may be less than 1 (e.g. 0.2).
Boolean Operators
Boolean operators allow terms to be combined through logic operators. Lucene supports AND, "+", OR, NOT and "-"
as Boolean operators. Java Lucene requires boolean operators to be ALL CAPS. Zend_Search_Lucene does not.
AND, OR, and NOT operators and "+", "-" defines two different styles to construct boolean queries. Unlike Java
Lucene, Zend_Search_Lucene doesn't allow these two styles to be mixed.
If the AND/OR/NOT style is used, then an AND or OR operator must be present between all query terms. Each term
may also be preceded by NOT operator. The AND operator has higher precedence than the OR operator. This differs
from Java Lucene behavior.
AND
The AND operator means that all terms in the "AND group" must match some part of the searched field(s).
To search for documents that contain "PHP framework" and "Zend Framework" use the query:
OR
The OR operator divides the query into several optional terms.
To search for documents that contain "PHP framework" or "Zend Framework" use the query:
NOT
The NOT operator excludes documents that contain the term after NOT. But an "AND group" which contains only
terms with the NOT operator gives an empty result set instead of a full set of indexed documents.
To search for documents that contain "PHP framework" but not "Zend Framework" use the query:
1008
Zend_Search_Lucene
+
The "+" or required operator stipulates that the term after the "+" symbol must match the document.
To search for documents that must contain "Zend" and may contain "Framework" use the query:
+Zend Framework
-
The "-" or prohibit operator excludes documents that match the term after the "-" symbol.
To search for documents that contain "PHP framework" but not "Zend Framework" use the query:
No Operator
If no operator is used, then the search behavior is defined by the "default boolean operator".
That implies each term is optional by default. It may or may not be present within document, but documents with this
term will receive a higher score.
To search for documents that requires "PHP framework" and may contain "Zend Framework" use the query:
Grouping
Java Lucene and Zend_Search_Lucene support using parentheses to group clauses to form sub queries. This can
be useful if you want to control the precedence of boolean logic operators for a query or mix different boolean query
styles:
Field Grouping
Lucene also supports using parentheses to group multiple clauses to a single field.
1009
Zend_Search_Lucene
To search for a title that contains both the word "return" and the phrase "pink panther" use the query:
+ - && || ! ( ) { } [ ] ^ " ~ * ? : \
For other instances of these characters use the \ before each special character you'd like to escape. For example to
search for (1+1):2 use the query:
\(1\+1\)\:2
User queries can be combined with queries created through the query API. Simply use the query parser to construct
a query from a string:
$query = Zend_Search_Lucene_Search_QueryParser::parse($queryString);
try {
$query = Zend_Search_Lucene_Search_QueryParser::parse($queryString);
} catch (Zend_Search_Lucene_Search_QueryParserException $e) {
echo "Query syntax error: " . $e->getMessage() . "\n";
}
The same technique should be used for the find() method of a Zend_Search_Lucene object.
Starting in 1.5, query parsing exceptions are suppressed by default. If query doesn't conform query
language, then it's tokenized using current default analyzer and all tokenized terms are used for searching.
Use Zend_Search_Lucene_Search_QueryParser::dontSuppressQueryParsingExceptions()
method to turn exceptions on.
Zend_Search_Lucene_Search_QueryParser::suppressQueryParsingExceptions() and
1010
Zend_Search_Lucene
Zend_Search_Lucene_Search_QueryParser::queryParsingExceptionsSuppressed()
methods are also intended to manage exceptions handling behavior.
Term Query
Term queries can be used for searching with a single term.
Query string:
word1
or
The term field is optional. Zend_Search_Lucene searches through all indexed fields in each document if the field
is not specified:
Multi-Term Query
Multi-term queries can be used for searching with a set of terms.
• required means that documents not matching this term will not match the query;
• prohibited means that documents matching this term will not match the query;
• neither, in which case matched documents are neither prohibited from, nor required to, match the term. A document
must match at least 1 term, however, to match the query.
If optional terms are added to a query with required terms, both queries will have the same result set but the optional
terms may affect the score of the matched documents.
Query string:
• 'field:' prefix is used to indicate a document field for a search. If it's omitted, then all fields are searched.
1011
Zend_Search_Lucene
or
$hits = $index->find($query);
It's also possible to specify terms list within MultiTerm query constructor:
$hits = $index->find($query);
Boolean Query
Boolean queries allow to construct query using other queries and boolean operators.
• required means that documents not matching this subquery will not match the query;
• prohibited means that documents matching this subquery will not match the query;
• optional, in which case matched documents are neither prohibited from, nor required to, match the subquery. A
document must match at least 1 subquery, however, to match the query.
If optional subqueries are added to a query with required subqueries, both queries will have the same result set but the
optional subqueries may affect the score of the matched documents.
Query string:
1012
Zend_Search_Lucene
• 'field:' prefix is used to indicate a document field for a search. If it's omitted, then all fields are searched.
or
$hits = $index->find($query);
It's also possible to specify subqueries list within Boolean query constructor:
...
$subqueries = array($subquery1, $subquery2, $subquery3);
$signs = array(true, null, false);
$hits = $index->find($query);
Each query which uses boolean operators can be rewritten using signs notation and constructed using API. For example:
is equivalent to
1013
Zend_Search_Lucene
Wildcard Query
Wildcard queries can be used to search for documents containing strings matching specified patterns.
Query string:
field1:test*
or
The term field is optional. Zend_Search_Lucene searches through all fields on each document if a field is not
specified:
Fuzzy Query
Fuzzy queries can be used to search for documents containing strings matching terms similar to specified term.
Query string:
field1:test~
This query matches documents containing 'test' 'text' 'best' words and others.
or
Query string:
1014
Zend_Search_Lucene
field1:test~0.4
or
The term field is optional. Zend_Search_Lucene searches through all fields on each document if a field is not
specified:
Phrase Query
Phrase Queries can be used to search for a phrase within documents.
Phrase Queries are very flexible and allow the user or developer to search for exact phrases as well as 'sloppy' phrases.
Phrases can also contain gaps or terms in the same places; they can be generated by the analyzer for different purposes.
For example, a term can be duplicated to increase the term its weight, or several synonyms can be placed into a single
position.
...
...
...
1015
Zend_Search_Lucene
A phrase query can be constructed in one step with a class constructor or step by step with
Zend_Search_Lucene_Search_Query_Phrase::addTerm() method calls.
Zend_Search_Lucene_Search_Query_Phrase(
[array $terms[, array $offsets[, string $field]]]
);
The $terms parameter is an array of strings that contains a set of phrase terms. If it's omitted or equal to null, then
an empty query is constructed.
The $offsets parameter is an array of integers that contains offsets of terms in a phrase. If it's omitted or equal to
null, then the terms' positions are assumed to be sequential with no gaps.
The $field parameter is a string that indicates the document field to search. If it's omitted or equal to null, then
the default field is searched.
Thus:
$query =
new Zend_Search_Lucene_Search_Query_Phrase(array('zend', 'framework'));
will search for the phrase 'zend ????? download' and match 'zend platform download', 'zend studio download', 'zend
core download', 'zend framework download', and so on.
will search for the phrase 'zend framework' in the 'title' field.
Zend_Search_Lucene_Search_Query_Phrase::addTerm(
Zend_Search_Lucene_Index_Term $term[, integer $position]
);
The $term parameter describes the next term in the phrase. It must indicate the same field as previous terms, or an
exception will be thrown.
Thus:
1016
Zend_Search_Lucene
will search for the phrase 'zend ????? download' and match 'zend platform download', 'zend studio download', 'zend
core download', 'zend framework download', and so on.
will search for the phrase 'zend framework' in the 'title' field.
The slop factor sets the number of other words permitted between specified words in the query phrase. If set to zero, then
the corresponding query is an exact phrase search. For larger values this works like the WITHIN or NEAR operators.
The slop factor is in fact an edit distance, where the edits correspond to moving terms in the query phrase. For example,
to switch the order of two words requires two moves (the first move places the words atop one another), so to permit
re-orderings of phrases, the slop factor must be at least two.
More exact matches are scored higher than sloppier matches; thus, search results are sorted by exactness. The slop is
zero by default, requiring exact matches.
Range Query
Range queries are intended for searching terms within specified interval.
Query string:
mod_date:[20020101 TO 20030101]
1017
Zend_Search_Lucene
title:{Aida TO Carmen}
or
Term fields are optional. Zend_Search_Lucene searches through all fields if the field is not specified:
Either (but not both) of the boundary terms may be set to null. Zend_Search_Lucene searches from the beginning
or up to the end of the dictionary for the specified field(s) in this case:
Character Set
UTF-8 and single-byte character set support
Zend_Search_Lucene works with the UTF-8 charset internally. Index files store unicode data in Java's "modified
UTF-8 encoding". Zend_Search_Lucene core completely supports this encoding with one exception. 6
Actual input data encoding may be specified through Zend_Search_Lucene API. Data will be automatically
converted into UTF-8 encoding.
6
Zend_Search_Lucene supports only Basic Multilingual Plane (BMP) characters (from 0x0000 to 0xFFFF) and doesn't support "supplementary
characters" (characters whose code points are greater than 0xFFFF)
Java 2 represents these characters as a pair of char (16-bit) values, the first from the high-surrogates range (0xD800-0xDBFF), the second from
the low-surrogates range (0xDC00-0xDFFF). Then they are encoded as usual UTF-8 characters in six bytes. Standard UTF-8 representation uses
four bytes for supplementary characters.
1018
Zend_Search_Lucene
ctype_alpha() is not UTF-8 compatible, so the analyzer converts text to 'ASCII//TRANSLIT' encoding before indexing.
The same processing is transparently performed during query parsing. 7
Default analyzer doesn't treats numbers as parts of terms. Use corresponding 'Num' analyzer if you don't want
words to be broken by numbers.
Any of this analyzers can be enabled with the code like this:
Zend_Search_Lucene_Analysis_Analyzer::setDefault(
new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8());
UTF-8 compatible analyzers were improved in Zend Framework 1.5. Early versions of analyzers assumed all
non-ascii characters are letters. New analyzers implementation has more accurate behavior.
This may need you to re-build index to have data and search queries tokenized in the same way, otherwise
search engine may return wrong result sets.
All of these analyzers need PCRE (Perl-compatible regular expressions) library to be compiled with UTF-8 support
turned on. PCRE UTF-8 support is turned on for the PCRE library sources bundled with PHP source code distribution,
but if shared library is used instead of bundled with PHP sources, then UTF-8 support state may depend on you
operating system.
if (@preg_match('/\pL/u', 'a') == 1) {
echo "PCRE unicode support is turned on.\n";
} else {
echo "PCRE unicode support is turned off.\n";
}
Case insensitive versions of UTF-8 compatible analyzers also need mbstring [http://www.php.net/manual/en/
ref.mbstring.php] extension to be enabled.
If you don't want mbstring extension to be turned on, but need case insensitive search, you may use the following
approach: normalize source data before indexing and query string before searching by converting them to lowercase:
// Indexing
setlocale(LC_CTYPE, 'de_DE.iso-8859-1');
...
Zend_Search_Lucene_Analysis_Analyzer::setDefault(
7
Conversion to 'ASCII//TRANSLIT' may depend on current locale and OS.
1019
Zend_Search_Lucene
new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8());
...
$doc->addField(Zend_Search_Lucene_Field::UnStored('contents',
strtolower($contents)));
// Searching
setlocale(LC_CTYPE, 'de_DE.iso-8859-1');
...
Zend_Search_Lucene_Analysis_Analyzer::setDefault(
new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8());
...
$hits = $index->find(strtolower($query));
Extensibility
Text Analysis
The Zend_Search_Lucene_Analysis_Analyzer class is used by the indexer to tokenize document text
fields.
You can assign your own text analyzer or choose it from the set
of predefined analyzers: Zend_Search_Lucene_Analysis_Analyzer_Common_Text and
Zend_Search_Lucene_Analysis_Analyzer_Common_Text_CaseInsensitive (default). Both of
them interpret tokens as sequences of letters.
Zend_Search_Lucene_Analysis_Analyzer_Common_Text_CaseInsensitive converts all tokens
to lower case.
Zend_Search_Lucene_Analysis_Analyzer::setDefault(
new Zend_Search_Lucene_Analysis_Analyzer_Common_Text());
...
$index->addDocument($doc);
1020
Zend_Search_Lucene
The nextToken() method should call the normalize() method on each token. This will allow you to use token
filters with your analyzer.
Here is an example of a custom analyzer, which accepts words with digits as terms:
/**
* Here is a custom text analyser, which treats words with digits as
* one term
*/
/**
* Reset token stream
*/
public function reset()
{
$this->_position = 0;
}
/**
* Tokenization stream API
* Get next token
* Returns null at the end of stream
*
* @return Zend_Search_Lucene_Analysis_Token|null
*/
public function nextToken()
{
if ($this->_input === null) {
return null;
}
$termStartPosition = $this->_position;
// read token
while ($this->_position < strlen($this->_input) &&
ctype_alnum( $this->_input[$this->_position] )) {
1021
Zend_Search_Lucene
$this->_position++;
}
return null;
}
}
Zend_Search_Lucene_Analysis_Analyzer::setDefault(
new My_Analyzer());
Tokens Filtering
The Zend_Search_Lucene_Analysis_Analyzer_Common analyzer also offers a token filtering mechanism.
Any custom filter must implement the normalize() method which may transform input token or signal that the
current token should be skipped.
• Zend_Search_Lucene_Analysis_TokenFilter_LowerCase
• Zend_Search_Lucene_Analysis_TokenFilter_ShortWords
• Zend_Search_Lucene_Analysis_TokenFilter_StopWords
The ShortWords and StopWords filters may be used with pre-defined or custom analyzers like this:
1022
Zend_Search_Lucene
new Zend_Search_Lucene_Analysis_TokenFilter_StopWords($stopWords);
$analyzer =
new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive();
$analyzer->addFilter($stopWordsFilter);
Zend_Search_Lucene_Analysis_Analyzer::setDefault($analyzer);
$analyzer =
new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive();
$analyzer->addFilter($shortWordsFilter);
Zend_Search_Lucene_Analysis_Analyzer::setDefault($analyzer);
$analyzer =
new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive();
$analyzer->addFilter($stopWordsFilter);
Zend_Search_Lucene_Analysis_Analyzer::setDefault($analyzer);
This file should be a common text file with one word in each line. The '#' character marks a line as a comment.
Scoring Algorithms
The score of a document d for a query q is defined as follows:
lengthNorm($term) - the normalization value for a field given the total number of terms contained in a field. This
value is stored within the index. These values, together with field boosts, are stored in an index and multiplied into
scores for hits on each field by the search code.
Matches in longer fields are less precise, so implementations of this method usually return smaller values when
numTokens is large, and larger values when numTokens is small.
1023
Zend_Search_Lucene
The presence of a large portion of the query terms indicates a better match with the query, so implementations of this
method usually return larger values when the ratio between these parameters is large and smaller values when the
ratio between them is small.
queryNorm(q) - the normalization value for a query given the sum of the squared weights of each of the query terms.
This value is then multiplied into the weight of each query term.
This does not affect ranking, but rather just attempts to make scores from different queries comparable.
The scoring algorithm can be customized by defining your own Similarity class. To do this
extend the Zend_Search_Lucene_Search_Similarity class as defined below, then use the
Zend_Search_Lucene_Search_Similarity::setDefault($similarity); method to set it as
default.
/**
* It's not used now. Computes the amount of a sloppy phrase match,
* based on an edit distance.
*/
public function sloppyFreq($distance) {
return 1.0;
}
Storage Containers
The abstract class Zend_Search_Lucene_Storage_Directory defines directory functionality.
1024
Zend_Search_Lucene
If a string is used as an input for the Zend_Search_Lucene constructor, then the index
reader (Zend_Search_Lucene object) treats it as a file system path and instantiates the
Zend_Search_Lucene_Storage_Directory_Filesystem object.
Zend_Search_Lucene_Storage_Directory methods:
/**
* Creates a new, empty file in the directory with the given $filename.
*
* @param string $name
* @return void
*/
abstract function createFile($filename);
/**
* Removes an existing $filename in the directory.
*
* @param string $filename
* @return void
*/
abstract function deleteFile($filename);
/**
* Returns true if a file with the given $filename exists.
*
* @param string $filename
* @return boolean
*/
abstract function fileExists($filename);
/**
* Returns the length of a $filename in the directory.
*
* @param string $filename
* @return integer
*/
abstract function fileLength($filename);
1025
Zend_Search_Lucene
/**
* Returns the UNIX timestamp $filename was last modified.
*
* @param string $filename
* @return integer
*/
abstract function fileModified($filename);
/**
* Renames an existing file in the directory.
*
* @param string $from
* @param string $to
* @return void
*/
abstract function renameFile($from, $to);
/**
* Sets the modified time of $filename to now.
*
* @param string $filename
* @return void
*/
abstract function touchFile($filename);
/**
* Returns a Zend_Search_Lucene_Storage_File object for a given
* $filename in the directory.
*
* @param string $filename
* @return Zend_Search_Lucene_Storage_File
*/
abstract function getFileObject($filename);
The Zend_Search_Lucene_Storage_File abstract class implements file abstraction and index file reading
primitives.
1026
Zend_Search_Lucene
/**
* Read a $length bytes from the file and advance the file pointer.
*
* @param integer $length
* @return string
*/
protected function _fread($length=1) {
...
}
}
Index Directory
After index creation, the index directory will contain several files:
• The *.cfs files contain index segments. Note! An optimized index always has only one segment.
• The deletable file is a list of files that are no longer used by the index, but which could not be deleted.
/**
* Index creation:
*/
8
The currently supported Lucene index file format version is 2.3 (starting from Zend Framework 1.6).
1027
Zend_Search_Lucene
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.document.*;
import java.io.*
...
...
indexWriter.addDocument(doc);
Advanced
Starting from 1.6, handling index format transformations
Zend_Search_Lucene component works with Java Lucene 1.4-1.9, 2.1 and 2.3 index formats.
Current index format may be requested using $index->getFormatVersion() call. It returns one of the
following values:
• Zend_Search_Lucene::FORMAT_2_1 for Java Lucene 2.1 index format (also used for Lucene 2.2).
Index modifications are performed only if any index update is done. That happens if a new document is added to an
index or index optimization is started manually by $index->optimize() call.
In a such case Zend_Search_Lucene may convert index to the higher format version. That always happens for the
indices in Zend_Search_Lucene::FORMAT_PRE_2_1 format, which are automatically converted to 2.1 format.
You may manage conversion process and assign target index format by $index->setFormatVersion()
which takes Zend_Search_Lucene::FORMAT_2_1 or Zend_Search_Lucene::FORMAT_2_3 constant as
a parameter:
1028
Zend_Search_Lucene
Important!
Once index is converted to upper version it can't be converted back. So make a backup of your index when
you plan migration to upper version, but want to have possibility to go back.
It stores added documents in memory and dumps new index segment to disk depending on MaxBufferedDocs
parameter.
If MaxBufferedDocs limit is not reached then there are some "unsaved" documents which are saved as a new
segment in the object's destructor method. The index auto-optimization procedure is invoked if necessary depending
on the values of the MaxBufferedDocs, MaxMergeDocs and MergeFactor parameters.
Static object properties (see below) are destroyed after the last line of the executed script.
class Searcher {
private static $_index;
Searcher::initIndex();
All the same, the destructor for static properties is correctly invoked at this point in the program's execution.
One potential problem is exception handling. Exceptions thrown by destructors of static objects don't have context,
because the destructor is executed after the script has already completed.
You might see a "Fatal error: Exception thrown without a stack frame in Unknown on line 0" error message instead
of exception description in such cases.
Zend_Search_Lucene provides a workaround to this problem with the commit() method. It saves all unsaved
changes and frees memory used for storing new segments. You are free to use the commit operation any time- or even
several times- during script execution. You can still use the Zend_Search_Lucene object for searching, adding
or deleting document after the commit operation. But the commit() call guarantees that if there are no document
added or deleted after the call to commit(), then the Zend_Search_Lucene destructor has nothing to do and
will not throw exception:
class Searcher {
private static $_index;
1029
Zend_Search_Lucene
...
Searcher::initIndex();
...
Best Practices
Field names
There are no limitations for field names in Zend_Search_Lucene.
Nevertheless it's a good idea not to use 'id' and 'score' names to avoid ambiguity in QueryHit properties names.
$hits = $index->find($query);
1030
Zend_Search_Lucene
Indexing performance
Indexing performance is a compromise between used resources, indexing time and index quality.
Each index segment is entirely independent portion of data. So indexes containing more segments need more memory
and time for searching.
Index optimization is a process of merging several segments into a new one. A fully optimized index contains only
one segment.
$index = Zend_Search_Lucene::open($indexPath);
$index->optimize();
Index optimization works with data streams and doesn't take a lot of memory but does require processor resources
and time.
Lucene index segments are not updatable by their nature (the update operation requires the segment file to be
completely rewritten). So adding new document(s) to an index always generates a new segment. This, in turn, decreases
index quality.
An index auto-optimization process is performed after each segment generation and consists of merging partial
segments.
There are three options to control the behavior of auto-optimization (see Index optimization section):
• MaxBufferedDocs is the number of documents that can be buffered in memory before a new segment is generated
and written to the hard drive.
• MaxMergeDocs is the maximum number of documents merged by auto-optimization process into a new segment.
Nota
All these options are Zend_Search_Lucene object properties- not index properties. They affect only
current Zend_Search_Lucene object behavior and may vary for different scripts.
MaxBufferedDocs doesn't have any effect if you index only one document per script execution. On the other hand, it's
very important for batch indexing. Greater values increase indexing performance, but also require more memory.
There is simply no way to calculate the best value for the MaxBufferedDocs parameter because it depends on average
document size, the analyzer in use and allowed memory.
A good way to find the right value is to perform several tests with the largest document you expect to be added to the
index 9. It's a best practice not to use more than a half of the allowed memory.
MaxMergeDocs limits the segment size (in terms of documents). It therefore also limits auto-optimization time by
guaranteeing that the addDocument() method is not executed more than a certain number of times. This is very
important for interactive applications.
9
memory_get_usage() and memory_get_peak_usage() may be used to control memory usage.
1031
Zend_Search_Lucene
Lowering the MaxMergeDocs parameter also may improve batch indexing performance. Index auto-optimization is
an iterative process and is performed from bottom up. Small segments are merged into larger segment, which are in
turn merged into even larger segments and so on. Full index optimization is achieved when only one large segment
file remains.
Small segments generally decrease index quality. Many small segments may also trigger the "Too many open files"
error determined by OS limitations 10.
in general, background index optimization should be performed for interactive indexing mode and MaxMergeDocs
shouldn't be too low for batch indexing.
MergeFactor affects auto-optimization frequency. Lower values increase the quality of unoptimized indexes. Larger
values increase indexing performance, but also increase the number of merged segments. This again may trigger the
"Too many open files" error.
4. ...
Zend_Search_Lucene checks during each addDocument() call to see if merging any segments may move the
newly created segment into the next group. If yes, then merging is performed.
So an index with N groups may contain MaxBufferedDocs + (N-1)*MergeFactor segments and contains at least
MaxBufferedDocs*MergeFactor(N-1) documents.
This gives good approximation for the number of segments in the index:
MaxBufferedDocs is determined by allowed memory. This allows for the appropriate merge factor to get a reasonable
number of segments.
Tuning the MergeFactor parameter is more effective for batch indexing performance than MaxMergeDocs. But it's
also more course-grained. So use the estimation above for tuning MergeFactor, then play with MaxMergeDocs to get
best batch indexing performance.
The index object is automatically closed when it, and all returned QueryHit objects, go out of scope.
If index object is stored in global variable than it's closed only at the end of script execution11.
1032
Zend_Search_Lucene
It doesn't prevent normal index shutdown process, but may prevent accurate error diagnostic if any error occurs during
shutdown.
There are two ways with which you may avoid this problem.
$index = Zend_Search_Lucene::open($indexPath);
...
unset($index);
And the second is to perform a commit operation before the end of script execution:
$index = Zend_Search_Lucene::open($indexPath);
$index->commit();
This possibility is also described in the "Advanced. Using index as static property" section.
Zend_Search_Lucene provides a termDocs() method for retrieving documents containing specified terms.
...
1033
Zend_Search_Lucene
Memory Usage
Zend_Search_Lucene is a relatively memory-intensive module.
It uses memory to cache some information and optimize searching and indexing performance.
The terms dictionary index is loaded during the search. It's actually each 128th 12 term of the full dictionary.
Thus memory usage is increased if you have a high number of unique terms. This may happen if you use untokenized
phrases as a field values or index a large volume of non-text information.
An unoptimized index consists of several segments. It also increases memory usage. Segments are independent, so
each segment contains its own terms dictionary and terms dictionary index. If an index consists of N segments it may
increase memory usage by N times in worst case. Perform index optimization to merge all segments into one to avoid
such memory consumption.
Indexing uses the same memory as searching plus memory for buffering documents. The amount of memory used may
be managed with MaxBufferedDocs parameter.
Index optimization (full or partial) uses stream-style data processing and doesn't require a lot of memory.
Encoding
Zend_Search_Lucene works with UTF-8 strings internally. So all strings returned by Zend_Search_Lucene
are UTF-8 encoded.
You shouldn't be concerned with encoding if you work with pure ASCII data, but you should be careful if this is not
the case.
Wrong encoding may cause error notices at the encoding conversion time or loss of data.
Zend_Search_Lucene offers a wide range of encoding possibilities for indexed documents and parsed queries.
1034
Zend_Search_Lucene
'utf-8'));
If optional encoding parameter is omitted, then the current locale is used. The current locale may contain character
encoding data in addition to the language specification:
setlocale(LC_ALL, 'fr_FR');
...
setlocale(LC_ALL, 'de_DE.iso-8859-1');
...
setlocale(LC_ALL, 'ru_RU.UTF-8');
...
If encoding is not specified, then the current locale is used to determine the encoding.
Encoding may be passed as an optional parameter, if the query is parsed explicitly before search:
$query =
Zend_Search_Lucene_Search_QueryParser::parse($queryStr, 'iso-8859-5');
$hits = $index->find($query);
...
Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('iso-8859-1');
$hits = $index->find($queryStr);
...
If the correct encoding is specified it can be correctly processed by analyzer. The actual behavior depends on which
analyzer is used. See the Character Set documentation section for details.
Index maintenance
It should be clear that Zend_Search_Lucene as well as any other Lucene implementation does not comprise a
"database".
Indexes should not be used for data storage. They do not provide partial backup/restore functionality, journaling,
logging, transactions and many other features associated with database management systems.
Index backup and restoration should be performed by copying the contents of the index folder.
If index corruption occurs for any reason, the corrupted index should be restored or completely rebuilt.
So it's a good idea to backup large indexes and store changelogs to perform manual restoration and roll-forward
operations if necessary. This practice dramatically reduces index restoration time.
1035
1036
Capítulo 47. Zend_Server
Introduction
The Zend_Server family of classes provides functionality for the various server classes,
including Zend_XmlRpc_Server, Zend_Rest_Server, Zend_Json_Server and Zend_Soap_Wsdl.
Zend_Server_Interface provides an interface that mimics PHP 5's SoapServer class; all server classes
should implement this interface in order to provide a standard server API.
The Zend_Server_Reflection tree provides a standard mechanism for performing function and class
introspection for use as callbacks with the server classes, and provides data suitable for use with
Zend_Server_Interface's getFunctions() and loadFunctions() methods.
Zend_Server_Reflection
Introduction
Zend_Server_Reflection provides a standard mechanism for performing function and class introspection for
use with server classes. It is based on PHP 5's Reflection API, augmenting it with methods for retrieving parameter
and return value types and descriptions, a full list of function and method prototypes (i.e., all possible valid calling
combinations), and function/method descriptions.
Typically, this functionality will only be used by developers of server classes for the framework.
Usage
Basic usage is simple:
$class = Zend_Server_Reflection::reflectClass('My_Class');
$function = Zend_Server_Reflection::reflectFunction('my_function');
// Get prototypes
$prototypes = $reflection->getPrototypes();
1037
Zend_Server
1038
Capítulo 48. Zend_Service
Introduction
Zend_Service is an abstract class which serves as a foundation for web service implementations, such as SOAP
or REST.
If you need support for generic, XML-based REST services, you may want to look at Zend_Rest_Client.
In addition to being able to extend the Zend_Service and use Zend_Rest_Client for REST-based web
services, Zend also provides support for popular web services. See the following sections for specific information on
each supported web service.
• Akismet
• Amazon
• Audioscrobbler
• Del.icio.us
• Flickr
• Simpy
• SlideShare
• StrikeIron
• Yahoo!
Zend_Service_Akismet
Introduction
Zend_Service_Akismet provides a client for the Akismet API [http://akismet.com/development/api/]. The
Akismet service is used to determine if incoming data is potentially spam. It also exposes methods for submitting
data as known spam or as false positives (ham). It was originally intended to help categorize and identify spam for
Wordpress, but it can be used for any type of data.
Akismet requires an API key for usage. You can get one by signing up for a WordPress.com [http://wordpress.com/]
account. You do not need to activate a blog. Simply acquiring the account will provide you with the API key.
Akismet requires that all requests contain a URL to the resource for which data is being filtered. Because of Akismet's
origins in WordPress, this resource is called the blog URL. This value should be passed as the second argument to
the constructor, but may be reset at any time using the setBlogUrl() method, or overridden by specifying a 'blog'
key in the various method calls.
1039
Zend_Service
If called with no arguments, verifyKey() uses the API key provided to the constructor.
• user_ip, the IP address of the user submitting the data (not your IP address, but that of a user on your site).
• user_agent, the reported UserAgent string (browser and version) of the user submitting the data.
• blog, the fully qualified URL to the resource or application. If not specified, the URL provided to the constructor
will be used.
• referrer, the content of the HTTP_REFERER header at the time of submission. (Note spelling; it does not follow
the header name.)
• permalink, the permalink location, if any, of the entry the data was submitted to.
• comment_type, the type of data provided. Values specified in the API include 'comment', 'trackback', 'pingback',
and an empty string (''), but it may be any value.
• comment_author_url, the URL or home page of the person submitting the data.
You may also submit any other environmental variables you feel might be a factor in determining if data is spam.
Akismet suggests the contents of the entire $_SERVER array.
The isSpam() method will return either true or false, or throw an exception if the API key is invalid.
$data = array(
'user_ip' => '111.222.111.222',
1040
Zend_Service
Zend_Service_Akismet::submitSpam() takes the same data array as passed to isSpam(), but does not
return a value. An exception will be raised if the API key used is invalid.
$data = array(
'user_ip' => '111.222.111.222',
'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.2;' .
'en-GB; rv:1.8.1) Gecko/20061010 Firefox/2.0',
'comment_type' => 'contact',
'comment_author' => 'John Doe',
'comment_author_email' => 'nospam@myhaus.net',
'comment_content' => "I'm not a spammer, honest!"
);
$akismet->submitSpam($data));
1041
Zend_Service
$data = array(
'user_ip' => '111.222.111.222',
'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.2;' .
'en-GB; rv:1.8.1) Gecko/20061010 Firefox/2.0',
'comment_type' => 'contact',
'comment_author' => 'John Doe',
'comment_author_email' => 'nospam@myhaus.net',
'comment_content' => "I'm not a spammer, honest!"
);
$akismet->submitHam($data));
Zend-specific Methods
While the Akismet API only specifies four methods, Zend_Service_Akismet has several additional methods
that may be used for retrieving and modifying internal properties.
• getBlogUrl() and setBlogUrl() allow you to retrieve and modify the blog URL used in requests.
• getApiKey() and setApiKey() allow you to retrieve and modify the API key used in requests.
• getCharset() and setCharset() allow you to retrieve and modify the character set used to make the request.
• getPort() and setPort() allow you to retrieve and modify the TCP port used to make the request.
• getUserAgent() and setUserAgent() allow you to retrieve and modify the HTTP user agent used to make
the request. Note: this is not the user_agent used in data submitted to the service, but rather the value provided in
the HTTP User-Agent header when making a request to the service.
The value used to set the user agent should be of the form some user agent/version | Akismet/
version. The default is Zend Framework/ZF-VERSION | Akismet/1.11, where ZF-VERSION is the
current Zend Framework version as stored in the Zend_Framework::VERSION constant.
Zend_Service_Amazon
Introduction
Zend_Service_Amazon is a simple API for using Amazon web services. Zend_Service_Amazon has two
APIs: a more traditional one that follows Amazon's own API, and a simpler "Query API" for constructing even complex
search queries easily.
Zend_Service_Amazon enables developers to retrieve information appearing throughout Amazon.com web sites
directly through the Amazon Web Services API. Ejemplos include:
• Amazon.com offers
• ListMania lists
1042
Zend_Service
In order to use Zend_Service_Amazon, you should already have an Amazon developer API key aswell as a secret
key. To get a key and for more information, please visit the Amazon Web Services [http://aws.amazon.com/] web site.
As of August 15th, 2009 you can only use the Amazon Product Advertising API through Zend_Service_Amazon,
when specifying the additional secret key.
Attention
Your Amazon developer API and secret keys are linked to your Amazon identity, so take appropriate measures
to keep them private.
Country Codes
By default, Zend_Service_Amazon connects to the United States ("US") Amazon web service. To connect from
a different country, simply specify the appropriate country code string as the second parameter to the constructor:
Country codes
Valid country codes are: CA, DE, FR, JP, UK, and US.
1043
Zend_Service
The itemLookup() method also accepts an optional second parameter for handling
search options. For full details, including a list of available options, please see
the relevant Amazon documentation [http://www.amazon.com/gp/aws/sdk/main.html/103-9285448-4703844?
s=AWSEcommerceService&v=2005-10-05&p=ApiReference/ItemLookupOperation].
Image information
To retrieve images information for your search results, you must set ResponseGroup option to Medium
or Large.
The itemSearch() method accepts a single array parameter for handling search
options. For full details, including a list of available options, please see the
relevant Amazon documentation [http://www.amazon.com/gp/aws/sdk/main.html/103-9285448-4703844?
s=AWSEcommerceService&v=2005-10-05&p=ApiReference/ItemSearchOperation]
Tip
The Zend_Service_Amazon_Query class is an easy to use wrapper around this method.
1044
Zend_Service
The Zend_Service_Amazon_Query API uses overloading to easily set up an item search and then allows you to
search based upon the criteria specified. Each of the options is provided as a method call, and each method's argument
corresponds to the named option's value:
Zend_Service_Amazon Classes
The following classes are all returned by Zend_Service_Amazon::itemLookup() and
Zend_Service_Amazon::itemSearch():
• Zend_Service_Amazon_Item
• Zend_Service_Amazon_Image
• Zend_Service_Amazon_ResultSet
• Zend_Service_Amazon_OfferSet
• Zend_Service_Amazon_Offer
• Zend_Service_Amazon_SimilarProduct
• Zend_Service_Amazon_Accessories
• Zend_Service_Amazon_CustomerReview
• Zend_Service_Amazon_EditorialReview
• Zend_Service_Amazon_ListMania
1045
Zend_Service
Zend_Service_Amazon_Item
Zend_Service_Amazon_Item is the class type used to represent an Amazon item returned by the web service.
It encompasses all of the items attributes, including title, description, reviews, etc.
Zend_Service_Amazon_Item::asXML()
string asXML();
Properties
Zend_Service_Amazon_Item has a number of properties directly related to their standard Amazon API
counterparts.
1046
Zend_Service
Zend_Service_Amazon_Image
Zend_Service_Amazon_Image represents a remote Image for a product.
Properties
Zend_Service_Amazon_ResultSet
Zend_Service_Amazon_ResultSet objects are returned by Zend_Service_Amazon::itemSearch() and allow
you to easily handle the multiple results returned.
SeekableIterator
Implements the SeekableIterator for easy iteration (e.g. using foreach), as well as direct access to
a specific result using seek().
Zend_Service_Amazon_ResultSet::totalResults()
int totalResults();
Zend_Service_Amazon_OfferSet
Each result returned by Zend_Service_Amazon::itemSearch() and Zend_Service_Amazon::itemLookup() contains a
Zend_Service_Amazon_OfferSet object through which pricing information for the item can be retrieved.
Properties
1047
Zend_Service
Zend_Service_Amazon_Offer
Each offer for an item is returned as an Zend_Service_Amazon_Offer object.
Zend_Service_Amazon_Offer Properties
Zend_Service_Amazon_SimilarProduct
When searching for items, Amazon also returns a list of similar products that the searcher may find to their liking.
Each of these is returned as a Zend_Service_Amazon_SimilarProduct object.
1048
Zend_Service
Each object contains the information to allow you to make sub-sequent requests to get the full information on the item.
Properties
Zend_Service_Amazon_Accessories
Accessories for the returned item are represented as Zend_Service_Amazon_Accessories objects
Properties
Zend_Service_Amazon_CustomerReview
Each Customer Review is returned as a Zend_Service_Amazon_CustomerReview object.
Properties
Zend_Service_Amazon_EditorialReview
Each items Editorial Reviews are returned as a Zend_Service_Amazon_EditorialReview object
1049
Zend_Service
Properties
Zend_Service_Amazon_Listmania
Each results List Mania List items are returned as Zend_Service_Amazon_Listmania objects.
Properties
Zend_Service_Amazon_Ec2
Introduction
Zend_Service_Amazon_Ec2 provides an interface to Amazon Elastic Clound Computing (EC2).
Static Methods
To make using the Ec2 class easier to use there are two static methods that can be invoked from any of the Ec2
Elements. The first static method is setKeys which will defind you AWS Access Keys as default keys. When you
then create any new object you don't need to pass in any keys to the constructor.
Zend_Service_Amazon_Ec2_Ebs::setKeys('aws_key','aws_secret_key');
To set the region that you are working in you can call the setRegion to set which Amazon Ec2 Region you are
working in. Currently there is only two region available us-east-1 and eu-west-1. If an invalid value is passed it will
throw an exception stating that.
1050
Zend_Service
Zend_Service_Amazon_Ec2_Ebs::setRegion('us-east-1');
Zend_Service_Amazon_Ec2: Instances
Instance Types
Amazon EC2 instances are grouped into two families: standard and High-CPU. Standard instances have memory
to CPU ratios suitable for most general purpose applications; High-CPU instances have proportionally more CPU
resources than memory (RAM) and are well suited for compute-intensive applications. When selecting instance types,
you might want to use less powerful instance types for your web server instances and more powerful instance types for
your database instances. Additionally, you might want to run CPU instance types for CPU-intensive data processing
tasks.
One of the advantages of EC2 is that you pay by the instance hour, which makes it convenient and inexpensive to test
the performance of your application on different instance families and types. One good way to determine the most
appropriate instance family and instance type is to launch test instances and benchmark your application.
Instance Types
The instance types are defined as constants in the code. Column eight in the table is the defined constant name
1051
Zend_Service
1052
Zend_Service
run will return information about each instance that is starting up.
This operation is asynchronous; it only queues a request to reboot the specified instance(s). The operation will succeed
if the instances are valid and belong to the user. Requests to reboot terminated instances are ignored.
1053
Zend_Service
Terminated Instances
Terminated instances will remain visible after termination (approximately one hour).
If you specify one or more instance IDs, Amazon EC2 returns information for those instances. If you do not specify
instance IDs, Amazon EC2 returns information for all relevant instances. If you specify an invalid instance ID, a fault
is returned. If you specify an instance that you do not own, it will not be included in the returned results.
Terminated Instances
Recently terminated instances might appear in the returned results. This interval is usually less than one hour.
If you do not want terminated instances to be returned, pass in a second variable of boolean true to describe
and the terminated instances will be ignored.
describeByImageId will return an array containing information on the instances thare were started by the passed
in imageId
Terminated Instances
Recently terminated instances might appear in the returned results. This interval is usually less than one hour.
If you do not want terminated instances to be returned, pass in a second variable of boolean true to describe
and the terminated instances will be ignored.
Instance console output is buffered and posted shortly after instance boot, reboot, and termination. Amazon EC2
preserves the most recent 64 KB output which will be available for at least one hour after the most recent post.
1054
Zend_Service
consoleOutput returns an array containing the instanceId, timestamp from the last output and the output
from the console.
The confirmProduct operation can only be executed by the owner of the AMI. This feature is useful when an
AMI owner is providing support and wants to verify whether a user's instance is eligible.
Zend_Service_Amazon_Ec2: Windows
Instances
Using Amazon EC2 instances running Windows is similar to using instances running Linux and UNIX. The following
are the major differences between instances that use Linux or UNIX and Windows:
• Remote Desktop—To access Windows instances, you use Remote Desktop instead of SSH.
• Administrative Password—To access Windows instances the first time, you must obtain the administrative password
using the ec2-get-password command.
• Simplified Bundling—To bundle a Windows instance, you use a single command that shuts down the instance,
saves it as an AMI, and restarts it.
1055
Zend_Service
As part of this service, Amazon EC2 instances can now run Microsoft Windows Server 2003. Our base Windows
image provides you with most of the common functionality associated with Windows. However, if you require more
than two concurrent Windows users or need to leverage applications that require LDAP, Kerberos, RADIUS, or other
credential services, you must use Windows with Authentication Services. For example, Microsoft Exchange Server
and Microsoft SharePoint Server require Windows with Authentication Services.
Nota
To get started using Windows instances, we recommend using the AWS Management Console. There
are differences in pricing between Windows and Windows with Authentication Services instances. For
information on pricing, go to the Amazon EC2 Product Page.
The Windows public AMIs that Amazon provides are unmodified versions of Windows with the following two
exceptions: we added drivers to improve the networking and disk I/O performance and we created the Amazon EC2
configuration service. The Amazon EC2 configuration service performs the following functions:
• Randomly sets the Administrator password on initial launch, encrypts the password with the user's SSH key, and
reports it to the console. This operation happens upon initial AMI launch. If you change the password, AMIs that
are created from this instance use the new password.
• Configures the computer name to the internal DNS name. To determine the internal DNS name, see Using Instance
Addressing.
• Sends the last three system and application errors from the event log to the console. This helps developers to identify
problems that caused an instance to crash or network connectivity to be lost.
• uploadExpiration The expiration of the upload policy. Amazon recommends 12 hours or longer. This is based in
nubmer of minutes. Default is 1440 minutes (24 hours)
bundle() returns a multi-demential array that contains instanceId, bundleId, state, startTime, updateTime, progress
s3Bucket and s3Prefix.
1056
Zend_Service
'aws_secret_key');
$return = $ec2_instance->bundle('instanceId', 's3Bucket', 's3Prefix');
describeBundle() returns a multi-demential array that contains instanceId, bundleId, state, startTime,
updateTime, progress s3Bucket and s3Prefix.
cancelBundle() returns a multi-demential array that contains instanceId, bundleId, state, startTime, updateTime,
progress s3Bucket and s3Prefix.
Zend_Service_Amazon_Ec2: Reserved
Instances
With Amazon EC2 Reserved Instances, you can make a low one-time payment for each instance to reserve and receive
a significant discount on the hourly usage charge for that instance.
Amazon EC2 Reserved Instances are based on instance type and location (region and Availability Zone) for a specified
period of time (e.g., 1 year or 3 years) and are only available for Linux or UNIX instances.
1057
Zend_Service
Amazon EC2 applies the two m1.small Reserved Instances to two of the instances in Availability Zone us-east-1a.
Amazon EC2 doesn't apply the two c1.medium Reserved Instances because the c1.medium instances are in a different
Availability Zone and does not apply the m1.xlarge Reserved Instances because there are no running m1.xlarge
instances.
Zend_Service_Amazon_Ec2: CloudWatch
Monitoring
Amazon CloudWatch is an easy-to-use web service that provides comprehensive monitoring for Amazon Elastic
Compute Cloud (Amazon EC2) and Elastic Load Balancing. For more details information check cout the Amazon
CloudWatch Developers Guide [http://docs.amazonwebservices.com/AmazonCloudWatch/latest/DeveloperGuide/
index.html?SvcIntro.html]
1058
Zend_Service
CloudWatch Usage
Ejemplo 48.28. Listing Aviable Metrics
listMetrics() returns a list of up to 500 valid metrics for which there is recorded data available to a you and a
NextToken string that can be used to query for the next set of results.
Nota
The maximum number of datapoints that the Amazon CloudWatch service will return in a single
GetMetricStatistics request is 1,440. If a request is made that would generate more datapoints than this
amount, Amazon CloudWatch will return an error. You can alter your request by narrowing the time range
(StartTime, EndTime) or increasing the Period in your single request. You may also get all of the data at the
granularity you originally asked for by making multiple requests with adjacent time ranges.
getMetricStatistics() only requires two parameters but it also has four additional parameters that are
optional.
• Required:
• MeasureName The measure name that corresponds to the measure for the gathered metric. Valid EC2 Values
are CPUUtilization, NetworkIn, NetworkOut, DiskWriteOps DiskReadBytes, DiskReadOps, DiskWriteBytes.
Valid Elastic Load Balancing Metrics are Latency, RequestCount, HealthyHostCount UnHealthyHostCount.
For more information click here [http://docs.amazonwebservices.com/AmazonCloudWatch/latest/DeveloperGuide/
arch-AmazonCloudWatch-metricscollected.html]
• Statistics The statistics to be returned for the given metric. Valid values are Average, Maximum, Minimum,
Samples, Sum. You can specify this as a string or as an array of values. If you don't specify one it
will default to Average instead of failing out. If you specify an incorrect option it will just skip it. For
more information click here [http://docs.amazonwebservices.com/AmazonCloudWatch/latest/DeveloperGuide/
arch-Amazon-CloudWatch-statistics.html]
• Optional:
• Dimensions Amazon CloudWatch allows you to specify one Dimension to further filter metric data on. If you don't
specify a dimension, the service returns the aggregate of all the measures with the given measure name and time
range.
• Unit The standard unit of Measurement for a given Measure. Valid Values: Seconds, Percent, Bytes, Bits, Count,
Bytes/Second, Bits/Second, Count/Second, and None. Constraints: When using count/second as the unit, you should
use Sum as the statistic instead of Average. Otherwise, the sample returns as equal to the number of requests instead
of the number of 60-second intervals. This will cause the Average to always equals one when the unit is count/
second.
• StartTime The timestamp of the first datapoint to return, inclusive. For example, 2008-02-26T19:00:00+00:00. We
round your value down to the nearest minute. You can set your start time for more than two weeks in the past.
However, you will only get data for the past two weeks. (in ISO 8601 format). Constraints: Must be before EndTime.
1059
Zend_Service
• EndTime The timestamp to use for determining the last datapoint to return. This is the last datapoint to fetch,
exclusive. For example, 2008-02-26T20:00:00+00:00 (in ISO 8601 format).
1060
Zend_Service
Name Description
implicit A user has implicit launch permissions for all AMIs he or
she owns.
The list of AMIs returned can be modified by specifying AMI IDs, AMI owners, or users with launch permissions. If
no options are specified, Amazon EC2 returns all AMIs for which the user has launch permissions.
If you specify one or more AMI IDs, only AMIs that have the specified IDs are returned. If you specify an invalid
AMI ID, a fault is returned. If you specify an AMI ID for which you do not have access, it will not be included in
the returned results.
If you specify one or more AMI owners, only AMIs from the specified owners and for which you have access are
returned. The results can include the account IDs of the specified owners, amazon for AMIs owned by Amazon or
self for AMIs that you own.
If you specify a list of executable users, only users that have launch permissions for the AMIs are returned. You can
specify account IDs (if you own the AMI(s)), self for AMIs for which you own or have explicit permissions, or all
for public AMIs.
describe returns an array for all the images that match the critera that was passed in. The array contains the imageId,
imageLocation, imageState, imageOwnerId, isPublic, architecture, imageType, kernelId, ramdiskId and platform.
1061
Zend_Service
'userId',
'userGroup');
describeAttribute returns an array with the value of the attribute that was requested.
You can create up to twenty Amazon EBS volumes of any size (from one GiB up to one TiB). Each Amazon EBS
volume can be attached to any Amazon EC2 instance in the same Availability Zone or can be left unattached.
Amazon EBS provides the ability to create snapshots of your Amazon EBS volumes to Amazon S3. You can use these
snapshots as the starting point for new Amazon EBS volumes and can protect your data for long term durability.
createNewVolume will return an array containing information about the new Volume which includes the volumeId,
size, zone, status and createTime.
1062
Zend_Service
createVolumeFromSnapshot will return an array containing information about the new Volume which includes
the volumeId, size, zone, status, createTime and snapshotId.
createSnapshot will return an array containing information about the new Volume Snapshot which includes the
snapshotId, volumeId, status, startTime and progress.
describeVolume will return an array with information about each Volume which includes the volumeId, size,
status and createTime. If the volume is attached to an instance, an addition value of attachmentSet will be returned. The
attachment set contains information about the instance that the EBS Volume is attached to, which includes volumeId,
instanceId, device, status and attachTime.
describeAttachedVolumes returns the same information as the describeVolume but only for the EBS
Volumes that are currently attached to the specified instanceId.
1063
Zend_Service
describeSnapshot will return an array containing information about each EBS Volume Snapshot which includes
the snapshotId, volumeId, status, startTime and progress.
attachVolume will return an array with information about the attach status which contains volumeId, instanceId,
device, status and attachTime
detachVolume returns an array containing status information about the EBS Volume which includes volumeId,
instanceId, device, status and attachTime.
Forced Detach
You should only force a detach if the previous detachment attempt did not occur cleanly (logging into an
instance, unmounting the volume, and detaching normally). This option can lead to data loss or a corrupted
file system. Use this option only as a last resort to detach a volume from a failed instance. The instance will
not have an opportunity to flush file system caches or file system meta data. If you use this option, you must
perform file system check and repair procedures.
1064
Zend_Service
Zend_Service_Amazon_Ec2: Elastic IP
Addresses
By default, all Amazon EC2 instances are assigned two IP addresses at launch: a private (RFC 1918) address and a
public address that is mapped to the private IP address through Network Address Translation (NAT).
If you use dynamic DNS to map an existing DNS name to a new instance's public IP address, it might take up to 24
hours for the IP address to propagate through the Internet. As a result, new instances might not receive traffic while
terminated instances continue to receive requests.
To solve this problem, Amazon EC2 provides elastic IP addresses. Elastic IP addresses are static IP addresses designed
for dynamic cloud computing. Elastic IP addresses are associated with your account, not specific instances. Any elastic
IP addresses that you associate with your account remain associated with your account until you explicitly release
them. Unlike traditional static IP addresses, however, elastic IP addresses allow you to mask instance or Availability
Zone failures by rapidly remapping your public IP addresses to any instance in your account.
describe returns an array that contains information on each Elastic IP Address which contains the publicIp and
the instanceId if it is assocated.
1065
Zend_Service
// describe a subset
$ips = $ec2_eip->describe(array('ip1', 'ip2', 'ip3'));
Zend_Service_Amazon_Ec2: Keypairs
Keypairs are used to access instances.
create returns an array which contains the keyName, keyFingerprint and keyMaterial.
1066
Zend_Service
$return = $ec2_kp->create('my-new-key');
Each Amazon EC2 region is designed to be completely isolated from the other Amazon EC2 regions. This achieves
the greatest possible failure independence and stability, and it makes the locality of each EC2 resource unambiguous.
describe will return an array containing information about which regions are available. Each array will contain
regionName and regionUrl.
1067
Zend_Service
foreach($regions as $region) {
print $region['regionName'] . ' -- ' . $region['regionUrl'] . '<br />';
}
describe will return an array containing information about which zones are available. Each array will contain
zoneName and zoneState.
foreach($zones as $zone) {
print $zone['zoneName'] . ' -- ' . $zone['zoneState'] . '<br />';
}
You can modify rules for a group at any time. The new rules are automatically enforced for all running instances and
instances launched in the future.
1068
Zend_Service
If you specify security group names, information about those security groups is returned. Otherwise, information for
all security groups is returned. If you specify a group that does not exist, a fault is returned.
describe will return an array containing information about security groups which includes the ownerId, groupName,
groupDescription and an array containing all the rules for that security group.
Authorizing Access
Ejemplo 48.59. Authorizing by IP
authorizeIp Adds permissions to a security group based on an IP address, protocol type and port range.
Permissions are specified by the IP protocol (TCP, UDP or ICMP), the source of the request (by IP range or an Amazon
EC2 user-group pair), the source and destination port ranges (for TCP and UDP), and the ICMP codes and types (for
ICMP). When authorizing ICMP, -1 can be used as a wildcard in the type and code fields.
Permission changes are propagated to instances within the security group as quickly as possible. However, depending
on the number of instances, a small delay might occur.
1069
Zend_Service
'ipRange');
Permission changes are propagated to instances within the security group as quickly as possible. However, depending
on the number of instances, a small delay might occur.
Revoking Access
Ejemplo 48.61. Revoke by IP
revokeIp Revokes permissions to a security group based on an IP address, protocol type and port range. The
permissions used to revoke must be specified using the same values used to grant the permissions.
Permissions are specified by the IP protocol (TCP, UDP or ICMP), the source of the request (by IP range or an Amazon
EC2 user-group pair), the source and destination port ranges (for TCP and UDP), and the ICMP codes and types (for
ICMP). When authorizing ICMP, -1 can be used as a wildcard in the type and code fields.
Permission changes are propagated to instances within the security group as quickly as possible. However, depending
on the number of instances, a small delay might occur.
Permission changes are propagated to instances within the security group as quickly as possible. However, depending
on the number of instances, a small delay might occur.
1070
Zend_Service
Zend_Service_Amazon_S3
Introduction
Amazon S3 provides a simple web services interface that can be used to store and retrieve any amount of data, at any
time, from anywhere on the web. It gives any developer access to the same highly scalable, reliable, fast, inexpensive
data storage infrastructure that Amazon uses to run its own global network of web sites. The service aims to maximize
benefits of scale and to pass those benefits on to developers.
After registering, you will receive an application key and a secret key. You will need both to access the S3 service.
API Documentation
The Zend_Service_Amazon_S3 class provides the PHP wrapper to the Amazon S3 REST interface.
Please consult the Amazon S3 documentation [http://developer.amazonwebservices.com/connect/kbcategory.jspa?
categoryID=48] for detailed description of the service. You will need to be familiar with basic concepts in order to
use this service.
Features
Zend_Service_Amazon_S3 provides the following functionality:
• A single point for configuring your amazon.s3 authentication credentials that can be used across the amazon.s3
namespaces.
• A proxy object that is more convenient to use than an HTTP client alone, mostly removing the need to manually
construct HTTP POST requests to access the REST service.
• A response wrapper that parses each response body and throws an exception if an error occurred, alleviating the
need to repeatedly check the success of many commands.
Getting Started
Once you have registered with Amazon S3, you're ready to store your first data object on the S3. The objects on S3
are stored in containers, called "buckets". Bucket names are unique on S3, and each user can have no more than 100
buckets simultaneously. Each bucket can contain unlimited amount of objects, identified by name.
The following example demonstrates creating a bucket, storing and retrieving the data.
require_once 'Zend/Service/Amazon/S3.php';
1071
Zend_Service
$s3->createBucket("my-own-bucket");
$s3->putObject("my-own-bucket/myobject", "somedata");
echo $s3->getObject("my-own-bucket/myobject");
Since Zend_Service_Amazon_S3 service requires authentication, you should pass your credentials (AWS key
and secret key) to the constructor. If you only use one account, you can set default credentials for the service:
require_once 'Zend/Service/Amazon/S3.php';
Zend_Service_Amazon_S3::setKeys($my_aws_key, $my_aws_secret_key);
$s3 = new Zend_Service_Amazon_S3();
Bucket operations
All objects in S3 system are stored in buckets. Bucket has to be created before any storage operation. Bucket name is
unique in the system, so you can not have bucket named the same as someone else's bucket.
Bucket name can contain lowercase letters, digits, periods (.), underscores (_), and dashes (-). No other symbols
allowed. Bucket name should start with letter or digit, and be 3 to 255 characters long. Names looking like an IP
address (e.g. "192.168.16.255") are not allowed.
• removeBucket() removes the bucket from the system. The bucket should be empty to be removed.
require_once 'Zend/Service/Amazon/S3.php';
$s3->cleanBucket("my-own-bucket");
$s3->removeBucket("my-own-bucket");
• getBuckets() returns the list of the names of all buckets belonging to the user.
require_once 'Zend/Service/Amazon/S3.php';
$list = $s3->getBuckets();
foreach($list as $bucket) {
echo "I have bucket $bucket\n";
}
1072
Zend_Service
Object operations
The object is the basic storage unit in S3. Object stores unstructured data, which can be any size up to 4 gigabytes.
There's no limit on how many objects can be stored on the system.
The object are contained in buckets. Object is identified by name, which can be any utf-8 string. It is common to use
hierarchical names (such as Pictures/Myself/CodingInPHP.jpg) to organise object names. Object name is
prefixed with bucket name when using object functions, so for object "mydata" in bucket "my-own-bucket" the name
would be my-own-bucket/mydata.
Objects can be replaced (by rewriting new data with the same key) or deleted, but not modified, appended, etc. Object
is always stored whole.
By default, all objects are private and can be accessed only by their owner. However, it is possible to specify object with
public access, in which case it will be available through the URL: http://s3.amazonaws.com/[bucket-
name]/[object-name].
• putObject($object, $data, $meta) created an object with name $object (should contain the bucket
name as prefix!) having $data as its content.
Optional $meta parameter is the array of metadata, which currently supports the following parameters as keys:
S3_CONTENT_TYPE_HEADER MIME content type of the data. If not specified, the type will be guessed
according to the file extension of the object name.
S3_ACL_HEADER The access to the item. Following access constants can be used:
S3_ACL_AUTH_READ Only the owner has write access to the item, and
other authenticated S3 users have read access.
This is useful for sharing data between S3
accounts without exposing them to the public.
By default, all the items are private.
require_once 'Zend/Service/Amazon/S3.php';
$s3->putObject("my-own-bucket/Pictures/Me.png", file_get_contents
array(Zend_Service_Amazon_S3::S3_ACL_HEADER =>
Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ));
// or:
$s3->putFile("me.png", "my-own-bucket/Pictures/Me.png",
1073
Zend_Service
array(Zend_Service_Amazon_S3::S3_ACL_HEADER =>
Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ));
echo "Go to http://s3.amazonaws.com/my-own-bucket/Pictures/Me.png
• getInfo($object) retrieves the metadata information about the object. The function will return array with
metadata information. Some of the useful keys are:
etag The ETag of the data, which is the MD5 hash of the data, surrounded by quotes (").
The function will return false if the key does not correspond to any existing object.
• getObjectsByBucket($bucket) returns the list of the object keys, contained in the bucket.
require_once 'Zend/Service/Amazon/S3.php';
$list = $s3->getObjectsByBucket("my-own-bucket");
foreach($list as $name) {
echo "I have $name key:\n";
$data = $s3->getObject("my-own-bucket/$name");
echo "with data: $data\n";
}
• putFile($path, $object, $meta) puts the content of the file in $path into the object named $object.
The optional $meta argument is the same as for putObject. If the content type is omitted, it will be guessed
basing on the source file name.
Stream wrapper
In addition to the interfaces described above, Zend_Service_Amazon_S3 also supports operating as a stream
wrapper. For this, you need to register the client object as the stream wrapper:
require_once 'Zend/Service/Amazon/S3.php';
$s3->registerStreamWrapper("s3");
1074
Zend_Service
mkdir("s3://my-own-bucket");
file_put_contents("s3://my-own-bucket/testdata", "mydata");
echo file_get_contents("s3://my-own-bucket/testdata");
Directory operations (mkdir, rmdir, opendir, etc.) will operate on buckets and thus their arguments should be
of the form of s3://bucketname. File operations operate on objects. Object creation, reading, writing, deletion,
stat and directory listing is supported.
Zend_Service_Amazon_Sqs
Introduction
Amazon Simple Queue Service (Amazon SQS) [http://aws.amazon.com/sqs/] offers a reliable, highly scalable, hosted
queue for storing messages as they travel between computers. By using Amazon SQS, developers can simply move data
between distributed components of their applications that perform different tasks, without losing messages or requiring
each component to be always available. Amazon SQS makes it easy to build an automated workflow, working in close
conjunction with the Amazon Elastic Compute Cloud (Amazon EC2) and the other AWS infrastructure web services.
Amazon SQS works by exposing Amazon's web-scale messaging infrastructure as a web service. Any computer on
the Internet can add or read messages without any installed software or special firewall configurations. Components
of applications using Amazon SQS can run independently, and do not need to be on the same network, developed with
the same technologies, or running at the same time.
After registering, you will receive an application key and a secret key. You will need both to access the SQS service.
API Documentation
The Zend_Service_Amazon_Sqs class provides the PHP wrapper to the Amazon SQS REST interface.
Please consult the Amazon SQS documentation [http://developer.amazonwebservices.com/connect/kbcategory.jspa?
categoryID=31] for detailed description of the service. You will need to be familiar with basic concepts in order to
use this service.
Features
Zend_Service_Amazon_Sqs provides the following functionality:
• A single point for configuring your amazon.sqs authentication credentials that can be used across the amazon.sqs
namespaces.
• A proxy object that is more convenient to use than an HTTP client alone, mostly removing the need to manually
construct HTTP POST requests to access the REST service.
• A response wrapper that parses each response body and throws an exception if an error occurred, alleviating the
need to repeatedly check the success of many commands.
1075
Zend_Service
Getting Started
Once you have registered with Amazon SQS, you're ready to create your queue and store some messages on SQS.
Each queue can contain unlimited amount of messages, identified by name.
The following example demonstrates creating a queue, storing and retrieving messages.
$queue_url = $sqs->create('test');
Since the Zend_Service_Amazon_Sqs service requires authentication, you should pass your credentials (AWS
key and secret key) to the constructor. If you only use one account, you can set default credentials for the service:
Zend_Service_Amazon_Sqs::setKeys($my_aws_key, $my_aws_secret_key);
$sqs = new Zend_Service_Amazon_Sqs();
Queue operations
All messages SQS are stored in queues. A queue has to be created before any message operations. Queue names must
be unique under your access key and secret key.
Queue names can contain lowercase letters, digits, periods (.), underscores (_), and dashes (-). No other symbols
allowed. Queue names can be a maximum of 80 characters.
1076
Zend_Service
• getQueues() returns the list of the names of all queues belonging to the user.
Message operations
After a queue is created, simple messages can be sent into the queue then received at a later point in time. Messages
can be up to 8KB in length. If longer messages are needed please see S3 [http://framework.zend.com/manual/en/
zend.service.amazon.s3.html]. There is no limit to the number of messages a queue can contain.
• sent($queue_url, $message) send the $message to the $queue_url SQS queue URL.
if ($sqs->deleteMessage($queue_url, $message['handle'])) {
echo "Message deleted";
1077
Zend_Service
}
else {
echo "Message not deleted";
}
}
Zend_Service_Audioscrobbler
Introduction
Zend_Service_Audioscrobbler is a simple API for using the Audioscrobbler REST Web Service. The
Audioscrobbler Web Service provides access to its database of Users, Artists, Albums, Tracks, Tags, Groups, and
Forums. The methods of the Zend_Service_Audioscrobbler class begin with one of these terms. The syntax
and namespaces of the Audioscrobbler Web Service are mirrored in Zend_Service_Audioscrobbler. For more
information about the Audioscrobbler REST Web Service, please visit the Audioscrobbler Web Service site [http://
www.audioscrobbler.net/data/webservices/].
Users
In order to retrieve information for a specific user, the setUser() method is first used to select the user for which
data are to be retrieved. Zend_Service_Audioscrobbler provides several methods for retrieving data specific
to a single user:
• userGetTopArtists(): Returns a SimpleXML object containing a list of the current user's most listened to
artists.
• userGetTopAlbums(): Returns a SimpleXML object containing a list of the current user's most listened to
albums.
• userGetTopTracks(): Returns a SimpleXML object containing a list of the current user's most listened to
tracks.
• userGetTopTags(): Returns a SimpleXML object containing a list of tags most applied by the current user.
• userGetTopTagsForAlbum(): Requires that an album be set via setAlbum(). Returns a SimpleXML object
containing the tags most applied to the current album by the current user.
• userGetTopTagsForTrack(): Requires that a track be set via setTrack(). Returns a SimpleXML object
containing the tags most applied to the current track by the current user.
• userGetFriends(): Returns a SimpleXML object containing the user names of the current user's friends.
• userGetNeighbours(): Returns a SimpleXML object containing the user names of people with similar
listening habits to the current user.
• userGetRecentTracks(): Returns a SimpleXML object containing the 10 tracks most recently played by the
current user.
1078
Zend_Service
• userGetRecentLovedTracks(): Returns a SimpleXML object containing a list of the 10 tracks most recently
loved by the current user.
• userGetRecentJournals(): Returns a SimpleXML object containing a list of the current user's most recent
journal entries.
• userGetWeeklyChartList(): Returns a SimpleXML object containing a list of weeks for which there exist
Weekly Charts for the current user.
1079
Zend_Service
$previousWeeklyArtists = $as->userGetPreviousWeeklyArtistChart();
Artists
Zend_Service_Audioscrobbler provides several methods for retrieving data about a specific artist, specified
via the setArtist() method:
• artistGetRelatedArtists(): Returns a SimpleXML object containing a list of Artists similar to the current
Artist.
• artistGetTopFans(): Returns a SimpleXML object containing a list of Users who listen most to the current
Artist.
• artistGetTopTracks(): Returns a SimpleXML object containing a list of the current Artist's top-rated Tracks.
• artistGetTopAlbums(): Returns a SimpleXML object containing a list of the current Artist's top-rated
Albums.
• artistGetTopTags(): Returns a SimpleXML object containing a list of the Tags most frequently applied to
current Artist.
Tracks
Zend_Service_Audioscrobbler provides two methods for retrieving data specific to a single track, specified
via the setTrack() method:
• trackGetTopFans(): Returns a SimpleXML object containing a list of Users who listen most to the current
Track.
• trackGetTopTags(): Returns a SimpleXML object containing a list of the Tags most frequently applied to
the current Track.
1080
Zend_Service
Tags
Zend_Service_Audioscrobbler provides several methods for retrieving data specific to a single tag, specified
via the setTag() method:
• tagGetOverallTopTags(): Returns a SimpleXML object containing a list of Tags most frequently used on
Audioscrobbler.
• tagGetTopArtists(): Returns a SimpleXML object containing a list of Artists to whom the current Tag was
most frequently applied.
• tagGetTopAlbums(): Returns a SimpleXML object containing a list of Albums to which the current Tag was
most frequently applied.
• tagGetTopTracks(): Returns a SimpleXML object containing a list of Tracks to which the current Tag was
most frequently applied.
Groups
Zend_Service_Audioscrobbler provides several methods for retrieving data specific to a single group,
specified via the setGroup() method:
• groupGetRecentJournals(): Returns a SimpleXML object containing a list of recent journal posts by Users
in the current Group.
• groupGetWeeklyChart(): Returns a SimpleXML object containing a list of weeks for which there exist
Weekly Charts for the current Group.
Forums
Zend_Service_Audioscrobbler provides a method for retrieving data specific to a single forum, specified via
the setForum() method:
• forumGetRecentPosts(): Returns a SimpleXML object containing a list of recent posts in the current forum.
1081
Zend_Service
Zend_Service_Delicious
Introduction
Zend_Service_Delicious is simple API for using del.icio.us [http://del.icio.us] XML and JSON web services.
This component gives you read-write access to posts at del.icio.us if you provide credentials. It also allows read-only
access to public data of all users.
Retrieving posts
Zend_Service_Delicious provides three methods for retrieving posts: getPosts(), getRecentPosts()
and getAllPosts(). All of these methods return an instance of Zend_Service_Delicious_PostList,
which holds all retrieved posts.
/**
* Get posts matching the arguments. If no date or url is given,
* most recent date will be used.
*
* @param string $tag Optional filtering by tag
* @param Zend_Date $dt Optional filtering by date
* @param string $url Optional filtering by url
* @return Zend_Service_Delicious_PostList
*/
public function getPosts($tag = null, $dt = null, $url = null);
/**
* Get recent posts
*
* @param string $tag Optional filtering by tag
* @param string $count Maximal number of posts to be returned
* (default 15)
* @return Zend_Service_Delicious_PostList
*/
public function getRecentPosts($tag = null, $count = 15);
/**
* Get all posts
*
* @param string $tag Optional filtering by tag
* @return Zend_Service_Delicious_PostList
1082
Zend_Service
*/
public function getAllPosts($tag = null);
Zend_Service_Delicious_PostList
Instances of this class are returned by the getPosts(), getAllPosts(), getRecentPosts(), and
getUserPosts() methods of Zend_Service_Delicious.
For easier data access this class implements the Countable, Iterator, and ArrayAccess interfaces.
// count posts
echo count($posts);
Nota
The ArrayAccess::offsetSet() and ArrayAccess::offsetUnset() methods throw
exceptions in this implementation. Thus, code like unset($posts[0]); and $posts[0] = 'A';
will throw exceptions because these properties are read-only.
Post list objects have two built-in filtering capabilities. Post lists may be filtered by tags and by URL.
1083
Zend_Service
Editing posts
Ejemplo 48.83. Post editing
// set title
$posts[0]->setTitle('New title');
// save changes
$posts[0]->save();
$posts[0]->setTitle('New title')
->setNotes('New notes')
->save();
Deleting posts
There are two ways to delete a post, by specifying the post URL or by calling the delete() method upon a post
object.
// by specifying URL
$delicious->deletePost('http://framework.zend.com');
1084
Zend_Service
$delicious->deletePost($posts[0]->getUrl());
Tags
Ejemplo 48.87. Tags
Bundles
Ejemplo 48.88. Bundles
// add bundle
$delicious->addBundle('newBundle', array('tag1', 'tag2'));
1085
Zend_Service
Public data
The del.icio.us web API allows access to the public data of all users.
Nota
When using only these methods, a username and password combination is not required when constructing a
new Zend_Service_Delicious object.
Public posts
When retrieving public posts with the getUserPosts() method, a Zend_Service_Delicious_PostList
object is returned, and it contains Zend_Service_Delicious_SimplePost objects, which contain basic
information about the posts, including URL, title, notes, and tags.
HTTP client
Zend_Service_Delicious uses Zend_Rest_Client for making HTTP requests to the del.icio.us web
service. To change which HTTP client Zend_Service_Delicious uses, you need to change the HTTP client
of Zend_Rest_Client.
1086
Zend_Service
When you are making more than one request with Zend_Service_Delicious to speed your requests, it's better
to configure your HTTP client to keep connections alive.
Zend_Rest_Client::getHttpClient()->setConfig(array(
'keepalive' => true
));
Nota
When a Zend_Service_Delicious object is constructed, the SSL transport of Zend_Rest_Client
is set to 'ssl' rather than the default of 'ssl2'. This is because del.icio.us has some problems with
'ssl2', such as requests taking a long time to complete (around 2 seconds).
Zend_Service_Flickr
Introduction
Zend_Service_Flickr is a simple API for using the Flickr REST Web Service. In order to use the Flickr web
services, you must have an API key. To obtain a key and for more information about the Flickr REST Web Service,
please visit the Flickr API Documentation [http://www.flickr.com/services/api/].
In the following example, we use the tagSearch() method to search for photos having "php" in the tags.
$results = $flickr->tagSearch("php");
Optional parameter
tagSearch() accepts an optional second parameter as an array of options.
• userSearch(): Accepts a string query of space-delimited tags and an optional second parameter as an array of
search options, and returns a set of photos as a Zend_Service_Flickr_ResultSet object.
1087
Zend_Service
• getIdByUsername(): Returns a string user ID associated with the given username string.
• getIdByEmail(): Returns a string user ID associated with the given email address string.
$results = $flickr->userSearch($userEmail);
$results = $flickr->groupPoolGetPhotos($groupId);
Optional parameter
groupPoolGetPhotos() accepts an optional second parameter as an array of options.
$image = $flickr->getImageDetails($imageId);
1088
Zend_Service
• Zend_Service_Flickr_ResultSet
• Zend_Service_Flickr_Result
• Zend_Service_Flickr_Image
Zend_Service_Flickr_ResultSet
Represents a set of Results from a Flickr search.
Nota
Implements the SeekableIterator interface for easy iteration (e.g., using foreach), as well as direct
access to a specific result using seek().
Properties
Zend_Service_Flickr_ResultSet::totalResults()
int totalResults();
Zend_Service_Flickr_Result
A single Image result from a Flickr query
Properties
1089
Zend_Service
Zend_Service_Flickr_Image
Represents an Image returned by a Flickr search.
Properties
Zend_Service_Nirvanix
Introduction
Nirvanix provides an Internet Media File System (IMFS), an Internet storage service that allows applications to upload,
store and organize files and subsequently access them using a standard Web Services interface. An IMFS is distributed
clustered file system, accessed over the Internet, and optimized for dealing with media files (audio, video, etc). The
1090
Zend_Service
goal of an IMFS is to provide massive scalability to deal with the challenges of media storage growth, with guaranteed
access and availability regardless of time and location. Finally, an IMFS gives applications the ability to access data
securely, without the large fixed costs associated with acquiring and maintaining physical storage assets.
After registering, you will receive a Username, Password, and Application Key. All three are required to use
Zend_Service_Nirvanix.
API Documentation
Access to the Nirvanix IMFS is available through both SOAP and a faster REST service.
Zend_Service_Nirvanix provides a relatively thin PHP 5 wrapper around the REST service.
Zend_Service_Nirvanix aims to make using the Nirvanix REST service easier but understanding the service
itself is still essential to be successful with Nirvanix.
Features
Nirvanix's REST service can be used effectively with PHP using the SimpleXML [http://www.php.net/simplexml]
extension and Zend_Http_Client alone. However, using it this way is somewhat inconvenient due to repetitive
operations like passing the session token on every request and repeatedly checking the response body for error codes.
• A single point for configuring your Nirvanix authentication credentials that can be used across the Nirvanix
namespaces.
• A proxy object that is more convenient to use than an HTTP client alone, mostly removing the need to manually
construct HTTP POST requests to access the REST service.
• A response wrapper that parses each response body and throws an exception if an error occurred, alleviating the
need to repeatedly check the success of many commands.
Getting Started
Once you have registered with Nirvanix, you're ready to store your first file on the IMFS. The most common operations
that you will need to do on the IMFS are creating a new file, downloading an existing file, and deleting a file.
Zend_Service_Nirvanix provides convenience methods for these three operations.
1091
Zend_Service
echo $imfs->getContents('/foo.txt');
$imfs->unlink('/foo.txt');
The first step to using Zend_Service_Nirvanix is always to authenticate against the service. This is done by
passing your credentials to the Zend_Service_Nirvanix constructor above. The associative array is passed
directly to Nirvanix as POST parameters.
After you have a proxy for the namespace you want to use, call methods on it. The proxy will allow you to use any
command available on the REST API. The proxy may also make convenience methods available, which wrap web
service commands. The example above shows using the IMFS convenience methods to create a new file, retrieve and
display that file, and finally delete the file.
A proxy object may provide convenience methods. These are methods that the Zend_Service_Nirvanix
provides to simplify the use of the Nirvanix web services. In the previous example, the methods putContents(),
getContents(), and unlink() do not have direct equivalents in the REST API. They are convenience methods
provided by Zend_Service_Nirvanix that abstract more complicated operations on the REST API.
For all other method calls to the proxy object, the proxy will dynamically convert the method call to the equivalent
HTTP POST request to the REST API. It does this by using the method name as the API command, and an associative
array in the first argument as the POST parameters.
Let's say you want to call the REST API method RenameFile [http://developer.nirvanix.com/sitefiles/1000/
API.html#_Toc175999923], which does not have a convenience method in Zend_Service_Nirvanix:
Above, a proxy for the IMFS namespace is created. A method, renameFile(), is then called on the proxy. This
method does not exist as a convenience method in the PHP code, so it is trapped by __call() and converted into a
POST request to the REST API where the associative array is used as the POST parameters.
1092
Zend_Service
Notice in the Nirvanix API documentation that sessionToken is required for this method but we did not give it to
the proxy object. It is added automatically for your convenience.
The result of this operation will either be a Zend_Service_Nirvanix_Response object wrapping the XML
returned by Nirvanix, or a Zend_Service_Nirvanix_Exception if an error occurred.
Examining Results
The Nirvanix REST API always returns its results in XML. Zend_Service_Nirvanix parses this
XML with the SimpleXML extension and then decorates the resulting SimpleXMLElement with a
Zend_Service_Nirvanix_Response object.
The simplest way to examine a result from the service is to use the built-in PHP functions like print_r():
<?php
$auth = array('username' => 'your-username',
'password' => 'your-password',
'appKey' => 'your-app-key');
Zend_Service_Nirvanix_Response Object
(
[_sxml:protected] => SimpleXMLElement Object
(
[ResponseCode] => 0
[FilesUploaded] => 1
[BytesUploaded] => 14
)
)
You can access any property or method of the decorated SimpleXMLElement. In the above example, $result-
>BytesUploaded could be used to see the number of bytes received. Should you want to access the
SimpleXMLElement directly, just use $result->getSxml().
The most common response from Nirvanix is success (ResponseCode of zero). It is not normally necessary to check
ResponseCode because any non-zero result will throw a Zend_Service_Nirvanix_Exception. See the
next section on handling errors.
Handling Errors
When using Nirvanix, it's important to anticipate errors that can be returned by the service and handle them
appropriately.
All operations against the REST service result in an XML return payload that contains a ResponseCode element,
such as the following example:
1093
Zend_Service
<Response>
<ResponseCode>0</ResponseCode>
</Response>
When the ResponseCode is zero such as in the example above, the operation was successful. When the operation
is not successful, the ResponseCode is non-zero and an ErrorMessage element should be present.
try {
$imfs = $nirvanix->getService('IMFS');
$imfs->unlink('/a-nonexistant-path');
In the example above, unlink() is a convenience method that wraps the DeleteFiles command
on the REST API. The filePath parameter required by the DeleteFiles [http://developer.nirvanix.com/
sitefiles/1000/API.html#_Toc175999918] command contains a path that does not exist. This will result in a
Zend_Service_Nirvanix exception being thrown with the message "Invalid path" and code 70005.
Zend_Service_ReCaptcha
Introduction
Zend_Service_ReCaptcha provides a client for the reCAPTCHA Web Service [http://recaptcha.net/]. Per the
reCAPTCHA site, "reCAPTCHA is a free CAPTCHA service that helps to digitize books." Each reCAPTCHA requires
the user to input two words, the first of which is the actual CAPTCHA, and the second of which is a word from some
scanned text that Optical Character Recognition (OCR) software has been unable to identify. The assumption is that
if a user correctly provides the first word, the second is likely correctly entered as well, and can be used to improve
OCR software for digitizing books.
In order to use the reCAPTCHA service, you will need to sign up for an account [http://recaptcha.net/
whyrecaptcha.html] and register one or more domains with the service in order to generate public and private keys.
Simplest use
Instantiate a Zend_Service_ReCaptcha object, passing it your public and private keys:
1094
Zend_Service
echo $recaptcha->getHTML();
When the form is submitted, you should receive two fields, 'recaptcha_challenge_field' and 'recaptcha_response_field'.
Pass these to the reCAPTCHA object's verify() method:
$result = $recaptcha->verify(
$_POST['recaptcha_challenge_field'],
$_POST['recaptcha_response_field']
);
Once you have the result, test against it to see if it is valid. The result is a Zend_Service_ReCaptcha_Response
object, which provides an isValid() method.
if (!$result->isValid()) {
// Failed validation
}
It is even simpler to use the reCAPTCHA Zend_Captcha adapter, or to use that adapter as a backend for the
CAPTCHA form element. In each case, the details of rendering and validating the reCAPTCHA are automated for you.
In order to use this component you will need an account [http://recaptcha.net/whyrecaptcha.html] to generate public
and private keys for the mailhide API.
1095
Zend_Service
// Display it
print($mailHide);
The example above will display "m...@example.com" where "..." has a link that opens up a popup window with a
reCAPTCHA challenge.
The public key, private key, and the email address can also be specified in the constructor of the class. A fourth
argument also exists that enables you to set some options for the component. The available options are listed in the
following table:
The configuration options can be set by sending them as the fourth argument to the constructor or by calling
setOptions($options), which takes an associative array or an instance of Zend_Config.
1096
Zend_Service
Zend_Service_Simpy
Introduction
Zend_Service_Simpy is a lightweight wrapper for the free REST API available for the Simpy social bookmarking
service.
In order to use Zend_Service_Simpy, you should already have a Simpy account. To get an account, visit the
Simpy web site [http://simpy.com]. For more information on the Simpy REST API, refer to the Simpy REST API
documentation [http://www.simpy.com/doc/api/rest].
The Simpy REST API allows developers to interact with specific aspects of the service that the Simpy web site offers.
The sections following will outline the use of Zend_Service_Simpy for each of these areas.
Links
When querying links, results are returned in descending order by date added. Links can be searched by title,
nickname, tags, note, or even the content of the web page associated with the link. Simpy offers searching by any
or all of these fields with phrases, boolean operators, and wildcards. See the search syntax [http://www.simpy.com/
faq#searchSyntax] and search fields [http://www.simpy.com/faq#searchFieldsLinks] sections of the Simpy FAQ for
more information.
1097
Zend_Service
/* Search for all links with 'French' in the title and without
'travel' in the tags */
$linkQuery->setQueryString('+title:French -tags:travel');
Links are represented uniquely by their URLs. In other words, if an attempt is made to save a link that has the same
URL as an existing link, data for the existing link will be overwritten with the data specified in the save attempt.
/* Save a link */
$simpy->saveLink(
'Zend Framework' // Title
'http://framework.zend.com', // URL
Zend_Service_Simpy_Link::ACCESSTYPE_PUBLIC, // Access Type
'zend, framework, php' // Tags
'Zend Framework home page' // Alternative title
'This site rocks!' // Note
);
1098
Zend_Service
Tags
When retrieved, tags are sorted in decreasing order (i.e. highest first) by the number of links that use the tag.
Notes
Notes can be saved, retrieved, and deleted. They are uniquely identified by a numeric ID value.
1099
Zend_Service
/* Save a note */
$simpy->saveNote(
'Test Note', // Title
'test,note', // Tags
'This is a test note.' // Description
);
/* Delete a note */
$simpy->deleteNote($note->getId());
Watchlists
Watchlists cannot be created or removed using the API, only retrieved. Thus, you must set up a watchlist via the Simpy
web site prior to attempting to access it using the API.
1100
Zend_Service
Introduction
The Zend_Service_SlideShare component is used to interact with the slideshare.net [http://
www.slideshare.net/] web services for hosting slide shows online. With this component, you can embed slide shows
which are hosted on this web site within a web site and even upload new slide shows to your account.
Once you have setup an account, you can begin using the Zend_Service_SlideShare component by creating a
new instance of the Zend_Service_SlideShare object and providing these values as shown below:
1101
Zend_Service
'SHAREDSECRET',
'USERNAME',
'PASSWORD');
class Zend_Service_SlideShare_SlideShow {
/**
* Retrieves the location of the slide show
*/
public function getLocation() {
return $this->_location;
}
/**
* Gets the transcript for this slide show
*/
public function getTranscript() {
return $this->_transcript;
}
/**
* Adds a tag to the slide show
*/
public function addTag($tag) {
$this->_tags[] = (string)$tag;
return $this;
}
/**
* Sets the tags for the slide show
*/
public function setTags(Array $tags) {
$this->_tags = $tags;
return $this;
}
/**
* Gets all of the tags associated with the slide show
*/
public function getTags() {
return $this->_tags;
}
/**
* Sets the filename on the local filesystem of the slide show
* (for uploading a new slide show)
*/
1102
Zend_Service
/**
* Retrieves the filename on the local filesystem of the slide show
* which will be uploaded
*/
public function getFilename() {
return $this->_slideShowFilename;
}
/**
* Gets the ID for the slide show
*/
public function getId() {
return $this->_slideShowId;
}
/**
* Retrieves the HTML embed code for the slide show
*/
public function getEmbedCode() {
return $this->_embedCode;
}
/**
* Retrieves the Thumbnail URi for the slide show
*/
public function getThumbnailUrl() {
return $this->_thumbnailUrl;
}
/**
* Sets the title for the Slide show
*/
public function setTitle($title) {
$this->_title = (string)$title;
return $this;
}
/**
* Retrieves the Slide show title
*/
public function getTitle() {
return $this->_title;
}
/**
* Sets the description for the Slide show
*/
public function setDescription($desc) {
$this->_description = (string)$desc;
1103
Zend_Service
return $this;
}
/**
* Gets the description of the slide show
*/
public function getDescription() {
return $this->_description;
}
/**
* Gets the numeric status of the slide show on the server
*/
public function getStatus() {
return $this->_status;
}
/**
* Gets the textual description of the status of the slide show on
* the server
*/
public function getStatusDescription() {
return $this->_statusDescription;
}
/**
* Gets the permanent link of the slide show
*/
public function getPermaLink() {
return $this->_permalink;
}
/**
* Gets the number of views the slide show has received
*/
public function getNumViews() {
return $this->_numViews;
}
}
Nota
The above pseudo-class only shows those methods which should be used by end-user developers. Other
available methods are internal to the component.
When using the Zend_Service_SlideShare component, this data class will be used frequently to browse or
add new slide shows to or from the web service.
1104
Zend_Service
$slideshow = $ss->getSlideShow(123456);
You can retrieve slide shows from a specific account by using the getSlideShowsByUsername() method and
providing the username from which the slide shows should be retrieved
You can retrieve slide shows which contain one or more specific tags by using the getSlideShowsByTag
method and providing one or more tags which the slide show must have assigned to it in order to be retrieved
You can retrieve slide shows which are a member of a specific group using the getSlideShowsByGroup method
and providing the name of the group which the slide show must belong to in order to be retrieved
Each of the above methods of retrieving multiple slide shows a similar approach is used. An example of using each
method is shown below:
$starting_offset = 0;
$limit = 10;
1105
Zend_Service
$frontendOptions = array(
'lifetime' => 7200,
'automatic_serialization' => true);
$backendOptions = array(
'cache_dir' => '/webtmp/');
$cache = Zend_Cache::factory('Core',
'File',
$frontendOptions,
$backendOptions);
Zend_Service_StrikeIron
Zend_Service_StrikeIron provides a PHP 5 client to StrikeIron web services. See the following sections:
1106
Zend_Service
Overview
StrikeIron [http://www.strikeiron.com] offers hundreds of commercial data services ("Data as a Service") such as
Online Sales Tax, Currency Rates, Stock Quotes, Geocodes, Global Address Verification, Yellow/White Pages,
MapQuest Driving Directions, Dun & Bradstreet Business Credit Checks, and much, much more.
Each StrikeIron web service shares a standard SOAP (and REST) API, making it easy to integrate and manage multiple
services. StrikeIron also manages customer billing for all services in a single account, making it perfect for solution
providers. Get started with free web services at http://www.strikeiron.com/sdp.
StrikeIron's services may be used through the PHP 5 SOAP extension [http://us.php.net/soap] alone. However, using
StrikeIron this way does not give an ideal PHP-like interface. The Zend_Service_StrikeIron component
provides a lightweight layer on top of the SOAP extension for working with StrikeIron services in a more convenient,
PHP-like manner.
Nota
The PHP 5 SOAP extension must be installed and enabled to use Zend_Service_StrikeIron.
• A single point for configuring your StrikeIron authentication credentials that can be used across many StrikeIron
services.
• A standard way of retrieving your StrikeIron subscription information such as license status and the number of hits
remaining to a service.
• The ability to use any StrikeIron service from its WSDL without creating a PHP wrapper class, and the option of
creating a wrapper for a more convenient interface.
After registering, you will receive a StrikeIron username and password. These will be used when connecting to
StrikeIron using Zend_Service_StrikeIron.
You will also need to sign up [http://www.strikeiron.com/ProductDetail.aspx?p=257] for StrikeIron's Super Data Pack
Web Service.
Both registration steps are free and can be done relatively quickly through the StrikeIron website.
Getting Started
Once you have registered [http://strikeiron.com/Register.aspx] for a StrikeIron account and signed up
for the Super Data Pack [http://www.strikeiron.com/ProductDetail.aspx?p=257], you're ready to start using
Zend_Service_StrikeIron.
StrikeIron consists of hundreds of different web services. Zend_Service_StrikeIron can be used with many
of these services but provides supported wrappers for three of them:
• US Address Verification
1107
Zend_Service
The class Zend_Service_StrikeIron provides a simple way of specifying your StrikeIron account information
and other options in its constructor. It also has a factory method that will return clients for StrikeIron services:
The getService() method will return a client for any StrikeIron service by the name of
its PHP wrapper class. In this case, the name SalesUseTaxBasic refers to the wrapper class
Zend_Service_StrikeIron_SalesUseTaxBasic. Wrappers are included for three services and described
in Bundled Services.
The getService() method can also return a client for a StrikeIron service that does not yet have a PHP wrapper.
This is explained in Using Services by WSDL.
// Get a client for the Sales & Use Tax Basic service
$taxBasic = $strikeIron->getService(array('class' => 'SalesUseTaxBasic'));
In the example above, the getService() method is used to return a client to the Sales & Use Tax Basic service.
The client object is stored in $taxBasic.
The getTaxRateCanada() method is then called on the service. An associative array is used to supply keyword
parameters to the method. This is the way that all StrikeIron methods are called.
The result from getTaxRateCanada() is stored in $rateInfo and has properties like province and GST.
Many of the StrikeIron services are as simple to use as the example above. See Bundled Services for detailed
information on three StrikeIron services.
Examining Results
When learning or debugging the StrikeIron services, it's often useful to dump the result returned from a method call.
The result will always be an object that is an instance of Zend_Service_StrikeIron_Decorator. This is a
small decorator [http://en.wikipedia.org/wiki/Decorator_pattern] object that wraps the results from the method call.
The simplest way to examine a result from the service is to use the built-in PHP functions like print_r() [http://
www.php.net/print_r]:
1108
Zend_Service
<?php
$strikeIron = new Zend_Service_StrikeIron(array('username' => 'your-username',
'password' => 'your-password'));
Zend_Service_StrikeIron_Decorator Object
(
[_name:protected] => GetTaxRateCanadaResult
[_object:protected] => stdClass Object
(
[abbreviation] => ON
[province] => ONTARIO
[GST] => 0.06
[PST] => 0.08
[total] => 0.14
[HST] => Y
)
)
In the output above, we see that the decorator ($rateInfo) wraps an object named GetTaxRateCanadaResult,
the result of the call to getTaxRateCanada().
This means that $rateInfo has public properties like abbreviation, province, and GST. These are accessed
like $rateInfo->province.
Tip
StrikeIron result properties sometimes start with an uppercase letter such as Foo or Bar where most PHP
object properties normally start with a lowercase letter as in foo or bar. The decorator will automatically
do this inflection so you may read a property Foo as foo.
If you ever need to get the original object or its name out of the decorator, use the respective methods
getDecoratedObject() and getDecoratedObjectName().
Handling Errors
The previous examples are naive, i.e. no error handling was shown. It's possible that StrikeIron will return a fault during
a method call. Events like bad account credentials or an expired subscription can cause StrikeIron to raise a fault.
An exception will be thrown when such a fault occurs. You should anticipate and catch these exceptions when making
method calls to the service:
try {
1109
Zend_Service
It's important to understand the difference between exceptions and normal failed method calls. Exceptions occur for
exceptional conditions, such as the network going down or your subscription expiring. Failed method calls that are
a common occurrence, such as getTaxRateCanada() not finding the province you supplied, will not result
an in exception.
Nota
Every time you make a method call to a StrikeIron service, you should check the response object for validity
and also be prepared to catch an exception.
Each StrikeIron client returned by the getService method has the ability to check the subscription status for that
service using the getSubscriptionInfo() method of the client:
// Get a client for the Sales & Use Tax Basic service
$strikeIron = new Zend_Service_StrikeIron(array('username' => 'your-username',
'password' => 'your-password'));
// Check remaining hits for the Sales & Use Tax Basic service
$subscription = $taxBasic->getSubscriptionInfo();
echo $subscription->remainingHits;
The getSubscriptionInfo() method will return an object that typically has a remainingHits property.
It's important to check the status on each service that you are using. If a method call is made to StrikeIron after the
remaining hits have been used up, an exception will occur.
Checking your subscription to a service does not use any remaining hits to the service. Each time any method call
to the service is made, the number of hits remaining will be cached and this cached value will be returned by
getSubscriptionInfo() without connecting to the service again. To force getSubscriptionInfo() to
override its cache and query the subscription information again, use getSubscriptionInfo(true).
1110
Zend_Service
The service contains a getZipCode() method that will retrieve information about a United States ZIP code or
Canadian postal code:
1111
Zend_Service
The service contains a verifyAddressUSA() method that will verify an address in the United States:
// valid address?
$valid = ($result->valid == 'VALID');
}
The service contains two methods, getTaxRateUSA() and getTaxRateCanada(), that will retrieve sales and
use tax data for the United States and Canada, respectively.
1112
Zend_Service
// Get a client for the Sales & Use Tax Basic service
$taxBasic = $strikeIron->getService(array('class' => 'SalesUseTaxBasic'));
To try a StrikeIron service that does not have a wrapper class available, give the wsdl option to getService()
instead of the class option:
Using StrikeIron services from the WSDL will require at least some understanding of the WSDL files. StrikeIron has
many resources on its site to help with this. Also, Jan Schneider [http://janschneider.de] from the Horde project [http://
horde.org] has written a small PHP routine [http://janschneider.de/news/25/268] that will format a WSDL file into
more readable HTML.
Please note that only the services described in the Bundled Services section are officially supported.
1113
Zend_Service
To view a SOAP transaction, call the getSoapClient() method to get the SOAPClient
instance and then call the appropriate methods like __getLastRequest() [http://www.php.net/manual/
en/function.soap-soapclient-getlastrequest.php] and __getLastRequest() [http://www.php.net/manual/en/
function.soap-soapclient-getlastresponse.php]:
$strikeIron =
new Zend_Service_StrikeIron(array('username' => 'your-username',
'password' => 'your-password',
'options' => array('trace' => true)));
// Get a client for the Sales & Use Tax Basic service
$taxBasic = $strikeIron->getService(array('class' => 'SalesUseTaxBasic'));
Zend_Service_Technorati
Introduction
Zend_Service_Technorati provides an easy, intuitive and object-oriented interface for using the Technorati
API. It provides access to all available Technorati API queries [http://technorati.com/developers/api/] and returns the
original XML response as a friendly PHP object.
Technorati [http://technorati.com/] is one of the most popular blog search engines. The API interface enables
developers to retrieve information about a specific blog, search blogs matching a single tag or phrase and get
information about a specific author (blogger). For a full list of available queries please see the Technorati API
documentation [http://technorati.com/developers/api/] or the Available Technorati queries section of this document.
Getting Started
Technorati requires a valid API key for usage. To get your own API Key you first need to create a new Technorati
account [http://technorati.com/signup/], then visit the API Key section [http://technorati.com/developers/apikey.html].
Once you have a valid API key, you're ready to start using Zend_Service_Technorati.
1114
Zend_Service
Each query method accepts an array of optional parameters that can be used to refine your query.
A Zend_Service_Technorati instance is not a single-use object. That is, you don't need to create a new instance
for each query call; simply use your current Zend_Service_Technorati object as long as you need it.
Ejemplo 48.109. Sending multiple queries with the same Zend_Service_Technorati instance
Consuming Results
You can get one of two types of result object in response to a query.
1115
Zend_Service
implements the SeekableIterator PHP interface. The best way to consume a result set object is to loop over it
with the PHP foreach statement.
Nota
SeekableIterator works as an array and counts positions starting from index 0. Fetching position
number 1 means getting the second result in the collection.
1116
Zend_Service
$author = $result->getAuthor();
echo '<h2>Blogs authored by ' . $author->getFirstName() . " " .
$author->getLastName() . '</h2>';
echo '<ol>';
foreach ($result->getWeblogs() as $weblog) {
echo '<li>' . $weblog->getName() . '</li>';
}
echo "</ol>";
Please read the Zend_Service_Technorati Classes section for further details about response classes.
Handling Errors
Each Zend_Service_Technorati query method throws a Zend_Service_Technorati_Exception
exception on failure with a meaningful error message.
There are several reasons that may cause a Zend_Service_Technorati query to fail.
Zend_Service_Technorati validates all parameters for any query request. If a parameter is invalid or it
contains an invalid value, a new Zend_Service_Technorati_Exception exception is thrown. Additionally,
the Technorati API interface could be temporally unavailable, or it could return a response that is not well formed.
1117
Zend_Service
• Cosmos
• Search
• Tag
• DailyCounts
• TopTags
• BlogInfo
• BlogPostTags
• GetInfo
Technorati Cosmos
Cosmos [http://technorati.com/developers/api/cosmos.html] query lets you see what blogs are linking to a given
URL. It returns a Zend_Service_Technorati_CosmosResultSet object. For full details please see
Zend_Service_Technorati::cosmos() in the API reference guide [http://framework.zend.com/apidoc/core/
].
Technorati Search
The Search [http://technorati.com/developers/api/search.html] query lets you see what blogs contain a given search
string. It returns a Zend_Service_Technorati_SearchResultSet object. For full details please see
Zend_Service_Technorati::search() in the API reference guide [http://framework.zend.com/apidoc/core/
].
1118
Zend_Service
Technorati Tag
The Tag [http://technorati.com/developers/api/tag.html] query lets you see what posts are associated with a
given tag. It returns a Zend_Service_Technorati_TagResultSet object. For full details please see
Zend_Service_Technorati::tag() in the API reference guide [http://framework.zend.com/apidoc/core/].
Technorati DailyCounts
The DailyCounts [http://technorati.com/developers/api/dailycounts.html] query provides daily counts of posts
containing the queried keyword. It returns a Zend_Service_Technorati_DailyCountsResultSet object.
For full details please see Zend_Service_Technorati::dailyCounts() in the API reference guide [http://
framework.zend.com/apidoc/core/].
Technorati TopTags
The TopTags [http://technorati.com/developers/api/toptags.html] query provides information on top tags indexed
by Technorati. It returns a Zend_Service_Technorati_TagsResultSet object. For full details please see
1119
Zend_Service
Technorati BlogInfo
The BlogInfo [http://technorati.com/developers/api/bloginfo.html] query provides information on what blog, if
any, is associated with a given URL. It returns a Zend_Service_Technorati_BlogInfoResult object.
For full details please see Zend_Service_Technorati::blogInfo() in the API reference guide [http://
framework.zend.com/apidoc/core/].
Technorati BlogPostTags
The BlogPostTags [http://technorati.com/developers/api/blogposttags.html] query provides information on the top
tags used by a specific blog. It returns a Zend_Service_Technorati_TagsResultSet object. For full
details please see Zend_Service_Technorati::blogPostTags() in the API reference guide [http://
framework.zend.com/apidoc/core/].
1120
Zend_Service
Technorati GetInfo
The GetInfo [http://technorati.com/developers/api/getinfo.html] query tells you things that Technorati knows about
a member. It returns a Zend_Service_Technorati_GetInfoResult object. For full details please see
Zend_Service_Technorati::getInfo() in the API reference guide [http://framework.zend.com/apidoc/
core/].
$author = $result->getAuthor();
echo "<h2>Blogs authored by " . $author->getFirstName() . " " .
$author->getLastName() . "</h2>";
echo "<ol>";
foreach ($result->getWeblogs() as $weblog) {
echo "<li>" . $weblog->getName() . "</li>";
}
echo "</ol>";
Technorati KeyInfo
The KeyInfo query provides information on daily usage of an API key. It
returns a Zend_Service_Technorati_KeyInfoResult object. For full details please see
Zend_Service_Technorati::keyInfo() in the API reference guide [http://framework.zend.com/apidoc/
core/].
Zend_Service_Technorati Classes
The following classes are returned by the various Technorati queries. Each
Zend_Service_Technorati_*ResultSet class holds a type-specific result set which can be easily
iterated, with each result being contained in a type result object. All result set classes extend
Zend_Service_Technorati_ResultSet class and implement the SeekableIterator interface, allowing
for easy iteration and seeking to a specific result.
• Zend_Service_Technorati_ResultSet
• Zend_Service_Technorati_CosmosResultSet
• Zend_Service_Technorati_SearchResultSet
• Zend_Service_Technorati_TagResultSet
• Zend_Service_Technorati_DailyCountsResultSet
• Zend_Service_Technorati_TagsResultSet
• Zend_Service_Technorati_Result
• Zend_Service_Technorati_CosmosResult
• Zend_Service_Technorati_SearchResult
• Zend_Service_Technorati_TagResult
1121
Zend_Service
• Zend_Service_Technorati_DailyCountsResult
• Zend_Service_Technorati_TagsResult
• Zend_Service_Technorati_GetInfoResult
• Zend_Service_Technorati_BlogInfoResult
• Zend_Service_Technorati_KeyInfoResult
Nota
Zend_Service_Technorati_GetInfoResult,
Zend_Service_Technorati_BlogInfoResult and
Zend_Service_Technorati_KeyInfoResult represent exceptions to the above because they
don't belong to a result set and they don't implement any interface. They represent a single response
object and they act as a wrapper for additional Zend_Service_Technorati objects, such as
Zend_Service_Technorati_Author and Zend_Service_Technorati_Weblog.
The Zend_Service_Technorati library includes additional convenient classes representing specific response
objects. Zend_Service_Technorati_Author represents a single Technorati account, also known as
a blog author or blogger. Zend_Service_Technorati_Weblog represents a single weblog object,
along with all specific weblog properties such as feed URLs or blog name. For full details please see
Zend_Service_Technorati in the API reference guide [http://framework.zend.com/apidoc/core/].
Zend_Service_Technorati_ResultSet
Zend_Service_Technorati_ResultSet is the most essential result set. The scope of this class is to be
extended by a query-specific child result set class, and it should never be used to initialize a standalone object. Each of
the specific result sets represents a collection of query-specific Zend_Service_Technorati_Result objects.
Zend_Service_Technorati_CosmosResultSet
Zend_Service_Technorati_CosmosResultSet represents a Technorati Cosmos query result set.
Nota
Zend_Service_Technorati_CosmosResultSet extends Zend_Service_Technorati_ResultSet.
1122
Zend_Service
Zend_Service_Technorati_SearchResultSet
Zend_Service_Technorati_SearchResultSet represents a Technorati Search query result set.
Nota
Zend_Service_Technorati_SearchResultSet extends Zend_Service_Technorati_ResultSet.
Zend_Service_Technorati_TagResultSet
Zend_Service_Technorati_TagResultSet represents a Technorati Tag query result set.
Nota
Zend_Service_Technorati_TagResultSet extends Zend_Service_Technorati_ResultSet.
Zend_Service_Technorati_DailyCountsResultSet
Zend_Service_Technorati_DailyCountsResultSet represents a Technorati DailyCounts query result
set.
Nota
Zend_Service_Technorati_DailyCountsResultSet extends
Zend_Service_Technorati_ResultSet.
Zend_Service_Technorati_TagsResultSet
Zend_Service_Technorati_TagsResultSet represents a Technorati TopTags or BlogPostTags queries
result set.
Nota
Zend_Service_Technorati_TagsResultSet extends Zend_Service_Technorati_ResultSet.
Zend_Service_Technorati_Result
Zend_Service_Technorati_Result is the most essential result object. The scope of this class is to be extended
by a query specific child result class, and it should never be used to initialize a standalone object.
Zend_Service_Technorati_CosmosResult
Zend_Service_Technorati_CosmosResult represents a single Technorati Cosmos query result object. It is
never returned as a standalone object, but it always belongs to a valid Zend_Service_Technorati_CosmosResultSet
object.
Nota
Zend_Service_Technorati_CosmosResult extends Zend_Service_Technorati_Result.
Zend_Service_Technorati_SearchResult
Zend_Service_Technorati_SearchResult represents a single Technorati Search query result object. It is
never returned as a standalone object, but it always belongs to a valid Zend_Service_Technorati_SearchResultSet
object.
1123
Zend_Service
Nota
Zend_Service_Technorati_SearchResult extends Zend_Service_Technorati_Result.
Zend_Service_Technorati_TagResult
Zend_Service_Technorati_TagResult represents a single Technorati Tag query result object. It is never
returned as a standalone object, but it always belongs to a valid Zend_Service_Technorati_TagResultSet object.
Nota
Zend_Service_Technorati_TagResult extends Zend_Service_Technorati_Result.
Zend_Service_Technorati_DailyCountsResult
Zend_Service_Technorati_DailyCountsResult represents a single Technorati DailyCounts query
result object. It is never returned as a standalone object, but it always belongs to a valid
Zend_Service_Technorati_DailyCountsResultSet object.
Nota
Zend_Service_Technorati_DailyCountsResult extends Zend_Service_Technorati_Result.
Zend_Service_Technorati_TagsResult
Zend_Service_Technorati_TagsResult represents a single Technorati TopTags or BlogPostTags
query result object. It is never returned as a standalone object, but it always belongs to a valid
Zend_Service_Technorati_TagsResultSet object.
Nota
Zend_Service_Technorati_TagsResult extends Zend_Service_Technorati_Result.
Zend_Service_Technorati_GetInfoResult
Zend_Service_Technorati_GetInfoResult represents a single Technorati GetInfo query result object.
Zend_Service_Technorati_BlogInfoResult
Zend_Service_Technorati_BlogInfoResult represents a single Technorati BlogInfo query result object.
Zend_Service_Technorati_KeyInfoResult
Zend_Service_Technorati_KeyInfoResult represents a single Technorati KeyInfo query result object. It
provides information about your Technorati API Key daily usage.
Zend_Service_Twitter
Introduction
Zend_Service_Twitter provides a client for the Twitter REST API [http://apiwiki.twitter.com/Twitter-API-
Documentation]. Zend_Service_Twitter allows you to query the public timeline. If you provide a username
1124
Zend_Service
and password for Twitter, it will allow you to get and update your status, reply to friends, direct message friends, mark
tweets as favorite, and much more.
Zend_Service_Twitter is broken up into subsections so you can easily identify which type of call is being
requested.
• account makes sure that your account credentials are valid, checks your API rate limit, and ends the current
session for the authenticated user.
• status retrieves the public and user timelines and shows, updates, destroys, and retrieves replies for the
authenticated user.
• user retrieves friends and followers for the authenticated user and returns extended information about a passed user.
• directMessage retrieves the authenticated user's received direct messages, deletes direct messages, and sends
new direct messages.
Authentication
With the exception of fetching the public timeline, Zend_Service_Twitter requires authentication to work.
Twitter currently uses HTTP Basic Authentication [http://en.wikipedia.org/wiki/Basic_authentication_scheme]. You
can pass in your username or registered email along with your password for Twitter to login.
You can also pass in an array that contains the username and password as the first argument.
Account Methods
• verifyCredentials() tests if supplied user credentials are valid with minimal overhead.
1125
Zend_Service
• rateLimitStatus() returns the remaining number of API requests available to the authenticating user before
the API limit is reached for the current hour.
Status Methods
• publicTimeline() returns the 20 most recent statuses from non-protected users with a custom user icon. The
public timeline is cached by Twitter for 60 seconds.
• friendsTimeline() returns the 20 most recent statuses posted by the authenticating user and that user's friends.
The friendsTimeline() method accepts an array of optional parameters to modify the query.
• since narrows the returned results to just those statuses created after the specified date/time (up to 24 hours old).
• userTimeline() returns the 20 most recent statuses posted from the authenticating user.
1126
Zend_Service
$response = $twitter->status->userTimeline();
The userTimeline() method accepts an array of optional parameters to modify the query.
• id specifies the ID or screen name of the user for whom to return the friends_timeline.
• since narrows the returned results to just those statuses created after the specified date/time (up to 24 hours old).
• count specifies the number of statuses to retrieve. May not be greater than 200.
• show() returns a single status, specified by the id parameter below. The status' author will be returned inline.
• update() updates the authenticating user's status. This method requires that you pass in the status update that
you want to post to Twitter.
• in_reply_to_status_id specifies the ID of an existing status that the status to be posted is in reply to.
• replies() returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating
user.
The replies() method accepts an array of optional parameters to modify the query.
• since narrows the returned results to just those statuses created after the specified date/time (up to 24 hours old).
• since_id returns only statuses with an ID greater than (that is, more recent than) the specified ID.
1127
Zend_Service
$response = $twitter->status->destroy(12345);
User Methods
• friends()r eturns up to 100 of the authenticating user's friends who have most recently updated, each with
current status inline.
The friends() method accepts an array of optional parameters to modify the query.
• id specifies the ID or screen name of the user for whom to return a list of friends.
• since narrows the returned results to just those statuses created after the specified date/time (up to 24 hours old).
• followers() returns the authenticating user's followers, each with current status inline.
The followers() method accepts an array of optional parameters to modify the query.
• id specifies the ID or screen name of the user for whom to return a list of followers.
• show() returns extended information of a given user, specified by ID or screen name as per the required id
parameter below.
The message() method accepts an array of optional parameters to modify the query.
1128
Zend_Service
• since_id returns only direct messages with an ID greater than (that is, more recent than) the specified ID.
• since narrows the returned results to just those statuses created after the specified date/time (up to 24 hours old).
• sent() returns a list of the 20 most recent direct messages sent by the authenticating user.
The sent() method accepts an array of optional parameters to modify the query.
• since_id returns only direct messages with an ID greater than (that is, more recent than) the specified ID.
• since narrows the returned results to just those statuses created after the specified date/time (up to 24 hours old).
• new() sends a new direct message to the specified user from the authenticating user. Requires both the user and
text parameters below.
• destroy() destroys the direct message specified in the required id parameter. The authenticating user must be
the recipient of the specified direct message.
Friendship Methods
• create() befriends the user specified in the id parameter with the authenticating user.
• destroy() discontinues friendship with the user specified in the id parameter and the authenticating user.
1129
Zend_Service
• exists() tests if a friendship exists between the user specified in the id parameter and the authenticating user.
Favorite Methods
• favorites() returns the 20 most recent favorite statuses for the authenticating user or user specified by the id
parameter.
The favorites() method accepts an array of optional parameters to modify the query.
• id specifies the ID or screen name of the user for whom to request a list of favorite statuses.
• create() favorites the status specified in the id parameter as the authenticating user.
• destroy() un-favorites the status specified in the id parameter as the authenticating user.
Block Methods
• exists() checks if the authenticating user is blocking a target user and can optionally return the blocked user's
object if a block does exists.
1130
Zend_Service
• returnResult specifies whether or not return the user object instead of just true or false.
• create() blocks the user specified in the id parameter as the authenticating user and destroys a friendship to the
blocked user if one exists. Returns the blocked user in the requested format when successful.
• destroy() un-blocks the user specified in the id parameter for the authenticating user. Returns the un-blocked
user in the requested format when successful.
• blocking() returns an array of user objects that the authenticating user is blocking.
• page specifies which page ou want to return. A single page contains 20 IDs.
• returnUserIds specifies whether to return an array of numeric user IDs the authenticating user is blocking
instead of an array of user objects.
Zend_Service_Twitter_Search
Introduction
Zend_Service_Twitter_Search provides a client for the Twitter Search API [http://apiwiki.twitter.com/
Search+API+Documentation]. The Twitter Search service is use to search Twitter. Currently, it only returns data in
Atom or JSON format, but a full REST service is in the future, which will support XML responses.
1131
Zend_Service
Twitter Trends
Returns the top ten queries that are currently trending on Twitter. The response includes the time of the request, the
name of each trending topic, and the url to the Twitter Search results page for that topic. Currently the search API for
trends only supports a JSON return so the function returns an array.
Searching Twitter
Using the search method returns tweets that match a specific query. There are a number of Search Operators [http://
search.twitter.com/operators] that you can use to query with.
The search method can accept six different optional URL parameters passed in as an array:
• lang restricts the tweets to a given language. lang must be given by an ISO 639-1 code [http://en.wikipedia.org/
wiki/ISO_639-1].
• page specifies the page number to return, up to a maximum of roughly 1500 results (based on rpp * page).
• since_id returns tweets with status IDs greater than the given ID.
• show_user specifies whether to add ">user<:" to the beginning of the tweet. This is useful for readers that do not
display Atom's author field. The default is "false".
• geocode returns tweets by users located within a given radius of the given latitude/longitude, where the user's
location is taken from their Twitter profile. The parameter value is specified by "latitude,longitude,radius", where
radius units must be specified as either "mi" (miles) or "km" (kilometers).
1132
Zend_Service
• getResponseType() and setResponseType() allow you to retrieve and modify the response type of the
search between JSON and Atom.
Zend_Service_Yahoo
Introduction
Zend_Service_Yahoo is a simple API for using many of the Yahoo! REST APIs. Zend_Service_Yahoo
allows you to search Yahoo! Web search, Yahoo! News, Yahoo! Local, Yahoo! Images. In order to use the Yahoo!
REST API, you must have a Yahoo! Application ID. To obtain an Application ID, please complete and submit the
Application ID Request Form [http://developer.yahoo.com/wsregapp/].
1133
Zend_Service
1134
Zend_Service
$results = $yahoo->inlinkDataSearch('http://framework.zend.com/');
foreach ($results as $result) {
echo $result->Title .'<br />';
}
Zend_Service_Yahoo Classes
The following classes are all returned by the various Yahoo! searches. Each search type returns a type-specific result
set which can be easily iterated, with each result being contained in a type result object. All result set classes implement
the SeekableIterator interface, allowing for easy iteration and seeking to a specific result.
• Zend_Service_Yahoo_ResultSet
• Zend_Service_Yahoo_WebResultSet
• Zend_Service_Yahoo_ImageResultSet
• Zend_Service_Yahoo_VideoResultSet
• Zend_Service_Yahoo_LocalResultSet
• Zend_Service_Yahoo_NewsResultSet
• Zend_Service_Yahoo_InlinkDataResultSet
• Zend_Service_Yahoo_PageDataResultSet
• Zend_Service_Yahoo_Result
• Zend_Service_Yahoo_WebResult
• Zend_Service_Yahoo_ImageResult
• Zend_Service_Yahoo_VideoResult
• Zend_Service_Yahoo_LocalResult
• Zend_Service_Yahoo_NewsResult
• Zend_Service_Yahoo_InlinkDataResult
• Zend_Service_Yahoo_PageDataResult
1135
Zend_Service
• Zend_Service_Yahoo_Image
Zend_Service_Yahoo_ResultSet
Each of the search specific result sets is extended from this base class.
Each of the specific result sets returns a search specific Zend_Service_Yahoo_Result objects.
Zend_Service_Yahoo_ResultSet::totalResults()
int totalResults();
Properties
Zend_Service_Yahoo_WebResultSet
Zend_Service_Yahoo_WebResultSet represents a Yahoo! Web Search result set.
Nota
Zend_Service_Yahoo_WebResultSet extends Zend_Service_Yahoo_ResultSet
Zend_Service_Yahoo_ImageResultSet
Zend_Service_Yahoo_ImageResultSet represents a Yahoo! Image Search result set.
Nota
Zend_Service_Yahoo_ImageResultSet extends Zend_Service_Yahoo_ResultSet
Zend_Service_Yahoo_VideoResultSet
Zend_Service_Yahoo_VideoResultSet represents a Yahoo! Video Search result set.
Nota
Zend_Service_Yahoo_VideoResultSet extends Zend_Service_Yahoo_ResultSet
1136
Zend_Service
Zend_Service_Yahoo_LocalResultSet
Zend_Service_Yahoo_LocalResultSet represents a Yahoo! Local Search result set.
Nota
Zend_Service_Yahoo_LocalResultSet extends Zend_Service_Yahoo_ResultSet
Zend_Service_Yahoo_NewsResultSet
Zend_Service_Yahoo_NewsResultSet represents a Yahoo! News Search result set.
Nota
Zend_Service_Yahoo_NewsResultSet extends Zend_Service_Yahoo_ResultSet
Zend_Service_Yahoo_InlinkDataResultSet
Zend_Service_Yahoo_InlinkDataResultSet represents a Yahoo! Inbound Link Search result set.
Nota
Zend_Service_Yahoo_InlinkDataResultSet extends Zend_Service_Yahoo_ResultSet
Zend_Service_Yahoo_PageDataResultSet
Zend_Service_Yahoo_PageDataResultSet represents a Yahoo! PageData Search result set.
Nota
Zend_Service_Yahoo_PageDataResultSet extends Zend_Service_Yahoo_ResultSet
Zend_Service_Yahoo_Result
Each of the search specific results is extended from this base class.
1137
Zend_Service
Properties
Zend_Service_Yahoo_WebResult
Each Web Search result is returned as a Zend_Service_Yahoo_WebResult object.
Properties
Zend_Service_Yahoo_ImageResult
Each Image Search result is returned as a Zend_Service_Yahoo_ImageResult object.
Properties
1138
Zend_Service
Zend_Service_Yahoo_VideoResult
Each Video Search result is returned as a Zend_Service_Yahoo_VideoResult object.
Properties
Zend_Service_Yahoo_LocalResult
Each Local Search result is returned as a Zend_Service_Yahoo_LocalResult object.
Properties
1139
Zend_Service
Zend_Service_Yahoo_NewsResult
Each News Search result is returned as a Zend_Service_Yahoo_NewsResult object.
Properties
Zend_Service_Yahoo_InlinkDataResult
Each Inbound Link Search result is returned as a Zend_Service_Yahoo_InlinkDatabResult object.
Zend_Service_Yahoo_PageDataResult
Each Page Data Search result is returned as a Zend_Service_Yahoo_PageDatabResult object.
Zend_Service_Yahoo_Image
All images returned either by the Yahoo! Image Search or the Yahoo! News Search are represented by
Zend_Service_Yahoo_Image objects
1140
Zend_Service
Properties
1141
1142
Capítulo 49. Zend_Session
Introduction
The Zend Framework Auth team greatly appreciates your feedback and contributions on our email list: fw-
auth@lists.zend.com [mailto:fw-auth@lists.zend.com]
With web applications written using PHP, a session represents a logical, one-to-one connection between server-
side, persistent state data and a particular user agent client (e.g., web browser). Zend_Session helps manage
and preserve session data, a logical complement of cookie data, across multiple page requests by the same client.
Unlike cookie data, session data are not stored on the client side and are only shared with the client when
server-side source code voluntarily makes the data available in response to a client request. For the purposes of
this component and documentation, the term "session data" refers to the server-side data stored in $_SESSION
[http://www.php.net/manual/en/reserved.variables.php#reserved.variables.session], managed by Zend_Session,
and individually manipulated by Zend_Session_Namespace accessor objects. Session namespaces provide
access to session data using classic namespaces [http://en.wikipedia.org/wiki/Namespace_%28computer_science%29]
implemented logically as named groups of associative arrays, keyed by strings (similar to normal PHP arrays).
Zend_Session_Namespace instances are accessor objects for namespaced slices of $_SESSION. The
Zend_Session component wraps the existing PHP ext/session with an administration and management
interface, as well as providing an API for Zend_Session_Namespace to persist session namespaces.
Zend_Session_Namespace provides a standardized, object-oriented interface for working with namespaces
persisted inside PHP's standard session mechanism. Support exists for both anonymous and authenticated
(e.g., "login") session namespaces. Zend_Auth, the authentication component of Zend Framework,
uses Zend_Session_Namespace to store some information associated with authenticated users. Since
Zend_Session uses the normal PHP ext/session functions internally, all the familiar configuration options and
settings apply (see http://www.php.net/session), with such bonuses as the convenience of an object-oriented interface
and default behavior that provides both best practices and smooth integration with Zend Framework. Thus, a standard
PHP session identifier, whether conveyed by cookie or within URLs, maintains the association between a client and
session state data.
Basic Usage
Zend_Session_Namespace instances provide the primary API for manipulating session data in the Zend
Framework. Namespaces are used to segregate all session data, although a default namespace exists for those
who only want one namespace for all their session data. Zend_Session utilizes ext/session and its special
$_SESSION superglobal as the storage mechanism for session state data. While $_SESSION is still available
in PHP's global namespace, developers should refrain from directly accessing it, so that Zend_Session and
Zend_Session_Namespace can most effectively and securely provide its suite of session related functionality.
1143
Zend_Session
It is possible to use Zend_Session in conjunction with other code that uses $_SESSION directly. To avoid problems,
however, it is highly recommended that such code only uses parts of $_SESSION that do not correspond to instances
of Zend_Session_Namespace.
Tutorial Ejemplos
If no namespace is specified when instantiating Zend_Session_Namespace, all data will be transparently stored
in a namespace called "Default". Zend_Session is not intended to work directly on the contents of session
namespace containers. Instead, we use Zend_Session_Namespace. The example below demonstrates use of this
default namespace, showing how to count the number of client requests during a session:
if (isset($defaultNamespace->numberOfPageRequests)) {
// this will increment for each page load.
$defaultNamespace->numberOfPageRequests++;
} else {
$defaultNamespace->numberOfPageRequests = 1; // first time
}
When multiple modules use instances of Zend_Session_Namespace having different namespaces, each module
obtains data encapsulation for its session data. The Zend_Session_Namespace constructor can be passed an
optional $namespace argument, which allows developers to partition session data into separate namespaces.
Namespacing provides an effective and popular way to secure session state data against changes due to accidental
naming collisions.
Namespace names are restricted to character sequences represented as non-empty PHP strings that do not begin with an
underscore ("_") character. Only core components included in Zend Framework should use namespace names starting
with "Zend".
The example above achieves the same effect as the code below, except that the session objects above preserve
encapsulation of session data within their respective namespaces.
1144
Zend_Session
$_SESSION['Zend_Auth']['user'] = "myusername";
$_SESSION['Some_Web_Service']['user'] = "mywebusername";
$aNamespace =
new Zend_Session_Namespace('some_namespace_with_data_present');
$namespace->foo = 100;
if (!isset($namespace->bar)) {
echo "\$namespace->bar not set\n";
}
unset($namespace->foo);
Advanced Usage
While the basic usage examples are a perfectly acceptable way to utilize Zend Framework sessions, there are some
best practices to consider. This section discusses the finer details of session handling and illustrates more advanced
usage of the Zend_Session component.
Starting a Session
If you want all requests to have a session facilitated by Zend_Session, then start the session in the bootstrap file:
Zend_Session::start();
1145
Zend_Session
By starting the session in the bootstrap file, you avoid the possibility that your session might be started after headers
have been sent to the browser, which results in an exception, and possibly a broken page for website viewers. Various
advanced features require Zend_Session::start() first. (More on advanced features later.)
There are four ways to start a session, when using Zend_Session. Two are wrong.
php_value session.auto_start 0
2. Wrong: Do not use PHP's session_start() [http://www.php.net/session_start] function directly. If you use
session_start() directly, and then start using Zend_Session_Namespace, an exception will be thrown
by Zend_Session::start() ("session has already been started"). If you call session_start() after
using Zend_Session_Namespace or calling Zend_Session::start(), an error of level E_NOTICE will
be generated, and the call will be ignored.
3. Correct: Use Zend_Session::start(). If you want all requests to have and use sessions, then place this
function call early and unconditionally in your bootstrap code. Sessions have some overhead. If some requests need
sessions, but other requests will not need to use sessions, then:
• Unconditionally set the strict option to TRUE using Zend_Session::setOptions() in your bootstrap.
• Call Zend_Session::start() only for requests that need to use sessions and before any
Zend_Session_Namespace objects are instantiated.
The strict option prevents new Zend_Session_Namespace() from automatically starting the
session using Zend_Session::start(). Thus, this option helps application developers enforce a design
decision to avoid using sessions for certain requests, since it causes an exception to be thrown when
Zend_Session_Namespace is instantiated before Zend_Session::start() is called. Developers should
carefully consider the impact of using Zend_Session::setOptions(), since these options have global
effect, owing to their correspondence to the underlying options for ext/session.
4. Correct: Just instantiate Zend_Session_Namespace whenever needed, and the underlying PHP session will
be automatically started. This offers extremely simple usage that works well in most situations. However, you then
become responsible for ensuring that the first new Zend_Session_Namespace() happens before any output
(e.g., HTTP headers [http://www.php.net/headers_sent]) has been sent by PHP to the client, if you are using the
default, cookie-based sessions (strongly recommended). See the section called “Error: Headers Already Sent” for
more information.
1146
Zend_Session
Namespace Expiration
Limits can be placed on the longevity of both namespaces and individual keys in namespaces. Common use cases
include passing temporary information between requests, and reducing exposure to certain security risks by removing
access to potentially sensitive information some time after authentication occurred. Expiration can be based on either
elapsed seconds or the number of "hops", where a hop occurs for each successive request.
$s = new Zend_Session_Namespace('expireAll');
$s->a = 'apple';
$s->p = 'pear';
$s->o = 'orange';
$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.
When working with data expiring from the session in the current request, care should be used when retrieving them.
Although the data are returned by reference, modifying the data will not make expiring data persist past the current
request. In order to "reset" the expiration time, fetch the data into temporary variables, use the namespace to unset
them, and then set the appropriate keys again.
1147
Zend_Session
// ...
// in the question view controller
$testSpace = new Zend_Session_Namespace('testSpace');
// expire only this variable
$testSpace->setExpirationSeconds(300, 'accept_answer');
$testSpace->accept_answer = true;
//...
Below, the controller that processes the answers to test questions determines whether or not to accept an answer based
on whether the user submitted the answer within the allotted time:
// ...
// in the answer processing controller
$testSpace = new Zend_Session_Namespace('testSpace');
if ($testSpace->accept_answer === true) {
// within time
}
else {
// not within time
}
// ...
To enable this behavior, pass TRUE to the second constructor argument when creating the last allowed instance of
Zend_Session_Namespace. Any subsequent attempt to instantiate the same namespace would result in a thrown
exception.
$authSpaceAccessor1->foo = 'bar';
assert($authSpaceAccessor2->foo, 'bar');
try {
$aNamespaceObject = new Zend_Session_Namespace('Zend_Auth');
} catch (Zend_Session_Exception $e) {
1148
Zend_Session
The second parameter in the constructor above tells Zend_Session_Namespace that any future instances with
the "Zend_Auth" namespace are not allowed. Attempting to create such an instance causes an exception to be
thrown by the constructor. The developer therefore becomes responsible for storing a reference to an instance object
($authSpaceAccessor1, $authSpaceAccessor2, or $authSpaceAccessor3 in the example above)
somewhere, if access to the session namespace is needed at a later time during the same request. For example, a
developer may store the reference in a static variable, add the reference to a registry [http://www.martinfowler.com/
eaaCatalog/registry.html] (see Capítulo 44, Zend_Registry), or otherwise make it available to other methods that may
need access to the session namespace.
If you are using an affected version of PHP and need to modify the array after assigning it to a session namespace key,
you may use either or both of the following workarounds.
1149
Zend_Session
To work around this, see the unit test testSetExpirationSeconds() in SessionTest.php and
SessionTestHelper.php, both located in tests/Zend/Session, which make use of PHP's exec() to
launch a separate process. The new process more accurately simulates a second, successive request from a browser.
The separate process begins with a "clean" session, just like any PHP script execution for a web request. Also, any
changes to $_SESSION made in the calling process become available to the child process, provided the parent closed
the session before using exec().
// testing setExpirationSeconds()
$script = 'SessionTestHelper.php';
$s = new Zend_Session_Namespace('space');
$s->a = 'apple';
$s->o = 'orange';
1150
Zend_Session
$s->setExpirationSeconds(5);
Zend_Session::regenerateId();
$id = Zend_Session::getId();
session_write_close(); // release session so process below can use it
sleep(4); // not long enough for things to expire
exec($script . "expireAll $id expireAll", $result);
$result = $this->sortResult($result);
$expect = ';a === apple;o === orange;p === pear';
$this->assertTrue($result === $expect,
"iteration over default Zend_Session namespace failed; " .
"expecting result === '$expect', but got '$result'");
Configuration Options
When the first session namespace is requested, Zend_Session will automatically start the PHP session,
unless already started with Zend_Session::start(). The underlying PHP session will use defaults from
Zend_Session, unless modified first by Zend_Session::setOptions().
1151
Zend_Session
To set a session configuration option, include the basename (the part of the name after "session.") as a key of an
array passed to Zend_Session::setOptions(). The corresponding value in the array is used to set the session
option value. If no options are set by the developer, Zend_Session will utilize recommended default options first,
then the default php.ini settings. Community feedback about best practices for these options should be sent to fw-
auth@lists.zend.com [mailto:fw-auth@lists.zend.com].
Next, load the configuration file and pass its array representation to Zend_Session::setOptions():
1152
Zend_Session
Zend_Session::setOptions($config->toArray());
Most options shown above need no explanation beyond that found in the standard PHP documentation, but those of
particular interest are noted below.
• integer remember_me_seconds - how long should session id cookie persist, after user agent has ended (e.g.,
browser application terminated).
• string save_path - The correct value is system dependent, and should be provided by the developer using an
absolute path to a directory readable and writable by the PHP process. If a writable path is not supplied, then
Zend_Session will throw an exception when started (i.e., when start() is called).
Security Risk
If the path is readable by other applications, then session hijacking might be possible. If the path is writable
by other applications, then session poisoning [http://en.wikipedia.org/wiki/Session_poisoning] might be
possible. If this path is shared with other users or other PHP applications, various security issues might
occur, including theft of session content, hijacking of sessions, and collision of garbage collection (e.g.,
another user's application might cause PHP to delete your application's session files).
For example, an attacker can visit the victim's website to obtain a session cookie. Then, he edits
the cookie path to his own domain on the same server, before visiting his own website to execute
var_dump($_SESSION). Armed with detailed knowledge of the victim's use of data in their sessions,
the attacker can then modify the session state (poisoning the session), alter the cookie path back to the
victim's website, and then make requests from the victim's website using the poisoned session. Even if two
applications on the same server do not have read/write access to the other application's save_path, if the
save_path is guessable, and the attacker has control over one of these two websites, the attacker could
alter their website's save_path to use the other's save_path, and thus accomplish session poisoning,
under some common configurations of PHP. Thus, the value for save_path should not be made public
knowledge and should be altered to a secure location unique to each application.
• string name - The correct value is system dependent and should be provided by the developer using a value unique
to the application.
Security Risk
If the php.ini setting for session.name is the same (e.g., the default "PHPSESSID"), and there are
two or more PHP applications accessible through the same domain name then they will share the same
session data for visitors to both websites. Additionally, possible corruption of session data may result.
• boolean use_only_cookies - In order to avoid introducing additional security risks, do not alter the default
value of this option.
Security Risk
If this setting is not enabled, an attacker can easily fix victim's session ids, using links on the attacker's
website, such as http://www.example.com/index.php?PHPSESSID=fixed_session_id.
The fixation works, if the victim does not already have a session id cookie for example.com. Once a victim
is using a known session id, the attacker can then attempt to hijack the session by pretending to be the
victim, and emulating the victim's user agent.
1153
Zend_Session
• Using output buffering [http://php.net/outcontrol] often is sufficient to prevent this issue, and may help improve
performance. For example, in php.ini, "output_buffering = 65535" enables output buffering with a 64K
buffer. Even though output buffering might be a good tactic on production servers to increase performance, relying
only on buffering to resolve the "headers already sent" problem is not sufficient. The application must not exceed the
buffer size, or the problem will occur whenever the output sent (prior to the HTTP headers) exceeds the buffer size.
• Alternatively, try rearranging the application logic so that actions manipulating headers are performed prior to
sending any output whatsoever.
• If a Zend_Session method is involved in causing the error message, examine the method carefully, and make sure
its use really is needed in the application. For example, the default usage of destroy() also sends an HTTP header
to expire the client-side session cookie. If this is not needed, then use destroy(false), since the instructions
to set cookies are sent with HTTP headers.
• Alternatively, try rearranging the application logic so that all actions manipulating headers are performed prior to
sending any output whatsoever.
• Remove any closing "?>" tags, if they occur at the end of a PHP source file. They are not needed, and newlines and
other nearly invisible whitespace following the closing tag can trigger output to the client.
Session Identifiers
Introduction: Best practice in relation to using sessions with Zend Framework calls for using a browser cookie (i.e. a
normal cookie stored in your web browser), instead of embedding a unique session identifier in URLs as a means to
track individual users. By default this component uses only cookies to maintain session identifiers. The cookie's value
is the unique identifier of your browser's session. PHP's ext/session uses this identifier to maintain a unique one-to-one
relationship between website visitors, and persistent session data storage unique to each visitor. Zend_Session*
wraps this storage mechanism ($_SESSION) with an object-oriented interface. Unfortunately, if an attacker gains
access to the value of the cookie (the session id), an attacker might be able to hijack a visitor's session. This problem is
not unique to PHP, or Zend Framework. The regenerateId() method allows an application to change the session
id (stored in the visitor's cookie) to a new, random, unpredictable value. Note: Although not the same, to make this
section easier to read, we use the terms "user agent" and "web browser" interchangeably.
Why?: If an attacker obtains a valid session identifier, an attacker might be able to impersonate a valid user (the
victim), and then obtain access to confidential information or otherwise manipulate the victim's data managed by your
application. Changing session ids helps protect against session hijacking. If the session id is changed, and an attacker
does not know the new value, the attacker can not use the new session id in their attempts to hijack the visitor's session.
Even if an attacker gains access to an old session id, regenerateId() also moves the session data from the old
session id "handle" to the new one, so no data remains accessible via the old session id.
1154
Zend_Session
a visitor re-authenticates their identity before editing their personal "profile"), or whenever a security "sensitive"
session parameter change occurs, consider using regenerateId() to create a new session id. If you call the
rememberMe() function, then don't use regenerateId(), since the former calls the latter. If a user has
successfully logged into your website, use rememberMe() instead of regenerateId().
By itself, calling Zend_Session::regenerateId() when the user's session is first used, does not prevent
session fixation attacks, unless you can distinguish between a session originated by an attacker emulating the victim.
At first, this might sound contradictory to the previous statement above, until we consider an attacker who first initiates
a real session on your website. The session is "first used" by the attacker, who then knows the result of the initialization
(regenerateId()). The attacker then uses the new session id in combination with an XSS vulnerability, or injects
the session id via a link on the attacker's website (works if use_only_cookies = off).
If you can distinguish between an attacker and victim using the same session id, then session hijacking can be dealt
with directly. However, such distinctions usually involve some form of usability tradeoffs, because the methods of
distinction are often imprecise. For example, if a request is received from an IP in a different country than the IP of
the request when the session was created, then the new request probably belongs to an attacker. Under the following
conditions, there might not be any way for a website application to distinguish between a victim and an attacker:
• attacker uses XSS vulnerability on your website to create a cookie on the victim's browser with the same, valid
session id (i.e. session fixation)
• both the victim and attacker originate from the same proxy farm (e.g. both are behind the same firewall at a large
company, like AOL)
The sample code below makes it much harder for an attacker to know the current victim's session id, unless the attacker
has already performed the first two steps above.
if (!isset($defaultNamespace->initialized)) {
Zend_Session::regenerateId();
$defaultNamespace->initialized = true;
}
1155
Zend_Session
rememberMe(integer $seconds)
Ordinarily, sessions end when the user agent terminates, such as when an end user exits a web browser program.
However, your application may provide the ability to extend user sessions beyond the lifetime of the client
program through the use of persistent cookies. Use Zend_Session::rememberMe() before a session is
started to control the length of time before a persisted session cookie expires. If you do not specify a number
of seconds, then the session cookie lifetime defaults to remember_me_seconds, which may be set using
Zend_Session::setOptions(). To help thwart session fixation/hijacking, use this function when a user
successfully authenticates with your application (e.g., from a "login" form).
forgetMe()
This function complements rememberMe() by writing a session cookie that has a lifetime ending when the user
agent terminates.
sessionExists()
Use this method to determine if a session already exists for the current user agent/request. It may be used before starting
a session, and independently of all other Zend_Session and Zend_Session_Namespace methods.
If you see the error message, "Cannot modify header information - headers already sent", then either avoid using
TRUE as the value for the first argument (requesting removal of the session cookie), or see the section called “Error:
Headers Already Sent”. Thus, Zend_Session::destroy(true) must either be called before PHP has sent
HTTP headers, or output buffering must be enabled. Also, the total output sent must not exceed the set buffer size, in
order to prevent triggering sending the output before the call to destroy().
Throws
By default, $readonly is enabled and further actions involving writing to the session data store will throw
an exception.
stop()
This method does absolutely nothing more than toggle a flag in Zend_Session to prevent further writing to
the session data store. We are specifically requesting feedback on this feature. Potential uses/abuses might include
temporarily disabling the use of Zend_Session_Namespace instances or Zend_Session methods to write to
the session data store, while execution is transferred to view- related code. Attempts to perform actions involving
writes via these instances or methods will throw an exception.
writeClose($readonly = true)
Shutdown the session, close writing and detach $_SESSION from the back-end storage mechanism. This will
complete the internal data transformation on this request. The optional $readonly boolean parameter can
1156
Zend_Session
remove write access by throwing an exception upon any attempt to write to the session via Zend_Session or
Zend_Session_Namespace.
Throws
By default, $readonly is enabled and further actions involving writing to the session data store will throw
an exception. However, some legacy application might expect $_SESSION to remain writable after ending
the session via session_write_close(). Although not considered "best practice", the $readonly
option is available for those who need it.
expireSessionCookie()
This method sends an expired session id cookie, causing the client to delete the session cookie. Sometimes this
technique is used to perform a client-side logout.
setSaveHandler(Zend_Session_SaveHandler_Interface
$interface)
Most developers will find the default save handler sufficient. This method provides an object-oriented wrapper for
session_set_save_handler() [http://php.net/session_set_save_handler].
namespaceIsset($namespace)
Use this method to determine if a session namespace exists, or if a particular index exists in a particular namespace.
Throws
An exception will be thrown if Zend_Session is not marked as readable (e.g., before Zend_Session
has been started).
namespaceUnset($namespace)
Use Zend_Session::namespaceUnset($namespace) to efficiently remove an entire namespace and its
contents. As with all arrays in PHP, if a variable containing an array is unset, and the array contains other objects,
those objects will remain available, if they were also stored by reference in other array/objects that remain accessible
via other variables. So namespaceUnset() does not perform a "deep" unsetting/deleting of the contents of the
entries in the namespace. For a more detailed explanation, please see References Explained [http://php.net/references]
in the PHP manual.
Throws
An exception will be thrown if the namespace is not writable (e.g., after destroy()).
namespaceGet($namespace)
DEPRECATED: Use getIterator() in Zend_Session_Namespace. This method returns an array of the
contents of $namespace. If you have logical reasons to keep this method publicly accessible, please provide feedback
to the fw-auth@lists.zend.com [mailto:fw-auth@lists.zend.com] mail list. Actually, all participation on any relevant
topic is welcome :)
1157
Zend_Session
Throws
An exception will be thrown if Zend_Session is not marked as readable (e.g., before Zend_Session
has been started).
getIterator()
Use getIterator() to obtain an array containing the names of all namespaces.
Throws
An exception will be thrown if Zend_Session is not marked as readable (e.g., before Zend_Session
has been started).
Zend_Session_SaveHandler_DbTable
The basic setup for Zend_Session_SaveHandler_DbTable must at least have four columns, denoted in the
config array or Zend_Config object: primary, which is the primary key and defaults to just the session id which by
default is a string of length 32; modified, which is the unix timestamp of the last modified date; lifetime, which is the
lifetime of the session (modified + lifetime > time();); and data, which is the serialized data stored in the session
1158
Zend_Session
You can also use Multiple Columns in your primary key for Zend_Session_SaveHandler_DbTable.
1159
1160
Capítulo 50. Zend_Soap
Zend_Soap_Server
Zend_Soap_Server class is intended to simplify Web Services server part development for PHP programmers.
It may be used in WSDL or non-WSDL mode, and using classes or functions to define Web Service API.
When Zend_Soap_Server component works in the WSDL mode, it uses already prepared WSDL document to
define server object behavior and transport layer options.
If the non-WSDL mode is used, then all protocol options have to be set using options mechanism.
Zend_Soap_Server constructor
Zend_Soap_Server constructor should be used a bit differently for WSDL and non-WSDL modes.
• 'classmap' ('classMap') which can be used to map some WSDL types to PHP classes.
The option must be an array with WSDL types as keys and names of PHP classes as values.
You also have to set 'uri' option in this case (see below).
The second constructor parameter ($options) is an array with options to create SOAP server object3.
3
Options may be set later using setOptions($options) method.
1161
Zend_Soap
• 'classmap' ('classMap') which can be used to map some WSDL types to PHP classes.
The option must be an array with WSDL types as keys and names of PHP classes as values.
The first one is to attach some class to the Zend_Soap_Server object which has to completely describe Web
Service API:
...
class MyClass {
/**
* This method takes ...
*
* @param integer $inputParam
* @return string
*/
public function method1($inputParam) {
...
}
/**
* This method takes ...
*
* @param integer $inputParam1
* @param string $inputParam2
* @return float
*/
public function method2($inputParam1, $inputParam2) {
...
}
...
}
...
$server = new Zend_Soap_Server(null, $options);
// Bind Class to Soap Server
$server->setClass('MyClass');
// Bind already initialized object to Soap Server
$server->setObject(new MyClass());
...
$server->handle();
1162
Zend_Soap
Important!
You should completely describe each method using method docblock if you plan to use autodiscover
functionality to prepare corresponding Web Service WSDL.
The second method of defining Web Service API is using set of functions and addFunction() or
loadFunctions() methods:
...
/**
* This function ...
*
* @param integer $inputParam
* @return string
*/
function function1($inputParam) {
...
}
/**
* This function ...
*
* @param integer $inputParam1
* @param string $inputParam2
* @return float
*/
function function2($inputParam1, $inputParam2) {
...
}
...
$server = new Zend_Soap_Server(null, $options);
$server->addFunction('function1');
$server->addFunction('function2');
...
$server->handle();
Zend_Soap_Server component performs request/response processing automatically, but allows to catch it and do
some pre- and post-processing.
Request processing
Zend_Soap_Server::handle() method takes request from the standard input stream ('php://input'). It may
be overridden either by supplying optional parameter to the handle() method or by setting request using
setRequest() method:
...
1163
Zend_Soap
Last processed request may be retrieved using getLastRequest() method as an XML string:
...
$server = new Zend_Soap_Server(...);
...
$server->handle();
$request = $server->getLastRequest();
Response pre-processing
Zend_Soap_Server::handle() method automatically emits generated response to the output stream. It may
be blocked using setReturnResponse() with TRUE or FALSE as a parameter4. Generated response is returned
by handle() method in this case.
...
$server = new Zend_Soap_Server(...);
...
// Get a response as a return value of handle() method
// instead of emitting it to the standard output
$server->setReturnResponse(true);
...
$response = $server->handle();
...
Last response may be also retrieved by getLastResponse() method for some post-processing:
...
$server = new Zend_Soap_Server(...);
...
$server->handle();
4
Current state of the Return Response flag may be requested with setReturnResponse() method.
1164
Zend_Soap
$response = $server->getLastResponse();
...
Zend_Soap_Client
The Zend_Soap_Client class simplifies SOAP client development for PHP programmers.
Under the WSDL mode, the Zend_Soap_Client component uses a WSDL document to define transport layer
options.
The WSDL description is usually provided by the web service the client will access. If the WSDL description is
not made available, you may want to use Zend_Soap_Client in non-WSDL mode. Under this mode, all SOAP
protocol options have to be set explicitly on the Zend_Soap_Client class.
Zend_Soap_Client Constructor
The Zend_Soap_Client constructor takes two parameters:
Both of these parameters may be set later using setWsdl($wsdl) and setOptions($options) methods
respectively.
Important!
If you use Zend_Soap_Client component in non-WSDL mode, you must set the 'location' and 'uri'
options.
• 'classmap' ('classMap') - can be used to map some WSDL types to PHP classes.
The option must be an array with WSDL types as keys and names of PHP classes as values.
Changing this option may switch Zend_Soap_Client object to or from WSDL mode.
• 'uri' - target namespace for the SOAP service (required for non-WSDL-mode, doesn't work for WSDL mode).
• 'location' - the URL to request (required for non-WSDL-mode, doesn't work for WSDL mode).
• 'style' - request style (doesn't work for WSDL mode): SOAP_RPC or SOAP_DOCUMENT.
• 'use' - method to encode messages (doesn't work for WSDL mode): SOAP_ENCODED or SOAP_LITERAL.
1165
Zend_Soap
• 'proxy_host', 'proxy_port', 'proxy_login', and 'proxy_password' - an HTTP connection through a proxy server.
Each web service method is mapped to the virtual Zend_Soap_Client object method which takes parameters with
common PHP types.
//****************************************************************
// Server code
//****************************************************************
// class MyClass {
// /**
// * This method takes ...
// *
// * @param integer $inputParam
// * @return string
// */
// public function method1($inputParam) {
// ...
// }
//
// /**
// * This method takes ...
// *
// * @param integer $inputParam1
// * @param string $inputParam2
// * @return float
// */
// public function method2($inputParam1, $inputParam2) {
// ...
1166
Zend_Soap
// }
//
// ...
// }
// ...
// $server = new Zend_Soap_Server(null, $options);
// $server->setClass('MyClass');
// ...
// $server->handle();
//
//****************************************************************
// End of server code
//****************************************************************
// $result1 is a string
$result1 = $client->method1(10);
...
// $result2 is a float
$result2 = $client->method2(22, 'some string');
WSDL Accessor
Nota
Zend_Soap_Wsdl class is used by Zend_Soap_Server component internally to operate with WSDL
documents. Nevertheless, you could also use functionality provided by this class for your own needs. The
Zend_Soap_Wsdl package contains both a parser and a builder of WSDL documents.
If you don't plan to do this, you can skip this documentation section.
Zend_Soap_Wsdl constructor
Zend_Soap_Wsdl constructor takes three parameters:
2. $uri - URI where the WSDL will be available (could also be a reference to the file in the filesystem.)
3. $strategy - optional flag used to identify the strategy for complex types (objects) detection. This was a boolean
$extractComplexTypes before version 1.7 and can still be set as a boolean for backwards compatibility. By
default the 1.6 detection behaviour is set. To read more on complex type detection strategies go to the section: the
section called “Adding complex type information”.
addMessage() method
addMessage($name, $parts) method adds new message description to the WSDL document (/definitions/
message element).
1167
Zend_Soap
$parts parameter is an array of message parts which describe SOAP call parameters. It's an associative array: 'part
name' (SOAP call parameter name) => 'part type'.
Type mapping management is performed using addTypes(), addTypes() and addComplexType() methods
(see below).
Nota
Messages parts can use either 'element' or 'type' attribute for typing (see http://www.w3.org/TR/
wsdl#_messages).
'element' attribute must refer to a corresponding element of data type definition. 'type' attribute refers to a
corresponding complexType entry.
All standard XSD types have both 'element' and 'complexType' definitions (see http://schemas.xmlsoap.org/
soap/encoding/).
addPortType() method
addPortType($name) method adds new port type to the WSDL document (/definitions/portType) with the
specified port type name.
addPortOperation() method
addPortOperation($portType, $name, $input = false, $output = false, $fault =
false) method adds new port operation to the specified port type of the WSDL document (/definitions/portType/
operation).
Each port operation corresponds to a class method (if Web Service is based on a class) or function (if Web Service is
based on a set of methods) in terms of Zend_Soap_Server implementation.
It also adds corresponding port operation messages depending on specified $input, $output and $fault
parameters.
Nota
Zend_Soap_Server component generates two messages for each port operation while describing service
based on Zend_Soap_Server class:
1168
Zend_Soap
addBinding() method
addBinding($name, $portType) method adds new binding to the WSDL document (/definitions/binding).
'binding' WSDL document node defines message format and protocol details for operations and messages defined by
a particular portType (see http://www.w3.org/TR/wsdl#_bindings).
The method creates binding node and returns it. Then it may be used to fill with actual data.
Zend_Soap_Server implementation uses $serviceName . 'Binding' name for 'binding' element of WSDL
document.
addBindingOperation() method
addBindingOperation($binding, $name, $input = false, $output = false, $fault =
false) method adds an operation to a binding element (/definitions/binding/operation) with the specified name.
It takes XML_Tree_Node object returned by addBinding() as an input ($binding parameter) to add 'operation'
element with input/output/false entries depending on specified parameters
Zend_Soap_Server implementation adds corresponding binding entry for each Web Service method with
input and output entries defining 'soap:body' element as '<soap:body use="encoded" encodingStyle="http://
schemas.xmlsoap.org/soap/encoding/"/>
addSoapBinding() method
addSoapBinding($binding, $style = 'document', $transport = 'http://
schemas.xmlsoap.org/soap/http') method adds SOAP binding ('soap:binding') entry to the binding
element (which is already linked to some port type) with the specified style and transport (Zend_Soap_Server
implementation uses RPC style over HTTP).
'/definitions/binding/soap:binding' element is used to signify that the binding is bound to the SOAP protocol format.
addSoapOperation() method
addSoapOperation($binding, $soap_action) method adds SOAP operation ('soap:operation') entry
to the binding element with the specified action. 'style' attribute of the 'soap:operation' element is not used since
programming model (RPC-oriented or document-oriented) may be using addSoapBinding() method
'soapAction' attribute of '/definitions/binding/soap:operation' element specifies the value of the SOAPAction header
for this operation. This attribute is required for SOAP over HTTP and must not be specified for other transports.
addService() method
addService($name, $port_name, $binding, $location) method adds '/definitions/service' element
to the WSDL document with the specified Wed Service name, port name, binding, and location.
1169
Zend_Soap
WSDL 1.1 allows to have several port types (sets of operations) per service. This ability is not used by
Zend_Soap_Server implementation and not supported by Zend_Soap_Wsdl class.
• script URI6 as a service URI for Web Service definition using classes.
where $name is a class name for the Web Service definition mode using class and script name for the Web Service
definition mode using set of functions.
Type mapping
Zend_Soap WSDL accessor implementation uses the following type mapping between PHP and SOAP types:
• PHP class <-> based on complex type strategy (See: the section called “Adding complex type information”) 7.
• If type is not matched to any of these types by some reason, then xsd:anyType is used.
...
$wsdl = new Zend_Soap_Wsdl('My_Web_Service', $myWebServiceUri);
...
$soapIntType = $wsdl->getType('int');
...
class MyClass {
...
1170
Zend_Soap
}
...
$soapMyClassType = $wsdl->getType('MyClass');
It's automatically used by getType() method to add corresponding complex types of method parameters or return
types.
Its detection and building algorithm is based on the currently active detection strategy for complex types.
You can set the detection strategy either by specifying the class name as string or instance of a
Zend_Soap_Wsdl_Strategy_Interface implementation as the third parameter of the constructor or using the
setComplexTypeStrategy($strategy) function of Zend_Soap_Wsdl. The following detection strategies
currently exist:
• Class Zend_Soap_Wsdl_Strategy_AnyType: Casts all complex types into the simple XSD type
xsd:anyType. Be careful this shortcut for complex type detection can probably only be handled successfully by
weakly typed languages such as PHP.
Class property MUST have docblock section with the described PHP type to have property included into WSDL
description.
addComplexType() checks if type is already described within types section of the WSDL document.
It prevents duplications if this method is called two or more times and recursion in the types definition section.
addDocumentation() method
addDocumentation($input_node, $documentation) method adds human readable documentation
using optional 'wsdl:document' element.
1171
Zend_Soap
'/definitions/binding/soap:binding' element is used to signify that the binding is bound to the SOAP protocol format.
AutoDiscovery
AutoDiscovery Introduction
SOAP functionality implemented within Zend Framework is intended to make all steps required for SOAP
communications more simple.
SOAP is language independent protocol. So it may be used not only for PHP-to-PHP communications.
There are three configurations for SOAP applications where Zend Framework may be utilized:
We always have to know, which functionality is provided by SOAP server to operate with it. WSDL [http://
www.w3.org/TR/wsdl] is used to describe network service API in details.
WSDL language is complex enough (see http://www.w3.org/TR/wsdl for the details). So it's difficult to prepare correct
WSDL description.
Another problem is synchronizing changes in network service API with already existing WSDL.
Both these problem may be solved by WSDL autogeneration. A prerequisite for this is a SOAP server autodiscovery.
It constructs object similar to object used in SOAP server application, extracts necessary information and generates
correct WSDL using this information.
There are two ways for using Zend Framework for SOAP server application:
TheZend_Soap_AutoDiscover class also supports datatypes mapping from PHP to XSD types [http://
www.w3.org/TR/xmlschema-2/].
Here is an example of common usage of the autodiscovery functionality. The handle() function generates the
WSDL file and posts it to the browser.
class My_SoapServer_Class {
...
}
1172
Zend_Soap
If you need access to the generated WSDL file either to save it to a file or as an XML string you can use the
dump($filename) or toXml() functions the AutoDiscover class provides.
if(isset($_GET['wsdl'])) {
$autodiscover = new Zend_Soap_AutoDiscover();
$autodiscover->setClass('HelloWorldService');
$autodiscover->handle();
} else {
// pointing to the current file here
$soap = new Zend_Soap_Server("http://example.com/soap.php?wsdl");
$soap->setClass('HelloWorldService');
$soap->handle();
}
Class autodiscovering
If class is used to provide SOAP server functionality, then the same class should be provided to
Zend_Soap_AutoDiscover for WSDL generation:
It's also used as a target namespace for all service related names (including described complex types).
1173
Zend_Soap
Method may have several prototypes if some method parameters are optional.
Important!
WSDL autodiscovery utilizes the PHP docblocks provided by the developer to determine the parameter and
return types. In fact, for scalar types, this is the only way to determine the parameter types, and for return
types, this is the only way to determine them.
That means, providing correct and fully detailed docblocks is not only best practice, but is required for
discovered class.
Functions autodiscovering
If set of functions are used to provide SOAP server functionality, then the same set should be provided to
Zend_Soap_AutoDiscovery for WSDL generation:
• Current script name is used as a name of the Web Service being described.
It's also used as a target namespace for all service related names (including described complex types).
Function may have several prototypes if some method parameters are optional.
Important!
WSDL autodiscovery utilizes the PHP docblocks provided by the developer to determine the parameter and
return types. In fact, for scalar types, this is the only way to determine the parameter types, and for return
types, this is the only way to determine them.
That means, providing correct and fully detailed docblocks is not only best practice, but is required for
discovered class.
1174
Zend_Soap
Autodiscovering Datatypes
Input/output datatypes are converted into network service types using the following mapping:
• PHP class <-> based on complex type strategy (See: the section called “Adding complex type information”) 8.
• If type is not matched to any of these types by some reason, then xsd:anyType is used.
1175
1176
Capítulo 51. Zend_Tag
Introduction
Zend_Tag is a component suite which provides a facility to work with taggable Items. As its base, it provides
two classes to work with Tags, Zend_Tag_Item and Zend_Tag_ItemList. Additionally, it comes with the
interface Zend_Tag_Taggable, which allows you to use any of your models as a taggable item in conjunction
with Zend_Tag.
Zend_Tag_Item is a basic taggable item implementation which comes with the essential functionality required
to work with the Zend_Tag suite. A taggable item always consists of a title and a relative weight (e.g. number of
occurrences). It also stores parameters which are used by the different sub-components of Zend_Tag.
To group multiple items together, Zend_Tag_ItemList exists as an array iterator and provides additional
functionality to calculate absolute weight values based on the given relative weights of each item in it.
// Assign tags to it
$list[] = new Zend_Tag_Item(array('title' => 'Code', 'weight' => 50));
$list[] = new Zend_Tag_Item(array('title' => 'Zend Framework', 'weight' => 1));
$list[] = new Zend_Tag_Item(array('title' => 'PHP', 'weight' => 5));
This will output the three items Code, Zend Framework and PHP with the absolute values 10, 1 and 2.
Zend_Tag_Cloud
Zend_Tag_Cloud is the rendering part of Zend_Tag. By default it comes with a set of HTML decorators, which
allow you to create tag clouds for a website, but also supplies you with two abstract classes to create your own
decorators, to create tag clouds in PDF documents for example.
You can instantiate and configure Zend_Tag_Cloud either programatically or completely via an array or an instance
of Zend_Config. The available options are:
• cloudDecorator: defines the decorator for the cloud. Can either be the name of the class which should be loaded
by the pluginloader, an instance of Zend_Tag_Cloud_Decorator_Cloud or an array containing the string
decorator and optionally an array options, which will be passed to the decorators constructor.
1177
Zend_Tag
• tagDecorator: defines the decorator for individual tags. This can either be the name of the class which should
be loaded by the pluginloader, an instance of Zend_Tag_Cloud_Decorator_Tag or an array containing the
string decorator and optionally an array options, which will be passed to the decorators constructor.
• prefixPath: prefix paths to add to the plugin loader. Must be an array containing the keys prefix and path or
multiple arrays containing the keys prefix and path. Invalid elements will be skipped.
• tags: a list of tags to assign to the cloud. Each tag must either implement Zend_Tag_Taggable or be an array
which can be used to instantiate Zend_Tag_Item.
This will output the tag cloud with the three tags, spread with the default font-sizes.
Decorators
Zend_Tag_Cloud requires two types of decorators to be able to render a tag cloud. This includes a decorator which
renders the single tags as well as a decorator which renders the surounding cloud. Zend_Tag_Cloud ships a default
decorator set for formatting a tag cloud in HTML. This set will by default create a tag cloud as ul/li-list, spread with
different font-sizes according to the weight values of the tags assigned to them.
URL parameter
As the HTML tag decorator always surounds the tag title with an anchor, you should define an URL parameter
for every tag used in it.
1178
Zend_Tag
The tag decorator can either spread different font-sizes over the anchors or a defined list of classnames. When
setting options for one of those possibilities, the corespondening one will automatically be enabled. The following
configuration options are available:
• fontSizeUnit: defines the font-size unit used for all font-sizes. The possible values are: em, ex, px, in, cm,
mm, pt, pc and %.
• minFontSize: the minimum font-size distributed through the tags (must be an integer).
• maxFontSize: the maximum font-size distributed through the tags (must be an integer).
• htmlTags: an array of HTML tags surounding the anchor. Each element can either be a string, which is used as
element type then, or an array containing an attribute list for the element, defined as key/value pair. In this case,
the array key is used as element type.
• htmlTags: an array of HTML tags surounding all tags. Each element can either be a string, which is used as
element type then, or an array containing an attribute list for the element, defined as key/value pair. In this case,
the array key is used as element type.
1179
1180
Capítulo 52. Zend_Test
Introducción
Zend_Test proporciona herramientas para facilitar las pruebas unitarias (unit testing) de sus aplicaciones basadas en
Zend Framework. Actualmente, ofrecemos facilidades para probar sus aplicaciones MVC basadas en Zend Framework
Zend_Test_PHPUnit
Zend_Test_PHPUnit provides a TestCase for MVC applications that contains assertions for testing against a
variety of responsibilities. Probably the easiest way to understand what it can do is to see an example.
• When a user logs in, they should be redirected to their profile page, and that profile page should show relevant
information.
This particular example assumes a few things. First, we're moving most of our bootstrapping to a plugin. This simplifies
setup of the test case as it allows us to specify our environment succinctly, and also allows us to bootstrap the application
in a single line. Also, our particular example is assuming that autoloading is setup so we do not need to worry about
requiring the appropriate classes (such as the correct controller, plugin, etc).
1181
Zend_Test
$this->assertQueryCount('form#loginForm', 1);
}
$this->resetRequest()
->resetResponse();
$this->request->setMethod('GET')
->setPost(array());
$this->dispatch('/user/view');
$this->assertRoute('default');
$this->assertModule('default');
$this->assertController('user');
$this->assertAction('view');
$this->assertNotRedirect();
$this->assertQuery('dl');
$this->assertQueryContentContains('h2', 'User: foobar');
}
}
This example could be written somewhat simpler -- not all the assertions shown are necessary, and are provided for
illustration purposes only. Hopefully, it shows how simple it can be to test your applications.
In order to test your MVC application, you will need to bootstrap it. There are several ways to do this, all of which
hinge on the public $bootstrap property.
First, and probably most straight-forward, simply create a Zend_Application instance as you would in your
index.php, and assign it to the $bootstrap property. Typically, you will do this in your setUp() method; you
will need to call parent::setUp() when done:
1182
Zend_Test
APPLICATION_PATH . '/configs/application.ini'
);
parent::setUp();
}
}
Second, you can set this property to point to a file. If you do this, the file should not dispatch the front controller, but
merely setup the front controller and any application specific needs.
// ...
}
Third, you can provide a PHP callback to execute in order to bootstrap your application. This method is seen in the
Login example. If the callback is a function or static method, this could be set at the class level:
// ...
}
In cases where an object instance is necessary, we recommend performing this in your setUp() method:
During normal operation, the setUp() method will bootstrap the application. This process first will include cleaning
up the environment to a clean request state, resetting any plugins and helpers, resetting the front controller instance,
and creating new request and response objects. Once this is done, it will then either include the file specified in
$bootstrap, or call the callback specified.
Bootstrapping should be as close as possible to how the application will be bootstrapped. However, there are several
caveats:
1183
Zend_Test
• Do not expect to test server specifics. In other words, the tests are not a guarantee that the code will run on a specific
server configuration, but merely that the application should run as expected should the router be able to route the
given request. To this end, do not set server-specific headers in the request object.
Once the application is bootstrapped, you can then start creating your tests.
First, you will need to dispatch a URL to test, using the dispatch() method of the TestCase:
There will be times, however, that you need to provide extra information -- GET and POST variables, COOKIE
information, etc. You can populate the request with that information:
1184
Zend_Test
// Dispatch:
$this->dispatch('/foo/bar');
// ...
}
}
Now that the request is made, it's time to start making assertions against it.
Due to its nature the redirector action helper plugin issues a redirect and exists after this. Because you cannot test parts
of an application that issue exit calls Zend_Test_PHPUnit_ControllerTestCase automatically disables the
exit part of the redirector which can cause different behaviours in tests and the real application. To make sure redirect
work correctly you should it them in the following way:
Important
Depending on your application this is not enough as additional action, preDispatch() or
postDispatch() logic might be executed. This cannot be handled in a good way with Zend Test currently.
1185
Zend_Test
Assertions
Assertions are at the heart of Unit Testing; you use them to verify that the results are what you expect. To this end,
Zend_Test_PHPUnit_ControllerTestCase provides a number of assertions to make testing your MVC apps
and controllers simpler.
This functionality is provided via Zend_Dom_Query, and integrated into a set of 'Query' assertions. Each of these
assertions takes as their first argument a CSS selector, with optionally additional arguments and/or an error message,
based on the assertion type. You can find the rules for writing the CSS selectors in the Zend_Dom_Query theory of
operation chapter. Query assertions include:
• assertQuery($path, $message = ''): assert that one or more DOM elements matching the given CSS
selector are present. If a $message is present, it will be prepended to any failed assertion message.
• assertQueryCount($path, $count, $message = ''): assert that there are exactly $count DOM
elements matching the given CSS selector present. If a $message is present, it will be prepended to any failed
assertion message.
• assertQueryCountMin($path, $count, $message = ''): assert that there are at least $count
DOM elements matching the given CSS selector present. If a $message is present, it will be prepended to any
failed assertion message. Note: specifying a value of 1 for $count is the same as simply using assertQuery().
• assertQueryCountMax($path, $count, $message = ''): assert that there are no more than $count
DOM elements matching the given CSS selector present. If a $message is present, it will be prepended to any
failed assertion message. Note: specifying a value of 1 for $count is the same as simply using assertQuery().
Additionally, each of the above has a 'Not' variant that provides a negative assertion:
assertNotQuery(), assertNotQueryContentContains(), assertNotQueryContentRegex(),
and assertNotQueryCount(). (Note that the min and max counts do not have these variants, for what should
be obvious reasons.)
XPath Assertions
Some developers are more familiar with XPath than with CSS selectors, and thus XPath variants of all the Query
assertions are also provided. These are:
1186
Zend_Test
Redirect Assertions
Often an action will redirect. Instead of following the redirect, Zend_Test_PHPUnit_ControllerTestCase
allows you to test for redirects with a handful of assertions.
• assertRedirectTo($url, $message = ''): assert that a redirect has occurred, and that the value of
the Location header is the $url provided.
• assertNotRedirectTo($url, $message = ''): assert that a redirect has either NOT occurred, or that
the value of the Location header is NOT the $url provided.
• assertRedirectRegex($pattern, $message = ''): assert that a redirect has occurred, and that the
value of the Location header matches the regular expression provided by $pattern.
• assertResponseCode($code, $message = ''): assert that the response resulted in the given HTTP
response code.
• assertHeader($header, $message = ''): assert that the response contains the given header.
• assertHeaderContains($header, $match, $message = ''): assert that the response contains the
given header and that its content contains the given string.
• assertHeaderRegex($header, $pattern, $message = ''): assert that the response contains the
given header and that its content matches the given regex.
Additionally, each of the above assertions have a 'Not' variant for negative assertions.
1187
Zend_Test
Request Assertions
It's often useful to assert against the last run action, controller, and module; additionally, you may want to assert against
the route that was matched. The following assertions can help you in this regard:
• assertModule($module, $message = ''): Assert that the given module was used in the last dispatched
action.
• assertController($controller, $message = ''): Assert that the given controller was selected in
the last dispatched action.
• assertAction($action, $message = ''): Assert that the given action was last dispatched.
• assertRoute($route, $message = ''): Assert that the given named route was matched by the router.
Ejemplos
Knowing how to setup your testing infrastructure and how to make assertions is only half the battle; now it's time to
start looking at some actual testing scenarios to see how you can leverage them.
• If a user is not authenticated, they will always be redirected to the login page of the controller, regardless of the
action specified.
• The login form page will show both the login form and the registration form.
• Authenticated users who visit the login page should be redirected to their profile page.
We could, and should define further tests, but these will do for now.
For our application, we will define a plugin, 'Initialize', that runs at routeStartup(). This allows us to encapsulate
our bootstrap in an OOP interface, which also provides an easy way to provide a callback. Let's look at the basics
of this class first:
1188
Zend_Test
/**
* @var string Current environment
*/
protected $_env;
/**
* @var Zend_Controller_Front
*/
protected $_front;
/**
* @var string Path to application root
*/
protected $_root;
/**
* Constructor
*
* Initialize environment, root path, and configuration.
*
* @param string $env
* @param string|null $root
* @return void
*/
public function __construct($env, $root = null)
{
$this->_setEnv($env);
if (null === $root) {
$root = realpath(dirname(__FILE__) . '/../../../');
}
$this->_root = $root;
$this->initPhpConfig();
$this->_front = Zend_Controller_Front::getInstance();
}
/**
* Route startup
*
* @return void
*/
public function routeStartup(Zend_Controller_Request_Abstract $request)
{
$this->initDb();
$this->initHelpers();
$this->initView();
$this->initPlugins();
$this->initRoutes();
$this->initControllers();
}
1189
Zend_Test
// ...
}
Once we have that in place, we can write our tests. However, what about those tests that require a user is logged in?
The easy solution is to use our application logic to do so... and fudge a little by using the resetRequest() and
resetResponse() methods, which will allow us to dispatch another request.
$this->resetRequest()
->resetResponse();
$this->request->setPost(array());
// ...
}
// ...
}
1190
Zend_Test
public function
testAuthenticatedUsersShouldBeRedirectedToProfileWhenVisitingLogin()
{
$this->loginUser('foobar', 'foobar');
$this->request->setMethod('GET');
$this->dispatch('/user');
$this->assertRedirectTo('/user/view');
}
1191
Zend_Test
{
$this->loginUser('foobar', 'foobar');
$this->request->setMethod('GET');
$this->dispatch('/user/logout');
$this->assertRedirectTo('/user');
}
Notice that these are terse, and, for the most part, don't look for actual content. Instead, they look for artifacts within
the response -- response codes and headers, and DOM nodes. This allows you to verify that the structure is as expected
-- preventing your tests from choking every time new content is added to the site.
Also notice that we use the structure of the document in our tests. For instance, in the final test, we look for a form
that has a node with the class of "errors"; this allows us to test merely for the presence of form validation errors, and
not worry about what specific errors might have been thrown.
This application may utilize a database. If so, you will probably need some scaffolding to ensure that the database is in a
pristine, testable configuration at the beginning of each test. PHPUnit already provides functionality for doing so; read
about it in the PHPUnit documentation [http://www.phpunit.de/pocket_guide/3.3/en/database.html]. We recommend
using a separate database for testing versus production, and in particular recommend using either a SQLite file or in-
memory database, as both options perform very well, do not require a separate server, and can utilize most SQL syntax.
Zend_Test_PHPUnit_Db
Coupling of data-access and the domain model often requires the use of a database for testing purposes. But the database
is persistent across different tests which leads to test results that can affect each other. Furthermore setting up the
database to be able to run a test is quite some work. PHPUnit's Database extension simplifies testing with a database
by offering a very simple mechanism to set up and teardown the database between different tests. This component
extends the PHPUnit Database extension with Zend Framework specific code, such that writing database tests against
a Zend Framework application is simplified.
Database Testing can be explained with two conceptual entities, DataSets and DataTables. Internally the PHPUnit
Database extension can build up an object structure of a database, its tables and containing rows from configuration
files or the real database content. This abstract object graph can then be compared using assertions. A common
use-case in database testing is setting up some tables with seed data, then performing some operations, and finally
asserting that the operated on database-state is equal to some predefined expected state. Zend_Test_PHPUnit_Db
simplifies this task by allowing to generate DataSets and DataTables from existing Zend_Db_Table_Abstract
or Zend_Db_Table_Rowset_Abstract instances.
1192
Zend_Test
Furthermore this component allows to integrate any Zend_Db_Adapter_Abstract for testing whereas the
original extension only works with PDO. A Test Adapter implementation for Zend_Db_Adapter_Abstract is
also included in this component. It allows to instantiate a Db Adapter that requires no database at all and acts as an
SQL and result stack which is used by the API methods.
Quickstart
Setup a Database TestCase
We are now writting some database tests for the Bug Database example in the Zend_Db_Table documentation.
First we begin to test that inserting a new bug is actually saved in the database correctly. First we have to setup a
test-class that extends Zend_Test_PHPUnit_DatabaseTestCase. This class extends the PHPUnit Database
Extension, which in turn extends the basic PHPUnit_Framework_TestCase. A database testcase contains two
abstract methods that have to be implemented, one for the database connection and one for the initial dataset that
should be used as seed or fixture.
Nota
You should be familiar with the PHPUnit Database extension to follow this quickstart easily. Although all
the concepts are explained in this documentation it may be helpful to read the PHPUnit documentation first.
/**
* Returns the test database connection.
*
* @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
*/
protected function getConnection()
{
if($this->_connectionMock == null) {
$connection = Zend_Db::factory(...);
$this->_connectionMock = $this->createZendDbConnection(
$connection, 'zfunittests'
);
Zend_Db_Table_Abstract::setDefaultAdapter($connection);
}
return $this->_connectionMock;
}
/**
* @return PHPUnit_Extensions_Database_DataSet_IDataSet
*/
protected function getDataSet()
{
return $this->createFlatXmlDataSet(
dirname(__FILE__) . '/_files/bugsSeed.xml'
);
}
}
1193
Zend_Test
Here we create the database connection and seed some data into the database. Some important details should be noted
on this code:
• You cannot directly return a Zend_Db_Adapter_Abstract from the getConnection() method, but a
PHPUnit specific wrapper which is generated with the createZendDbConnection() method.
• The database schema (tables and database) is not re-created on every testrun. The database and tables have to be
created manually before running the tests.
• Database tests by default truncate the data during setUp() and then insert the seed data which is returned from
the getDataSet() method.
We will work with this four entries in the database table "zfbugs" in the next examples. The required MySQL schema
for this example is:
1194
Zend_Test
$data = array(
'created_on' => '2007-03-22 00:00:00',
'updated_on' => '2007-03-22 00:00:00',
'bug_description' => 'Something wrong',
'bug_status' => 'NEW',
'reported_by' => 'garfield',
'verified_by' => 'garfield',
'assigned_to' => 'mmouse',
);
$bugsTable->insert($data);
$this->assertDataSetsEqual(
$this->createFlatXmlDataSet(dirname(__FILE__)
. "/_files/bugsInsertIntoAssertion.xml"),
$ds
);
}
}
Now up to the $bugsTable->insert($data); everything looks familiar. The lines after that contain the
assertion methodname. We want to verify that after inserting the new bug the database has been updated correctly
with the given data. For this we create a Zend_Test_PHPUnit_Db_DataSet_QueryDataSet instance and
give it a database connection. We will then tell this dataset that it contains a table "zfbugs" which is given by an SQL
statement. This current/actual state of the database is compared to the expected database state which is contained in
another XML file "bugsInsertIntoAssertions.xml". This XML file is a slight deviation from the one given above and
contains another row with the expected data:
1195
Zend_Test
There are other ways to assert that the current database state equals an expected state. The "Bugs" table in the example
already nows a lot about its inner state, so why not use this to our advantage? The next example will assert that deleting
from the database is possible:
$bugsTable->delete(
$bugsTable->getAdapter()->quoteInto("bug_id = ?", 4)
);
$this->assertDataSetsEqual(
$this->createFlatXmlDataSet(dirname(__FILE__)
. "/_files/bugsDeleteAssertion.xml"),
$ds
);
}
}
Here we only have one table and check against an expected database state in "bugsDeleteAssertion.xml" which is the
original seed dataset without the row with id 4.
Since we have only checked that two specific tables (not datasets) are equal in the previous examples we should also
look at how to assert that two tables are equal. Therefore we will add another test to our TestCase which verifies
updating behaviour of a dataset.
$data = array(
'updated_on' => '2007-05-23',
'bug_status' => 'FIXED'
);
$bugsTable->update($data, $where);
$rowset = $bugsTable->fetchAll();
1196
Zend_Test
$this->assertTablesEqual(
$expectedRowsets, $ds
);
}
}
Here we create the current database state from a Zend_Db_Table_Rowset_Abstract instance in conjunction
with the Zend_Test_PHPUnit_Db_DataSet_DbRowset($rowset) instance which creates an internal
data-representation of the rowset. This can again be compared against another data-table by using the $this-
>assertTablesEqual() assertion.
It should be noted that testing domain and business logic with integration tests such as Zend Framework's
Controller and Database TestCases is a bad practice. The purpose of an Integration test is to check that several
parts of an application work smoothly when wired together. These integration tests do not replace the need
for a set of unit tests that test the domain and business logic at a much smaller level, the isolated class.
1. For each test PHPUnit creates a new instance of the TestCase and calls the setUp() method.
2. The Database TestCase creates an instance of a Database Tester which handles the setting up and tearing down
of the database.
3. The database tester collects the information on the database connection and initial dataset from
getConnection() and getDataSet() which are both abstract methods and have to be implemented by any
Database Testcase.
4. By default the database tester truncates the tables specified in the given dataset, and then inserts the data given
as initial fixture.
1197
Zend_Test
5. When the database tester has finished setting up the database, PHPUnit runs the test.
6. After running the test, tearDown() is called. Because the database is wiped in setUp() before inserting the
required initial fixture, no actions are executed by the database tester at this stage.
Nota
The Database TestCase expects the database schema and tables to be setup correctly to run the tests. There
is no mechanism to create and tear down database tables.
The Zend_Test_PHPUnit_DatabaseTestCase class has some convenience functions that can help writing
tests that interact with the database and the database testing extension.
The next table lists only the new methods compared to the PHPUnit_Extensions_Database_TestCase,
whose API is documented in the PHPUnit Documentation [http://www.phpunit.de/manual/current/en/database.html].
1198
Zend_Test
$databaseFixture =
new PHPUnit_Extensions_Database_DataSet_FlatXmlDataSet(
dirname(__FILE__) . '/_files/initialUserFixture.xml'
);
$databaseTester->setupDatabase($databaseFixture);
}
}
Now the Flat XML dataset "initialUserFixture.xml" is used to set the database into an initial state before each test,
exactly as the DatabaseTestCase works internally.
The Test Adapter acts as a stack for various database results. Its order of results have to be userland implemented,
which might be a tedious task for tests that call many different database queries, but its just the right helper for tests
where only a handful of queries are executed and you know the exact order of the results that have to be returned to
your userland code.
1199
Zend_Test
}
$rs = $adapter->query('SELECT ...'); // Returns Statement 1
Behaviour of any real database adapter is simulated as much as possible such that methods like fetchAll(),
fetchObject(), fetchColumn and more are working for the test adapter.
You can also put INSERT, UPDATE and DELETE statement onto the result stack, these however only return a
statement which allows to specifiy the result of $stmt->rowCount().
By default the query profiler is enabled, so that you can retrieve the executed SQL statements and their bound
parameters to check for the correctness of the execution.
$qp = $adapter->getProfiler()->getLastQueryProfile();
The test adapter never checks if the query specified is really of the type SELECT, DELETE, INSERT or UPDATE
which is returned next from the stack. The correct order of returning the data has to be implemented by the user of
the test adapter.
The Test adapter also specifies methods to simulate the use of the methods listTables(), describeTables()
and lastInsertId().
1200
Capítulo 53. Zend_Text
Zend_Text_Figlet
Zend_Text_Figlet is a component which enables developers to create a so called FIGlet text. A FIGlet text is a
string, which is represented as ASCII art. FIGlets use a special font format, called FLT (FigLet Font). By default, one
standard font is shipped with Zend_Text_Figlet, but you can download additional fonts at http://www.figlet.org
[http://www.figlet.org/fontdb.cgi].
Compressed fonts
Zend_Text_Figlet supports gzipped fonts. This means that you can take an .flf file and gzip it. To
allow Zend_Text_Figlet to recognize this, the gzipped font must have the extension .gz. Further, to
be able to use gzipped fonts, you have to have enabled the GZIP extension of PHP.
Encoding
Zend_Text_Figlet expects your strings to be UTF-8 encoded by default. If this is not the case, you can
supply the character encoding as second parameter to the render() method.
You can define multiple options for a FIGlet. When instantiating Zend_Text_Figlet, you can supply an array
or an instance of Zend_Config.
• font - Defines the font which should be used for rendering. If not defines, the built-in font will be used.
• outputWidth - Defines the maximum width of the output string. This is used for word-wrap as well as
justification. Beware of too small values, they may result in an undefined behaviour. The default value is 80.
• handleParagraphs - A boolean which indicates, how new lines are handled. When set to true, single new lines
are ignored and instead treated as single spaces. Only multiple new lines will be handled as such. The default value
is FALSE.
• smushMode - An integer bitfield which defines, how the single characters are smushed together. Can be the sum
of multiple values from Zend_Text_Figlet::SM_*. There are the following smush modes: SM_EQUAL,
SM_LOWLINE, SM_HIERARCHY, SM_PAIR, SM_BIGX, SM_HARDBLANK, SM_KERN and SM_SMUSH.
A value of 0 doesn't disable the entire smushing, but forces SM_KERN to be applied, while a value of -1 disables it.
An explanation of the different smush modes can be found here [http://www.jave.de/figlet/figfont.txt]. By default
the setting of the font file is used. The smush mode option is normally used only by font designers testing the various
layoutmodes with a new font.
1201
Zend_Text
Assuming you are using a monospace font, this would look as follows:
Zend_Text_Table
Zend_Text_Table is a component to create text based tables on the fly with different decorators. This can be
helpful, if you either want to send structured data in text emails, which are used to have mono-spaced fonts, or to display
table information in a CLI application. Zend_Text_Table supports multi-line columns, colspan and align as well.
Encoding
Zend_Text_Table expects your strings to be UTF-8 encoded by default. If this is not
the case, you can either supply the character encoding as a parameter to the constructor
or the setContent method of Zend_Text_Table_Column. Alternatively if you have a
different encoding in the entire process, you can define the standard input charset with
Zend_Text_Table::setInputCharset($charset). In case you need another output charset for
the table, you can set this with Zend_Text_Table::setOutputCharset($charset).
• columnWidths (required): An array defining all columns width their widths in characters.
• decorator: The decorator to use for the table borders. The default is unicode, but you may also specify ascii
or give an instance of a custom decorator object.
• padding: The left and right padding withing the columns in characters. The default padding is zero.
• AutoSeparate: The way how the rows are separated with horizontal lines. The default is a separation between
all rows. This is defined as a bitmask containing one ore more of the following constants of Zend_Text_Table:
• Zend_Text_Table::AUTO_SEPARATE_NONE
• Zend_Text_Table::AUTO_SEPARATE_HEADER
• Zend_Text_Table::AUTO_SEPARATE_FOOTER
• Zend_Text_Table::AUTO_SEPARATE_ALL
Where header is always the first row, and the footer is always the last row.
Rows are simply added to the table by creating a new instance of Zend_Text_Table_Row, and appending it to the
table via the appendRow method. Rows themselves have no options. You can also give an array to directly to the
appendRow method, which then will automatically converted to a row object, containing multiple column objects.
1202
Zend_Text
The same way you can add columns to the rows. Create a new instance of Zend_Text_Table_Column and
then either set the column options in the constructor or later with the set* methods. The first parameter is the
content of the column which may have multiple lines, which in the best case are separated by just the \n character.
The second parameter defines the align, which is left by default and can be one of the class constants of
Zend_Text_Table_Column:
• ALIGN_LEFT
• ALIGN_CENTER
• ALIGN_RIGHT
The third parameter is the colspan of the column. For example, when you choose "2" as colspan, the column will span
over two columns of the table. The last parameter defines the encoding of the content, which should be supplied, if
the content is neither ASCII nor UTF-8. To append the column to the row, you simply call appendColumn in your
row object with the column object as parameter. Alternatively you can directly give a string to the appendColumn
method.
To finally render the table, you can either use the render method of the table, or use the magic method __toString
by doing echo $table; or $tableString = (string) $table.
// Either simple
$table->appendRow(array('Zend', 'Framework'));
// Or verbose
$row = new Zend_Text_Table_Row();
$row->appendColumn(new Zend_Text_Table_Column('Zend'));
$row->appendColumn(new Zend_Text_Table_Column('Framework'));
$table->appendRow($row);
echo $table;
#################################
#Zend #Framework #
#################################
1203
1204
Capítulo 54. Zend_TimeSync
Introduction
Zend_TimeSync is able to receive internet or network time from a time server using the NTP or SNTP protocol.
With Zend_TimeSync, Zend Framework is able to act independently from the time settings of the server where
it is running.
To be independent from the actual time of the server, Zend_TimeSync works with the difference of the real time
which is sent through NTP or SNTP and the internal server's time.
Background
Zend_TimeSync is not able to change the server's time, but it will return a Zend_Date instance from which
the difference from the server's time can be worked with.
Why Zend_TimeSync ?
So why would someone use Zend_TimeSync ?
Normally every server within a multi-server farm will have a service running which synchronizes its own time with a
time server. So within a standard environment it should not be necessary to use Zend_TimeSync. But it can become
handy if there is no service available and if you don't have the right to install such a service.
Here are some example use cases, for which Zend_TimeSync is perfect suited:
If your application is running on a server and this server does not have any time service running, it may make sense
to use Zend_TimeSync in your application.
If your database is running on a different server and this server is not connected with NTP or SNTP to the application
server, you might have problems using storing and using time stamp data.
• Multiple servers
If your application is running on more than one server and these servers' time bases are not syncronized, you can
expect problems within your application when part of the application is coming from one server and another part
from another server.
• Batch processing
If you want to work with a time service within a batch file or within a command line application, Zend_TimeSync
may be of use.
Zend_TimeSync may provide a good solution in all of these cases and can be used if you are unable to run any
services on your server.
What is NTP ?
The Network Time Protocol (NTP) is a protocol for synchronizing multiple systems' clocks over packet-
switched, variable-latency data networks. NTP uses UDP port 123 as its transport layer. See the wikipedia article
[http://en.wikipedia.org/wiki/Network_Time_Protocol] for details about this protocol.
1205
Zend_TimeSync
What is SNTP?
The Simple Network Time Protocol (SNTP) is a protocol synchronizing multiple systems' clocks over
packet-switched, variable-latency data networks. SNTP uses UDP port 37 as its transport layer. It is closely related
to the Network Time Protocol, but simpler.
Problematic usage
Be warned that when you are using Zend_TimeSync you will have to think about some details related to the structure
of time sync and the internet itself. Correct usage and best practices will be described here. Read carefully before you
begin using Zend_TimeSync.
• Distance
The distance from your application server to the time server. If your server is in Europe, it would make little sense
to select a time server in Tahiti. Always select a server which is not far away. This reduces the request time and
overall network load.
• Speed
How long it takes to receive the request is also relevant. Try different servers to get the best result. If you are
requesting a server which is never accessible, you will always have an unnecessary delay.
• Splitting
Do not always use the same server. All time servers will lock out requests from servers that are flooding the server.
If your application requires heavy use of time servers, you should consider one of the pools described later.
So where can you find a time server? Generally you can use any timeserver you can connect to. This can be a time
server within your LAN or any public time server you have access to. If you decide to use a public time server, you
should consider using a server pool. Server pools are public addresses from which you will get a random, pooled time
server by requesting the time. This way you will not have to split your requests. There are public server pools available
for many regions which you may use to avoid problems mentioned above.
See pool.ntp.org [http://www.pool.ntp.org] to find your nearest server pool. For example, if your server is located
within Germany you can connect to 0.europe.pool.ntp.org.
Nota
All examples in this chapter use a public, generic time server: 0.europe.pool.ntp.org. You should use a public,
generic time server which is close to your application server. See http://www.pool.ntp.org for information.
1206
Zend_TimeSync
print $server->getDate()->getIso();
So what is happening in the background of Zend_TimeSync? First the syntax of the time server is checked. In our
example, '0.pool.ntp.org' is checked and recognised as a possible address for a time server. Then when calling
getDate() the actual set time server is requested and it will return its own time. Zend_TimeSync then calculates
the difference to the actual time of the server running the script and returns a Zend_Date object with the correct time.
For details about Zend_Date and its methods see the Zend_Date documentation.
Zend_TimeSync is a simple solution that can handle multiple time servers and supports an automatic fallback
mechanism. There are two supported ways; you can either specify an array of time servers when creating the instance,
or you can add additional time servers to the instance using the addServer() method.
print $server->getDate()->getIso();
There is no limit to the number of time servers you can add. When a time server can not be reached, Zend_TimeSync
will fallback and try to connect to the next time server.
When you supply more than one time server- which is considered a best practice for Zend_TimeSync- you should
name each server. You can name your servers with array keys, with the second parameter at instantiation, or with the
second parameter when adding another time server.
print $server->getDate()->getIso();
Naming the time servers allows you to request a specific time server as we will see later in this chapter.
You set the proper protocol in the address of the time server. There are two protocols which are supported by
Zend_TimeSync: NTP and SNTP. The default protocol is NTP. If you are using NTP, you can omit the protocol in
the address as demonstrated in the previous examples.
1207
Zend_TimeSync
print $server->getDate()->getIso();
Zend_TimeSync can handle mixed time servers. So you are not restricted to only one protocol; you can add any
server independently from its protocol.
But sometimes the port that the protocols use differs from the standard one. You can define the port which has to be
used for each server within the address. Just add the number of the port after the address. If no port is defined, then
Zend_TimeSync will use the standard port.
print $server->getDate()->getIso();
The option timeout defines the number of seconds after which a connection is detected as broken when there was
no response. The default value is 1, which means that Zend_TimeSync will fallback to the next time server if the
requested time server does not respond in one second.
With the setOptions() method, you can set any option. This function accepts an array where the key is the option
to set and the value is the value of that option. Any previously set option will be overwritten by the new value. If you
want to know which options are set, use the getOptions() method. It accepts either a key which returns the given
option if specified, or, if no key is set, it will return all set options.
print $server->getDate()->getIso();
print_r(Zend_TimeSync::getOptions();
print "Timeout = " . Zend_TimeSync::getOptions('timeout');
As you can see, the options for Zend_TimeSync are static. Each instance of Zend_TimeSync will use the same
options.
1208
Zend_TimeSync
$actual = $server->getServer();
$server = $server->setServer('additional');
print_r ($server->getInfo());
The returned information differs with the protocol used and can also differ with the server used.
Handling Exceptions
Exceptions are collected for all time servers and returned as an array. So you can iterate through all thrown exceptions
as shown in the following example:
$serverlist = array(
// invalid servers
'invalid_a' => 'ntp://a.foo.bar.org',
'invalid_b' => 'sntp://b.foo.bar.org',
);
try {
$result = $server->getDate();
echo $result->getIso();
} catch (Zend_TimeSync_Exception $e) {
$exceptions = $e->get();
1209
Zend_TimeSync
}
}
1210
Capítulo 55. Zend_Tool_Framework
Introduction
Zend_Tool_Framework is a framework for exposing common functionalities such as the creation of project
scaffolds, code generation, search index generation, and much more. Functionality may be written and exposed via
PHP classes dropped into the PHP include_path, providing incredible flexibility of implementation. The functionality
may then be consumed by writing implementation and/or protocol-specific clients -- such as console clients, XML-
RPC, SOAP, and much more.
• Common interfaces and abstracts that allow developers to create functionality and capabilities that are dispatchable
by tooling clients.
• Base client functionality and a concrete console implementation that connect external tools and interfaces to the
Zend_Tool_Framework. The Console client may be used in CLI environments such as unix shells and the
Windows console.
• "Provider" and "Manifest" interfaces that can be utilized by the tooling system. "Providers" represent the functional
aspect of the framework, and define the actions that tooling clients may call. "Manifests" act as metadata registries
that provide additional context for the various defined providers.
• An introspective loading system that will scan the environment for providers and determine what is required to
dispatch them.
• A standard set of system providers that allow the system to report what the full capabilities of the system are as well
as provide useful feedback. This also includes a comprehensive "Help System".
Definitions that you should be aware of through this manual with respect to Zend_Tool_Framework include:
• Client - The subsystem of Zend_Tool_Framework that exposes an interface such that tooling clients can
connect, query and execute commands.
• Console Client / Command Line Interface / zf.php - The tooling client for the command line.
• Provider - A subsystem and a collection of built-in functionality that the framework exports.
• Manifest - A subsystem for defining, organizing, and disseminating provider requirement data.
• Zend_Tool_Project Provider - A set of providers specifically for creating and maintaining Zend Framework-
based projects.
1211
Zend_Tool_Framework
Architecture
Registry
Because providers and manifests may come from anywhere in the include_path, a registry is provided to simplify
access to the various pieces of the toolchain. This registry is injected into registry-aware components, which may then
pull dependencies from them as necessary. Most dependencies registered with the registry will be sub-component-
specific repositories.
interface Zend_Tool_Framework_Registry_Interface
{
public function setClient(Zend_Tool_Framework_Client_Abstract $client);
public function getClient();
public function setLoader(Zend_Tool_Framework_Loader_Abstract $loader);
public function getLoader();
public function setActionRepository(
Zend_Tool_Framework_Action_Repository $actionRepository
);
public function getActionRepository();
public function setProviderRepository(
Zend_Tool_Framework_Provider_Repository $providerRepository
);
public function getProviderRepository();
public function setManifestRepository(
Zend_Tool_Framework_Manifest_Repository $manifestRepository
);
public function getManifestRepository();
public function setRequest(Zend_Tool_Framework_Client_Request $request);
public function getRequest();
public function setResponse(Zend_Tool_Framework_Client_Response $response);
public function getResponse();
}
The various objects the registry manages will be discussed in their appropriate sections.
interface Zend_Tool_Framework_Registry_EnabledInterface
{
public function setRegistry(
Zend_Tool_Framework_Registry_Interface $registry
);
}
Providers
Zend_Tool_Framework_Provider represents the functional or "capability" aspect of the framework.
Fundamentally, Zend_Tool_Framework_Provider will provide the interfaces necessary to produce
1212
Zend_Tool_Framework
"providers", or bits of tooling functionality that can be called and used inside the Zend_Tool_Framework
toolchain. The simplistic nature of implementing this provider interface allows the developer a "one-stop-shop" of
adding functionality or capabilities to Zend_Tool_Framework.
The provider interface is an empty interface and enforces no methods (this is the Marker Interface pattern):
interface Zend_Tool_Framework_Provider_Interface
{}
Or, if you wish, you can implement the base (or abstract) Provider which will give you access to the
Zend_Tool_Framework_Registry:
Loaders
The purpose of a Loader is to find Providers and Manifest files that
contain classes which implement either Zend_Tool_Framework_Provider_Interface or
Zend_Tool_Framework_Manifest_Interface. Once these files are found by a loader, providers are loaded
into the Provider Repository and manifest metadata is loaded into the Manifest Repository.
The _getFiles() method should return an array of files (absolute paths). The built-in loader supplied
with Zend Framework is called the IncludePath loader. By default, the Tooling framework will use
an include_path based loader to find files that might include Providers or Manifest Metadata objects.
Zend_Tool_Framework_Loader_IncludePathLoader, without any other options, will search for files
inside the include path that end in Mainfest.php, Tool.php or Provider.php. Once found, they will be tested
(by the load() method of the Zend_Tool_Framework_Loader_Abstract) to determine if they implement
any of the supported interfaces. If they do, an instance of the found class is instantiated, and it is appended to the
proper repository.
1213
Zend_Tool_Framework
class Zend_Tool_Framework_Loader_IncludePathLoader
extends Zend_Tool_Framework_Loader_Abstract
{
As you can see, the IncludePath loader will search all include_paths for the files that match the
$_filterAcceptFilePattern and not match the $_filterDenyDirectoryPattern.
Manifests
In short, the Manifest shall contain specific or arbitrary metadata that is useful to any provider or client, as well as be
responsible for loading any additional providers into the provider repository.
To introduce metadata into the manifest repository, all one must do is implement the empty
Zend_Tool_Framework_Manifest_Interface, and provide a getMetadata() method which shall
return an array of objects that implement Zend_Tool_Framework_Manifest_Metadata.
interface Zend_Tool_Framework_Manifest_Interface
{
public function getMetadata();
}
Metadata objects are loaded (by a loader defined below) into the Manifest Repository
(Zend_Tool_Framework_Manifest_Repository). Manifests will be processed after all Providers have been
found to be loaded into the provider repository. This shall allow Manifests to create Metadata objects based on what
is currently inside the provider repository.
There are a few different metadata classes that can be used to describe metadata. The
Zend_Tool_Framework_Manifest_Metadata is the base metadata object. As you can see by the following
code snippet, the base metadata class is fairly lightweight and abstract in nature:
class Zend_Tool_Framework_Metadata_Basic
{
1214
Zend_Tool_Framework
There are other built in metadata classes as well for describing more specialized metadata: ActionMetadata and
ProviderMetadata. These classes will help you describe in more detail metadata that is specific to either actions
or providers, and the reference is expected to be a reference to an action or a provider respectively. These classes are
described in the following code snippet.
class Zend_Tool_Framework_Manifest_ActionMetadata
extends Zend_Tool_Framework_Manifest_Metadata
{
class Zend_Tool_Framework_Manifest_ProviderMetadata
extends Zend_Tool_Framework_Manifest_Metadata
{
'Type' in these classes is used to describe the type of metadata the object is responsible for. In the cases of the
ActionMetadata, the type would be 'Action', and conversely in the case of the ProviderMetadata the type
is 'Provider'. These metadata types will also include additional structured information about both the "thing" they are
describing as well as the object (the getReference()) they are referencing with this new metadata.
In order to create your own metadata type, all one must do is extend the base
Zend_Tool_Framework_Manifest_Metadata class and return these new metadata objects via a local
Manifest class or object. These user based classes will live in the Manifest Repository
Once these metadata objects are in the repository, there are then two different methods that can be used in order to
search for them in the repository.
class Zend_Tool_Framework_Manifest_Repository
{
/**
* To use this method to search, $searchProperties should contain the names
* and values of the key/value pairs you would like to match within the
* manifest.
*
* For Ejemplo:
* $manifestRepository->findMetadatas(array(
* 'action' => 'Foo',
1215
Zend_Tool_Framework
/**
* The following will return exactly one of the matching search criteria,
* regardless of how many have been returned. First one in the manifest is
* what will be returned.
*/
public function findMetadata(Array $searchProperties = array(),
$includeNonExistentProperties = true)
{
$metadatas = $this->getMetadatas($searchProperties,
$includeNonExistentProperties);
return array_shift($metadatas);
}
}
Looking at the search methods above, the signatures allow for extremely flexible searching. In order to find a metadata
object, simply pass in an array of matching constraints via an array. If the data is accessible through the Property
accessor (the getSomething() methods implemented on the metadata object), then it will be passed back to the
user as a "found" metadata object.
Clients
Clients are the interface which bridges a user or external tool into the Zend_Tool_Framework system. Clients can
come in all shapes and sizes: RPC endpoints, Command Line Interface, or even a web interface. Zend_Tool has
implemented the command line interface as the default interface for interacting with the Zend_Tool_Framework
system.
To implement a client, one would need to extend the following abstract class:
/**
* This method should be implemented by the client implementation to parse
1216
Zend_Tool_Framework
* out and set up the request objects action, provider and parameter
* information.
*/
abstract protected function _preDispatch();
/**
* This method should be implemented by the client implementation to take
* the output of the response object and return it (in an client specific
* way) back to the Tooling Client.
*
* (not required, but suggested)
*/
abstract protected function _postDispatch();
}
As you can see, there 1 method is required to fulfill the needs of a client (two others suggested), the
initialization, prehandling and post handling. For a more in depth study of how the command line client works,
please see the source code [http://framework.zend.com/svn/framework/standard/branches/release-1.8/library/Zend/
Tool/Framework/Client/Console.php].
1217
Zend_Tool_Framework
The primary purpose, and the purpose most immediately used by existing providers is to allow developers to customize
the way the "out of the box" providers do work.
One of the more commonly requested features is to be able to provide custom project profiles to
Zend_Tool_Project's Project Provider. This would allow developers to store a custom profile in a special place
that can be used repeatedly by the Zend_Tool system in order to build custom profiles. Another commonly requested
feature is to be able to configure the behavior of providers with a configuration setting. In order to achieve this, not only
do we have to have a Zend_Tool configuration file, but we also have to have a place to find this configuration file.
On *nix-based machines, PHP will be populated with an environment variable named HOME with a path to the current
users home directory. Typically, this path will be very similar to /home/myusername.
On Windows-based machines, PHP will typically be populated with an environment variable named HOMEPATH
with the current users home directory. This directory is usually found in either C:\Documents and Settings
\Username\, or in Vista at C:\Users\Username.
If either a home directory cannot be found, or you wish to change the location of where Zend_Tool_Framework
Console Client finds the home directory, you can provide an environment variable named ZF_HOME to specify where
to find the home directory.
Local Storage
Once a home directory can be located, Zend_Tool_Framework's Console Client can either autodiscover the local
storage directory, or it can be told where to expect the local storage directory.
Assuming the home directory has been found (here noted as $HOME), the Console Client will then look for the local
storage directory in $HOME/.zf/. If found, it will set the local storage directory to this location.
If the directory cannot be found, or the developer wishes to override this location, that can be done by setting an
environment variable. Regardless if $HOME has been previously set or not, the developer may supply the environment
variable ZF_STORAGE_DIR.
Once the path to a local storage directory is found, the directory must exist for it to be passed into the
Zend_Tool_Framework runtime, as it will not be created for you.
User Configuration
Like local storage, once a home directory can be located, Zend_Tool_Framework's Console Client can then either
attempt to autodiscover the path to a configuration file, or it can be told specifically where to find the configuration file.
1218
Zend_Tool_Framework
Assuming the home directory has been found (here noted as $HOME), the Console Client will then attempt to look for
the existence of a configuration file located at $HOME/.zf.ini. This file, if found, will be used as the configuration
file for Zend_Tool_Framework.
If that location does not exist, but a local storage directory does, then the Console Client will then attempt to
locate the configuration file within the local storage directory. Assuming the local storage directory exists in
$LOCAL_STORAGE, then if a file exists as $LOCAL_STORAGE/zf.ini, it will be found by the Console Client
and utilized as the Zend_Tool_Framework configuration file.
If the file cannot be autodiscovered or the developer wishes to specify the location of location of the configuration file,
the developer can do so by setting an environment variable. If the environment variable ZF_CONFIG_FILE is set, then
its value will be used as the location of the configuration file to use with the Console Client. The ZF_CONFIG_FILE
can point to any Zend_Config readable INI, XML or PHP File.
If the file does not exist in either the autodiscovered or the provided location, it will not be used as
Zend_Tool_Framework does not attempt to create the file automatically.
project.profile = some/path/to/some-directory
The only reserved ini prefix is the value "php". The "php" prefix to values will be reserved to store names and values of
runtime settable php values, such as include_path or error_reporting. To override the include_path and error_reporting
with an ini value, a developer would set:
php.include_path = "/path/to/includes1:/path/to/includes2"
php.error_reporting = 1
Important
The reserved prefix "php" only works with INI files. You can't set PHP INI values with PHP or XML config.
1219
1220
Capítulo 56. Zend_Tool_Project
Introduction
Zend_Tool_Project builds on and extends the capabilities of Zend_Tool_Framework to that of managing a
"project". In general, a "project" is a planned endeavor or an initiative. In the computer world, projects generally are
a collection of resources. These resources can be files, directories, databases, schemas, images, styles, and more.
This same concept applies to Zend Framework projects. In Zend Framework projects, you have controllers, actions,
views, models, databases and so on and so forth. In terms of Zend_Tool, we need a way to track these types of
resources - thus Zend_Tool_Project.
Zend_Tool_Project is capable of tracking project resources throughout the development of a project. So, for
example, if in one command you created a controller, and in the next command you wish to create an action within
that controller, Zend_Tool_Project is gonna have to know about the controller file you created so that you can
(in the next action), be able to append that action to it. This is what keeps our projects up to date and stateful.
Another important point to understand about projects is that typically, resources are organized in a hierarchical fashion.
With that in mind, Zend_Tool_Project is capable of serializing the current project into a internal representation
that allows it to keep track of not only what resources are part of a project at any given time, but also where they are
in relation to one another.
Create A Project
Nota
The following examples will assume you have the command line interface of Zend_Tool_Framework
available to you.
Nota
To issue any of the commands for Zend_Tool_Project with CLI, you must be in the directory where
the project was initially created.
To get started with Zend_Tool_Project, you simply need to create a project. Creating a project is simple: go to
a place on your filesystem, create a directory, change to that directory, then issue the following command:
The following table will describe the capabilities of providers that are available to you. As you can see in this table,
there is a "Project" provider. The Project provider has a couple of actions associated to it, and with those actions a
number of options that can be used to modify the behavior of the action and provider.
1221
Zend_Tool_Project
1222
Capítulo 57. Zend_Translate
Introduction
Zend_Translate is Zend Framework's solution for multilingual applications.
In multilingual applications, the content must be translated into several languages and display content depending on
the user's language. PHP offers already several ways to handle such problems, however the PHP solution has some
problems:
• Inconsistent API: There is no single API for the different source formats. The usage of gettext for example is very
complicated.
• PHP supports only gettext and native array: PHP itself offers only support for array or gettext. All other source
formats have to be coded manually, because there is no native support.
• No detection of the default language: The default language of the user cannot be detected without deeper knowledge
of the backgrounds for the different web browsers.
• Gettext is not thread-safe: PHP's gettext library is not thread safe, and it should not be used in a multithreaded
environment. This is due to problems with gettext itself, not PHP, but it is an existing problem.
Zend_Translate does not have the above problems. This is why we recommend using Zend_Translate instead
of PHP's native functions. The benefits of Zend_Translate are:
• Supports multiple source formats: Zend_Translate supports several source formats, including those supported
by PHP, and other formats including TMX and CSV files.
• Thread-safe gettext: The gettext reader of Zend_Translate is thread-safe. There are no problems using it in
multi-threaded environments.
• Easy and generic API: The API of Zend_Translate is very simple and requires only a handful of functions. So
it's easy to learn and easy to maintain. All source formats are handled the same way, so if the format of your source
files change from Gettext to TMX, you only need to change one line of code to specify the storage adapter.
• Detection of the user's standard language: The preferred language of the user accessing the site can be detected
and used by Zend_Translate.
• Automatic source detection: Zend_Translate is capable of detecting and integrating multiple source files and
additionally detect the locale to be used depending on directory or filenames.
Starting multi-lingual
So let's get started with multi-lingual business. What we want to do is translate our string output so the view produces
the translated output. Otherwise we would have to write one view for each language, and no one would like to do this.
Generally, multi-lingual sites are very simple in their design. There are only four steps you would have to do:
1223
Zend_Translate
The following sections guide you through all four steps. Read through the next few pages to create your own multi-
lingual web application.
Nota
When deciding your adapter you should also be aware of the used encoding. Even if Zend Framework declares
UTF-8 as default encoding you will sometimes be in the need of other encoding. Zend_Translate will
not change any encoding which is defined within the source file which means that if your Gettext source
is build upon ISO-8859-1 it will also return strings in this encoding without converting them. There is only
one restriction:
1224
Zend_Translate
When you use a xml based source format like TMX or XLIFF you must define the encoding within the xml
files header because xml files without defined encoding will be treated as UTF-8 by any xml parser by default.
You should also be aware that actually the encoding of xml files is limited to the encodings supported by
PHP which are UTF-8, ISO-8859-1 and US-ASCII.
Zend_Translate_Adapter_Array
The Array Adapter is the Adapter which is simplest to use for programmers. But when you have numerous translation
strings or many languages you should think about another Adapter. For example, if you have 5000 translation strings,
the Array Adapter is possibly not the best choice for you.
You should only use this Adapter for small sites with a handful of languages, and if you or your programmer team
creates the translations yourselves.
Zend_Translate_Adapter_Csv
The Csv Adapter is the Adapter which is simplest to use for customers. CSV files are readable by standard text editors,
but text editors often do not support utf8 character sets.
You should only use this Adapter if your customer wants to do translations himself.
Nota
Beware that the Csv Adapter has problems when your Csv files are encoded differently than the locale setting
of your environment. This is due to a Bug of PHP itself which will not be fixed before PHP 6.0 (http://
bugs.php.net/bug.php?id=38471). So you should be aware that the Csv Adapter due to PHP restrictions is
not locale aware.
Zend_Translate_Adapter_Gettext
The Gettext Adapter is the Adapter which is used most frequently. Gettext is a translation source format which was
introduced by GNU, and is now used worldwide. It is not human readable, but there are several freeware tools (for
instance, POEdit [http://sourceforge.net/projects/poedit/]), which are very helpful. The Zend_Translate Gettext
Adapter is not implemented using PHP's gettext extension. You can use the Gettext Adapter even if you do not have
the PHP gettext extension installed. Also the Adapter is thread-safe and the PHP gettext extension is currently not
thread-safe.
Most people will use this adapter. With the available tools, professional translation is very simple. But gettext data are
is stored in a machine-readable format, which is not readable without tools.
Zend_Translate_Adapter_Ini
The Ini Adapter is a very simple Adapter which can even be used directly by customers. INI files are readable by
standard text editors, but text editors often do not support utf8 character sets.
You should only use this Adapter when your customer wants to do translations himself. Do not use this adapter as
generic translation source.
1225
Zend_Translate
characters, you may find your translations no longer work when using the INI adapter. If this is the case, we
recommend utilizing a different adapter.
Zend_Translate_Adapter_Tbx
The Tbx Adapter is an Adapter which will be used by customers which already use the TBX format for their internal
translation system. Tbx is no standard translation format but more a collection of already translated and pre translated
source strings. When you use this adapter you have to be sure that all your needed source string are translated. TBX
is a XML file based format and a completely new format. XML files are human-readable, but the parsing is not as
fast as with gettext files.
This adapter is perfect for companies when pre translated source files already exist. The files are human readable and
system-independent.
Zend_Translate_Adapter_Tmx
The Tmx Adapter is the Adapter which will be used by most customers which have multiple systems which use the
same translation source, or when the translation source must be system-independent. TMX is a XML file based format,
which is announced to be the next industry standard. XML files are human-readable, but the parsing is not as fast as
with gettext files.
Most medium to large companies use this adapter. The files are human readable and system-independent.
Zend_Translate_Adapter_Qt
The Qt Adapter is for all customers which have TS files as their translation source which are made by QtLinguist. QT
is a XML file based format. XML files are human-readable, but the parsing is not as fast as with gettext files.
Several big players have build software upon the QT framework. The files are human readable and system-independent.
Zend_Translate_Adapter_Xliff
The Xliff Adapter is the Adapter which will be used by most customers which want to have XML files but do not
have tools for TMX. XLIFF is a XML file based format, which is related to TMX but simpler as it does not support
all possibilities of it. XML files are human-readable, but the parsing is not as fast as with gettext files.
Most medium companies use this adapter. The files are human readable and system-independent.
Zend_Translate_Adapter_XmlTm
The XmlTm Adapter is the Adapter which will be used by customers which do their layout themself. XmlTm is a
format which allows the complete html source to be included in the translation source, so the translation is coupled
with the layout. XLIFF is a XML file based format, which is related to XLIFF but its not as simple to read.
This adapter should only be used when source files already exist. The files are human readable and system-independent.
Any adapter class you want to use with Zend_Translate must be a subclass of Zend_Translate_Adapter.
Zend_Translate_Adapter is an abstract class which already defines all what is needed for translation. What
has to be done by you, is the definition of the reader for translation datas.
1226
Zend_Translate
The usage of the prefix "Zend" should be limited to Zend Framework. If you extend Zend_Translate with
your own adapter, you should name it like "Company_Translate_Adapter_MyFormat". The following code shows an
example of how a self written adapter class could be implemented:
try {
$translate = new Zend_Translate('Company_Translate_Adapter_MyFormat',
'/path/to/translate.xx',
'en',
array('myoption' => 'myvalue'));
} catch (Exception $e) {
// File not found, no adapter class...
// General failure
}
To use caching you will just have to give a cache object to the Zend_Translate::setCache() method. It takes
a instance of Zend_Cache as only parameter. Also if you use any adapter direct you can use the setCache()
method. For convenience there are also the static methods getCache(), hasCache(), clearCache() and
removeCache().
$cache = Zend_Cache::factory('Core',
'File',
$frontendOptions,
$backendOptions);
Zend_Translate::setCache($cache);
$translate = new Zend_Translate('gettext',
'/path/to/translate.mo',
'en');
Nota
You must set the cache before you use or initiate any adapter or instance of Zend_Translate. Otherwise
your translation source will not be cached until you add a new source with the addTranslation()
method.
print "Ejemplo\n";
print "=======\n";
print "Here is line one\n";
print "Today is the " . date("d.m.Y") . "\n";
print "\n";
print "Here is line two\n";
1227
Zend_Translate
The example above shows some output with no support for translation. You probably write your code in your native
language. Generally you need to translate not only the output, but also error and log messages.
The next step is to integrate Zend Translate into your existing code. Of course it is much easier if you had already
written your code with translation in mind, than changing your code afterwards.
$translate->setLocale('fr');
print $translate->_("Here is line two") . "\n";
Now let's take a deeper look into what has been done and how to integrate Zend_Translate into your own code.
In this example we chose the Gettext Adapter. We place our file source-de.mo into the directory /path/to/translation.
The gettext file will have German translation included, and we also added another language source for French.
The next step is to wrap all strings which are to be translated. The simplest approach is to have only simple strings
or sentences like this:
Some strings do not needed to be translated. The separating line is always a separating line, even in other languages.
Having data values integrated into a translation string is also supported through the use of embedded parameters.
Instead of print(), use the printf() function and replace all parameters with %1\$s parts. The first is %1\$s,
the second is %2\$s, and so on. This way a translation can be done without knowing the exact value. In our example,
the date is always the actual day, but the string can be translated without the knowledge of the actual day.
Each string is identified in the translation storage by a message ID. You can use message IDs instead of strings in
your code, like this:
1228
Zend_Translate
You can not see what your code should output just by viewing your code.
Also you will have problems if some strings are not translated. You must always keep in mind how translation works.
First Zend_Translate checks whether the specified language has a translation for the given message ID or string.
If no translation string has been found it refers to the next lower level language as defined within Zend_Locale.
So "de_AT" becomes "de" only. If there is no translation found for "de" either, then the original message is returned.
This way you always have an output, even in case the message translation does not exist in your message storage.
Zend_Translate never throws an error or exception when translating strings.
You have to decide where to store your translation source files. Using Zend_Translate you are not restricted in
any way. The following structures are preferable:
/application/
/languages/
/languages/lang.en
/languages/lang.de
/library/
Positive: all source files for every languages are stored in one directory. No splitting of related files.
/application/
/languages/
/languages/en/
/languages/en/first.en
/languages/en/second.en
/languages/de/
/languages/de/first.de
/languages/de/second.de
/library
Positive: Every language is stored in their own directories. Easy translation, as every language team has to translate
only one directory. Also the usage of multiple files is transparent.
/application/
/application/languages/
1229
Zend_Translate
/application/languages/first.en
/application/languages/first.de
/application/languages/second.en
/application/languages/second.de
/library/
Positive: all source files for every language are stored in one directory. No splitting of related files.
Negative: having multiple files for the same language can be problematic.
/application/
/languages/
/languages/de/
/languages/de/LC_MESSAGES/
/languages/de/LC_MESSAGES/first.mo
/languages/de/LC_MESSAGES/second.mo
/languages/en/
/languages/en/LC_MESSAGES/
/languages/en/LC_MESSAGES/first.mo
/languages/en/LC_MESSAGES/second.mo
/library/
Negative: having sub-sub directories may be confusing for people who have not used gettext before.
/application/
/application/models/
/application/models/MyModel.php
/application/models/MyModel.de
/application/models/MyModel.en
/application/controllers/
/application/controllers/MyController.php
/application/controllers/MyController.de
/application/controllers/MyController.en
/library/
Negative: too many and also small translation files result in being tendious to translate. Also every file has to be
added as translation source.
Single structured and language structured source files are most usable for Zend_Translate.
So now, that we know which structure we want to have, we should create our translation source files.
1230
Zend_Translate
Nota
Note that most of the described formats should be created by using a tool or a generation process. These Tools
and processes are not part of Zend Framework and for most of the described formats free tools are available.
$english = array(
'message1' => 'message1',
'message2' => 'message2',
'message3' => 'message3');
$german = array(
'message1' => 'Nachricht1',
'message2' => 'Nachricht2',
'message3' => 'Nachricht3');
Since release 1.5 it is also supported to have arrays included within an external file. You just have to provide the
filename and Zend_Translate will automatically include it and look for the array. See the following example for
details:
// myarray.php
return array(
'message1' => 'Nachricht1',
'message2' => 'Nachricht2',
'message3' => 'Nachricht3');
// controller
$translate = new Zend_Translate('array', '/path/to/myarray.php', 'de');
Nota
Files which do not return an array will fail to be included. Also any output within this file will be ignored
and suppressed.
1231
Zend_Translate
As you can see the adapters are used exactly the same way, with one small difference: change array to gettext. All
other usages are exactly the same as with all other adapters. With the gettext adapter you no longer have to be aware of
gettext's standard directory structure, bindtextdomain and textdomain. Just give the path and filename to the adapter.
Nota
You should always use UTF-8 as source encoding. Otherwise you will have problems when using two
different source encodings. E.g. one of your source files is encoded with ISO-8815-11 and another one with
CP815. You can set only one encoding for your source file, so one of your languages probably will not display
correctly.
UTF-8 is a portable format which supports all languages. When using UTF-8 for all languages, you will
eliminate the problem of incompatible encodings.
Many gettext editors add adapter informations as empty translation string. This is the reason why empty strings are
not translated when using the gettext adapter. Instead they are erased from the translation table and provided by the
getAdapterInfo() method. It will return the adapter informations for all added gettext files as array using the
filename as key.
TMX files can have several languages within the same file. All other included languages are added automatically, so
you do not have to call addLanguage().
1232
Zend_Translate
If you want to have only specified languages from the source translated you can set the option 'defined_language'
to TRUE. With this option you can add the wished languages explicitly with addLanguage(). The default value
for this option is to add all languages.
There are three different options for the CSV adapter. You can set 'delimiter', 'limit' and 'enclosure'.
The default delimiter for CSV string is ';', but with the option 'delimiter' you can decide to use another one.
The default limit for a line within a CSV file is '0'. This means that the end of a CSV line is searched automatically. If
you set 'limit' to any value, then the CSV file will be read faster, but any line exceeding this limit will be truncated.
The default enclosure to use for CSV files is '"'. You can set a different one using the option 'enclosure'.
$translate->addTranslation('/path/to/other.csv', 'fr');
Nota
When you are using non-ASCII characters within your CSV file, like umlauts or UTF-8 chars, then you
should always use enclosure. Omitting the enclosure can lead to missing characters in your translation.
1233
Zend_Translate
[Test]
;TestPage Comment
Message_1="Nachricht 1 (de)"
Message_2="Nachricht 2 (de)"
Message_3="Nachricht :3 (de)"
INI files have several restrictions. If a value in the ini file contains any non-alphanumeric characters it needs to be
enclosed in double-quotes ("). There are also reserved words which must not be used as keys for ini files. These
include: NULL, yes, no, TRUE, and FALSE. Values NULL, no and FALSE results in "", yes and TRUE results in
1. Characters {}|&~![()" must not be used anywhere in the key and have a special meaning in the value. Do not
use them as it will produce unexpected behaviour.
You can set options temporarily when using addTranslation($data, $locale, array $options
= array()) as third and optional parameter. And you can use the method setOptions() to set the options
permanently.
...
Here you can find all available options for the different adapters with a description of their usage:
1234
Zend_Translate
1235
Zend_Translate
When you want to have self defined options, you are also able to use them within all adapters. The setOptions()
method can be used to define your option. setOptions() needs an array with the options you want to set. If an
given option exists it will be signed over. You can define as much options as needed as they will not be checked by
the adapter. Just make sure not to overwrite any existing option which is used by an adapter.
To return the option you can use the getOptions() method. When getOptions() is called without a parameter
it will return all options set. When the optional parameter is given you will only get the specified option.
Handling languages
When working with different languages there are a few methods which will be useful.
The getLocale() method can be used to get the currently set language. It can either hold an instance of
Zend_Locale or the identifier of a locale.
The setLocale() method sets a new standard language for translation. This prevents the need of setting the
optional language parameter more than once to the translate() method. If the given language does not exist, or
no translation data is available for the language, setLocale() tries to downgrade to the language without the region
if any was given. A language of en_US would be downgraded to en. When even the downgraded language can not
be found an exception will be thrown.
The isAvailable() method checks if a given language is already available. It returns TRUE if data for the given
language exist.
And finally the getList() method can be used to get all currently set languages for an adapter returned as array.
1236
Zend_Translate
The algorithm will search for the best fitting locale depending on the user's browser and your environment. See the
following example for details:
// Ejemplo 1:
// When no fitting language is found, the message ID is returned
$translate = new Zend_Translate(
'gettext',
'my_it.mo',
'auto',
array('scan' => Zend_Translate::LOCALE_FILENAME));
// Ejemplo 2:
// Best found fitting language is 'fr'
$translate = new Zend_Translate(
'gettext',
'my_fr.mo',
'auto',
array('scan' => Zend_Translate::LOCALE_FILENAME));
// Ejemplo 3:
// Best found fitting language is 'de' ('de_AT' will be degraded)
$translate = new Zend_Translate(
'gettext',
'my_de.mo',
'auto',
array('scan' => Zend_Translate::LOCALE_FILENAME));
1237
Zend_Translate
// Ejemplo 4:
// Returns 'it' as translation source and overrides the automatic settings
$translate = new Zend_Translate(
'gettext',
'my_it.mo',
'auto',
array('scan' => Zend_Translate::LOCALE_FILENAME));
$translate->addTranslation('my_ru.mo', 'ru');
$translate->setLocale('it_IT');
After setting a language manually with the setLocale() method the automatic detection will be switched off and
overridden.
If you want to use it again, you can set the language auto with setLocale() which will reactivate the automatic
detection for Zend_Translate.
Since Zend Framework 1.7.0 Zend_Translate also recognises an application wide locale. You can simply set a
Zend_Locale instance to the registry like shown below. With this notation you can forget about setting the locale
manually with each instance when you want to use the same locale multiple times.
if (!$translate->isAvailable($locale->getLanguage())) {
// not available languages are rerouted to another language
$translate->setLocale($defaultlanguage);
}
$translate->getLocale();
Nota
Automatic source detection is available since Zend Framework version 1.5 .
The usage is quite the same as initiating a single translation source with one difference. You must give a directory
which has to be scanned instead a file.
1238
Zend_Translate
// /language/
// /language/login/login.tmx
// /language/logout/logout.tmx
// /language/error/loginerror.tmx
// /language/error/logouterror.tmx
So Zend_Translate does not only search the given directory, but also all subdirectories for translation source
files. This makes the usage quite simple. But Zend_Translate will ignore all files which are not sources or which
produce failures while reading the translation data. So you have to make sure that all of your translation sources are
correct and readable because you will not get any failure if a file is bogus or can not be read.
Nota
Depending on how deep your directory structure is and how much files are within this structure it can take
a long time for Zend_Translate to complete.
In our example we have used the TMX format which includes the language to be used within the source. But many
of the other source formats are not able to include the language within the file. Even this sources can be used with
automatic scanning if you do some pre-requisits as described below:
Zend_Translate needs the 'scan' option to know that it should search the names of all directories for languages.
See the following example for details:
Nota
This works only for adapters which do not include the language within the source file. Using this option
for example with TMX will be ignored. Also language definitions within the filename will be ignored when
using this option.
Nota
You should be aware if you have several subdirectories under the same structure. Assuming we have a
structure like /language/module/de/en/file.mo. In this case the path contains multiple strings
1239
Zend_Translate
which would be detected as locale. It could be either de or en. In such a case the behaviour is undefined and
it is recommended to use file detection in such situations.
Complete filename
Having the whole file named after the language is the simplest way but only viable if you have only one file per
language.
/languages/
/languages/en.mo
/languages/de.mo
/languages/es.mo
/languages/
/languages/view.en
/languages/view.de
/languages/view.es
Filename tokens
Zend_Translate is also capable of detecting the language if it is included within the filename. But if you go this
way you will have to separate the language with a token. There are three supported tokens which can be used: a dot
'.', an underscore '_', or a hyphen '-'.
1240
Zend_Translate
/languages/
/languages/view_en.mo -> detects english
/languages/view_de.mo -> detects german
/languages/view_it.mo -> detects italian
The first found string delimited by a token which can be interpreted as a locale will be used. See the following example
for details.
/languages/
/languages/view_en_de.mo -> detects english
/languages/view_en_es.mo -> detects english and overwrites the first file
/languages/view_it_it.mo -> detects italian
All three tokens are used to detect the locale. When the filename contains multiple tokens, the first found token depends
on the order of the tokens which are used. See the following example for details.
/languages/
/languages/view_en-it.mo -> detects english because '_' will be used before '-'
/languages/view-en_it.mo -> detects italian because '_' will be used before '-'
/languages/view_en.it.mo -> detects italian because '.' will be used before '_'
isTranslated($messageId, $original = false, $locale = null) takes the text you want to
check as its first parameter, and as optional third parameter the locale for which you want to do the check. The optional
second parameter declares whether translation is fixed to the declared language or a lower set of translations can be
used. If you have a text which can be returned for 'en' but not for 'en_US' you will normally get the translation returned,
but by setting $original to true, isTranslated() will return false.
$english = array(
'message1' => 'Nachricht 1',
'message2' => 'Nachricht 2',
'message3' => 'Nachricht 3');
if ($translate->isTranslated('message1')) {
print "'message1' can be translated";
}
1241
Zend_Translate
You have to follow two or three simple steps. First, you have to create an instance of Zend_Log. Then you have to
attach this instance to Zend_Translate. See the following example:
$translate->translate('unknown string');
Now you will have a new notice in the log: Untranslated message within 'de': unknown string.
Nota
You should note that any translation which can not be found will be logged. This means all translations when a
user requests a language which is not supported. Also every request for a message which can not be translated
will be logged. Be aware, that 100 people requesting the same translation, will result 100 logged notices.
This feature can not only be used to log messages but also to attach this untranslated messages into an empty translation
file. To do so you will have to write your own log writer which writes the format you want to have and strips the
prepending "Untranslated message".
You can also set the 'logMessage' option when you want to have your own log message. Use the '%message%'
token for placing the messageId within your log message, and the '%locale%' token for the requested locale. See the
following example for a self defined log message:
1242
Zend_Translate
$translate->setOptions(array(
'log' => $log,
'logMessage' => "Missing '%message%' within locale '%locale%'",
'logUntranslated' => true));
$translate->translate('unknown string');
The getMessageIds($locale = null) method returns all known message IDs as array.
The getMessages($locale = null) method returns the complete translation source as an array. The message
ID is used as key and the translation data as value.
Both methods accept an optional parameter $locale which, if set, returns the translation data for the specified
language. If this parameter is not given, the actual set language will be used. Keep in mind that normally all translations
should be available in all languages. Which means that in a normal situation you will not have to set this parameter.
Additionally the getMessages() method can be used to return the complete translation dictionary using the pseudo-
locale 'all'. This will return all available translation data for each added locale.
Nota
Attention: the returned array can be very big, depending on the number of added locales and the amount of
translation data.
So what are plurals? Generally spoken plurals are words which take into account numeric meanings. But as you may
imaging each language has it's own definition of plurals. English, for example, supports one plural. We have a singular
definition, for example "car", which means implicit one car, and we have the plural definition, "cars" which could
mean more than one car but also zero cars. Other languages like russian or polish have more plurals and also the rules
for plurals are different.
1243
Zend_Translate
When you want to use plurals with Zend_Translate you must not need to know how the plurals are defined, only
the translator must know as he does the translation. The only information you need to have is the language.
There are two way for using plurals... the traditional one, which means that you use a own method, and a modern one,
which allows you to do plural translations with the same method as normal translations.
The number will be used to detect the plural which has to be returned. A optional forth parameter can be used to give
a locale which will be used to return the translation.
To use plural translations with translate() you need to give an array as messageId instead of an string. This array
must have the original plural messageId's, then the amount and at last an optional locale when your given messageId's
are not in english notation.
Using modern plural translations it is also possible to use any language as source for messageId's.
Ejemplo 57.19. Ejemplo of modern plural translations using a different source language
Let's expect we want to use russian and let's also expect that the given messageId's are russian and not english.
1244
Zend_Translate
As you can see you can give more than just the one english plural. But you must give the source language in this case
so Zend_Translate knows which plural rules it has to apply.
When you omit the plural language then english will be used per default and any additional plural definition will be
ignored.
array(
'plural_0' => array(
'plural_0 (ru)',
'plural_1 (ru)',
'plural_2 (ru)',
'plural_3 (ru)'
),
'plural_1' => ''
);
In the above example plural_0 and plural_1 are the plural definitions from the source code. And the array
at plural_0 has all translated plural forms available. Take a look at the following example with real content and
translation from english source to german.
array(
'Car' => array(
'Auto',
'Autos'
),
'Cars' => ''
1245
Zend_Translate
);
When your translated language supports more plural forms then simply add them to the array below the first plural
form. When your source language suppors more plural forms, than simply add a new empty translation.
All translated plural forms have to be added after the first plural of the source language. And all further plural forms
of the source language have to be added below but without translation. Note that you must add a delimiter to empty
source plurals.
Nota
Note that gettext does not support the usage of source languages which are not using english plural forms.
When you plan to use a source language which supports other plural forms like russian for example, then
you can not use gettext sources.
1246
Capítulo 58. Zend_Uri
Zend_Uri
Overview
Zend_Uri is a component that aids in manipulating and validating Uniform Resource Identifiers [http://www.w3.org/
Addressing/] (URIs). Zend_Uri exists primarily to service other components, such as Zend_Http_Client, but
is also useful as a standalone utility.
URIs always begin with a scheme, followed by a colon. The construction of the many different schemes varies
significantly. The Zend_Uri class provides a factory that returns a subclass of itself which specializes in each scheme.
The subclass will be named Zend_Uri_<scheme>, where <scheme> is the scheme, lowercased with the first
letter capitalized. An exception to this rule is HTTPS, which is also handled by Zend_Uri_Http.
To create a new URI from scratch, pass only the scheme to Zend_Uri::factory()1. If an unsupported scheme
is passed, a Zend_Uri_Exception will be thrown.
If the scheme or URI passed is supported, Zend_Uri::factory() will return a subclass of itself that specializes
in the scheme to be created.
The URI will be parsed and validated. If it is found to be invalid, a Zend_Uri_Exception will be thrown
immediately. Otherwise, Zend_Uri::factory() will return a subclass of itself that specializes in the scheme to
be manipulated.
1
At the time of writing, Zend_Uri only supports the HTTP and HTTPS schemes.
1247
Zend_Uri
URI Validation
The Zend_Uri::check() method can only be used if validation of an existing URI is needed.
Zend_Uri::check() returns a boolean, which is more convenient than using Zend_Uri::factory() and
catching the exception.
Zend_Uri can be set to accept these "unwise" characters by setting the 'allow_unwise' option to boolean TRUE using
Zend_Uri::setConfig():
Nota
Zend_Uri::setConfig() sets configuration options globally. It is recommended to reset the
'allow_unwise' option to 'false', like in the example above, unless you are certain you want to always allow
unwise characters globally.
1248
Zend_Uri
$uri = Zend_Uri::factory('http://www.zend.com');
The getScheme() instance method returns only the scheme part of the URI object.
$uri = Zend_Uri::factory('http://www.zend.com');
The getUri() method returns the string representation of the entire URI.
$uri = Zend_Uri::factory('http://www.zend.com');
The valid() instance method provides a means to check that the URI object is still valid.
1249
1250
Capítulo 59. Zend_Validate
Introducción
Cuando se necesita validar algún tipo de dato, el componente Zend_Validate ofrece un conjunto de validadores,
como así también un sencillo mecanismo de encadenado de validaciones por el cual múltiples validadores pueden
aplicarse a un dato en un orden definido por el usuario.
¿Qué es un validador?
Un validador examina su entrada con respecto a algunos requerimientos y produce un resultado booleano si la entrada
valida satisfactoriamente con los requisitos. Si la entrada no cumple los requisitos, un validador también podrá
proporcionar información adicional sobre que requisito(s) no son satisfechos.
Por ejemplo, una aplicación web podría requerir que un usuario ingrese su nombre, de entre seis y doce caracteres
de longitud y que sólo puede contener caracteres alfanuméricos. Se puede usar un validador para asegurar que los
usuarios cumplan estos requisitos. Si el nombre de usuario elegido no cumple con uno o ambos de los requisitos, sería
útil saber cuál de estos requisitos no se cumple.
Nota
El método getMessages() devuelve información del fracaso de la validación sólo para la llamada más
reciente a isValid(). Cada llamada a isValid() borra los mensajes y errores causados por una llamada
anterior isValid(), porque es probable que cada llamada a isValid() se refiera al valor de una entrada
diferente.
if ($validator->isValid($email)) {
// email parece ser válido
} else {
// email es inválido; muestre la razones
foreach ($validator->getMessages() as $messageId => $message) {
echo "Falla de validación '$messageId': $message\n";
}
1251
Zend_Validate
El segundo argumento opcional es un string que identifica la plantilla de mensajes que se establecerá en caso del
fracaso de la validación, lo que es útil cuando una clase de validación define más de una causa para el fallo. Si omite
el segundo argumento, setMessage() asume que el mensaje que especifique debe ser utilizado por la plantilla
del primer mensaje que declaró en la clase de validación. Muchas clases de validación sólo definen una plantilla de
mensaje de error, así que no hay necesidad de especificar el cambio de plantilla de mensaje.
$validator->setMessage(
'El string \'%value%\' es muy corto; debe tener al menos %min% ' .
'caracteres',
Zend_Validate_StringLength::TOO_SHORT);
if (!$validator->isValid('word')) {
$messages = $validator->getMessages();
echo current($messages);
Puede establecer varios mensajes usando el método setMessages(). Su argumento es un array que contiene pares
de clave/mensaje.
$validator->setMessages( array(
Zend_Validate_StringLength::TOO_SHORT =>
'El string \'%value%\' es muy corto',
Zend_Validate_StringLength::TOO_LONG =>
'El string \'%value%\' es muy largo'
));
Incluso, si su aplicación requiere una mayor flexibilidad para informar los fallos de validación, puede acceder a
las propiedades por el mismo nombre, tal como los tokens de mensajes apoyados por una determinada clase de
validación. La propiedad value siempre está disponible en un validador; es el valor que especificó en el argumento
de isValid(). En cada clase de validación se puede dar apoyo a otras propiedades basándose en el esquema de
caso por caso.
1252
Zend_Validate
if (!validator->isValid('word')) {
echo 'Palabra fallada: '
. $validator->value
. '; su longitud no está entre '
. $validator->min
. ' y '
. $validator->max
. "\n";
}
if (Zend_Validate::is($email, 'EmailAddress')) {
// Si, el email parece ser válido
}
El método is() devuelve un valor booleano, lo mismo que el método isValid(). Cuando se utiliza el método
estático is(), no están disponibles los mensajes de fracaso de validación.
El uso estático puede ser conveniente para invocar un validador ad-hoc (hecho especialmente), pero si tiene la necesidad
de ejecutar el validador para múltiples entradas, es más eficiente usar métodos no estáticos, creando una instancia del
objeto validador y llamando a su método isValid().
También la clase Zend_Filter_Input le permite crear ejemplos y ejecutar múltiples filtros y clases de validadores
por demanda, para procesar juegos de datos de entrada. Ver the section called “Zend_Filter_Input”.
Translating messages
Validate classes provide a setTranslator() method with which you can specify a instance of
Zend_Translate which will translate the messages in case of a validation failure. The getTranslator()
method returns the set translator instance.
1253
Zend_Validate
$validator->setTranslator($translate);
With the static setDefaultTranslator() method you can set a instance of Zend_Translate which will be
used for all validation classes, and can be retrieved with getDefaultTranslator(). This prevents you from
setting a translator manually for all validator classes, and simplifies your code.
Nota
When you have set an application wide locale within your registry, then this locale will be used as default
translator.
Sometimes it is necessary to disable the translator within a validator. To archive this you can use the
setDisableTranslator() method, which accepts a boolean parameter, and translatorIsDisabled()
to get the set value.
It is also possible to use a translator instead of setting own messages with setMessage(). But doing so, you should
keep in mind, that the translator works also on messages you set your own.
Alnum
Devuelve true si y sólo si $valor contiene caracteres alfanuméricos únicamente. Este validador incluye una opción
para considerar también al espacio en blanco como caracter válido.
Nota
Los caracteres alfabéticos significan caracteres que componen palabras en cada idioma. Sin embargo, el
alfabeto inglés es tratado como caracteres alfabéticos en los siguientes idiomas: chino, japonés, coreano. El
lenguaje es especificado por Zend_Locale.
Alpha
Devuelve true si y sólo si $valor sólo contiene caracteres alfabéticos. Este validador incluye una opción para
considerar también al espacio en blanco como caracter válido.
1254
Zend_Validate
Barcode
Este validador es instanciado con un tipo de código de barras contra el valor del código de barras que quiere validar. En
la actualidad acepta los tipos de código de barras "UPC-A" (Universal Product Code) y "EAN-13" (European Article
Number), además el método isValid() devuelve verdadero si y sólo si la entrada valida satisfactoriamente contra
el algoritmo de validación del código de barras. Antes de enviar los datos de entrada al validador, debe asegurarse de
eliminar todos los caracteres distintos a los dígitos cero a nueve (0-9).
Between
Devuelve true si y sólo si $valor está entre los valores límites mínimo y máximo. La comparación es inclusiva por
defecto ($valor puede ser igual a una valor límite), aunque esto puede ser anulado a fin de hacer una comparación
estricta, donde $valor debe ser estrictamente mayor al mínimo y estrictamente menor al máximo.
Ccnum
Devuelve true si y sólo si $valor sigue el algoritmo Luhn (mod-10 checksum) para tarjetas de crédito.
Date
Devuelve true si y sólo si $valor es una fecha válida en el formato YYYY-MM-DD (AAAA-MM-DD). Si se usa la
opción locale entonces la fecha será validada de acuerdo a lo establecido para ese locale. Además, si se establece
la opción format ese formato se utiliza para la validación. Para más detalles acerca de los parámetros opcionales
ver en: Zend_Date::isDate().
Basic usage
An example of basic usage of the validators:
if ($validator->isValid($emailaddress)) {
// email address appears to be valid
} else {
// email address is invalid; print the reasons
foreach ($validator->getMessages() as $message) {
echo "$message\n";
}
}
1255
Zend_Validate
The above will test that a given email address is in the database table. If no record is found containing the value of
$emailaddress in the specified column, then an error message is displayed.
The above will test that a given username is not in the database table. If a record is found containing the value of
$username in the specified column, then an error message is displayed.
Excluding records
Zend_Validate_Db_RecordExists and Zend_Validate_Db_NoRecordExists also provide a means
to test the database, excluding a part of the table, either by providing a where clause as a string, or an array with the
keys "field" and "value".
When providing an array for the exclude clause, the != operator is used, so you can check the rest of a table for a
value before altering a record (for example on a user profile form)
if ($validator->isValid($username)) {
// username appears to be valid
} else {
// username is invalid; print the reason
$messages = $validator->getMessages();
foreach ($messages as $message) {
echo "$message\n";
}
1256
Zend_Validate
The above example will check the table to ensure no records other than the one where id = $user_id contains
the value $username.
You can also provide a string to the exclude clause so you can use an operator other than !=. This can be useful for
testing against composite keys.
$post_id = $post->getId();
$clause = $db->quoteInto('post_id = ?', $category_id);
$validator = new Zend_Validate_Db_RecordExists(
array(
'table' => 'posts_categories',
'field' => 'post_id',
'exclude' => $clause
)
);
if ($validator->isValid($username)) {
// username appears to be valid
} else {
// username is invalid; print the reason
$messages = $validator->getMessages();
foreach ($messages as $message) {
echo "$message\n";
}
}
The above example will check the posts_categories table to ensure that a record with the post_id has a value
matching $category_id
Database Adapters
You can also specify an adapter. This will allow you to work with applications using multiple database adapters, or
where you have not set a default adapter. As in the example below:
Database Schemas
You can specify a schema within your database for adapters such as PostgreSQL and DB/2 by simply supplying an
array with table and schema keys. As in the example below:
1257
Zend_Validate
Digits
Devuelve true si y sólo si $valor contiene solamente dígitos.
Dirección de Email
Zend_Validate_EmailAddress Le permite validar una dirección de email. El validador primero divide la
dirección de email en la parte local @ nombre de host e intenta igualar a estos contra especificaciones conocidas para
direcciones y nombres de host para el correo electrónico.
Utilización básica
Esto coincide con el correo electrónico $email y si fracasa, alimenta $validator->getMessages() con
mensajes de error útiles.
Algunos formatos obsoletos de email actualmente no validan (por ejemplo los retornos de carro o "\" un caracter en
una dirección de correo electrónico).
La parte nombre de host de una dirección de correo es validado contra Zend_Validate_Hostname. Por defecto
sólo son aceptados nombres de host DNS de la forma domain.com, aunque si lo desea también puede aceptar
direcciones IP y nombres de host locales.
Para ello necesita instanciar a Zend_Validate_EmailAddress pasando un parámetro para indicar el tipo de
nombres de host que quiere aceptar. Más detalles están incluidos en Zend_Validate_EmailAddress, aunque
abajo hay un ejemplo de cómo aceptar tanto nombres de host DNS y locales:
1258
Zend_Validate
Zend_Validate_Hostname::ALLOW_LOCAL);
if ($validator->isValid($email)) {
// email parece ser válido
} else {
// email es inválido; muestre las razones
foreach ($validator->getMessages() as $message) {
echo "$message\n";
}
}
Sólo porque una dirección de correo electrónico está en el formato correcto, no necesariamente significa que esa
dirección de correo electrónico existe realmente. Para ayudar a resolver este problema, puede usar la validación MX
para comprobar si existe una entrada MX (email) en el registro DNS para correo electrónico en ese nombre de host.
Esto le dice que el nombre de host acepta email, pero no le dice si la dirección de correo electrónico exacta es válida
en si misma.
La comprobación MX no está activada por defecto y en este momento es soportada sólo por plataformas UNIX. Para
habilitar el control MX puede pasar un segundo parámetro al constructor Zend_Validate_EmailAddress.
Alternativamente, para activar o desactivar la validación MX puede pasar true o false a $validator-
>setValidateMx().
Al habilitarlo, se usarán las funciones de red para comprobar la presencia de un registro MX en el nombre de host de
la dirección de correo electrónico que desea validar. Tenga en cuenta esto probablemente hará más lento su script.
$validator->hostnameValidator->setValidateIdn(false);
Tenga en cuenta que los IDNs se validarán solo si usted permite que nombres de host DNS sean validados.
Por defecto, un nombre de host se cotejará con una lista conocida de TLDs. Está activado por defecto, aunque
puede deshabilitarlo cambiando el ajuste a través del objeto interno Zend_Validate_Hostname que existe en
Zend_Validate_EmailAddress.
$validator->hostnameValidator->setValidateTld(false);
1259
Zend_Validate
Tenga en cuenta que los TLDs se validarán solo si usted permite que nombres de host DNS sean validados.
Float
Devuelve true si y sólo si $value es un valor de punto flotante. Desde Zend Framework 1.8 toma en cuenta
la localizacion actual del navegador, las variables o el uso. Puede usar get/setLocale para cambiar la configuracion
regional o crear una instancia para este validador
GreaterThan
Devuelve true si y sólo si $valor es mayor al límite mínimo.
Hex
Devuelve true si y sólo si $valor contiene caracteres hexadecimales (0-9 y A-F).
Uso básico
También se puede encontrar coincidencias de direcciones IP, nombres de host locales, o una combinación de todos los
tipos permitidos. Esto puede hacerse pasando un parámetro a Zend_Validate_Hostname cuando lo instancia. El
parámetro debe ser un entero que determina qué tipos de nombres de host están permitidos. Se recomienda el uso de
las constantes de Zend_Validate_Hostname para hacerlo.
Las constantes de Zend_Validate_Hostname son: ALLOW_DNS para permitir sólo nombres de host DNS,
ALLOW_IP para permitir direcciones IP, ALLOW_LOCAL para permitir nombres de host de la red local, y ALLOW_ALL
para permitir todos estos tres tipos. Para comprobar que direcciones IP puede utilizar, vea el siguiente ejemplo:
1260
Zend_Validate
if ($validator->isValid($hostname)) {
// hostname parece ser válido
} else {
// hostname es inválido; muestre las razones
foreach ($validator->getMessages() as $message) {
echo "$message\n";
}
}
Usando ALLOW_ALL para aceptar todos los tipos de nombres de host, también puede combinar estos tipos
para realizar combinaciones. Por ejemplo, para aceptar nombres de host DNS y locales, instancie el objeto
Zend_Validate_Hostname como:
Algunos (ccTLD), es decir países "Country Code Top Level Domains" , como 'de' (Alemania), aceptan caracteres
internacionales como nombres de dominio. Estos son conocidos como Nombres de Dominio Internacionales (IDN,
por sus siglas en inglés). Se puede buscar una coincidencia de estos dominios con Zend_Validate_Hostname, a través
de caracteres extendidos que se utilizan en el proceso de validación.
• at (Austria)
• ch (Suiza)
• li (Liechtenstein)
• de (Alemania)
• fi (Finlandia)
• hu (Hungría)
• no (Noruega)
• se (Suecia)
Cotejar dominios IDN es tan simple como usar el validador estándar Hostname, ya que este viene habilitado
por defecto. Si desea desactivar la validación IDN, se puede hacer ya sea pasando un parámetro al constructor
Zend_Validate_Hostname o a través del método $validator->setValidateIdn().
$validator =
new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_DNS, false);
1261
Zend_Validate
Tenga en cuenta que una validación IDN solo se realizará si tiene habilidada la validación para nombres de host DNS.
Por defecto un nombre de host se cotejará con una lista de TLDs conocidos. Si esta funcionalidad no es necesaria,
puede ser desactivada en la misma forma que deshabilita el soporte IDN. Puede deshabilitar la validación TLD pasando
un tercer parámetro al constructor Zend_Validate_Hostname. En el siguiente ejemplo estamos dando respaldo a la
validación IDN a través del segundo parámetro.
$validator =
new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_DNS,
true,
false);
Tenga en cuenta que una validación de TLDs solo se realizará si tiene habilidada la validación para nombres de host
DNS.
Iban
Returns true if and only if $value contains a valid IBAN (International Bank Account Number). IBAN numbers
are validated against the country where they are used and by a checksum.
There are two ways to validate IBAN numbers. As first way you can give a locale which represents a country. Any
given IBAN number will then be validated against this country.
This should be done when you want to validate IBAN numbers for a single countries. The simpler way of validation
is not to give a locale like shown in the next example.
But this shows one big problem: When you have to accept only IBAN numbers from one single country, for example
france, then IBAN numbers from other countries would also be valid. Therefor just remember: When you have to
1262
Zend_Validate
validate a IBAN number against a defined country you should give the locale. And when you accept all IBAN numbers
regardless of any country omit the locale for simplicity.
InArray
Devuelve true si y sólo si $valor se encuentra en un array, y si la opción es estricta entonces también verificará
el tipo de dato de $valor.
Int
Returns true if and only if $value is a valid integer. Since Zend Framework 1.8 this validator takes into account
the actual locale from browser, environment or application wide set locale. You can of course use the get/setLocale
accessors to change the used locale or give it while creating a instance of this validator.
Ip
Devuelve true si y sólo si $valor es una dirección IP válida.
LessThan
Devuelve true si y sólo si $valor es menor al límite máximo.
NotEmpty
Devuelve true si y sólo si $valor no es vacío.
Regex
Devuelve true si y sólo si $valor coincide con el patrón de una expresión regular.
Sitemap Validators
The following validators conform to the Sitemap XML protocol [http://www.sitemaps.org/protocol.php].
Sitemap_Changefreq
Validates whether a string is valid for using as a 'changefreq' element in a Sitemap XML document. Valid values are:
'always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', or 'never'.
Returns TRUE if and only if the value is a string and is equal to one of the frequencies specified above.
Sitemap_Lastmod
Validates whether a string is valid for using as a 'lastmod' element in a Sitemap XML document. The lastmod element
should contain a W3C date string, optionally discarding information about time.
Returns TRUE if and only if the given value is a string and is valid according to the protocol.
1263
Zend_Validate
$validator->isValid('1999-11-11T22:23:52-02:00'); // true
$validator->isValid('2008-05-12T00:42:52+02:00'); // true
$validator->isValid('1999-11-11'); // true
$validator->isValid('2008-05-12'); // true
$validator->isValid('1999-11-11t22:23:52-02:00'); // false
$validator->isValid('2008-05-12T00:42:60+02:00'); // false
$validator->isValid('1999-13-11'); // false
$validator->isValid('2008-05-32'); // false
$validator->isValid('yesterday'); // false
Sitemap_Loc
Validates whether a string is valid for using as a 'loc' element in a Sitemap XML document. This uses
Zend_Form::check() internally. Read more at URI Validation.
Sitemap_Priority
Validates whether a value is valid for using as a 'priority' element in a Sitemap XML document. The value should be
a decimal between 0.0 and 1.0. This validator accepts both numeric values and string values.
$validator->isValid('0.1'); // true
$validator->isValid('0.789'); // true
$validator->isValid(0.8); // true
$validator->isValid(1.0); // true
$validator->isValid('1.1'); // false
$validator->isValid('-0.4'); // false
$validator->isValid(1.00001); // false
$validator->isValid(0xFF); // false
$validator->isValid('foo'); // false
StringLength
Devuelve true si y sólo si la longitud del string $valor es por lo menos un mínimo y no mayor a un máximo (cuando
la opción max no es NULL). Desde la versión 1.5.0, el método setMin() lanza una excepción si la longitud mínima
tiene un valor mayor que la longitud máxima establecida, y el método setMax() lanza una excepción si la longitud
máxima se fija a un valor inferior que la longitud mínima establecida. Desde la versión 1.0.2, esta clase soporta UTF-8
y a otras codificaciones, basado en el valor actual de: iconv.internal_encoding [http://www.php.net/manual/
en/ref.iconv.php#iconv.configuration].
Cadenas de Validadores
Frecuentemente deben aplicarse múltiples validaciones a algún valor en un orden particular. El siguiente código
demuestra una forma de resolver el ejemplo de la introducción, donde el nombre de usuario debe tener entre 6 y 12
caracteres alfanuméricos.
1264
Zend_Validate
// Valida el username
if ($validatorChain->isValid($username)) {
// username pasó la validación
} else {
// username falló en la validación; muestre las razones
foreach ($validatorChain->getMessages() as $message) {
echo "$message\n";
}
}
Los validadores se ejecutan en el orden en que se agregaron a Zend_Validate. En el ejemplo anterior, el nombre de
usuario, primero se comprueba que su longitud esté entre 6 y 12 caracteres y luego se controla para garantizar que sólo
contiene caracteres alfanuméricos. La segunda validación; de caracteres alfanuméricos; se realiza independientemente
de que la primera validación; de longitud entre 6 y 12 caracteres; tenga éxito. Esto significa que si ambas validaciones
fallan, getMessages() devolverá mensajes de fracaso desde ambos validadores.
En algunos casos tiene sentido detener la cadena de validación si falla alguno de los procesos de validación.
Zend_Validate acepta tales casos pasando como segundo parámetro el método addValidator(). Poniendo
$breakChainOnFailure a true, el validador agregado quebrará la cadena de ejecución por el fracaso, que evita
correr cualquier otra validación que se decida que es innecesaria o inapropiada para la situación. Si el ejemplo anterior
fue escrito como sigue, entonces el sistema de validación alfanumérica no se ejecutará si falla la longitud del string
de validación:
Cualquier objeto que implemente Zend_Validate_Interface puede ser utilizado en una cadena de validación.
Escribiendo Validadores
Zend_Validate provee un conjunto de validadores que suelen necesitarse, pero inevitablemente, los
desarrolladores quiere escribir sus propios validadores personalizados para sus necesidades particulares. La tarea de
escribir un validador personalizado se describe en esta sección.
Zend_Validate_Interface define tres métodos, isValid(), getMessages(), y getErrors(), que pueden ser
implementadas por clases de usuario a fin de crear objetos de validación personalizados. Un objeto que
implementa una interfaz Zend_Validate_Interface puede añadirse a una cadena de validación con
Zend_Validate::addValidator(). Tales objetos también pueden ser usados con Zend_Filter_Input.
De la descripción anterior de Zend_Validate_Interface, podrá inferir que las clases de validación que
proporciona Zend Framework devuelven un valor booleano para cuando un valor se valida satisfactoriamente o no.
También proporcionan información sobre por qué un valor falló en la validación. La disponibilidad de las razones
para los fracasos de validación puede ser valiosa para una aplicación por diversos motivos, tales como proporcionar
estadísticas para análisis de usabilidad.
1265
Zend_Validate
extienda Zend_Validate_Abstract. En la extensión de la clase deberá aplicar la lógica del método isValid()
y definir las variables y plantillas de mensajes que correspondan a los tipos de fallos de validación que puedan suceder.
Si falla un valor en su test de validación, entonces isValid() deberá devolver false. Si el valor pasa su test de
validación, entonces isValid() deberá devolver true.
En general, el método isValid() no debería arrojar excepciones, salvo que sea imposible determinar si el valor
de entrada es válido o no. Algunos ejemplos de casos razonables para lanzar una excepción podría ser si un archivo
no puede abrirse, que un servidor LDAP no pudiera ser contactado, o una conexión a una base de datos no estuviera
disponible. Estos son casos en los que puede ser necesario determinar el éxito o fracaso de la validación.
if (!is_float($value)) {
$this->_error();
return false;
}
return true;
}
}
La clase define una plantilla para su único mensaje de fallo de validación, que incluye el mágico parámetro %value%.
La llamada a _setValue() prepara al objeto para insertar automáticamente en el mensaje de fallo al valor probado,
si éste falla en la validación. La llamada a _error() sigue las pistas para establecer una razón por el fracaso de la
validación. Dado que esta clase sólo define un mensaje de fallo, no es necesario darle a _error() el nombre de la
plantilla del mensaje de fallo.
1266
Zend_Validate
public $minimum = 0;
public $maximum = 100;
if (!is_numeric($value)) {
$this->_error(self::MSG_NUMERIC);
return false;
}
return true;
}
}
Las propiedades públicas $minimum y $maximum se han establecido para proporcionar los límites mínimo y
máximo, respectivamente, de un valor a validar. La clase también define dos variables de mensajes que corresponden
a las propiedades públicas y permiten usar min y max en plantillas de mensajes como parámetros mágicos, al igual
que con value.
Tenga en cuenta que si cualquiera de las comprobaciones de validación falla en isValid(), ya está preparado
un mensaje apropiado, y el método inmediatamente devuelve false. Estas reglas de validación son por lo tanto
secuencialmente dependientes. Es decir, si uno de los tests falla, no hay necesidad de poner a prueba las posteriores
reglas de validación. Sin embargo, esta necesidad no será el caso. El siguiente ejemplo ilustra cómo escribir una clase
1267
Zend_Validate
con reglas de validación independientes, donde el objeto validación puede devolver múltiples razones por las cuales
fracasó un intento de validación en particular.
Ejemplo 59.5. Validación con Condiciones Independientes, Múltiples Razones del Fracaso
Considere escribir una clase de validación y control de contraseñas - cuando es necesario que un usuario elija una
contraseña que cumple determinados criterios para ayudar a tener cuentas de usuario seguras. Supongamos que la
seguridad de la contraseña aplica criterios que fuerzan a lo siguiente:
$isValid = true;
if (strlen($value) < 8) {
$this->_error(self::LENGTH);
$isValid = false;
}
if (!preg_match('/[A-Z]/', $value)) {
$this->_error(self::UPPER);
$isValid = false;
}
if (!preg_match('/[a-z]/', $value)) {
$this->_error(self::LOWER);
$isValid = false;
}
1268
Zend_Validate
if (!preg_match('/\d/', $value)) {
$this->_error(self::DIGIT);
$isValid = false;
}
return $isValid;
}
}
Las cuatro pruebas de criterio en isValid() no devuelven inmediatamente false. Esto permite a la clase de
validación a proporcionar todas las razones por las que la contraseña de entrada no cumplió los requisitos de validación.
Si, por ejemplo, un usuario ingresara el string "#$%" como contraseña, isValid() causaría que los cuatro mensajes
de fracaso de validación sean devueltos por un llamado posterior a getMessages().
Validation Messages
Each validator which is based on Zend_Validate provides one or multiple messages in the case of a failed
validation. You can use this information for setting own messages or when you have to translate the messages a
validator can return. The following table lists all available messages which are returned by each validator.
1269
Zend_Validate
1270
Zend_Validate
1271
Zend_Validate
1272
Zend_Validate
1273
Zend_Validate
Additionally you can retrieve all message templates of a validator with the method getMessageTemplates(). It
returns you an array with the messages a validator could return in the case of a failed validation.
To get the actual set size use Zend_Validate::getMessageLength(). If it is -1, then the returned message
will not be truncated. This is default behaviour.
To limit the returned message size use Zend_Validate::setMessageLength(). Set it to any integer size you
need. When the returned message exceeds the set size, then the message will be truncated and the string '...' will be
added instead of the rest of the message.
Zend_Validate::setMessageLength(100);
Nota
Note that the set message length is used for all validators, even for self defined ones as long as they extend
Zend_Validate_Abstract.
1274
Capítulo 60. Zend_Version
Getting the Zend Framework Version
Zend_Version provides a class constant Zend_Version::VERSION that contains a string identifying the
version number of your Zend Framework installation. Zend_Version::VERSION might contain "1.7.4", for
example.
// returns -1, 0 or 1
$cmp = Zend_Version::compareVersion('2.0.0');
1275
1276
Capítulo 61. Zend_View
Introduction
Zend_View is a class for working with the "view" portion of the model-view-controller pattern. That is, it exists
to help keep the view script separate from the model and controller scripts. It provides a system of helpers, output
filters, and variable escaping.
Zend_View is template system agnostic; you may use PHP as your template language, or create instances of other
template systems and manipulate them within your view script.
Essentially, using Zend_View happens in two major steps: 1. Your controller script creates an instance of
Zend_View and assigns variables to that instance. 2. The controller tells the Zend_View to render a particular view,
thereby handing control over the view script, which generates the view output.
Controller Script
As a simple example, let us say your controller has a list of book data that it wants to have rendered by a view. The
controller script might look something like this:
// use a model to get the data for book authors and titles.
$data = array(
array(
'author' => 'Hernando de Soto',
'title' => 'The Mystery of Capitalism'
),
array(
'author' => 'Henry Hazlitt',
'title' => 'Economics in One Lesson'
),
array(
'author' => 'Milton Friedman',
'title' => 'Free to Choose'
)
);
View Script
Now we need the associated view script, "booklist.php". This is a PHP script like any other, with one exception: it
executes inside the scope of the Zend_View instance, which means that references to $this point to the Zend_View
instance properties and methods. (Variables assigned to the instance by the controller are public properties of the
Zend_View instance). Thus, a very basic view script could look like this:
1277
Zend_View
if ($this->books): ?>
</table>
<?php endif;?>
Note how we use the "escape()" method to apply output escaping to variables.
Options
Zend_View has several options that may be set to configure the behaviour of your view scripts.
• basePath: indicate a base path from which to set the script, helper, and filter path. It assumes a directory structure
of:
base/path/
helpers/
filters/
scripts/
This may be set via setBasePath(), addBasePath(), or the basePath option to the constructor.
• encoding: indicate the character encoding to use with htmlentities(), htmlspecialchars(), and other
operations. Defaults to ISO-8859-1 (latin1). May be set via setEncoding() or the encoding option to the
constructor.
• escape: indicate a callback to be used by escape(). May be set via setEscape() or the escape option
to the constructor.
• filter: indicate a filter to use after rendering a view script. May be set via setFilter(), addFilter(),
or the filter option to the constructor.
• strictVars: force Zend_View to emit notices and warnings when uninitialized view variables are accessed.
This may be set by calling strictVars(true) or passing the strictVars option to the constructor.
1278
Zend_View
In previous versions, we often recommended using short tags (<? and <?=), as they make the view scripts slightly
less verbose. However, the default for the php.ini short_open_tag setting is typically off in production or on
shared hosts -- making their use not terribly portable. If you use template XML in view scripts, short open tags will
cause the templates to fail validation. Finally, if you use short tags when short_open_tag is off, the view scripts
will either cause errors or simply echo PHP code back to the viewer.
If, despite these warnings, you wish to use short tags but they are disabled, you have two options:
This will only be possible if you are allowed to create and utilize .htaccess files. This directive can also be
added to your httpd.conf file.
• Enable an optional stream wrapper to convert short tags to long tags on the fly:
$view->setUseStreamWrapper(true);
This registers Zend_View_Stream as a stream wrapper for view scripts, and will ensure that your code continues
to work as if short tags were enabled.
Utility Accessors
Typically, you'll only ever need to call on assign(), render(), or one of the methods for setting/adding filter,
helper, and script paths. However, if you wish to extend Zend_View yourself, or need access to some of its internals,
a number of accessors exist:
• clearVars() will clear all assigned variables; useful when you wish to re-use a view object, but want to control
what variables are available.
• getHelperPath($helper) will retrieve the resolved path to the named helper class.
1279
Zend_View
• getFilterPath($filter) will retrieve the resolved path to the named filter class.
Controller Scripts
The controller is where you instantiate and configure Zend_View. You then assign variables to the view, and tell
the view to render output using a particular script.
Assigning Variables
Your controller script should assign necessary variables to the view before it hands over control to the view script.
Normally, you can do assignments one at a time by assigning to property names of the view instance:
However, this can be tedious when you have already collected the values to be assigned into an array or object.
The assign() method lets you assign from an array or object "in bulk". The following examples have the same effect
as the above one-by-one property assignments.
Alternatively, you can use the assign method to assign one-by-one by passing a string variable name, and then the
variable value.
1280
Zend_View
$view->assign('c', "Sea");
Obviously, your view scripts are probably located elsewhere. To tell Zend_View where it should look for view
scripts, use the setScriptPath() method.
In fact, you can "stack" paths using the addScriptPath() method. As you add paths to the stack, Zend_View will look
at the most-recently-added path for the requested view script. This allows you override default views with custom
views so that you may create custom "themes" or "skins" for some views, while leaving others alone.
// $_GET['foo'] == '../../../etc'
1281
Zend_View
$view->addScriptPath($_GET['foo']);
$view->render('passwd');
While this example is contrived, it does clearly show the potential issue. If you must rely on user input to
set your script path, properly filter the input and check to ensure it exists under paths controlled by your
application.
View Scripts
Once your controller has assigned variables and called render(), Zend_View then includes the requested view
script and executes it "inside" the scope of the Zend_View instance. Therefore, in your view scripts, references to
$this actually point to the Zend_View instance itself.
Variables assigned to the view from the controller are referred to as instance properties. For example, if the controller
were to assign a variable 'something', you would refer to it as $this->something in the view script. (This allows you to
keep track of which values were assigned to the script, and which are internal to the script itself.)
By way of reminder, here is the example view script from the Zend_View introduction.
</table>
<?php endif;?>
Escaping Output
One of the most important tasks to perform in a view script is to make sure that output is escaped properly; among
other things, this helps to avoid cross-site scripting attacks. Unless you are using a function, method, or helper that
does escaping on its own, you should always escape variables when you output them.
Zend_View comes with a method called escape() that does such escaping for you.
1282
Zend_View
By default, the escape() method uses the PHP htmlspecialchars() function for escaping. However, depending on your
environment, you may wish for escaping to occur in a different way. Use the setEscape() method at the controller level
to tell Zend_View what escaping callback to use.
The callback function or method should take the value to be escaped as its first parameter, and all other parameters
should be optional.
include_once 'template.inc';
$tpl = new Template();
if ($this->books) {
$tpl->setFile(array(
"booklist" => "booklist.tpl",
"eachbook" => "eachbook.tpl",
));
1283
Zend_View
$tpl->set_var('title', $this->escape($val['title']);
$tpl->parse("books", "eachbook", true);
}
$tpl->pparse("output", "booklist");
} else {
$tpl->setFile("nobooks", "nobooks.tpl")
$tpl->pparse("output", "nobooks");
}
/**
* Return the actual template engine object
*/
public function getEngine();
/**
* Set the path to view scripts/templates
*/
public function setScriptPath($path);
/**
* Set a base path to all view resources
*/
public function setBasePath($path, $prefix = 'Zend_View');
/**
* Add an additional base path to view resources
1284
Zend_View
*/
public function addBasePath($path, $prefix = 'Zend_View');
/**
* Retrieve the current script paths
*/
public function getScriptPaths();
/**
* Overloading methods for assigning template variables as object
* properties
*/
public function __set($key, $value);
public function __isset($key);
public function __unset($key);
/**
* Manual assignment of template variables, or ability to assign
* multiple variables en masse.
*/
public function assign($spec, $value = null);
/**
* Unset all assigned template variables
*/
public function clearVars();
/**
* Render the template named $name
*/
public function render($name);
Using this interface, it becomes relatively easy to wrap a third-party template engine as a Zend_View-compatible
class. As an example, the following is one potential wrapper for Smarty:
/**
* Constructor
*
* @param string $tmplPath
* @param array $extraParams
* @return void
*/
public function __construct($tmplPath = null, $extraParams = array())
{
$this->_smarty = new Smarty;
1285
Zend_View
/**
* Return the template engine object
*
* @return Smarty
*/
public function getEngine()
{
return $this->_smarty;
}
/**
* Set the path to the templates
*
* @param string $path The directory to set as the path.
* @return void
*/
public function setScriptPath($path)
{
if (is_readable($path)) {
$this->_smarty->template_dir = $path;
return;
}
/**
* Retrieve the current template directory
*
* @return string
*/
public function getScriptPaths()
{
return array($this->_smarty->template_dir);
}
/**
* Alias for setScriptPath
*
* @param string $path
* @param string $prefix Unused
* @return void
*/
public function setBasePath($path, $prefix = 'Zend_View')
1286
Zend_View
{
return $this->setScriptPath($path);
}
/**
* Alias for setScriptPath
*
* @param string $path
* @param string $prefix Unused
* @return void
*/
public function addBasePath($path, $prefix = 'Zend_View')
{
return $this->setScriptPath($path);
}
/**
* Assign a variable to the template
*
* @param string $key The variable name.
* @param mixed $val The variable value.
* @return void
*/
public function __set($key, $val)
{
$this->_smarty->assign($key, $val);
}
/**
* Allows testing with empty() and isset() to work
*
* @param string $key
* @return boolean
*/
public function __isset($key)
{
return (null !== $this->_smarty->get_template_vars($key));
}
/**
* Allows unset() on object properties to work
*
* @param string $key
* @return void
*/
public function __unset($key)
{
$this->_smarty->clear_assign($key);
}
/**
* Assign variables to the template
*
* Allows setting a specific key to the specified value, OR passing
1287
Zend_View
$this->_smarty->assign($spec, $value);
}
/**
* Clear all assigned variables
*
* Clears all variables assigned to Zend_View either via
* {@link assign()} or property overloading
* ({@link __get()}/{@link __set()}).
*
* @return void
*/
public function clearVars()
{
$this->_smarty->clear_all_assign();
}
/**
* Processes a template and returns the output.
*
* @param string $name The template to process.
* @return string The output.
*/
public function render($name)
{
return $this->_smarty->fetch($name);
}
}
In this example, you would instantiate the Zend_View_Smarty class instead of Zend_View, and then use it in
roughly the same fashion as Zend_View:
1288
Zend_View
->setViewBasePathSpec($view->_smarty->template_dir)
->setViewScriptPathSpec(':controller/:action.:suffix')
->setViewScriptPathNoControllerSpec(':action.:suffix')
->setViewSuffix('tpl');
View Helpers
In your view scripts, often it is necessary to perform certain complex functions over and over: e.g., formatting a date,
generating form elements, or displaying action links. You can use helper classes to perform these behaviors for you.
A helper is simply a class. Let's say we want a helper named 'fooBar'. By default, the class is prefixed
with 'Zend_View_Helper_' (you can specify a custom prefix when setting a helper path), and the last segment
of the class name is the helper name; this segment should be TitleCapped; the full class name is then:
Zend_View_Helper_FooBar. This class should contain at the minimum a single method, named after the helper,
and camelCased: fooBar().
To use a helper in your view script, call it using $this->helperName(). Behind the scenes, Zend_View will
load the Zend_View_Helper_HelperName class, create an object instance of it, and call its helperName()
1289
Zend_View
method. The object instance is persistent within the Zend_View instance, and is reused for all future calls to $this-
>helperName().
Initial Helpers
Zend_View comes with an initial set of helper classes, most of which relate to form element generation and perform
the appropriate output escaping automatically. In addition, there are helpers for creating route-based URLs and HTML
lists, as well as declaring variables. The currently shipped helpers include:
• declareVars(): Primarily for use when using strictVars(), this helper can be used to declare template
variables that may or may not already be set in the view object, as well as to set default values. Arrays passed as
arguments to the method will be used to set default values; otherwise, if the variable does not exist, it is set to an
empty string.
• form($name, $attribs, $content): Generates an XHTML form. All $attribs are escaped and
rendered as XHTML attributes of the form tag. If $content is present and not a boolean false, then that content
is rendered within the start and close form tags; if $content is a boolean false (the default), only the opening
form tag is generated.
By default, when no $value is provided and no $options are present, '0' is assumed to be the unchecked value, and
'1' the checked value. If a $value is passed, but no $options are present, the checked value is assumed to be the
value passed.
$options should be an array. If the array is indexed, the first value is the checked value, and the second the unchecked
value; all other values are ignored. You may also pass an associative array with the keys 'checked' and 'unChecked'.
If $options has been passed, if $value matches the checked value, then the element will be marked as checked. You
may also mark the element as checked or unchecked by passing a boolean value for the attribute 'checked'.
1290
Zend_View
In all cases, the markup prepends a hidden element with the unchecked value; this way, if the value is unchecked,
you will still get a valid value returned to your form.
• formErrors($errors, $options): Generates an XHTML unordered list to show errors. $errors should
be a string or an array of strings; $options should be any attributes you want placed in the opening list tag.
You can specify alternate opening, closing, and separator content when rendering the errors by calling several
methods on the helper:
• formLabel($name, $value, $attribs): Creates a <label> element, setting the for attribute to $name,
and the actual label text to $value. If disable is passed in attribs, nothing will be returned.
1291
Zend_View
• url($urlOptions, $name, $reset): Creates a URL string based on a named route. $urlOptions
should be an associative array of key/value pairs used by the particular route.
• htmlList($items, $ordered, $attribs, $escape): generates unordered and ordered lists based
on the $items passed to it. If $items is a multidimensional array, a nested list will be built. If the $escape
flag is true (default), individual items will be escaped using the view objects registered escaping mechanisms; pass
a false value if you want to allow markup in your lists.
Using these in your view scripts is very easy, here is an example. Note that you all you need to do is call them; they
will load and instantiate themselves as they are needed.
The resulting output from the view script will look something like this:
1292
Zend_View
Actions that result in a _forward() or redirect are considered invalid, and will return an empty string.
The API for the Action view helper follows that of most MVC components that invoke controller actions:
action($action, $controller, $module = null, array $params = array()). $action
and $controller are required; if no module is specified, the default module is assumed.
BaseUrl Helper
While most URLs generated by the framework have the base URL prepended automatically, developers will need to
prepend the base URL to their own URLs in order for paths to resources to be correct.
/*
* The following assume that the base URL of the page/application is "/mypage".
*/
/*
* Prints:
* <base href="/mypage/" />
*/
<base href="<?php echo $this->baseUrl(); ?>" />
/*
* Prints:
* <link rel="stylesheet" type="text/css" href="/mypage/css/base.css" />
*/
1293
Zend_View
Nota
For simplicity's sake, we strip out the entry PHP file (e.g., "index.php") from the base URL that was
contained in Zend_Controller. However, in some situations this may cause a problem. If one occurs,
use $this->getHelper('BaseUrl')->setBaseUrl() to set your own BaseUrl.
Cycle Helper
The Cycle helper is used to alternate a set of values.
The output
<tr style="background-color:'#F0F0F0'">
<td>First</td>
</tr>
<tr style="background-color:'#FFFFFF'">
<td>Second</td>
</tr>
1294
Zend_View
<?php endforeach;?>
Partial Helper
The Partial view helper is used to render a specified template within its own variable scope. The primary use is
for reusable template fragments with which you do not need to worry about variable name clashes. Additionally, they
allow you to specify partial view scripts from specific modules.
A sibling to the Partial, the PartialLoop view helper allows you to pass iterable data, and render a partial for
each item.
PartialLoop Counter
The PartialLoop view helper assigns a variable to the view named partialCounter which passes
the current position of the array to the view script. This provides an easy way to have alternating colors on
table rows for example.
You would then call it from your view script using the following:
<ul>
<li>From: Team Framework</li>
<li>Subject: view partials</li>
</ul>
What is a model?
A model used with the Partial view helper can be one of the following:
• Array. If an array is passed, it should be associative, as its key/value pairs are assigned to the view with
keys as view variables.
• Object implementing toArray() method. If an object is passed an has a toArray() method, the results of
toArray() will be assigned to the view object as view variables.
• Standard object. Any other object will assign the results of object_get_vars() (essentially all public
properties of the object) to the view object.
1295
Zend_View
If your model is an object, you may want to have it passed as an object to the partial script, instead of
serializing it to an array of variables. You can do this by setting the 'objectKey' property of the appropriate
helper:
The PartialLoop view helper helps solve this issue. It allows you to pass an iterable item (array or object
implementing Iterator) as the model. It then iterates over this, passing, the items to the partial script as the model.
Items in the iterator may be any model the Partial view helper allows.
$model = array(
array('key' => 'Mammal', 'value' => 'Camel'),
array('key' => 'Bird', 'value' => 'Penguin'),
array('key' => 'Reptile', 'value' => 'Asp'),
array('key' => 'Fish', 'value' => 'Flounder'),
);
In your view script, you could then invoke the PartialLoop helper:
<dl>
<?php echo $this->partialLoop('partialLoop.phtml', $model) ?>
</dl>
<dl>
<dt>Mammal</dt>
<dd>Camel</dd>
<dt>Bird</dt>
1296
Zend_View
<dd>Penguin</dd>
<dt>Reptile</dt>
<dd>Asp</dd>
<dt>Fish</dt>
<dd>Flounder</dd>
</dl>
For instance, if there's a pager partial you wish to use that's in the 'list' module, you could grab it as follows:
In this way, you can re-use partials created specifically for other modules. That said, it's likely a better practice to put
re-usable partials in shared view script paths.
Placeholder Helper
The Placeholder view helper is used to persist content between view scripts and view instances. It also offers
some useful features such as aggregating content, capturing view script content for later use, and adding pre- and post-
text to content (and custom separators for aggregated content).
<?php
echo $this->placeholder('foo');
// outputs "Some text for later"
?>
The Placeholder view helper uses containers that extend ArrayObject, providing a rich featureset for
manipulating arrays. In addition, it offers a variety of methods for formatting the content stored in the container:
• setPrefix($prefix) sets text with which to prefix the content. Use getPrefix() at any time to determine
what the current setting is.
• setPostfix($prefix) sets text with which to append the content. Use getPostfix() at any time to
determine what the current setting is.
1297
Zend_View
• setSeparator($prefix) sets text with which to separate aggregated content. Use getSeparator() at
any time to determine what the current setting is.
• setIndent($prefix) can be used to set an indentation value for content. If an integer is passed, that number
of spaces will be used; if a string is passed, the string will be used. Use getIndent() at any time to determine
what the current setting is.
<?php
echo $this->placeholder('foo');
// outputs as unordered list with pretty indentation
?>
Because the Placeholder container objects extend ArrayObject, you can also assign content to a specific key
in the container easily, instead of simply pushing it into the container. Keys may be accessed either as object properties
or as array keys.
<?php
$foo = $this->placeholder('foo');
echo $foo['bar'];
?>
$type should be one of the Placeholder constants APPEND or SET. If APPEND, captured content is appended
to the list of current content in the placeholder; if SET, captured content is used as the sole value of the placeholder
(potentially replacing any previous content). By default, $type is APPEND.
$key can be used to specify a specific key in the placeholder container to which you want content captured.
captureStart() locks capturing until captureEnd() is called; you cannot nest capturing with the same
placeholder container. Doing so will raise an exception.
• captureEnd() stops capturing content, and places it in the container object according to how
captureStart() was called.
1298
Zend_View
• Doctype
• HeadLink
• HeadMeta
• HeadScript
• HeadStyle
• HeadTitle
• InlineScript
Doctype Helper
Valid HTML and XHTML documents should include a DOCTYPE declaration. Besides being difficult to remember,
these can also affect how certain elements in your document should be rendered (for instance, CDATA escaping in
<script> and <style> elements.
The Doctype helper allows you to specify one of the following types:
• XHTML11
1299
Zend_View
• XHTML1_STRICT
• XHTML1_TRANSITIONAL
• XHTML1_FRAMESET
• XHTML_BASIC1
• HTML4_STRICT
• HTML4_LOOSE
• HTML4_FRAMESET
• HTML5
$doctype = $view->doctype()->getDoctype();
Typically, you'll simply want to know if the doctype is XHTML or not; for this, the isXhtml() method will suffice:
if ($view->doctype()->isXhtml()) {
// do something differently
}
HeadLink Helper
The HTML <link> element is increasingly used for linking a variety of resources for your site: stylesheets, feeds,
favicons, trackbacks, and more. The HeadLink helper provides a simple interface for creating and aggregating these
elements for later retrieval and output in your layout script.
The HeadLink helper has special methods for adding stylesheet links to its stack:
1300
Zend_View
The $media value defaults to 'screen', but may be any valid media value. $conditionalStylesheet is a string
or boolean false, and will be used at rendering time to determine if special comments should be included to prevent
loading of the stylesheet on certain platforms. $extras is an array of any extra values that you want to be added
to the tag.
Additionally, the HeadLink helper has special methods for adding 'alternate' links to its stack:
The headLink() helper method allows specifying all attributes necessary for a <link> element, and allows you
to also specify placement -- whether the new element replaces all others, prepends (top of stack), or appends (end
of stack).
HeadMeta Helper
The HTML <meta> element is used to provide meta information about your HTML document -- typically keywords,
document character set, caching pragmas, etc. Meta tags may be either of the 'http-equiv' or 'name' types, must contain
a 'content' attribute, and can also have either of the 'lang' or 'scheme' modifier attributes.
The HeadMeta helper supports the following methods for setting and adding meta tags:
1301
Zend_View
The $keyValue item is used to define a value for the 'name' or 'http-equiv' key; $content is the value for the
'content' key, and $modifiers is an optional associative array that can contain keys for 'lang' and/or 'scheme'.
You may also set meta tags using the headMeta() helper method, which has the following signature:
headMeta($content, $keyValue, $keyType = 'name', $modifiers = array(),
$placement = 'APPEND'). $keyValue is the content for the key specified in $keyType, which should
be either 'name' or 'http-equiv'. $placement can be either 'SET' (overwrites all previously stored values),
'APPEND' (added to end of stack), or 'PREPEND' (added to top of stack).
HeadMeta overrides each of append(), offsetSet(), prepend(), and set() to enforce usage of the special
methods as listed above. Internally, it stores each item as a stdClass token, which it later serializes using the
itemToString() method. This allows you to perform checks on the items in the stack, and optionally modify these
items by simply modifying the object returned.
For instance, if you wish to specify SEO keywords, you'd be creating a meta name tag with the name 'keywords' and
the content the keywords you wish to associate with your page:
If you wished to set some client-side caching rules, you'd set http-equiv tags with the rules you wish to enforce:
Another popular use for meta tags is setting the content type, character set, and language:
1302
Zend_View
As a final example, an easy way to display a transitional message before a redirect is using a "meta refresh":
When you're ready to place your meta tags in the layout, simply echo the helper:
HeadScript Helper
The HTML <script> element is used to either provide inline client-side scripting elements or link to a remote
resource containing client-side scripting code. The HeadScript helper allows you to manage both.
The HeadScript helper supports the following methods for setting and adding scripts:
In the case of the *File() methods, $src is the remote location of the script to load; this is usually in the form
of a URL or a path. For the *Script() methods, $script is the client-side scripting directives you wish to use
in the element.
// adding scripts
$this->headScript()->appendFile(
'/js/prototype.js',
'text/javascript',
array('conditional' => 'lt IE 7')
);
HeadScript also allows capturing scripts; this can be useful if you want to create the client-side script
programmatically, and then place it elsewhere. The usage for this will be showed in an example below.
1303
Zend_View
Finally, you can also use the headScript() method to quickly add script elements; the signature for this is
headScript($mode = 'FILE', $spec, $placement = 'APPEND'). The $mode is either 'FILE' or
'SCRIPT', depending on if you're linking a script or defining one. $spec is either the script file to link or the script
source itself. $placement should be either 'APPEND', 'PREPEND', or 'SET'.
HeadScript overrides each of append(), offsetSet(), prepend(), and set() to enforce usage of the
special methods as listed above. Internally, it stores each item as a stdClass token, which it later serializes using
the itemToString() method. This allows you to perform checks on the items in the stack, and optionally modify
these items by simply modifying the object returned.
$this->headScript()->setAllowArbitraryAttributes(true);
// adding scripts
$this->headScript()->appendFile('/js/prototype.js')
->appendScript($onloadScript);
Order is often important with client-side scripting; you may need to ensure that libraries are loaded in a specific order
due to dependencies each have; use the various append, prepend, and offsetSet directives to aid in this task:
When you're finally ready to output all scripts in your layout script, simply echo the helper:
1304
Zend_View
• The script will be appended to the stack. If you wish for it to replace the stack or be added to the top, you will need
to pass 'SET' or 'PREPEND', respectively, as the first argument to captureStart().
• The script MIME type is assumed to be 'text/javascript'; if you wish to specify a different type, you will need to
pass it as the second argument to captureStart().
• If you wish to specify any additional attributes for the <script> tag, pass them in an array as the third argument
to captureStart().
HeadStyle Helper
The HTML <style> element is used to include CSS stylesheets inline in the HTML <head> element.
The HeadStyle helper supports the following methods for setting and adding stylesheet declarations:
In all cases, $content is the actual CSS declarations. $attributes are any additional attributes you wish to
provide to the style tag: lang, title, media, or dir are all permissible.
// adding scripts
1305
Zend_View
HeadStyle also allows capturing style declarations; this can be useful if you want to create the declarations
programmatically, and then place them elsewhere. The usage for this will be showed in an example below.
Finally, you can also use the headStyle() method to quickly add declarations elements; the signature for this is
headStyle($content$placement = 'APPEND', $attributes = array()). $placement should
be either 'APPEND', 'PREPEND', or 'SET'.
HeadStyle overrides each of append(), offsetSet(), prepend(), and set() to enforce usage of the
special methods as listed above. Internally, it stores each item as a stdClass token, which it later serializes using
the itemToString() method. This allows you to perform checks on the items in the stack, and optionally modify
these items by simply modifying the object returned.
// adding styles
$this->headStyle()->appendStyle($styles);
Order is very important with CSS; you may need to ensure that declarations are loaded in a specific order due to the
order of the cascade; use the various append, prepend, and offsetSet directives to aid in this task:
// place at end:
$this->headStyle()->appendStyle($finalStyles);
// place at beginning
$this->headStyle()->prependStyle($firstStyles);
When you're finally ready to output all style declarations in your layout script, simply echo the helper:
1306
Zend_View
• The style declarations will be appended to the stack. If you wish for them to replace the stack or be added to the top,
you will need to pass 'SET' or 'PREPEND', respectively, as the first argument to captureStart().
• If you wish to specify any additional attributes for the <style> tag, pass them in an array as the second argument
to captureStart().
HeadTitle Helper
The HTML <title> element is used to provide a title for an HTML document. The HeadTitle helper allows you
to programmatically create and store the title for later retrieval and output.
The HeadTitle helper is a concrete implementation of the Placeholder helper. It overrides the toString()
method to enforce generating a <title> element, and adds a headTitle() method for quick and easy setting
and aggregation of title elements. The signature for that method is headTitle($title, $setType =
'APPEND'); by default, the value is appended to the stack (aggregating title segments), but you may also specify
either 'PREPEND' (place at top of stack) or 'SET' (overwrite stack).
When you're finally ready to render the title in your layout script, simply echo the helper:
1307
Zend_View
All of these helpers share a similar interface. For this reason, this documentation will only contain examples of two
of these helpers.
<object data="/path/to/flash.swf"
type="application/x-shockwave-flash"
classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">
</object>
Additionally you can specify attributes, parameters and content that can be rendered along with the <object>. This
will be demonstrated using the htmlObject helper.
echo $this->htmlObject(
'/path/to/file.ext',
'mime/type',
array(
'attr1' => 'aval1',
'attr2' => 'aval2'
),
array(
'param1' => 'pval1',
'param2' => 'pval2'
),
'some content'
);
/*
This would output:
1308
Zend_View
</object>
*/
InlineScript Helper
The HTML <script> element is used to either provide inline client-side scripting elements or link to a remote
resource containing client-side scripting code. The InlineScript helper allows you to manage both. It is derived
from HeadScript, and any method of that helper is available; however, use the inlineScript() method in place
of headScript().
Some JS libraries need to be included in the HTML head; use HeadScript for those scripts.
JSON Helper
When creating views that return JSON, it's important to also set the appropriate response header. The JSON view
helper does exactly that. In addition, by default, it disables layouts (if currently enabled), as layouts generally aren't
used with JSON responses.
Content-Type: application/json
Most AJAX libraries look for this header when parsing responses to determine how to handle the content.
To keep layouts, the second parameter needs to be boolean TRUE. When the second parameter is an array,
keeping layouts can be achieved by including a keepLayouts key with a value of a boolean TRUE.
1309
Zend_View
Navigation Helpers
The navigation helpers are used for rendering navigational elements from Zend_Navigation_Container instances.
• Breadcrumbs, used for rendering the path to the currently active page.
• Links, used for rendering navigational head links (e.g. <link rel="next" href="..." />)
• Sitemap, used for rendering sitemaps conforming to the Sitemaps XML format [http://www.sitemaps.org/
protocol.php].
• {get|set}Container() gets/sets the navigation container the helper should operate on by default, and
hasContainer() checks if the helper has container registered.
• {get|set}Translator() gets/sets the translator used for translating labels and titles, and {get|
set}UseTranslator() controls whether the translator should be enabled. The method hasTranslator()
checks if the helper has a translator registered.
• __toString(), magic method to ensure that helpers can be rendered by echoing the helper instance directly.
In addition to the method stubs from the interface, the abstract class also implements the following methods:
• {get|set}Indent() gets/set indentation. The setter accepts a String or an int. In the case of an int, the helper
will use the given number of spaces for indentation. I.e., setIndent(4) means 4 initial spaces of indentation.
Indentation can be specified for all helpers except the Sitemap helper.
• {get|set}MinDepth() gets/set the minimum depth a page must have to be included by the helper. Setting
NULL means no minimum depth.
• {get|set}MaxDepth() gets/set the maximum depth a page can have to be included by the helper. Setting
NULL means no maximum depth.
• {get|set}RenderInvisible() gets/set whether to render items that have been marked as invisible or not.
• __call() is used for proxying calls to the container registered in the helper, which means you can call methods
on a helper as if it was a container. See example below.
1310
Zend_View
• findActive($container, $minDepth, $maxDepth) is used for finding the deepest active page in
the given container. If depths are not given, the method will use the values retrieved from getMinDepth() and
getMaxDepth(). The deepest active page must be between $minDepth and $maxDepth inclusively. Returns
an array containing a reference to the found page instance and the depth at which the page was found.
• accept() is used for determining if a page should be accepted when iterating containers. This method checks for
page visibility and verifies that the helper's role is allowed access to the page's resource/privilege.
• static setDefaultAcl() is used for setting a defualt ACL object that will be used by helpers.
• static setDefaultRole() is used for setting a default ACL that will be used by helpers
If a navigation container is not explicitly set in a helper using $helper->setContainer($nav), the helper will
look for a container instance with the key Zend_Navigation in the registry. If a container is not explicitly set
or found in the registry, the helper will create an empty Zend_Navigation container when calling $helper-
>getContainer().
$this->navigation()->addPage(array(
'type' => 'uri',
'label' => 'New page'));
The call above will add a page to the container in the Navigation helper.
The proxy helper will inject its own translator to the helper it proxies to if the proxied helper doesn't already have
a translator.
Nota
There is no translation in the sitemap helper, since there are no page labels or titles involved in an XML
sitemap.
1311
Zend_View
If a page is not accepted by ACL, any descendant page will also be excluded from rendering.
The proxy helper will inject its own ACL and role to the helper it proxies to if the proxied helper doesn't already
have any.
• Unless otherwise is stated in other examples, the user is requesting the URL http://www.example.com/
products/server/faq/, which translates to the page labeled FAQ under Foo Server.
• The assumed ACL and router setup is shown below the container setup.
/*
* Navigation container (config/array)
1312
Zend_View
1313
Zend_View
1314
Zend_View
)
),
array(
'label' => 'Administration',
'module' => 'admin',
'controller' => 'index',
'action' => 'index',
'resource' => 'mvc:admin', // resource
'pages' => array(
array(
'label' => 'Write new article',
'module' => 'admin',
'controller' => 'post',
'aciton' => 'write'
)
)
)
);
// ...or simply:
$view->navigation($container);
// Setup ACL:
$acl = new Zend_Acl();
1315
Zend_View
$acl->addRole(new Zend_Acl_Role('member'));
$acl->addRole(new Zend_Acl_Role('admin'));
$acl->add(new Zend_Acl_Resource('mvc:admin'));
$acl->add(new Zend_Acl_Resource('mvc:community.account'));
$acl->allow('member', 'mvc:community.account');
$acl->allow('admin', null);
Breadcrumbs Helper
Breadcrumbs are used for indicating where in a sitemap a user is currently browsing, and are typically rendered
like this: "You are here: Home > Products > FantasticProduct 1.0". The breadcrumbs helper follows the guidelines
from Breadcrumbs Pattern - Yahoo! Design Pattern Library [http://developer.yahoo.com/ypatterns/pattern.php?
pattern=breadcrumbs], and allows simple customization (minimum/maximum depth, indentation, separator, and
whether the last element should be linked), or rendering using a partial view script.
The Breadcrumbs helper works like this; it finds the deepest active page in a navigation container, and renders an
upwards path to the root. For MVC pages, the "activeness" of a page is determined by inspecting the request object,
as stated in the section on Zend_Navigation_Page_Mvc.
The helper sets the minDepth property to 1 by default, meaning breadcrumbs will not be rendered if the deepest
active page is a root page. If maxDepth is specified, the helper will stop rendering when at the specified depth (e.g.
stop at level 2 even if the deepest active page is on level 3).
• {get|set}Separator() gets/sets separator string that is used between breadcrumbs. Defualt is ' > '.
• {get|set}LinkLast() gets/sets whether the last breadcrumb should be rendered as an anchor or not. Default
is FALSE.
• {get|set}Partial() gets/sets a partial view script that should be used for rendering breadcrumbs. If a partial
view script is set, the helper's render() method will use the renderPartial() method. If no partial is set, the
renderStraight() method is used. The helper expects the partial to be a String or an Array with two elements.
If the partial is a String, it denotes the name of the partial script to use. If it is an Array, the first element will be
used as the name of the partial view script, and the second element is the module where the script is found.
The two calls above take advantage of the magic __toString() method,
and are equivalent to:
1316
Zend_View
Output:
<a href="/products">Products</a> > <a href="/products/server">Foo Server</a> > FAQ
Output:
<a href="/products">Products</a> > <a href="/products/server">Foo Server</a> >
<?php
echo $this->navigation()
->breadcrumbs()
->setLinkLast(true) // link last page
->setMaxDepth(1) // stop at level 1
->setSeparator(' ▶' . PHP_EOL); // cool separator with newline
?>
Output:
<a href="/products">Products</a> ▶
<a href="/products/server">Foo Server</a>
/////////////////////////////////////////////////////
<?php
$this->navigation()->breadcrumbs()->setMinDepth(10);
echo $this->navigation()->breadcrumbs();
?>
Output:
Nothing, because the deepest active page is not at level 10 or deeper.
In a layout:
1317
Zend_View
$partial = ;
echo $this->navigation()->breadcrumbs()
->setPartial(array('breadcrumbs.phtml', 'default'));
Contents of application/modules/default/views/breadcrumbs.phtml:
Output:
Links Helper
The links helper is used for rendering HTML LINK elements. Links are used for describing document relationships
of the currently active page. Read more about links and link types at Document relationships: the LINK element
(HTML4 W3C Rec.) [http://www.w3.org/TR/html4/struct/links.html#h-12.3] and Link types (HTML4 W3C Rec.)
[http://www.w3.org/TR/html4/types.html#h-6.12] in the HTML4 W3C Recommendation.
There are two types of relations; forward and reverse, indicated by the keyords 'rel' and 'rev'. Most methods in
the helper will take a $rel param, which must be either 'rel' or 'rev'. Most methods also take a $type param,
which is used for specifying the link type (e.g. alternate, start, next, prev, chapter, etc).
Relationships can be added to page objects manually, or found by traversing the container registered in the helper.
The method findRelation($page, $rel, $type) will first try to find the given $rel of $type from
the $page by calling $page->findRel($type) or $page->findRel($type). If the $page has a relation
that can be converted to a page instance, that relation will be used. If the $page instance doesn't have the specified
$type, the helper will look for a method in the helper named search$rel$type (e.g. searchRelNext()
or searchRevAlternate()). If such a method exists, it will be used for determining the $page's relation by
traversing the container.
Not all relations can be determined by traversing the container. These are the relations that will be found by searching:
• searchRelNext(), forward 'next' relation; finds the next page in the container, i.e. the page after the active page.
• searchRelPrev(), forward 'prev' relation; finds the previous page, i.e. the page before the active page.
• searchRelCapítulo(), forward 'chapter' relations; finds all pages on level 0 except the 'start' relation or the
active page if it's on level 0.
• searchRelSection(), forward 'section' relations; finds all child pages of the active page if the active page is
on level 0 (a 'chapter').
• searchRelSubsection(), forward 'subsection' relations; finds all child pages of the active page if the active
pages is on level 1 (a 'section').
• searchRevSection(), reverse 'section' relation; finds the parent of the active page if the active page is on level
1 (a 'section').
• searchRevSubsection(), reverse 'subsection' relation; finds the parent of the active page if the active page
is on level 2 (a 'subsection').
1318
Zend_View
Nota
When looking for relations in the page instance ($page->getRel($type) or $page-
>getRev($type)), the helper accepts the values of type String, Array, Zend_Config,
or Zend_Navigation_Page. If a string is found, it will be converted to a
Zend_Navigation_Page_Uri. If an array or a config is found, it will be converted to one or several
page instances. If the first key of the array/config is numeric, it will be considered to contain several pages,
and each element will be passed to the page factory. If the first key is not numeric, the array/config will be
passed to the page factory directly, and a single page will be returned.
The helper also supports magic methods for finding relations. E.g. to find forward alternate relations,
call $helper->findRelAlternate($page), and to find reverse section relations, call $helper-
>findRevSection($page). Those calls correspond to $helper->findRelation($page, 'rel',
'alternate'); and $helper->findRelation($page, 'rev', 'section'); respectively.
To customize which relations should be rendered, the helper uses a render flag. The render flag is an integer value,
and will be used in a bitwse and (&) operation [http://php.net/manual/en/language.operators.bitwise.php] against the
helper's render constants to determine if the relation that belongs to the render constant should be rendered.
• Zend_View_Helper_Navigation_Link::RENDER_ALTERNATE
• Zend_View_Helper_Navigation_Link::RENDER_STYLESHEET
• Zend_View_Helper_Navigation_Link::RENDER_START
• Zend_View_Helper_Navigation_Link::RENDER_NEXT
• Zend_View_Helper_Navigation_Link::RENDER_PREV
• Zend_View_Helper_Navigation_Link::RENDER_CONTENTS
• Zend_View_Helper_Navigation_Link::RENDER_INDEX
• Zend_View_Helper_Navigation_Link::RENDER_GLOSSARY
• Zend_View_Helper_Navigation_Link::RENDER_COPYRIGHT
• Zend_View_Helper_Navigation_Link::RENDER_CHAPTER
• Zend_View_Helper_Navigation_Link::RENDER_SECTION
• Zend_View_Helper_Navigation_Link::RENDER_SUBSECTION
• Zend_View_Helper_Navigation_Link::RENDER_APPENDIX
• Zend_View_Helper_Navigation_Link::RENDER_HELP
• Zend_View_Helper_Navigation_Link::RENDER_BOOKMARK
• Zend_View_Helper_Navigation_Link::RENDER_CUSTOM
• Zend_View_Helper_Navigation_Link::RENDER_ALL
The constants from RENDER_ALTERNATE to RENDER_BOOKMARK denote standard HTML link types.
RENDER_CUSTOM denotes non-standard relations that specified in pages. RENDER_ALL denotes standard and non-
standard relations.
1319
Zend_View
• {get|set}RenderFlag() gets/sets the render flag. Default is RENDER_ALL. See examples below on how to
set the render flag.
1320
Zend_View
)
)
));
Output:
<link rel="alternate" href="/products/server/faq/format/xml">
<link rel="start" href="/" title="Home">
<link rel="next" href="/products/server/editions" title="Editions">
<link rel="prev" href="/products/server" title="Foo Server">
<link rel="chapter" href="/products" title="Products">
<link rel="chapter" href="/company/about" title="Company">
<link rel="chapter" href="/community" title="Community">
<link rel="canonical" href="http://www.example.com/?page=server-faq">
<link rev="subsection" href="/products/server" title="Foo Server">
Output:
<link rel="start" href="/" title="Home">
<link rel="next" href="/products/server/editions" title="Editions">
<link rel="prev" href="/products/server" title="Foo Server">
Output:
<link rel="alternate" href="/products/server/faq/format/xml">
<link rel="start" href="/" title="Home">
<link rel="next" href="/products/server/editions" title="Editions">
<link rel="prev" href="/products/server" title="Foo Server">
<link rel="chapter" href="/products" title="Products">
<link rel="chapter" href="/company/about" title="Company">
<link rel="chapter" href="/community" title="Community">
<link rev="subsection" href="/products/server" title="Foo Server">
1321
Zend_View
$helper->setRenderFlag(Zend_View_Helper_Navigation_Links::RENDER_ALL ^
Zend_View_Helper_Navigation_Links::RENDER_CHAPTER);
Output:
<link rel="alternate" href="/products/server/faq/format/xml">
<link rel="start" href="/" title="Home">
<link rel="next" href="/products/server/editions" title="Editions">
<link rel="prev" href="/products/server" title="Foo Server">
<link rel="canonical" href="http://www.example.com/?page=server-faq">
<link rev="subsection" href="/products/server" title="Foo Server">
Menu Helper
The Menu helper is used for rendering menus from navigation containers. By default, the menu will be rendered using
HTML UL and LI tags, but the helper also allows using a partial view script.
• {get|set}OnlyActiveBranch() gets/sets a flag specifying whether only the active branch of a container
should be rendered.
• {get|set}RenderParents() gets/sets a flag specifying whether parents should be rendered when only
rendering active branch of a container. If set to FALSE, only the deepest active menu will be rendered.
• {get|set}Partial() gets/sets a partial view script that should be used for rendering menu. If a partial view
script is set, the helper's render() method will use the renderPartial() method. If no partial is set, the
renderMenu() method is used. The helper expects the partial to be a String or an Array with two elements. If
the partial is a String, it denotes the name of the partial script to use. If it is an Array, the first element will be used
as the name of the partial view script, and the second element is the module where the script is found.
• htmlify() overrides the method from the abstract class to return span elements if the page has no href.
• renderMenu($container = null, $options = array()) is the default render method, and will
render a container as a HTML UL list.
If $container is not given, the container registered in the helper will be rendered.
$options is used for overriding options specified temporarily without rsetting the values in the helper instance.
It is an associative array where each key corresponds to an option in the helper.
Recognized options:
• onlyActiveBranch; whether only active branch should be rendered. Expects a Boolean value.
• renderParents; whether parents should be rendered if only rendering active branch. Expects a Boolean value.
If an option is not given, the value set in the helper will be used.
1322
Zend_View
• renderPartial() is used for rendering the menu using a partial view script.
Or simply:
<?php echo $this->navigation()->menu() ?>
Output:
<ul class="navigation">
<li>
<a title="Go Home" href="/">Home</a>
</li>
<li class="active">
<a href="/products">Products</a>
<ul>
<li class="active">
<a href="/products/server">Foo Server</a>
<ul>
<li class="active">
<a href="/products/server/faq">FAQ</a>
</li>
<li>
<a href="/products/server/editions">Editions</a>
</li>
<li>
<a href="/products/server/requirements">System Requirements</a>
</li>
</ul>
</li>
<li>
<a href="/products/studio">Foo Studio</a>
<ul>
<li>
<a href="/products/studio/customers">Customer Stories</a>
</li>
<li>
<a href="/prodcts/studio/support">Support</a>
</li>
</ul>
</li>
</ul>
</li>
<li>
<a title="About us" href="/company/about">Company</a>
<ul>
1323
Zend_View
<li>
<a href="/company/about/investors">Investor Relations</a>
</li>
<li>
<a class="rss" href="/company/news">News</a>
<ul>
<li>
<a href="/company/news/press">Press Releases</a>
</li>
<li>
<a href="/archive">Archive</a>
</li>
</ul>
</li>
</ul>
</li>
<li>
<a href="/community">Community</a>
<ul>
<li>
<a href="/community/account">My Account</a>
</li>
<li>
<a class="external" href="http://forums.example.com/">Forums</a>
</li>
</ul>
</li>
</ul>
<?php
// render only the 'Community' menu
$community = $this->navigation()->findOneByLabel('Community');
$options = array(
'indent' => 16,
'ulClass' => 'community'
);
echo $this->navigation()
->menu()
->renderMenu($community, $options);
?>
Output:
<ul class="community">
<li>
<a href="/community/account">My Account</a>
</li>
<li>
<a class="external" href="http://forums.example.com/">Forums</a>
</li>
1324
Zend_View
</ul>
array(
'ulClass' => $ulClass,
'indent' => $indent,
'minDepth' => null,
'maxDepth' => null,
'onlyActiveBranch' => true,
'renderParents' => false
);
<?php
echo $this->navigation()
->menu()
->renderSubMenu(null, 'sidebar', 4);
?>
<?php
echo $this->navigation()
->menu()
->setMaxDepth(1);
?>
Output:
<ul class="navigation">
<li>
<a title="Go Home" href="/">Home</a>
</li>
<li class="active">
<a href="/products">Products</a>
1325
Zend_View
<ul>
<li class="active">
<a href="/products/server">Foo Server</a>
</li>
<li>
<a href="/products/studio">Foo Studio</a>
</li>
</ul>
</li>
<li>
<a title="About us" href="/company/about">Company</a>
<ul>
<li>
<a href="/company/about/investors">Investor Relations</a>
</li>
<li>
<a class="rss" href="/company/news">News</a>
</li>
</ul>
</li>
<li>
<a href="/community">Community</a>
<ul>
<li>
<a href="/community/account">My Account</a>
</li>
<li>
<a class="external" href="http://forums.example.com/">Forums</a>
</li>
</ul>
</li>
</ul>
<?php
echo $this->navigation()
->menu()
->setMinDepth(1);
?>
Output:
<ul class="navigation">
<li class="active">
<a href="/products/server">Foo Server</a>
<ul>
<li class="active">
<a href="/products/server/faq">FAQ</a>
</li>
<li>
<a href="/products/server/editions">Editions</a>
</li>
<li>
1326
Zend_View
<?php
echo $this->navigation()
->menu()
->setOnlyActiveBranch(true);
?>
Output:
<ul class="navigation">
<li class="active">
<a href="/products">Products</a>
<ul>
<li class="active">
<a href="/products/server">Foo Server</a>
<ul>
1327
Zend_View
<li class="active">
<a href="/products/server/faq">FAQ</a>
</li>
<li>
<a href="/products/server/editions">Editions</a>
</li>
<li>
<a href="/products/server/requirements">System Requirements</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
Ejemplo 61.37. Rendering only the active branch of a menu with minimum depth
<?php
echo $this->navigation()
->menu()
->setOnlyActiveBranch(true)
->setMinDepth(1);
?>
Output:
<ul class="navigation">
<li class="active">
<a href="/products/server">Foo Server</a>
<ul>
<li class="active">
<a href="/products/server/faq">FAQ</a>
</li>
<li>
<a href="/products/server/editions">Editions</a>
</li>
<li>
<a href="/products/server/requirements">System Requirements</a>
</li>
</ul>
</li>
</ul>
Ejemplo 61.38. Rendering only the active branch of a menu with maximum depth
<?php
echo $this->navigation()
->menu()
->setOnlyActiveBranch(true)
->setMaxDepth(1);
?>
Output:
1328
Zend_View
<ul class="navigation">
<li class="active">
<a href="/products">Products</a>
<ul>
<li class="active">
<a href="/products/server">Foo Server</a>
</li>
<li>
<a href="/products/studio">Foo Studio</a>
</li>
</ul>
</li>
</ul>
Ejemplo 61.39. Rendering only the active branch of a menu with maximum depth and no
parents
<?php
echo $this->navigation()
->menu()
->setOnlyActiveBranch(true)
->setRenderParents(false)
->setMaxDepth(1);
?>
Output:
<ul class="navigation">
<li class="active">
<a href="/products/server">Foo Server</a>
</li>
<li>
<a href="/products/studio">Foo Studio</a>
</li>
</ul>
In a layout:
In application/modules/default/views/menu.phtml:
1329
Zend_View
Output:
Sitemap Helper
The Sitemap helper is used for generating XML sitemaps, as defined by the Sitemaps XML format [http://
www.sitemaps.org/protocol.php]. Read more about Sitemaps on Wikpedia [http://en.wikipedia.org/wiki/Sitemaps].
By default, the sitemap helper uses sitemap validators to validate each element that is rendered. This can be disabled
by calling $helper->setUseSitemapValidators(false).
Nota
If you disable sitemap validators, the custom properties (see table) are not validated at all.
• always
• hourly
• daily
• weekly
• monthly
• yearly
1330
Zend_View
Element Description
• never
• {get|set}FormatOutput() gets/sets a flag indicating whether XML output should be formatted. This
corresponds to the formatOutput property of the native DOMDocument class. Read more at PHP:
DOMDocument - Manual [http://php.net/domdocument]. Default is FALSE.
• {get|set}UseSchemaValidation() gets/sets a flag indicating whether the helper should use XML Schema
validation when generating the DOM sitemap. Default is FALSE. If TRUE.
• {get|set}ServerUrl() gets/sets server URL that will be prepended to non-absolute URLs in the url()
method. If no server URL is specified, it will be determined by the helper.
// format output
$this->navigation()
->sitemap()
->setFormatOutput(true); // default is false
// print sitemap
echo $this->navigation()->sitemap();
Notice how pages that are invisible or pages with ACL roles incompatible with the view helper are filtered out:
1331
Zend_View
1332
Zend_View
</urlset>
Render the sitemap using no ACL role (should filter out /community/account):
echo $this->navigation()
->sitemap()
->setFormatOutput(true)
->setRole();
1333
Zend_View
<url>
<loc>http://www.example.com/community</loc>
</url>
<url>
<loc>http://forums.example.com/</loc>
</url>
</urlset>
echo $this->navigation()
->sitemap()
->setFormatOutput(true)
->setMaxDepth(1);
Navigation Helper
The Navigation helper is a proxy helper that relays calls to other navigational helpers. It can be considered an
entry point to all navigation-related view tasks. The aforementioned navigational helpers are in the namespace
1334
Zend_View
The Navigation helper finds other helpers that implement the Zend_View_Helper_Navigation_Helper
interface, which means custom view helpers can also be proxied. This would, however, require that the custom helper
path is added to the view.
When proxying to other helpers, the Navigation helper can inject its container, ACL/role, and translator. This
means that you won't have to explicitly set all three in all navigational helpers, nor resort to injecting by means of
Zend_Registry or static methods.
• findHelper() finds the given helper, verifies that it is a navigational helper, and injects container, ACL/role
and translator.
• {get|set}InjectContainer() gets/sets a flag indicating whether the container should be injected to proxied
helpers. Default is TRUE.
• {get|set}InjectAcl() gets/sets a flag indicating whether the ACL/role should be injected to proxied helpers.
Default is TRUE.
Translate Helper
Often web sites are available in several languages. To translate the content of a site you should simply use Zend
Translate and to integrate Zend Translate within your view you should use the Translate View Helper.
In all following examples we are using the simple Array Translation Adapter. Of course you can also use any instance
of Zend_Translate and also any subclasses of Zend_Translate_Adapter. There are several ways to initiate
the Translate View Helper:
A registered instance of Zend_Translate is the preferred usage for this helper. You can also select the locale to
be used simply before you add the adapter to the registry.
Nota
We are speaking of locales instead of languages because a language also may contain a region. For example
English is spoken in different dialects. There may be a translation for British and one for American English.
Therefore, we say "locale" instead of "language."
1335
Zend_View
If you are more familiar with the fluent interface, then you can also create an instance within your view and initiate
the helper afterwards.
If you are using the helper without Zend_View then you can also use it directly.
You would use this way if you are not working with Zend_View and need to create translated output.
As already seen, the translate() method is used to return the translation. Just call it with the needed messageid of
your translation adapter. But it can also replace parameters within the translation string. Therefore, it accepts variable
parameters in two ways: either as a list of parameters, or as an array of parameters. As examples:
Nota
Keep in mind that if you are using parameters which are also text, you may also need to translate these
parameters.
1336
Zend_View
Sometimes it is necessary to change the locale of the translation. This can be done either dynamically per translation
or statically for all following translations. And you can use it with both a parameter list and an array of parameters.
In both cases the locale must be given as the last single parameter.
This example returns the Italian translation for the messageid. But it will only be used once. The next translation will
use the locale from the adapter. Normally you will set the desired locale within the translation adapter before you add
it to the registry. But you can also set the locale from within the helper:
The above example sets 'it' as the new default locale which will be used for all further translations.
Of course there is also a getLocale() method to get the currently set locale.
1337
Zend_View
$this->translate()->setLocale('it');
$this->translate("Today is %1\$s in %2\$s. Actual time: %3\$s", $date);
Helper Paths
As with view scripts, your controller can specify a stack of paths for Zend_View to search for helper classes. By
default, Zend_View looks in "Zend/View/Helper/*" for helper classes. You can tell Zend_View to look in other
locations using the setHelperPath() and addHelperPath() methods. Additionally, you can indicate a class
prefix to use for helpers in the path provided, to allow namespacing your helper classes. By default, if no class prefix
is provided, 'Zend_View_Helper_' is assumed.
In fact, you can "stack" paths using the addHelperPath() method. As you add paths to the stack, Zend_View
will look at the most-recently-added path for the requested helper class. This allows you to add to (or even override)
the initial distribution of helpers with your own custom helpers.
// now when you call $this->helperName(), Zend_View will look first for
// "/path/to/some/helpers/HelperName" using class name
// "Your_View_Helper_HelperName", then for
// "/other/path/to/helpers/HelperName.php" using class name
// "My_View_Helper_HelperName", and finally for
// "Zend/View/Helper/HelperName.php" using class name
// "Zend_View_Helper_HelperName".
• The class name must, at the very minimum, end with the helper name itself, using MixedCaps. E.g., if you were
writing a helper called "specialPurpose", the class name would minimally need to be "SpecialPurpose". You may,
1338
Zend_View
and should, give the class name a prefix, and it is recommended that you use 'View_Helper' as part of that prefix:
"My_View_Helper_SpecialPurpose". (You will need to pass in the prefix, with or without the trailing underscore,
to addHelperPath() or setHelperPath()).
• The class must have a public method that matches the helper name; this is the method that will be called when your
template calls "$this->specialPurpose()". In our "specialPurpose" helper example, the required method declaration
would be "public function specialPurpose()".
• In general, the class should not echo or print or otherwise generate output. Instead, it should return values to be
printed or echoed. The returned values should be escaped appropriately.
• The class must be in a file named after the helper class. Again using our "specialPurpose" helper example, the file
has to be named "SpecialPurpose.php".
Place the helper class file somewhere in your helper path stack, and Zend_View will automatically load, instantiate,
persist, and execute it for you.
Then in a view script, you can call the SpecialPurpose helper as many times as you like; it will be instantiated
once, and then it persists for the life of that Zend_View instance.
Sometimes you will need access to the calling Zend_View object -- for instance, if you need to use the registered
encoding, or want to render another view script as part of your helper. To get access to the view object, your helper
class should have a setView($view) method, like the following:
class My_View_Helper_ScriptPath
{
public $view;
1339
Zend_View
If your helper class has a setView() method, it will be called when the helper class is first instantiated, and passed
the current view object. It is up to you to persist the object in your class, as well as determine how it should be accessed.
If you are extending Zend_View_Helper_Abstract, you do not need to define this method, as it is defined for
you.
Zend_View_Abstract
Zend_View_Abstract is the base class on which Zend_View is built; Zend_View itself simply extends it and
declares a concrete implementation of the _run() method (which is invoked by render()).
Many developers find that they want to extend Zend_View_Abstract to add custom functionality, and inevitably
run into issues with its design, which includes a number of private members. This document aims to explain the
decision behind the design.
Zend_View is something of an anti-templating engine in that it uses PHP natively for its templating. As a result, all
of PHP is available, and view scripts inherit the scope of their calling object.
It is this latter point that is salient to the design decisions. Internally, Zend_View::_run() does the following:
As such, the view scripts have access to the current object ($this), and any methods or members of that object. Since
many operations depend on members with limited visibility, this poses a problem: the view scripts could potentially
make calls to such methods or modify critical properties directly. Imagine a script overwriting $_path or $_file
inadvertently -- any further calls to render() or view helpers would break!
Fortunately, PHP 5 has an answer to this with its visibility declarations: private members are not accessible by objects
extending a given class. This led to the current design: since Zend_View extends Zend_View_Abstract, view
scripts are thus limited to only protected or public methods and members of Zend_View_Abstract -- effectively
limiting the actions it can perform, and allowing us to secure critical areas from abuse by view scripts.
1340
Capítulo 62. Zend_Wildfire
Zend_Wildfire
Zend_Wildfire is a component that facilitates communication between PHP code and Wildfire [http://
www.wildfirehq.org/] client components.
The purpose of the Wildfire Project is to develop standardized communication channels between a large variety of
components and a dynamic and scriptable plugin architecture. At this time, the primary focus is to provide a system
that allows server-side PHP code to inject logging messages into the Firebug Console [http://www.getfirebug.com/].
The Zend_Log_Writer_Firebug component is provided for the purpose of logging to Firebug, and a
communication protocol has been developed that uses HTTP request and response headers to send data between the
server and client components. It is great for logging intelligence data to the browser that is generated during script
execution, without interfering with the page content. With this approach, it is possible to debug AJAX requests that
require clean JSON and XML responses.
1341
1342
Capítulo 63. Zend_XmlRpc
Introduction
From its home page [http://www.xmlrpc.com/], XML-RPC is described as a "...remote procedure calling using HTTP
as the transport and XML as the encoding. XML-RPC is designed to be as simple as possible, while allowing complex
data structures to be transmitted, processed and returned."
Zend Framework provides support for both consuming remote XML-RPC services and building new XML-RPC
servers.
Zend_XmlRpc_Client
Introdución
Zend Framework provee soporte para consumo remoto para servicios XML-RPC como un cliente en el paquete
Zend_XmlRpc_Client . Su mejor característica es la conversión automática de tipos entre PHP y XML-RPC, un
servidor de objeto proxy, y acceso a capacidades de instrospección del servidor.
Method Calls
El constructor de Zend_XmlRpc_Client recibe la URL del servidor XML-RPC como su primer parámetro. La
nueva instacia devuelta puede ser usada para llamar cualquier número de métodos remotos en el punto final.
Para llamar un método remoto con el cliente XML-RPC, instáncealo y usa el método de instancia call() . El código
de ejemplo a continuación utiliza una demostración en el servidor XML-RPC en el sitio web de Zend Framework .
Puede utilizarlo para probar o explorar los componentes Zend_XmlRpc.
echo $client->call('test.sayHello');
// hello
El valor XML-RPC devuelto desde la llamada al método remoto automáticamente será convertida al tipo nativo PHP
equivalente . En el ejemplo anterior, es devuelto un string PHP y está listo para ser usado inmediatamente.
El primer parámetro del método call() recibe el nombre del método remoto que llamar. Si el método remoto requiere
algún parámetro, éste puede ser enviado por el suministro de un segundo, parámetro opcional a call() con un array
de valores para pasar el método remoto:
$arg1 = 1.1;
$arg2 = 'foo';
1343
Zend_XmlRpc
si el método remoto no requiere parámetros, este parámetro opcional podrá ser excluido o se puede pasar un
array() vacío. El array de parámeters para el método repoto puede contener tipos nativos PHPs, objetos
Zend_XmlRpc_Value , o una combinación de estos.
El método call() convertirá automáticamente la respuesta XML-RPC y devolverá su tipo nativo PHP equivalente.
Un objeto Zend_XmlRpc_Response para el valor devuelto también estará disponible para llamar el método
getLastResponse() después de la llamada.
Tipos y Conversiones
Algunas llamadas a métodos remoto requieren parámetros. Éstos son dados al método call() de
Zend_XmlRpc_Client como un array en el segundo parámetro. Cada parámetro puede ser dado como un tipo
nativo PHP, que será convertido automáticamente, o como un objeto que representa un tipo específico de XML-RPC
(uno de los objetos Zend_XmlRpc_Value).
However, this in itself can lead to issues. First off, servers that do not support system.methodSignature
will log failed requests, and Zend_XmlRpc_Client will resort to casting the value to an XML-RPC array
type. Additionally, this means that any call with array arguments will result in an additional call to the remote
server.
To disable the lookup entirely, you can call the setSkipSystemLookup() method prior to making your
XML-RPC call:
$client->setSkipSystemLookup(true);
1344
Zend_XmlRpc
• When you want to make sure the correct parameter type is passed to the procedure (i.e. the procedure requires an
integer and you may get it from a database as a string)
• When the procedure requires base64 or dateTime.iso8601 type (which doesn't exists as a PHP native type)
• When auto-conversion may fail (i.e. you want to pass an empty XML-RPC struct as a parameter. Empty structs are
represented as empty arrays in PHP but, if you give an empty array as a parameter it will be auto-converted to an
XML-RPC array since it's not an associative array)
There are two ways to create a Zend_XmlRpc_Value object: instantiate one of the Zend_XmlRpc_Value
subclasses directly, or use the static factory method Zend_XmlRpc_Value::getXmlRpcValue().
Automatic Conversion
When building a new Zend_XmlRpc_Value object, its value is set by a PHP type. The PHP type will
be converted to the specified type using PHP casting. For example, if a string is given as a value to the
Zend_XmlRpc_Value_Integer object, it will be converted using (int)$value.
To instantiate a server proxy, call the getProxy() instance method of Zend_XmlRpc_Client. This will return an
instance of Zend_XmlRpc_Client_ServerProxy. Any method call on the server proxy object will be forwarded
to the remote, and parameters may be passed like any other PHP method.
1345
Zend_XmlRpc
The getProxy() method receives an optional argument specifying which namespace of the remote server to proxy.
If it does not receive a namespace, the default namespace will be proxied. In the next example, the test namespace
will be proxied:
If the remote server supports nested namespaces of any depth, these can also be used through the server proxy. For
example, if the server in the example above had a method test.foo.bar(), it could be called as $test->foo-
>bar().
Error Handling
Two kinds of errors can occur during an XML-RPC method call: HTTP errors and XML-RPC faults. The
Zend_XmlRpc_Client recognizes each and provides the ability to detect and trap them independently.
HTTP Errors
If any HTTP error occurs, such as the remote HTTP server returns a 404 Not Found, a
Zend_XmlRpc_Client_HttpException will be thrown.
try {
Regardless of how the XML-RPC client is used, the Zend_XmlRpc_Client_HttpException will be thrown
whenever an HTTP error occurs.
XML-RPC Faults
An XML-RPC fault is analogous to a PHP exception. It is a special type returned from an XML-RPC method call that
has both an error code and an error message. XML-RPC faults are handled differently depending on the context of
how the Zend_XmlRpc_Client is used.
1346
Zend_XmlRpc
When the call() method or the server proxy object is used, an XML-RPC fault will result in a
Zend_XmlRpc_Client_FaultException being thrown. The code and message of the exception will map
directly to their respective values in the original XML-RPC fault response.
try {
$client->call('badMethod');
// $e->getCode() returns 1
// $e->getMessage() returns "Unknown method"
Cuando el método doRequest() sea usado para realizar una petición, no lanzará una excepción. En vez de eso,
devolverá un objeto Zend_XmlRpc_Response que contendrá el error. Esto puede comprobarse con isFault()
método instancia de Zend_XmlRpc_Response.
Server Introspection
Some XML-RPC servers support the de facto introspection methods under the XML-RPC system. namespace.
Zend_XmlRpc_Client provides special support for servers with these capabilities.
1347
Zend_XmlRpc
$client->doRequest($request);
Whenever an XML-RPC method call is made by the client through any means, either the call() method,
doRequest() method, or server proxy, the last request object and its resultant response object will always be
available through the methods getLastRequest() and getLastResponse() respectively.
The HTTP client can be retrieved at any time with the getHttpClient() method. For most cases, the default
HTTP client will be sufficient. However, the setHttpClient() method allows for a different HTTP client instance
to be injected.
The setHttpClient() is particularly useful for unit testing. When combined with the
Zend_Http_Client_Adapter_Test, remote services can be mocked out for testing. See the unit tests for
Zend_XmlRpc_Client for examples of how to do this.
Zend_XmlRpc_Server
Introduction
Zend_XmlRpc_Server is intended as a fully-featured XML-RPC server, following the specifications outlined
at www.xmlrpc.com [http://www.xmlrpc.com/spec]. Additionally, it implements the system.multicall() method,
allowing boxcarring of requests.
Basic Usage
An example of the most basic use case:
Server Structure
Zend_XmlRpc_Server is composed of a variety of components, ranging from the server itself to request, response,
and fault objects.
To bootstrap Zend_XmlRpc_Server, the developer must attach one or more classes or functions to the server, via
the setClass() and addFunction() methods.
1348
Zend_XmlRpc
Zend_XmlRpc_Server_Faultobject. These objects both have __toString() methods that create valid XML-
RPC XML responses, allowing them to be directly echoed.
Conventions
Zend_XmlRpc_Server allows the developer to attach functions and class method calls as dispatchable XML-RPC
methods. Via Zend_Server_Reflection, it does introspection on all attached methods, using the function and
method docblocks to determine the method help text and method signatures.
XML-RPC types do not necessarily map one-to-one to PHP types. However, the code will do its best to guess the
appropriate type based on the values listed in @param and @return lines. Some XML-RPC types have no immediate
PHP equivalent, however, and should be hinted using the XML-RPC type in the PHPDoc. These include:
/**
* This is a sample function
*
* @param base64 $val1 Base64-encoded data
* @param dateTime.iso8601 $val2 An ISO date
* @param struct $val3 An associative array
* @return struct
*/
function myFunc($val1, $val2, $val3)
{
}
PhpDocumentor does no validation of the types specified for params or return values, so this will have no impact on
your API documentation. Providing the hinting is necessary, however, when the server is validating the parameters
provided to the method call.
It is perfectly valid to specify multiple types for both params and return values; the XML-RPC specification even
suggests that system.methodSignature should return an array of all possible method signatures (i.e., all possible
combinations of param and return values). You may do so just as you normally would with PhpDocumentor, using
the '|' operator:
/**
* This is a sample function
*
* @param string|base64 $val1 String or base64-encoded data
* @param string|dateTime.iso8601 $val2 String or an ISO date
* @param array|struct $val3 Normal indexed array or an associative array
* @return boolean|struct
*/
function myFunc($val1, $val2, $val3)
{
}
1349
Zend_XmlRpc
One note, however: allowing multiple signatures can lead to confusion for developers using the services; generally
speaking, an XML-RPC method should only have a single signature.
Utilizing Namespaces
XML-RPC has a concept of namespacing; basically, it allows grouping XML-RPC methods by dot-delimited
namespaces. This helps prevent naming collisions between methods served by different classes. As an example, the
XML-RPC server is expected to server several methods in the 'system' namespace:
• system.listMethods
• system.methodHelp
• system.methodSignature
If you want to add namespaces to the methods you serve, simply provide a namespace to the appropriate method when
attaching a function or class:
Custom Responses
Similar to request objects, Zend_XmlRpc_Server can return custom response objects; by default, a
Zend_XmlRpc_Response_Http object is returned, which sends an appropriate Content-Type HTTP header for
use with XML-RPC. Possible uses of a custom object would be to log responses, or to send responses back to STDOUT.
1350
Zend_XmlRpc
Exception classes can be whitelisted to be used as fault responses, however. To do so, simply utilize
Zend_XmlRpc_Server_Fault::attachFaultException() to pass an exception class to whitelist:
Zend_XmlRpc_Server_Fault::attachFaultException('My_Project_Exception');
If you utilize an exception class that your other project exceptions inherit, you can then whitelist a whole family of
exceptions at a time. Zend_XmlRpc_Server_Exceptions are always whitelisted, to allow reporting specific
internal errors (undefined methods, etc.).
Any exception not specifically whitelisted will generate a fault response with a code of '404' and a message of
'Unknown error'.
To reduce this performance hit somewhat, Zend_XmlRpc_Server_Cache can be used to cache the server
definition between requests. When combined with __autoload(), this can greatly increase performance.
function __autoload($class)
{
Zend_Loader::loadClass($class);
}
if (!Zend_XmlRpc_Server_Cache::get($cacheFile, $server)) {
require_once 'My/Services/Glue.php';
require_once 'My/Services/Paste.php';
require_once 'My/Services/Tape.php';
Zend_XmlRpc_Server_Cache::save($cacheFile, $server);
}
echo $server->handle();
The above example attempts to retrieve a server definition from xmlrpc.cache in the same directory as the script. If
unsuccessful, it loads the service classes it needs, attaches them to the server instance, and then attempts to create a
new cache file with the server definition.
Usage Ejemplos
Below are several usage examples, showing the full spectrum of options available to developers. Usage examples will
each build on the previous example provided.
1351
Zend_XmlRpc
Basic Usage
The example below attaches a function as a dispatchable XML-RPC method and handles incoming calls.
/**
* Return the MD5 sum of a value
*
* @param string $value Value to md5sum
* @return string MD5 sum of value
*/
function md5Value($value)
{
return md5($value);
}
Attaching a class
The example below illustrates attaching a class' public methods as dispatchable XML-RPC methods.
require_once 'Services/Comb.php';
require_once 'Services/Comb.php';
require_once 'Services/Brush.php';
require_once 'Services/Pick.php';
require_once 'Services/Exception.php';
require_once 'Services/Comb.php';
1352
Zend_XmlRpc
require_once 'Services/Brush.php';
require_once 'Services/Pick.php';
require_once 'Services/Request.php';
require_once 'Services/Exception.php';
require_once 'Services/Comb.php';
require_once 'Services/Brush.php';
require_once 'Services/Pick.php';
echo $server->handle($request);
require_once 'Services/Request.php';
require_once 'Services/Response.php';
require_once 'Services/Exception.php';
require_once 'Services/Comb.php';
require_once 'Services/Brush.php';
require_once 'Services/Pick.php';
1353
Zend_XmlRpc
echo $server->handle($request);
// Save cache
Zend_XmlRpc_Server_Cache::save($cacheFile, $server);
}
echo $server->handle($request);
1354
Appendix A. Requisitos de Zend
Framework
Zend Framework requiere un intérprete PHP 5 con un servidor web configurado para manejar scripts PHP
correctamente. Algunas características requieren extensiones adicionales o características de servidor web; en muchos
casos el framework puede ser utilizado sin ellos, aunque el rendimiento puede sufrir o las características auxiliares
pueden no ser completamente funcionales. Un ejemplo de dicha dependencia es mod_rewrite en un entorno Apache,
que puede ser utilizado para ejecutar "pretty URL" como "http://www.example.com/user/edit". Si mod_rewrite no está
habilitado, ZF puede ser configurado para apoyar las URL como. La pretty URL, puede ser utilizada para acortar las
URL de representación textual o para la optimisation de los motores de búsqueda(SEO), pero no afectan directamente
a la funcionalidad de la aplicación.
Versión de PHP
Zend recomienda PHP 5.2.3 o superior por mejoras en la seguridad críticas y en el rendimiento, aunque Zend
Framework requiere sólo PHP 5.1.4 o posterior.
Zend Framework tiene una extensa colección de unidades de prueba, que puede ejecutar utilizando PHPUnit 3.0 o
posterior.
Extensiones de PHP
Usted encontrará un cuadro con todas las extensiones que suelen encontrarse en PHP y debajo cómo se usan en Zend
Framework. Usted debe verificar que las extensiones de componentes ZF que estará usando en su aplicación están
disponibles en sus entornos PHP. Muchas aplicaciones no exigirán de cada extensión que figuran a continuación.
Una dependencia de tipo "hard" indica que los componentes o clases no pueden funcionar correctamente si las
respectivas extensiones no están disponibles, mientras que una dependencia de tipo "soft" indica que el componente
puede usar la extensión si está disponible pero funcionará correctamente si no lo está. Muchos de los componentes
utilizarán automáticamente determinadas extensiones si están disponibles para optimizar el rendimiento pero
ejecutarán el código con una funcionalidad similar en el propio componente si las extensiones no están disponibles.
1355
Requisitos de Zend Framework
1356
Requisitos de Zend Framework
1357
Requisitos de Zend Framework
1358
Requisitos de Zend Framework
1359
Requisitos de Zend Framework
1360
Requisitos de Zend Framework
1361
Requisitos de Zend Framework
1362
Requisitos de Zend Framework
Una dependencia de tipo "hard" indica que los componentes o clases no pueden funcionar correctamente si las
extensiones respectivas no están disponibles, mientras que una dependencia de tipo "soft" indica que el componente
puede usar la extensión si está disponible, pero funcionará correctamente si no lo está. Muchos de los componentes
utilizarán automáticamente determinadas extensiones si están disponibles para optimizar el rendimiento, pero ejecutará
un código con una funcionalidad similar en el propio componente si las extensiones no están disponibles.
Tabla A.2. Componentes de Zend Framework y las extensiones que usan de PHP
Componentes de Zend Tipo de Dependencia Subclase Extensión PHP
Framework
pcre [http://www.php.net/
manual/en/ref.pcre.php]
All Components Hard --- SPL [http://www.php.net/
manual/en/ref.spl.php]
standard
Zend_Acl [http:// --- --- ---
framework.zend.com/
manual/en/zend.acl.html]
Zend_Auth_Adapter_Http ctype [http://
Zend_Auth [http:// www.php.net/manual/en/
[http://
framework.zend.com/ ref.ctype.php]
Hard framework.zend.com/
manual/en/
manual/en/ hash [http://www.php.net/
zend.auth.html]
zend.auth.adapter.http.html] manual/en/ref.hash.php]
Zend_Cache_Backend_Apc apc [http://www.php.net/
[http:// manual/en/ref.apc.php]
framework.zend.com/
manual/en/
zend.cache.backends.html]
Zend_Cache_Backend_Memcached
memcache [http://
[http:// www.php.net/manual/en/
framework.zend.com/ ref.memcache.php]
Zend_Cache [http:// manual/en/
framework.zend.com/ zend.cache.backends.html]
Hard
manual/en/ Zend_Cache_Backend_Sqlite sqlite [http://
zend.cache.html] [http:// www.php.net/manual/en/
framework.zend.com/ ref.sqlite.php]
manual/en/
zend.cache.backends.html]
Zend_Cache_Backend_Zlib zlib [http://www.php.net/
[http:// manual/en/ref.zlib.php]
framework.zend.com/
manual/en/
zend.cache.backends.html]
1363
Requisitos de Zend Framework
1364
Requisitos de Zend Framework
1365
Requisitos de Zend Framework
1366
Requisitos de Zend Framework
1367
Requisitos de Zend Framework
1368
Requisitos de Zend Framework
1369
Requisitos de Zend Framework
1370
Requisitos de Zend Framework
Una dependencia de tipo "hard" indica que los componentes o clases no funcionarán correctamente si los respectivos
componentes dependientes no están disponibles, mientras que una dependencia de tipo "soft" indica que el componente
puede necesitar el componente dependiente en situaciones especiales o con adaptadores especiales.
Nota
Incluso si es posible separar componentes indiduales para usarlo desde Zend Framework completo, usted
debe tener en cuenta que esto puede conducir a problemas cuando se perdieron los ficheros o los componentes
se utilizan dinámicamente.
1371
Requisitos de Zend Framework
1372
Requisitos de Zend Framework
1373
Requisitos de Zend Framework
1374
Requisitos de Zend Framework
1375
Requisitos de Zend Framework
1376
Requisitos de Zend Framework
1377
Requisitos de Zend Framework
1378
Requisitos de Zend Framework
1379
Appendix B. Notas de Migración de
Zend Framework
Zend Framework 1.10
When upgrading from a previous release to Zend Framework 1.10 or higher you should note the following migration
notes.
Zend_File_Transfer
Count validation
Before release 1.10 the MimeType validator used a wrong naming. For consistency the following constants have
been changed:
When you are translating these messages within your code then use the new constants. As benefit you don't need to
translate the original string anymore to get a correct spelling.
Zend_Validate
Self written validators
When setting returning a error from within a self written validator you have to call the _error() method. Before
Zend Framework 1.10 you were able to call this method without giving a parameter. It used then the first found message
template.
This behaviour is problematic when you have validators with more than one different message to be returned. Also
when you extend an existing validator you can get unexpected results. This could lead to the problem that your user
get not the message you expected.
1380
Notas de Migración
de Zend Framework
To prevent this problem the _error() method is no longer allowed to be called without giving a parameter.
As with Zend Framework 1.10 the values of the constants were changed to be unique. When you used the constants as
proposed in the manual there is no change for you. But when you used the content of the constants in your code then
you will have to change them. The following table shows you the changed values:
Zend_File_Transfer
MimeType validation
For security reasons we had to turn off the default fallback mechanism of the MimeType, ExcludeMimeType,
IsCompressed and IsImage validators. This means, that if the fileInfo or magicMime extensions can not be found,
the validation will always fail.
1381
Notas de Migración
de Zend Framework
If you are in need of validation by using the HTTP fields which are provided by the user then you can turn on this
feature by using the enableHeaderCheck() method.
Security hint
You should note that relying on the HTTP fields, which are provided by your user, is a security risk. They
can easily be changed and could allow your user to provide a malcious file.
// at initiation
$valid = new Zend_File_Transfer_Adapter_Http(array('headerCheck' => true);
// or afterwards
$valid->enableHeaderCheck();
Zend_Filter
Prior to the 1.9 release, Zend_Filter allowed the usage of the static get() method. As with release 1.9 this method
has been renamed to filterStatic() to be more descriptive. The old get() method is marked as deprecated.
Zend_Http_Client
Changes to internal uploaded file information storage
In version 1.9 of Zend Framework, there has been a change in the way Zend_Http_Client internally stores
information about files to be uploaded, set using the Zend_Http_Client::setFileUpload() method.
This change was introduced in order to allow multiple files to be uploaded with the same form name, as an array of
files. More information about this issue can be found in this bug report [http://framework.zend.com/issues/browse/
ZF-5744].
// Upload two files with the same form element name, as an array
$client = new Zend_Http_Client();
$client->setFileUpload('file1.txt',
'userfile[]',
'some raw data',
'text/plain');
$client->setFileUpload('file2.txt',
'userfile[]',
'some other data',
'application/octet-stream');
1382
Notas de Migración
de Zend Framework
// );
As you can see, this change permits the usage of the same form element name with more than one file - however, it
introduces a subtle backwards-compatibility change and as such should be noted.
Deprecation of Zend_Http_Client::_getParametersRecursive()
Starting from version 1.9, the protected method _getParametersRecursive() is no longer used by
Zend_Http_Client and is deprecated. Using it will cause an E_NOTICE message to be emitted by PHP.
If you subclass Zend_Http_Client and call this method, you should look into using the
Zend_Http_Client::_flattenParametersArray() static method instead.
Again, since this _getParametersRecursive is a protected method, this change will only affect users who
subclass Zend_Http_Client.
Zend_Locale
Depreciated methods
Some specialized translation methods have been depreciated because they duplicate existing behaviour. Note that the
old methods will still work, but a user notice is triggered which describes the new call. The methods will be erased
with 2.0. See the following list for old and new method call.
1383
Notas de Migración
de Zend Framework
Zend_View_Helper_Navigation
Prior to the 1.9 release, the menu helper (Zend_View_Helper_Navigation_Menu) did not render sub menus
correctly. When the onlyActiveBranch was TRUE and the option renderParents FALSE, nothing would be
rendered if the deepest active page was at a depth lower than the minDepth option.
In simpler words; if minDepth was set to 1 and the active page was at one of the first level pages, nothing would
be rendered, as the following example shows.
<?php
$container = new Zend_Navigation(array(
array(
'label' => 'Home',
'uri' => '#'
),
array(
'label' => 'Products',
'uri' => '#',
'active' => true,
'pages' => array(
array(
'label' => 'Server',
'uri' => '#'
),
array(
'label' => 'Studio',
'uri' => '#'
)
)
),
array(
'label' => 'Solutions',
'uri' => '#'
)
));
Before release 1.9, the code snippet above would output nothing.
1384
Notas de Migración
de Zend Framework
<ul class="navigation">
<li>
<a href="#">Server</a>
</li>
<li>
<a href="#">Studio</a>
</li>
</ul>
Zend_Controller
Standard Route Changes
As translated segments were introduced into the new standard route, the '@' character is now a special character in the
beginning of a route segment. To be able to use it in a static segment, you must escape it by prefixing it with second
'@' character. The same rule now applies for the ':' character.
Zend_Locale
Default caching
As with Zend Framework 1.8 a default caching was added. The reason behind this change was, that most users had
performance problems but did not add caching at all. As the I18n core is a bottleneck when no caching is used we
decided to add a default caching when no cache has been set to Zend_Locale.
Sometimes it is still wanted to prevent caching at all even if this decreases performance. To do so you can simply
disable caching by using the disableCache() method.
Zend_Locale::disableCache(true);
Zend_Controller
Dispatcher Interface Changes
Users brought to our attention the fact that Zend_Controller_Action_Helper_ViewRenderer were using
a method of the dispatcher abstract class that was not in the dispatcher interface. We have now added the following
method to ensure that custom dispatchers will continue to work with the shipped implementations:
1385
Notas de Migración
de Zend Framework
• formatModuleName(): should be used to take a raw controller name, such as one that would be packaged inside
a request object, and reformat it to a proper class name that a class extending Zend_Controller_Action
would use
Zend_File_Transfer
Changes when using filters and validators
As noted by users, the validators from Zend_File_Transfer do not work in conjunction with Zend_Config
due to the fact that they have not used named arrays.
Therefor, all filters and validators for Zend_File_Transfer have been reworked. While the old signatures
continue to work, they have been marked as deprecated, and will emit a PHP notice asking you to fix them.
The following list shows you the changes you will have to do for proper usage of the parameters.
Filter: Rename
• Old method API: Zend_Filter_File_Rename($oldfile, $newfile, $overwrite)
• New method API: Zend_Filter_File_Rename($options) where $options accepts the following array keys:
source equals to $oldfile, target equals to $newfile, overwrite equals to $overwrite.
Ejemplo B.4. Changes for the rename filter from 1.6 to 1.7
Validator: Count
• Old method API: Zend_Validate_File_Count($min, $max)
• New method API: Zend_Validate_File_Count($options) where $options accepts the following array keys: min
equals to $min, max equals to $max.
Ejemplo B.5. Changes for the count validator from 1.6 to 1.7
1386
Notas de Migración
de Zend Framework
$upload->addValidator('Count',
false,
array('min' => 2,
'max' => 3));
Validator:Extension
• Old method API: Zend_Validate_File_Extension($extension, $case)
• New method API: Zend_Validate_File_Extension($options) where $options accepts the following array keys:
* equals to $extension and can have any other key, case equals to $case.
Ejemplo B.6. Changes for the extension validator from 1.6 to 1.7
Validator: FilesSize
• Old method API: Zend_Validate_File_FilesSize($min, $max, $bytestring)
• New method API: Zend_Validate_File_FilesSize($options) where $options accepts the following array keys:
min equals to $min, max equals to $max, bytestring equals to $bytestring.
Additionally, the useByteString() method signature has changed. It can only be used to test if the validator is
expecting to use byte strings in generated messages. To set the value of the flag, use the setUseByteString()
method.
Ejemplo B.7. Changes for the filessize validator from 1.6 to 1.7
1387
Notas de Migración
de Zend Framework
Validator: Hash
• Old method API: Zend_Validate_File_Hash($hash, $algorithm)
• New method API: Zend_Validate_File_Hash($options) where $options accepts the following array keys: *
equals to $hash and can have any other key, algorithm equals to $algorithm.
Ejemplo B.8. Changes for the hash validator from 1.6 to 1.7
Validator: ImageSize
• Old method API: Zend_Validate_File_ImageSize($minwidth, $minheight, $maxwidth, $maxheight)
• New method API: Zend_Validate_File_FilesSize($options) where $options accepts the following array keys:
minwidth equals to $minwidth, maxwidth equals to $maxwidth, minheight equals to $minheight, maxheight
equals to $maxheight.
Ejemplo B.9. Changes for the imagesize validator from 1.6 to 1.7
Validator: Size
• Old method API: Zend_Validate_File_Size($min, $max, $bytestring)
1388
Notas de Migración
de Zend Framework
• New method API: Zend_Validate_File_Size($options) where $options accepts the following array keys: min
equals to $min, max equals to $max, bytestring equals to $bytestring.
Ejemplo B.10. Changes for the size validator from 1.6 to 1.7
Zend_Locale
Changes when using isLocale()
According to the coding standards isLocale() had to be changed to return a boolean. In previous releases a string
was returned on success. For release 1.7 a compatibility mode has been added which allows to use the old behaviour
of a returned string, but it triggers a user warning to mention you to change to the new behaviour. The rerouting which
the old behaviour of isLocale() could have done is no longer neccessary as all I18N will now process a rerouting
themself.
To migrate your scripts to the new API, simply use the method as shown below.
if (Zend_Locale::isLocale($locale)) {
}
Note that you can use the second parameter to see if the locale is correct without processing a rerouting.
1389
Notas de Migración
de Zend Framework
if (Zend_Locale::isLocale($locale, false)) {
if (Zend_Locale::isLocale($locale, true)) {
// no locale at all
}
To migrate your scripts to the new API, simply use the method as shown below.
$locale = Zend_Locale::getOrder(Zend_Locale::BROWSER);
Note that the second parameter of the old getDefault() implementation is not available anymore, but the returned
values are the same.
Nota
Per default the old behaviour is still active, but throws a user warning. When you have changed your code
to the new behaviour you should also change the compatibility mode to false so that no warning is thrown
anymore.
Zend_Translate
Setting languages
When using automatic detection of languages, or setting languages manually to Zend_Translate you may have
mentioned that from time to time a notice is thrown about not added or empty translations. In some previous release
also an exception was raised in some cases.
1390
Notas de Migración
de Zend Framework
The reason is, that when a user requests a non existing language, you have no simple way to detect what's going wrong.
So we added those notices which show up in your log and tell you that the user requested a language which you do
not support. Note that the code, even when we trigger such an notice, keeps working without problems.
But when you use a own error or exception handler, like xdebug, you will get all notices returned, even if this was not
your intention. This is due to the fact that these handlers override all settings from within PHP.
To get rid of these notices you can simply set the new option 'disableNotices' to true. It defaults to false.
In this case we will get an notice about a not available language 'fr'. Simply add the option and the notices will be
disabled.
Zend_View
Nota
The API changes within Zend_View are only notable for you when you are upgrading to release 1.7.5 or
higher.
Prior to the 1.7.5 release, the Zend Framework team was notified of a potential Local File Inclusion (LFI) vulnerability
in the Zend_View::render() method. Prior to 1.7.5, the method allowed, by default, the ability to specify view
scripts that included parent directory notation (e.g., "../" or "..\"). This opens the possibility for an LFI attack if unfiltered
user input is passed to the render() method:
Zend_View now by default raises an exception when such a view script is requested.
1391
Notas de Migración
de Zend Framework
Zend_Controller
Dispatcher Interface Changes
Users brought to our attention the fact that Zend_Controller_Front and
Zend_Controller_Router_Route_Module were each using methods of the dispatcher that were not in the
dispatcher interface. We have now added the following three methods to ensure that custom dispatchers will continue
to work with the shipped implementations:
Zend_File_Transfer
Changes when using validators
As noted by users, the validators from Zend_File_Transfer do not work the same way like the default ones
from Zend_Form. Zend_Form allows the usage of a $breakChainOnFailure parameter which breaks the
validation for all further validators when an validation error has occurred.
To migrate your scripts to the new API, simply add a FALSE after defining the wished validator.
Ejemplo B.14. How to change your file validators from 1.6.1 to 1.6.2
1392
Notas de Migración
de Zend Framework
Zend_Controller
Though most basic functionality remains the same, and all documented functionality remains the same, there is one
particular undocumented "feature" that has changed.
When writing URLs, the documented way to write camelCased action names is to use a word separator; these are '.'
or '-' by default, but may be configured in the dispatcher. The dispatcher internally lowercases the action name, and
uses these word separators to re-assemble the action method using camelCasing. However, because PHP functions
are not case sensitive, you could still write URLs using camelCasing, and the dispatcher would resolve these to the
same location. For example, 'camel-cased' would become 'camelCasedAction' by the dispatcher, whereas 'camelCased'
would become 'camelcasedAction'; however, due to the case insensitivity of PHP, both will execute the same method.
This causes issues with the ViewRenderer when resolving view scripts. The canonical, documented way is that all
word separators are converted to dashes, and the words lowercased. This creates a semantic tie between the actions
and view scripts, and the normalization ensures that the scripts can be found. However, if the action 'camelCased' is
called and actually resolves, the word separator is no longer present, and the ViewRenderer attempts to resolve to a
different location -- camelcased.phtml instead of camel-cased.phtml.
Some developers relied on this "feature", which was never intended. Several changes in the 1.5.0 tree, however, made
it so that the ViewRenderer no longer resolves these paths; the semantic tie is now enforced. First among these, the
dispatcher now enforces case sensitivity in action names. What this means is that referring to your actions on the url
using camelCasing will no longer resolve to the same method as using word separators (i.e., 'camel-casing'). This leads
to the ViewRenderer now only honoring the word-separated actions when resolving view scripts.
If you find that you were relying on this "feature", you have several options:
• Best option: rename your view scripts. Pros: forward compatibility. Cons: if you have many view scripts that relied
on the former, unintended behavior, you will have a lot of renaming to do.
• Second best option: The ViewRenderer now delegates view script resolution to Zend_Filter_Inflector;
you can modify the rules of the inflector to no longer separate the words of an action with a dash:
$viewRenderer =
Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$inflector = $viewRenderer->getInflector();
$inflector->setFilterRule(':action', array(
new Zend_Filter_PregReplace(
'#[^a-z0-9' . preg_quote(DIRECTORY_SEPARATOR, '#') . ']+#i',
''
),
'StringToLower'
));
The above code will modify the inflector to no longer separate the words with dash; you may also want to remove
the 'StringToLower' filter if you do want the actual view script names camelCased as well.
1393
Notas de Migración
de Zend Framework
If renaming your view scripts would be too tedious or time consuming, this is your best option until you can find
the time to do so.
• Least desirable option: You can force the dispatcher to dispatch camelCased action names with a new front controller
flag, useCaseSensitiveActions:
$front->setParam('useCaseSensitiveActions', true);
This will allow you to use camelCasing on the url and still have it resolve to the same action as when you use word
separators. However, this will mean that the original issues will cascade on through; you will likely need to use the
second option above in addition to this for things to work at all reliably.
Note, also, that usage of this flag will raise a notice that this usage is deprecated.
Zend_Controller
The principal changes introduced in 1.0.0RC1 are the introduction of and default enabling of the ErrorHandler plugin
and the ViewRenderer action helper. Please read the documentation to each thoroughly to see how they work and what
effect they may have on your applications.
The ErrorHandler plugin runs during postDispatch() checking for exceptions, and forwarding to a specified
error handler controller. You should include such a controller in your application. You may disable it by setting the
front controller parameter noErrorHandler:
$front->setParam('noErrorHandler', true);
The ViewRenderer action helper automates view injection into action controllers as well as autorendering of view
scripts based on the current action. The primary issue you may encounter is if you have actions that do not render
view scripts and neither forward or redirect, as the ViewRenderer will attempt to render a view script based on
the action name.
There are several strategies you can take to update your code. In the short term, you can globally disable the
ViewRenderer in your front controller bootstrap prior to dispatching:
However, this is not a good long term strategy, as it means most likely you'll be writing more code.
When you're ready to start using the ViewRenderer functionality, there are several things to look for in your
controller code. First, look at your action methods (the methods ending in 'Action'), and determine what each is doing.
If none of the following is happening, you'll need to make changes:
• Calls to $this->render();
• Calls to $this->_forward();
1394
Notas de Migración
de Zend Framework
• Calls to $this->_redirect();
$this->_helper->viewRenderer->setNoRender();
If you find that none of your action methods are rendering, forwarding, or redirecting, you will likely want to put the
above line in your preDispatch() or init() methods:
If you are calling render(), and you're using the Conventional Modular directory structure, you'll want to change
your code to make use of autorendering:
• If you're rendering multiple view scripts in a single action, you don't need to change a thing.
• If you're simply calling render() with no arguments, you can remove such lines.
• If you're calling render() with arguments, and not doing any processing afterwards or rendering multiple view
scripts, you can change these calls to read $this->_helper->viewRenderer();.
If you're not using the conventional modular directory structure, there are a variety of methods for setting the view base
path and script path specifications so that you can make use of the ViewRenderer. Please read the ViewRenderer
documentation for information on these methods.
If you're using a view object from the registry, or customizing your view object, or using a different view
implementation, you'll want to inject the ViewRenderer with this object. This can be done easily at any time.
$viewRenderer =
Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->setView($view);
There are many ways to modify the ViewRenderer, including setting a different view script to render, specifying
replacements for all replaceable elements of a view script path (including the suffix), choosing a response named
segment to utilize, and more. If you aren't using the conventional modular directory structure, you can even associate
different path specifications with the ViewRenderer.
We encourage you to adapt your code to use the ErrorHandler and ViewRenderer as they are now core
functionality.
1395
Notas de Migración
de Zend Framework
Zend_Currency
Creating an object of Zend_Currency has become simpler. You no longer have to give a script or set it to NULL.
The optional script parameter is now an option which can be set through the setFormat() method.
The setFormat() method takes now an array of options. These options are set permanently and override all
previously set values. Also a new option 'precision' has been added. The following options have been refactored:
• format: Replacement for the old 'locale' parameter which does not set new currencies but only the number format.
• name: Replacement for the ole 'rules' parameter. Sets the full currencies name.
$currency->setFormat(array $options);
The toCurrency() method no longer supports the optional 'script' and 'locale' parameters. Instead it takes an options
array which can contain the same keys as for the setFormat() method.
Zend_Controller
0.9.3 introduces action helpers. As part of this change, the following methods have been removed as they are now
encapsulated in the redirector action helper:
• setRedirectPrependBase(); use
Zend_Controller_Action_Helper_Redirector::setPrependBase().
1396
Notas de Migración
de Zend Framework
Read the action helpers documentation for more information on how to retrieve and manipulate helper objects, and
the redirector helper documentation for more information on setting redirect options (as well as alternate methods for
redirecting).
Zend_Controller
Per previous changes, the most basic usage of the MVC components remains the same:
Zend_Controller_Front::run('/path/to/controllers');
However, the directory structure underwent an overhaul, several components were removed, and several others either
renamed or added. Changes include:
• A new route class for use with the rewrite router was introduced,
Zend_Controller_Router_Route_Module; it covers the default route used by the MVC, and has support
for controller modules.
$action is always required; if no controller is specified, an action in the current controller is assumed. $module
is always ignored unless $controller is specified. Finally, any $params provided will be appended to the
request object. If you do not require the controller or module, but still need to pass parameters, simply specify NULL
for those values.
1397
Notas de Migración
de Zend Framework
Zend_Controller
The most basic usage of the MVC components has not changed; you can still do each of the following:
Zend_Controller_Front::run('/path/to/controllers');
/* -- create a router -- */
$router = new Zend_Controller_RewriteRouter();
$router->addRoute('user',
'user/:username',
array('controller' => 'user', 'action' => 'info')
);
/* -- set it in a controller -- */
$ctrl = Zend_Controller_Front::getInstance();
$ctrl->setRouter($router);
We encourage use of the Response object to aggregate content and headers. This will allow for more flexible output
format switching (for instance, JSON or XML instead of XHTML) in your applications. By default, dispatch()
will render the response, sending both headers and rendering any content. You may also have the front controller return
the response using returnResponse(), and then render the response using your own logic. A future version of
the front controller may enforce use of the response object via output buffering.
There are many additional features that extend the existing API, and these are noted in the documentation.
The main changes you will need to be aware of will be found when subclassing the various components. Key amongst
these are:
• Zend_Controller_Front::dispatch() by default traps exceptions in the response object, and does not
render them, in order to prevent sensitive system information from being rendered. You can override this in several
ways:
$front->throwExceptions(true);
$response->renderExceptions(true);
$front->setResponse($response);
$front->dispatch();
// or:
$front->returnResponse(true);
$response = $front->dispatch();
$response->renderExceptions(true);
echo $response;
1398
Notas de Migración
de Zend Framework
• run() is no longer defined as final, but is also no longer used by the front
controller; its sole purpose is for using the class as a page controller. It now
takes two optional arguments, a Zend_Controller_Request_Abstract $request and a
Zend_Controller_Response_Abstract $response.
• indexAction() no longer needs to be defined, but is encouraged as the default action. This allows using the
RewriteRouter and action controllers to specify different default action methods.
• _redirect() now takes an optional second argument, the HTTP code to return with the redirect, and an
optional third argument, $prependBase, that can indicate that the base URL registered with the request object
should be prepended to the url specified.
• noRouteAction() has been removed. The appropriate way to handle non-existent action methods should you
wish to route them to a default action is using __call():
1399
Notas de Migración
de Zend Framework
1400
Appendix C. Estándares de
codificación de Zend Framework para
PHP
Introducción
Alcance
Este documento provee las pautas para el formato del código y la documentación a personas y equipos que contribuyan
con Zend Framework. Muchos de los desarrolladores que usan Zend Framework han encontrado útiles estos estándares
debido a que el estilo de su código permanece consistente con otros códigos fuente basados en Zend Framework.
También debe resaltarse que especificar completamente los estándares de código requiere un esfuerzo significativo.
Nota
Nota: A veces, los desarrolladores consideran el establecimiento de estándares más importante que lo que el
estándar sugiere realmente al nivel más detallado de diseño. Estas pautas en los estándares de código de Zend
Framework han demostrado funcionar bien en otros projectos ZF. Puede modificar estos estándares o usarlos
en conformidad con los términos de nuestra licencia [http://framework.zend.com/license]
• Convenciones de nombrado
• Estilo de código
• Documentación integrada
Objetivos
Los estándares de código resultan importantes en cualquier proyecto de desarrollo, pero son especialmente importantes
cuando muchos desarrolladores trabajan en el mismo proyecto. Los estándares de código ayudan a asegurar que el
código tenga una alta calidad, menos errores, y pueda ser mantenido fácilmente.
Nota
IMPORTANTE: La inclusión de datos binarios arbitrarios permitidos por __HALT_COMPILER() está
prohibida en los archivos PHP de Zend Framework, así como en cualquier fichero derivado. El uso de esta
característica sólo está permitido en algunos scripts de instalación.
1401
Estándares de codificación de
Zend Framework para PHP
Identación
La identación suele estar compuesta por 4 espacios. Las tabulaciones no están permitidas.
Final de línea
El Final de Línea sigue la convención de archivos de texto Unix. Las líneas deben acabar con un carácter linefeed
(LF). Los caracteres Linefeed están representados con el número 10 ordinal, o el número 0x0A hexadecimal.
Nota: No use retornos de carro (carriage returns, CR) como en las fuentes de Apple (0x0D) o la combinación de retorno
de carro - linefeed (CRLF) estandar para sistemas operativos Windows (0x0D, 0x0A).
Convenciones de Nombres
Clases
Zend Framework se estandariza una convencion de nombres de clases donde los nombres de las clases apuntan
directamente a las carpetas en las que estan contenidas. La carpeta raiz de la biblioteca estandar de ZF es la carpeta
"Zend/", mientras que la carpeta raíz de las bibliotecas extra de ZF es la carpeta "ZendX/". Todas las clases de Zend
Framework están almacenadas jerárquicamente bajo estas carpetas raíz.
Los nombres de clases pueden contener sólo caracteres alfanuméricos. Los números están permitidos en los nombres
de clase, pero desaconsejados en la mayoría de casos. Las barras bajas (_) están permitidas solo como separador de
ruta (el archivo "Zend/Db/Table.php" debe apuntar al nombre de clase "Zend_Db_Table").
Si el nombre de una clase esta compuesto por mas de una palabra, la primera letra de cada palabra debe aparecer en
mayúsculas. Poner en mayúsculas las letras siguientes no está permitido, ej: "Zend_PDF" no está permitido, mientras
que "Zend_Pdf" es admisible.
Estas convenciones definen un mecanismo de pseudo-espacio de nombres para Zend Framework. Zend Framework
adoptará la funcionalidad PHP de espacio de nombres cuando esté disponible y sea factible su uso en las aplicaciones
de nuestros desarrolladores.
Vea los nombres de clase en las bibliotecas estandar y adicionales (extras) como ejemplos de esta convención de
nombres.
Nota
IMPORTANTE: El código que deba distribuirse junto a las bibliotecas ZF, pero no forma parte de las
bibliotecas estándar o extras de Zend (e.g.: código o bibliotecas que no estén distribuídas por Zend) no puede
empezar nunca por "Zend_" o "ZendX_".
Clases Abstractas
En general, las clases abstractas siguen las mismas convenciones que las clases, con una regla adicional: Los
nombres de las clases abstractas deben acabar con el término, "Abstract", y ese término no debe ser precedida
1402
Estándares de codificación de
Zend Framework para PHP
Nota
Esta convención de nombres es nuevo con la versión 1.9.0 de Zend Framework. Las clases que preceden
aquella versión no pueden seguir esta regla, pero serán renombradas en el futuro a fin de cumplir la regla.
Interfaces
En general, las clases abstractas siguen las mismas convenciones que las classes, con una regla adicional: Los
nombres de las interfaces opcionalmente pueden acabar con el término, "Interface",pero término no debe ser precedida
por un guión bajo. Ejemplo, Zend_Controller_Plugin_Interface es considerado un nombre no válido,
pero Zend_Controller_PluginInterface o Zend_Controller_Plugin_PluginInterface serian
nombres válidos.
Si bien esta regla no es necesaria, se recomienda encarecidamente su uso, ya que proporciona una buena refrencia
visual a los desarrolladores, como saber que archivos contienen interfaces en lugar de clases.
Nota
Esta convención de nombres es nuevo con la versión 1.9.0 de Zend Framework. Las clases que preceden
aquella versión no pueden seguir esta regla, pero serán renombradas en el futuro a fin de cumplir la regla.
Nombres de Archivo
Para cualquier otro archivo, sólo caracteres alfanuméricos, barras bajas (_) y guiones (-) están permitidos. Los espacios
en blanco están estrictamente prohibidos.
Cualquier archivo que contenga código PHP debe terminar con la extensión ".php", con la excepción de los scripts
de la vista. Los siguientes ejemplos muestran nombres de archivo admisibles para clases de Zend Framework..:
Zend/Db.php
Zend/Controller/Front.php
Zend/View/Helper/FormRadio.php
Los nombres de archivo deben apuntar a nombres de clases como se describe arriba.
Funciones y Métodos
Los nombres de funciones pueden contener únicamente caracteres alfanuméricos. Las guiones bajos (_) no estan
permitidos. Los números están permitidos en los nombres de función pero no se aconseja en la mayoría de los casos.
Los nombres de funciones deben empezar siempre con una letra minúscula. Cuando un nombre de función consiste
en más de una palabra, la primera letra de cada nueva palabra debe estar en mayúsculas. Esto es llamado comúnmente
como formato "camelCase".
1403
Estándares de codificación de
Zend Framework para PHP
Por norma general, se recomienda la elocuencia. Los nombres de función deben ser lo suficientemente elocuentes
como para describir su propósito y comportamiento.
filterInput()
getElementById()
widgetFactory()
Para la programación orientada a objetos, los métodos de acceso para las instancias o variables estáticas deben ir
antepuestos con un "get" o un "set". Al implementar el patron de diseño, tales como el patrón singleton o el patrón
factory, el nombre del método debe contener en la práctica el nombre del patrón para describir su comportamiento
de forma más completa.
Para el caso en que los métodos son declarados con el modificador "private" o "protected", el primer carácter del
nombre de la variable debe ser una barra baja (_). Este es el único uso admisible de una barra baja en un nombre de
método. Los métodos declarados como públicos no deberían contener nunca una barra baja.
Las funciones de alcance global (también llamadas "funciones flotantes") están permitidas pero desaconsejadas en la
mayoría de los casos. Considere envolver esas funciones en una clase estática.
Variables
Los nombres de variables pueden contener caracteres alfanuméricos. Las barras bajas (_) no están permitidas. Los
números están permitidos en los nombres de variable pero no se aconseja en la mayoría de los casos.
Para las variables de instancia que son declaradas con el modificador "private" o "protected", el primer carácter de
la variable debe ser una única barra baja (_). Este es el único caso admisible de una barra baja en el nombre de una
variable. Las variables declaradas como "public" no pueden empezar nunca por barra baja.
Al igual que los nombres de funciones (ver sección 3.3), los nombres de variables deben empezar siempre con una
letra en minúscula y seguir la convención "camelCaps".
Por norma general, se recomienda la elocuencia. Las variables deberían ser siempre tan elocuentes como prácticas
para describir los datos que el desarrollador pretende almacenar en ellas. Variables escuetas como "$i" y "$n" están
desaconsejadas, salvo para el contexto de los bucles más pequeños. Si un bucle contiene más de 20 líneas de código,
las variables de índice deberían tener nombres más descriptivos.
Constantes
Las constantes pueden contener tanto caracteres alfanuméricos como barras bajas (_). Los números están permitidos.
Todos las letras pertenecientes al nombre de una constante deben aparecer en mayúsculas.
Las palabras dentro del nombre de una constante deben separarse por barras bajas (_). Por ejemplo,
EMBED_SUPPRESS_EMBED_EXCEPTION está permitido, pero EMBED_SUPPRESSEMBEDEXCEPTION no.
Las constantes deben ser definidas como miembros de clase con el modificador "const". Definir constantes en el
alcance global con la función "define" está permitido pero no recomendado.
1404
Estándares de codificación de
Zend Framework para PHP
Estilo de código
Demarcación de código PHP
El código PHP debe estar delimitado siempre por la forma completa de las etiquetas PHP estándar:
<?php
?>
Las etiquetas cortas (short tags) no se permiten nunca. Para archivos que contengan únicamente código PHP, la etiqueta
de cierrre debe omitirse siempre (Ver the section called “General”).
Cadenas de Caracteres
Cadenas Literales de Caracteres
Cuando una cadena es literal (no contiene sustitución de variables), el apóstrofo o "comilla" debería ser usado siempre
para delimitar la cadena:
$a = 'Ejemplo String';
En esta sintáxis es preferible escapar apóstrofes, ya que es mucho más fácil de leer.
Sustitución de Variables
La sustitución de variables está permitida en cualquiera de estas formas:
Concatenación de cadenas
Las cadenas deben ser concatenadas usando el operador punto ("."). Un espacio debe añadirse siempre antes y después
del operador "." para mejorar la legibilidad:
1405
Estándares de codificación de
Zend Framework para PHP
Al concatenar cadenas con el operador ".", se recomienda partir la sentencia en múltiples líneas para mejorar la
legibilidad. En estos casos, cada linea sucesiva debe llevar un margen de espacios en blanco de forma que el operador
"." está alineado bajo el operador "=":
Arrays
Arrays Indexados Numéricamente
No están permitidos números negativos como índices.
Un array indexado puede empezar por cualquier valor no negativo, sin embargo, no se recomiendan índices base
distintos a 0.
Al declarar arrays indexados con la función array, un espacio de separación deben añadirse después de cada coma,
para mejorar la legibilidad:
Se permite declarar arrays indexados multilínea usando la construcción "array". En este caso, cada línea sucesiva debe
ser tabulada con cuatro espacios de forma que el principio de cada línea está alineado:
Alternativamente, el elemento inicial del array puede comenzar en la siguiente línea. Si es así, debe ser alineado en
un nivel de sangría superior a la línea que contiene la declaración del array, y todas las sucesivas líneas deben tener
la mismo indentación, el paréntesis de cierre debe ser en una nueva línea al mismo nivel de indentación que la línea
que contiene la declaración del array:
$sampleArray = array(
1, 2, 3, 'Zend', 'Studio',
$a, $b, $c,
56.44, $d, 500,
);
Al utilizar esta última declaración, recomendamos la utilización de una coma detrás de el último elemento de la matriz,
lo que minimizará el impacto de añadir nuevos elementos en las siguientes líneas, y ayuda a garantizar que no se
produzcan errores debido a la falta de una coma.
Arrays Asociativos
Al declarar arrays asociativos con la construcción array, se recomienda partir la declaración en múltiples líneas. En
este caso, cada línea sucesiva debe ser tabuladas con cuatro espacios de forma que tanto las llaves como los valores
están alineados:
1406
Estándares de codificación de
Zend Framework para PHP
Alternativamente, el elemento inicial del array puede comenzar en la siguiente línea. Si es así, debe ser alineado en
un nivel de sangría superior a la línea que contiene la declaración del array, y todas las sucesivas líneas deben tener
la mismo indentación, el paréntesis de cierre debe ser en una nueva línea al mismo nivel de indentación que la línea
que contiene la declaración del array: Para mejor legibilidad, los diversos operadores de asiganción "=>" deben ser
rellenados con espacios en blanco hasta que se alinien.
$sampleArray = array(
'firstKey' => 'firstValue',
'secondKey' => 'secondValue',
);
Al utilizar esta última declaración, recomendamos la utilización de una coma detrás de el último elemento de la matriz,
lo que minimizará el impacto de añadir nuevos elementos en las siguientes líneas, y ayuda a garantizar que no se
produzcan errores debido a la falta de una coma.
Clases
Declaración de clases
Las Clases deben ser nombradas de acuerdo a las convencion de nombres de Zend Framework.
La llave "{" deberá escribirse siempre en la línea debajo del nombre de la clase ("one true brace").
Cada clase debe contener un bloque de documentación acorde con el estándar de PHPDocumentor.
Todo el código contenido en una clase debe ser separado con cuatro espacios.
Incluir código adicional en archivos de clase está permitido pero esta desaconsejado. En archivos de ese tipo, dos
líneas en blanco deben separar la clase de cualquier código PHP adicional en el archivo de clase.
/**
* Bloque de Documentación aquí
*/
class SampleClass
{
// el contenido de la clase
// debe separarse con cuatro espacios
}
Las clases que extiendan otras clases o interfaces deberían declarar sus dependencias en la misma línea siempre que
sea posible.
1407
Estándares de codificación de
Zend Framework para PHP
{
}
Si como resultado de esas declaraciones, la longitud de la línea excede la longitud del Tamaño máximo de línea, se
debe romper la línea antes de la palabra clave "extends" y / o "implements" e indentarlo con un nivel de indentación
(4 espacios).
class SampleClass
extends FooAbstract
implements BarInterface
{
}
If the class implements multiple interfaces and the declaration exceeds the maximum line length, break after each
comma separating the interfaces, and indent the interface names such that they align.
class SampleClass
implements BarInterface,
BazInterface
{
}
Cualquier variable declarada en una clase debe ser listada en la parte superior de la clase, por encima de las
declaraciones de cualquier método.
La construcción var no está permitido. Las variables de miembro siempre declaran su visibilidad usando uno
los modificadores private, protected, o public. Dar acceso a las variables de miembro declarándolas
directamente como public está permitido pero no se aconseja en favor de accesor methods (set & get).
Funciones y Métodos
Declaración de Funciones y Métodos
Las Funciones deben ser nombradas de acuerdo a las convenciones de nombrado de Zend Framework.
Los métodos dentro de clases deben declarar siempre su visibilidad usando un modificador private, protected,
o public.
Como en las clases, la llave "{" debe ser escrita en la línea siguiente al nombre de la función ("one true brace" form).
No está permitido un espacio entre el nombre de la función y el paróntesis de apertura para los argumentos.
/**
1408
Estándares de codificación de
Zend Framework para PHP
In cases where the argument list exceeds the maximum line length, you may introduce line breaks. Additional
arguments to the function or method must be indented one additional level beyond the function or method declaration.
A line break should then occur before the closing argument paren, which should then be placed on the same line as
the opening brace of the function or method with one space separating the two, and at the same indentation level as
the function or method declaration. The following is an example of one such situation:
/**
* Documentation Block Here
*/
class Foo
{
/**
* Documentation Block Here
*/
public function bar($arg1, $arg2, $arg3,
$arg4, $arg5, $arg6
) {
// all contents of function
// must be indented four spaces
}
}
Nota
NOTA: El paso por referencia es el único mecanismo de paso de parámetros permitido en una declaración
de método.
/**
* Bloque de Documentación aquí
*/
class Foo
{
/**
* Bloque de Documentación aquí
*/
public function bar(&$baz)
{}
}
1409
Estándares de codificación de
Zend Framework para PHP
El valor de retorno no debe estar indicado entre paréntesis. Esto podría afectar a la legibilidad, además de romper el
código si un método se modifica posteriormente para que devuelva por referencia.
/**
* Bloque de Documentación aquí
*/
class Foo
{
/**
* INCORRECTO
*/
public function bar()
{
return($this->bar);
}
/**
* CORRECTO
*/
public function bar()
{
return $this->bar;
}
}
threeArguments(1, 2, 3);
La llamada por referencia está estrictamente prohibida. Vea la sección de declaraciones de funciones para el método
correcto de pasar argumentos por referencia.
Al pasar arrays como argumentos a una función, la llamada a la función puede incluir el indicador "hint" y puede
separarse en múltiples líneas para aumentar la legibilidad. En esos casos, se aplican las pautas normales para escribir
arrays:
threeArguments(array(
1, 2, 3, 'Zend', 'Studio',
$a, $b, $c,
56.44, $d, 500
), 2, 3);
1410
Estándares de codificación de
Zend Framework para PHP
Sentencias de Control
If/Else/Elseif
Las sentencias de control basadas en las construcciones if y elseif deben tener un solo espacio en blanco antes
del paréntesis de apertura del condicional y un solo espacio en blanco después del paréntesis de cierre.
Dentro de las sentencias condicionales entre paréntesis, los operadores deben separarse con espacios, por legibilidad.
Se aconseja el uso de paréntesis internos para mejorar la agrupación lógica en expresiones condicionales más largas.
La llave de apertura "{" se escribe en la misma línea que la sentencia condicional. La llave de cierre "}" se escribe
siempre en su propia línea. Cualquier contenido dentro de las llaves debe separarse con cuatro espacios en blanco.
if ($a != 2) {
$a = 2;
}
If the conditional statement causes the line length to exceed the maximum line length and has several clauses, you
may break the conditional into multiple lines. In such a case, break the line prior to a logic operator, and pad the line
such that it aligns under the first character of the conditional clause. The closing paren in the conditional will then
be placed on a line with the opening brace, with one space separating the two, at an indentation level equivalent to
the opening control statement.
if (($a == $b)
&& ($b == $c)
|| (Foo::CONST == $d)
) {
$a = $d;
}
The intention of this latter declaration format is to prevent issues when adding or removing clauses from the conditional
during later revisions.
Para las declaraciones "if" que incluyan "elseif" o "else", las convenciones de formato son similares a la construcción
"if". Los ejemplos siguientes demuestran el formato correcto para declaraciones "if" con construcciones "else" y/o
"elseif":
if ($a != 2) {
$a = 2;
} else {
$a = 7;
}
if ($a != 2) {
$a = 2;
} elseif ($a == 3) {
$a = 4;
} else {
$a = 7;
}
if (($a == $b)
1411
Estándares de codificación de
Zend Framework para PHP
PHP permite escribir sentencias sin llaves -{}- en algunas circunstancias. Este estándar de código no hace ninguna
diferenciación- toda sentencia "if", "elseif" o "else" debe usar llaves.
El uso de la construcción "elseif" está permitido pero no se aconseja, en favor de la combinación "else if".
Switch
Las declaraciones de control escritas con la declaración "switch" deben tener un único espacio en blanco antes del
paréntesis de apertura del condicional y después del paréntesis de cierre.
Todo contenido dentro de una declaración "switch" debe separarse usando cuatro espacios. El contenido dentro de
cada declaración "case" debe separarse usando cuatro espacios adicionales.
switch ($numPeople) {
case 1:
break;
case 2:
break;
default:
break;
}
Nota
NOTA: En ocasiones, resulta útil escribir una declaración case que salta al siguiente case al no incluir un
break o return dentro de ese case. Para distinguir estos casos de posibles errores, cualquier declaración
donde break o return sean omitidos deberán contener un comentario indicando que se omitieron
intencionadamente.
Documentación integrada
Formato de documentación
Todos los bloques de documentación ("docblocks") deben ser compatibles con el formato de phpDocumentor.
Describir el formato de phpDocumentor está fuera del alcance de este documento. Para más información, visite: http://
phpdoc.org/
1412
Estándares de codificación de
Zend Framework para PHP
Todos los archivos de clase deben contener un bloque de documentación "a nivel de archivo" al principio de cada
archivo y un bloque de documentación "a nivel de clase" inmediatamente antes de cada clase. Ejemplo de estos bloques
de documentación pueden encontrarse debajo.
Archivos
Cada archivo que contenga código PHP debe tener un bloque de documentación al principio del archivo que contenga
como mínimo las siguientes etiquetas phpDocumentor:
/**
* Descripción corta del fichero
*
* Descripción larga del fichero (si la hubiera)...
*
* LICENSE: Some license information
*
* @category Zend
* @package Zend_Magic
* @subpackage Wand
* @copyright 2008 Zend Technologies
* @license http://framework.zend.com/license BSD License
* @version $Id:$
* @link http://framework.zend.com/package/PackageName
* @since File available since Release 1.5.0
*/
The @package annotation must be assigned, and should be equivalent to the component name of the class contained
in the file; typically, this will only have two segments, the "Zend" prefix, and the component name.
The @subpackage annotation is optional. If provided, it should be the subcomponent name, minus the class prefix. In
the example above, the assumption is that the class in the file is either "Zend_Magic_Wand", or uses that classname
as part of its prefix.
Clases
Cada clase debe contener un bloque de documentación que contenga como mínimo las siguientes etiquetas
phpDocumentor:
/**
* Descripción corta de la clase
*
* Descripcion larga de la clase (si la hubiera)...
*
* @category Zend
* @package Zend_Magic
* @subpackage Wand
* @copyright 2008 Zend Technologies
* @license http://framework.zend.com/license BSD License
* @version Release: @package_version@
* @link http://framework.zend.com/package/PackageName
* @since Class available since Release 1.5.0
1413
Estándares de codificación de
Zend Framework para PHP
The @package annotation must be assigned, and should be equivalent to the component to which the class belongs;
typically, this will only have two segments, the "Zend" prefix, and the component name.
The @subpackage annotation is optional. If provided, it should be the subcomponent name, minus the class prefix. In
the example above, the assumption is that the class described is either "Zend_Magic_Wand", or uses that classname
as part of its prefix.
Funciones
Cada función, incluyendo métodos de objeto, debe contener un bloque de documentación que contenga como mínimo:
No es necesario incluir la etiqueta "@access" si el nivel de acceso es conocido de antemano por el modificador "public",
"private", o "protected" usado para declarar la función.
Si una función/método puede lanzar una excepción, utilice @throws para todos los tipos de excepciones conocidas:
1414
Appendix D. Zend Framework
Documentation Standard
Overview
Scope
This document provides guidelines for creation of the end-user documentation found within Zend Framework.
It is intended as a guide to Zend Framework contributors, who must write documentation as part of component
contributions, as well as to documentation translators. The standards contained herein are intended to ease translation
of documentation, minimize visual and stylistic differences between different documentation files, and make finding
changes in documentation easier with diff tools.
You may adopt and/or modify these standards in accordance with the terms of our license [http://framework.zend.com/
license].
Topics covered in the Zend Framework documentation standards include documentation file formatting and
recommendations for documentation quality.
XML files from translated languages must also include a revision tag containing the revision of the corresponding
English-language file the translation was based on.
Indentation
Indentation should consist of 4 spaces. Tabs are not allowed.
Tags which are at the same level must have the same indentation.
<sect1>
1415
Zend Framework
Documentation Standard
</sect1>
<sect1>
</sect1>
Tags which are one level under the previous tag must be indented with 4 additional spaces.
<sect1>
<sect2>
</sect2>
</sect1>
Multiple block tags within the same line are not allowed; multiple inline tags are allowed, however.
Line Termination
Line termination follows the Unix text file convention. Lines must end with a single linefeed (LF) character. Linefeed
characters are represented as ordinal 10, or hexadecimal 0x0A.
Note: Do not use carriage returns (CR) as is the convention in Apple OS's (0x0D) or the carriage return - linefeed
combination (CRLF) as is standard for the Windows OS (0x0D, 0x0A).
Empty tags
Empty tags are not allowed; all tags must contain text or child tags.
<para>
</para>
1416
Zend Framework
Documentation Standard
<!-- OK -->
This is the class <classname>Zend_Class</classname>.
Closing block tags may be preceded by whitespace equivalent to the current indentation level, but no more than that
amount.
<!-- OK -->
<sect1>
</sect1>
<!-- OK -->
This is the class <classname>Zend_Class</classname>
<para>
Another paragraph.
</para>
<!-- OK -->
<para>
Some text...
... and more text
1417
Zend Framework
Documentation Standard
</para>
<para>
Another paragraph.
</para>
<!-- OK -->
<para>
Some text...
</para>
<para>
More text...
</para>
The first child tag should open directly below its parent, with no empty line between them; the last child tag should
close directly before the closing tag of its parent.
<sect2>
</sect2>
<sect2>
</sect2>
<sect2>
</sect2>
</sect1>
<!-- OK -->
<sect1>
<sect2>
</sect2>
<sect2>
</sect2>
1418
Zend Framework
Documentation Standard
<sect2>
</sect2>
</sect1>
Program Listings
The opening <programlisting> tag must indicate the appropriate "language" attribute and be indented at the same
level as its sibling blocks.
<para>Sibling paragraph.</para>
<programlisting language="php"><![CDATA[
<programlisting> sections must not add linebreaks or whitespace at the beginning or end of the section, as these are
then represented in the final output.
$render = "xxx";
]]></programlisting>
<!-- OK -->
<programlisting language="php"><![CDATA[
$render = "xxx";
]]></programlisting>
Ending CDATA and <programlisting> tags should be on the same line, without any indentation.
<!-- OK -->
<programlisting language="php"><![CDATA[
$render = "xxx";
]]></programlisting>
The <programlisting> tag should contain the "language" attribute with a value appropriate to the contents of the
program listing. Typical values include "css", "html", "ini", "javascript", "php", "text", and "xml".
1419
Zend Framework
Documentation Standard
For program listings containing only PHP code, PHP tags (e.g., "<?php", "?>") are not required, and should not be
used. They simply clutter the narrative, and are implied by the use of the <programlisting> tag.
<programlisting language="php"<![CDATA[
<?php
// ...
?>
]]></programlisting>
Line lengths within program listings should follow the coding standards recommendations.
Refrain from using require_once(), require(), include_once(), and include() calls within PHP
listings. They simply clutter the narrative, and are largely obviated when using an autoloader. Use them only when
they are essential to the example.
<para>
The class <classname>Zend_Class</classname>.
</para>
varname
Variables must be wrapped in the <varname> tag. Variables must be written using the "$" sigil. No other content is
allowed within this tag, unless a class name is used, which indicates a class variable.
<para>
The variable <varname>$var</varname> and the class variable
<varname>Zend_Class::$var</varname>.
1420
Zend Framework
Documentation Standard
</para>
methodname
Methods must be wrapped in the <methodname> tag. Methods must either include the full method signature or at the
least a pair of closing parentheses (e.g., "()"). No other content is allowed within this tag, unless a class name is used,
which indicates a class method.
<para>
The method <methodname>foo()</methodname> and the class method
<methodname>Zend_Class::foo()</methodname>. A method with a full signature:
<methodname>foo($bar, $baz)</methodname>
</para>
constant
Use the <constant> tag when denoting constants. Constants must be written in UPPERCASE. No other content is
allowed within this tag, unless a class name is used, which indicates a class constant.
<para>
The constant <constant>FOO</constant> and the class constant
<constant>Zend_Class::FOO</constant>.
</para>
filename
Filenames and paths must be wrapped in the <filename> tag. No other content is allowed in this tag.
<para>
The filename <filename>application/Bootstrap.php</filename>.
</para>
command
Commands, shell scripts, and program calls must be wrapped in the <command> tag. If the command includes
arguments, these should also be included within the tag.
<para>
Execute <command>zf.sh create project</command>.
</para>
code
Usage of the <code> tag is discouraged, in favor of the other inline tasks discussed previously.
1421
Zend Framework
Documentation Standard
<!-- OK -->
<title>Using Zend_Class</title>
Recommendations
Use editors without autoformatting
For editing the documentation, typically you should not use formal XML editors. Such editors normally autoformat
existing documents to fit their own standards and/or do not strictly follow the docbook standard. As examples, we
have seen them erase the CDATA tags, change 4 space seperation to tabs or 2 spaces, etc.
The style guidelines were written in large part to assist translators in recognizing the lines that have changed using
normal diff tools. Autoformatting makes this process more difficult.
Use Images
Good images and diagrams can improve readability and comprehension. Use them whenever they will assist in these
goals. Images should be placed in the documentation/manual/en/figures/ directory, and be named after
the section identifier in which they occur.
When writing your examples for inclusion in the manual, follow all coding standards and documentation standards.
Use Links
Link to other sections of the manual or to external sources instead of recreating documentation.
Linking to other sections of the manual may be done using either the <xref> tag (which will substitute the section title
for the link text) or the <link> tag (to which you must provide link text).
<para>
"Xref" links to a section: <xref
linkend="doc-standard.recommendations.links" />.
</para>
<para>
1422
Zend Framework
Documentation Standard
<para>
The <ulink url="http://framework.zend.com/">Zend Framework site</ulink>.
</para>
1423
Appendix E. Recommended Project
Structure for Zend Framework MVC
Applications
Overview
Many developers seek guidance on the best project structure for a Zend Framework project in a relatively flexible
environment. A "flexible" environment is one in which the developer can manipulate their file systems and web server
configurations as needed to achieve the most ideal project structure to run and secure their application. The default
project structure will assume that the developer has such flexibility at their disposal.
The following directory structure is designed to be maximally extensible for complex projects, while providing a simple
subset of folder and files for project with simpler requirements. This structure also works without alteration for both
modular and non-modular Zend Framework applications. The .htaccess files require URL rewrite functionality in
the web server as described in the Rewrite Configuration Guide, also included in this appendix.
It is not the intention that this project structure will support all possible Zend Framework project requirements. The
default project profile used by Zend_Tool reflect this project structure, but applications with requirements not
supported by this structure should use a custom project profile.
1424
Recommended Project Structure for
Zend Framework MVC Applications
docs/
library/
public/
css/
images/
js/
.htaccess
index.php
scripts/
jobs/
build/
temp/
tests/
The following describes the use cases for each directory as listed.
• application/: This directory contains your application. It will house the MVC system, as well as configurations,
services used, and your bootstrap file.
• controllers/, models/, and views/: These directories serve as the default controller, model or view
directories. Having these three directories inside the application directory provides the best layout for starting a
simple project as well as starting a modular project that has global controllers/models/views.
• controllers/helpers/: These directories will contain action helpers. Action helpers will be namespaced
either as "Controller_Helper_" for the default module or "<Module>_Controller_Helper" in other
modules.
• layouts/: This layout directory is for MCV-based layouts. Since Zend_Layout is capable of MVC- and
non-MVC-based layouts, the location of this directory reflects that layouts are not on a 1-to-1 relationship with
controllers and are independent of templates within views/.
• modules/: Modules allow a developer to group a set of related controllers into a logically organized group. The
structure under the modules directory would resemble the structure under the application directory.
• services/: This directory is for your application specific web-service files that are provided by your
application, or for implementing a Service Layer [http://www.martinfowler.com/eaaCatalog/serviceLayer.html]
for your models.
• Bootstrap.php: This file is the entry point for your application, and should implement
Zend_Application_Bootstrap_Bootstrapper. The purpose for this file is to bootstrap the
application and make components available to the application by initializing them.
• data/: This directory provides a place to store application data that is volatile and possibly temporary. The
disturbance of data in this directory might cause the application to fail. Also, the information in this directory may
or may not be committed to a subversion repository. Ejemplos of things in this directory are session files, cache
files, sqlite databases, logs and indexes.
• library/: This directory is for common libraries on which the application depends, and should be on the PHP
include_path. Developers should place their application's library code under this directory in a unique namespace,
following the guidelines established in the PHP manual's Userland Naming Guide [http://www.php.net/manual/
en/userlandnaming.php], as well as those established by Zend itself. This may directory may also include Zend
Framework itself; if so, you would house it in library/Zend/.
1425
Recommended Project Structure for
Zend Framework MVC Applications
• public/: This directory contains all public files for your application. index.php sets up and invokes
Zend_Application, which in turn invokes the application/Bootstrap.php file, resulting in
dispatching the front controller. The web root of your web server would typically be set to this directory.
• scripts/: This directory contains maintenance and/or build scripts. Such scripts might include command line,
cron, or phing build scripts that are not executed at runtime but are part of the correct functioning of the application.
• temp/: The temp/ folder is set aside for transient application data. This information would not typically be
committed to the applications svn repository. If data under the temp/ directory were deleted, the application should
be able to continue running with a possible decrease in performance until data is once again restored or recached.
• tests/: This directory contains application tests. These could be hand-written, PHPUnit tests, Selenium-RC based
tests or based on some other testing framework. By default, library code can be tested by mimicing the directory
structure of your library/ directory. Additionally, functional tests for your application could be written mimicing
the application/ directory structure (including the application subdirectory).
Module Structure
The directory structure for modules should mimic that of the application/ directory in the recommended project
structure:
<modulename>/
configs/
application.ini
controllers/
helpers/
layouts/
filters/
helpers/
scripts/
models/
services/
views/
filters/
helpers/
scripts/
Bootstrap.php
The purpose of these directories remains exactly the same as for the recommended project directory structure.
1426
Recommended Project Structure for
Zend Framework MVC Applications
<VirtualHost my.domain.com:80>
ServerName my.domain.com
DocumentRoot /path/to/server/root/my.domain.com/public
RewriteEngine off
<Location />
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
</Location>
</VirtualHost>
Note the slash ("/") prefixing index.php; the rules for .htaccess differ in this regard.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
There are many ways to configure mod_rewrite; if you would like more information, see Jayson Minard's Blueprint
for PHP Applications: Bootstrapping [http://devzone.zend.com/a/70].
1427
Recommended Project Structure for
Zend Framework MVC Applications
<add input="{REQUEST_FILENAME}"
matchType="IsFile" pattern=""
ignoreCase="false" />
<add input="{REQUEST_FILENAME}"
matchType="IsDirectory"
pattern=""
ignoreCase="false" />
</conditions>
<action type="None" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^.*$" />
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
1428
Appendix F. Guía de Rendimiento de
Zend Framework
Introduction
The purpose of this appendix is to provide some concrete strategies for improving the performance of your Zend
Framework applications. The guide is presented in a "Question and Answer" format, and broken into areas of concern.
Class Loading
Anyone who ever performs profiling of a Zend Framework application will immediately recognize that class loading
is relatively expensive in Zend Framework. Between the sheer number of class files that need to be loaded for many
components, to the use of plugins that do not have a 1:1 relationship between their class name and the file system,
the various calls to include_once() and require_once() can be problematic. This chapter intends to provide
some concrete solutions to these issues.
There are two easy ways to ensure this. First, you can hardcode the paths in your php.ini, httpd.conf, or
.htaccess. Second, you can use PHP's realpath() function when setting your include_path:
$paths = array(
realpath(dirname(__FILE__) . '/../library'),
'.',
);
set_include_path(implode(PATH_SEPARATOR, $paths);
You can use relative paths -- so long as they are relative to an absolute path:
define('APPLICATION_PATH', realpath(dirname(__FILE__)));
$paths = array(
APPLICATION_PATH . '/../library'),
'.',
);
set_include_path(implode(PATH_SEPARATOR, $paths);
However, even so, it's typically a trivial task to simply pass the path to realpath().
1429
Guía de Rendimiento
de Zend Framework
Another optimization is to combine paths. For instance, Zend Framework follows PEAR naming conventions; thus, if
you are using PEAR libraries (or libraries from another framework or component library that follows PEAR CS), try
to put all of these libraries on the same include_path. This can often be achieved by something as simple as symlinking
one or more libraries into a common directory.
First, we'll create a library directory in our project. Inside that directory, we'll symlink our Zend Framework's
library/Zend directory, as well as the necessary directories from our PEAR installation:
library
Archive/
PEAR/
PHPUnit/
Zend/
This allows us to add our own library code if necessary, while keeping shared libraries intact.
Next, we'll opt to create our include_path programmatically within our public/index.php file. This allows us to
move our code around on the file system, without needing to edit the include_path every time.
We'll borrow ideas from each of the suggestions above: we'll use absolute paths, as determined using realpath();
we'll include Zend Framework's include path early; we've already consolidated include_paths; and we'll put the current
directory as the last path. In fact, we're doing really well here -- we're going to end up with only two paths.
$paths = array(
realpath(dirname(__FILE__) . '/../library'),
1430
Guía de Rendimiento
de Zend Framework
'.'
);
set_include_path(implode(PATH_SEPARATOR, $paths));
However, most benefits you may reap from autoloading are negated if your library code is still performing
require_once() calls -- which is precisely the case with Zend Framework. So, the question is: how can you
eliminate those require_once() calls in order to maximize autoloader performance?
% cd path/to/ZendFramework/library
% find . -name '*.php' -not -wholename '*/Loader/Autoloader.php' -print0 | \
xargs -0 sed --regexp-extended --in-place 's/(require_once)/\/\/ \1/g'
This one-liner (broken into two lines for readability) iterates through each PHP file and tells it to replace each instance
of 'require_once' with '// require_once', effectively commenting out each such statement.
This command could be added to an automated build or release process trivially, helping boost performance in your
production application. It should be noted, however, that if you use this technique, you must utilize autoloading; you
can do that from your "public/index.php" file with the following code:
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
The plugin loader allows you to register class prefix / path pairs, allowing you to specify class files in non-standard
paths. Each prefix can have multiple paths associated with it. Internally, the plugin loader loops through each prefix,
and then through each path attached to it, testing to see if the file exists and is readable on that path. It then loads
it, and tests to see that the class it is looking for is available. As you might imagine, this can lead to many stat calls
on the file system.
Multiply this by the number of components that use the PluginLoader, and you get an idea of the scope of this issue.
At the time of this writing, the following components made use of the PluginLoader:
• Zend_Controller_Action_HelperBroker: helpers
1431
Guía de Rendimiento
de Zend Framework
• Zend_File_Transfer: adapters
• Zend_Form: elements, validators, filters, decorators, captcha and file transfer adapters
• Zend_Paginator: adapters
Zend_Db Performance
Zend_Db is a database abstraction layer, and is intended to provide a common API for SQL operations.
Zend_Db_Table is a Table Data Gateway, intended to abstract common table-level database operations. Due to
their abstract nature and the "magic" they do under the hood to perform their operations, they can sometimes introduce
performance overhead.
1432
Guía de Rendimiento
de Zend Framework
Run EXPLAIN on your queries, and test a variety of approaches until you can reliably hit your indices in the most
performant way -- and then hardcode the SQL as a class property or constant.
If the SQL requires variable arguments, provide placeholders in the SQL, and utilize a combination of vsprintf()
and array_walk() to inject the values into the SQL:
• Array: this is the fastest, as it is, by definition, parsed into a native PHP format immediately on inclusion.
• CSV: uses fgetcsv() to parse a CSV file and transform it into a native PHP format.
• INI: uses parse_ini_file() to parse an INI file and transform it into a native PHP format. This and the CSV
adapter are roughly equivalent performance-wise.
• Gettext: The gettext adapter from Zend Framework does not use the gettext extension as it is not thread safe and
does not allow specifying more than one locale per server. As a result, it is slower than using the gettext extension
directly, but, because the gettext format is binary, it's faster to parse than XML.
1433
Guía de Rendimiento
de Zend Framework
If high performance is one of your concerns, we suggest utilizing one of the above adapters.
You can read about caching of translation and localization strings in the following locations:
• Zend_Locale caching
View Rendering
When using Zend Framework's MVC layer, chances are you will be using Zend_View. Zend_View is performs
well compared to other view or templating engines; since view scripts are written in PHP, you do not incur the overhead
of compiling custom markup to PHP, nor do you need to worry that the compiled PHP is not optimized. However,
Zend_View presents its own issues: extension is done via overloading (view helpers), and a number of view helpers,
while carrying out key functionality do so with a performance cost.
Internally, Zend_View uses the PluginLoader to look up helper classes. This means that for each helper you call,
Zend_View needs to pass the helper name to the PluginLoader, which then needs to determine the class name, load
the class file if necessary, and then return the class name so it may be instantiated. Subsequent uses of the helper are
much faster, as Zend_View keeps an internal registry of loaded helpers, but if you use many helpers, the calls add up.
The question, then, is: how can you speed up helper resolution?
1434
Guía de Rendimiento
de Zend Framework
/**
* Proxy to url view helper
*
* @param array $urlOptions Options passed to the assemble method
* of the Route object.
* @param mixed $name The name of a Route to use. If null it will
* use the current Route
* @param bool $reset Whether or not to reset the route defaults
* with those provided
* @return string Url for the link href attribute.
*/
public function url(array $urlOptions = array(), $name = null,
$reset = false, $encode = true
) {
if (!array_key_exists('url', $this->_localHelperObjects)) {
$this->_localHelperObjects['url'] = new Zend_View_Helper_Url();
$this->_localHelperObjects['url']->setView($view);
}
$helper = $this->_localHelperObjects['url'];
return $helper->url($urlOptions, $name, $reset, $encode);
}
/**
* Echo a message
*
* Direct implementation.
*
* @param string $string
* @return string
*/
public function message($string)
{
return "<h1>" . $this->escape($message) . "</h1>\n";
}
}
Either way, this technique will substantially reduce the overhead of the helper system by avoiding calls to the
PluginLoader entirely, and either benefiting from autoloading or bypassing it altogether.
1435
Guía de Rendimiento
de Zend Framework
• $module: the name of the module in which the view script resides; or, if no third argument is provided and this
is an array or object, it will be the $model argument.
• $model: an array or object to pass to the partial representing the clean data to assign to the view.
The power and use of partial() come from the second and third arguments. The $module argument allows
partial() to temporarily add a script path for the given module so that the partial view script will resolve to that
module; the $model argument allows you to explicitly pass variables for use with the partial view. If you're not
passing either argument, use render() instead!
Basically, unless you are actually passing variables to the partial and need the clean variable scope, or rendering a view
script from another MVC module, there is no reason to incur the overhead of partial(); instead, use Zend_View's
built-in render() method to render the view script.
1436
Guía de Rendimiento
de Zend Framework
While the ActionStack still requires a dispatch cycle, this is still cheaper than the action() view helper as it does not
need to clone objects and reset internal state. Additionally, it ensures that all pre and post dispatch plugins are invoked,
which may be of particular concern if you are using front controller plugins for handling ACL's to particular actions.
// bug/list.phtml:
echo "<ul>\n";
foreach ($this->bugs as $bug) {
printf("<li><b>%s</b>: %s</li>\n",
$this->escape($bug->id),
$this->escape($bug->summary)
);
}
echo "</ul>\n";
This could be refactored to a view helper that looks like the following:
1437
Guía de Rendimiento
de Zend Framework
This has two benefits: it no longer incurs the overhead of the action() view helper, and also presents a more
semantically understandable API.
1438
Appendix G. Copyright Information
Los siguientes son los derechos de autor aplicables a las porciones de Zend Framework.
1439
Index
1440