Tag Archives: zend

Custom Filters in Zend Framework

Although there might be tons of articles about this theme I’m going to describe it in breve. What I was supposed to do is to filter a string, which is supposed to be a date, but if only the year or year/month pair were present I should filter them correctly.

So the first thing I tried to achieve is to write a custom Zend Filter. It seem to me a simple task and it is.

The only thing you should do is to define a class in Zend/Filter/ folder with the name of the filter of course. In my case this was Zend_Filter_Mydate, which really sounds very strange, but you can name it after whatever you want.

The only thing you need in that class is a public function filter by giving it one parameter, in most cases, and returning the filtered value.

Here’s a snippet:

<?php
 
require_once 'Zend_Filter_Interface.php'
 
class Zend_Filter_Mydate extends Zend_Filter_Interface
{
 
    public function filter($value)
    {
        // do something with the source and return the filtered value
    }
 
}

Send Mail with Zend Framework

Simple, but Doesn’t Work!

It may sound strange, because all this is quite well documented in the Zend Framework documentation. Indeed it’s very very simple and all is done only by few lines of code.

$mail = new Zend_Mail();
$mail->setFrom('sender@example.com');
$mail->setBodyHtml('some message - it may be html formatted text');
$mail->addTo('recipient@example.com', 'recipient');
$mail->setSubject('subject');
$mail->send();

Even it look simple though, it may not work on your localhost while you’re trying to make it work! Because you’ve to have sendmail setup. And in Zend Framework sendmail is the default transport protocol for sending mails.

What You Have to Do to Make it Work?

Actually you’ve simply to add a transport protocol, just like that:

$tr = new Zend_Mail_Transport_Smtp('smtp.example.com',
                    array('auth' => 'login',
                             'username' => 'username',
                             'password' => 'password'));
Zend_Mail::setDefaultTransport($tr);

Where Did I Get These?

Well look at your favorite mail account you probably use every day. It may be hosted in GMail or wherever, but there are some configurations you have to port under Zend’s code and everything will be just fine until you develop the app!

Get the browser locale and language from Zend_Locale

In a short snipped I’d like to mention the method of use in Zend_Locale:

$locale = new Zend_Locale();
$browserLocale = $locale->getBrowser();

it’s quite useful! Sometimes you don’t need to redirect user only by IP or domain name. Think about browser’s language!

Cool tutorial about Zend_Paginator

Every part of Zend Framework is indeed very profesional and useful. But as it happens often some modules are less used than others. Don’t know why but my feeling is that Zend_Paginator, a wonderful tool for pagination is really misunderstood. And in fact it does one of the most common web development tasks. It builds the abstraction for a component that everybody uses in a web project – pagination.

It make sense if there where more tutorials like the following one describing its usage! Many thanks to Joey Rivera for a great tutorial about Zend_Paginator.

Even more this tutorials goes behind the pure usage of the paginator but it helps you understand the integration with one of the most used web apps today – Twitter and another Zend Framework component – Zend_Cache.

joey rivera blog

Zend_Date – make it work and benefit with locales

Although the documentation of Zend Framework is one of the most well structured documentation I’ve ever read it happened to me to stuck into it. And that happened exactly when I tried to use the date component of ZF – Zend_Date.

Why I’d prefer Zend_Date?

We all know what the date function of PHP is, and what it does. It has reach set of formating string that help you do the job but as I’m working mostly on multilingual projects almost every project needs multilingual dates, and more or less nobody wants only numbers into the dates. And all this is pretty natural, instead of using the simple

01.01.2010

format, which beside it’s natural ugliness is not clear. Because as you may know in the US this can be understood as month.date.year, while in Europe this is date.month.year. And when it comes to dates like 02.01.2010 this can be really frustrating.

Beside this ugly format, you can choose to format your dates either with:

1 January 2010 or 1 Jan 2010

which is clear enough, but it’s only for english speakers. You know that in a spanish speaking country you should format the same date as:

1 Ene 2010

where “ene” means enero – january in spanish. How should you overcome this natural PHP problem?

Well the answer is by using Zend_Date

The problem I found in Zend_Date usage is that the native copy/paste technique from the doc page doesn’t seem to work. What the doc page says you is:

// setup the default timezone
date_default_timezone_set('Europe/London');

and than you can simply request a date with:

$date = new Zend_Date(‘2010-01-02’);
echo $date->get('d MMM yyyy');

where this should print 1 Jan 2010.

It’s a shame that this doesn’t work. The good part is that this problem can be overcome very simply. You just need to setup some more things just before calling the get method.

First setup a cache mechanism where you can cache the date locales. This is really good practice because it speeds up the framework when dealing with dates. Just add those lines after the default timezone is set:

// setup the default timezone
date_default_timezone_set('Europe/London');

$locale = new Zend_Locale(‘es_ES’);
Zend_Registry::set('Zend_Locale', $locale);

$frontendOptions = array(
		'lifetime' => 600, // 10 minutes
		'automatic_serialization' => true
);

$backendOptions = array(
                'cache_dir' => '../cache/'
		);

// getting a Zend_Cache_Core object
$cache = Zend_Cache::factory('Core',
		             'File',
		              $frontendOptions,
		              $backendOptions);

First you setup the Zend_Locale – a locale for the entire application and set it up in this example for spanish, than setup the cache mechanism.

This is a typical cache setup for Zend Framework. If you’d like to dive more into cacheing you can checkout the doc pages for Zend_Cache.

After these lines just setup Zend_Date options with:

Zend_Date::setOptions(array('format_type' => 'php',
                   'cache' => $cache));

This is really what you need to do to start. All these lines of code you may place into the bootstrap.php or into some front controller plugin.

Then you can call Zend_Date’s get method wherever you’d like in every controller you need it!