Tag Archives: zend

Zend_Date::setOptions and format_type in Zend Framework 1.10.3

I recently upgraded from the old Zend Framework 1.9.5 to the latest – 1.10.3. Everything just looked to be working quite fine, except the date formatting. Although I had the same date formatting and options in the old version it seems to be not working.

I had the format_type set to PHP and even though I formatted the dates with a Zend Framework specific format: “d MMM yyyy”.

Zend_Date::setOptions(array('format_type' => 'php'));
// ...
$date = new Zend_Date($somedate);
echo $date->get('d MMM yyyy');

Now as it appears format_type php in 1.10.3 is giving completely different result, and the correct format is date() like format in PHP.

Zend_Date::setOptions(array('format_type' => 'php'));
// ...
$date = new Zend_Date($somedate);
echo $date->get('d M yy');

Zend_Controller_Router_Route_Regex – the Power of Regular Expressions

I posted once of custom URLs within Zend Framework, but back than I wasn’t quite sure enough there isn’t a better, more elegant way to do this job. Of course as it appears, there is! With no need to is to use the power of regular expressions with Zend_Controller_Router_Route_Regex. Thus you can match dynamically the URL.

Let’s assume you’d like to have a link with something like www.example.com/3939-some-title-here. This is really very common. So to proceed you’d have to have some regex like:

$pattern = '/\d+-.*/;

So what about Zend Framework

Here the things are going naturally more OOP.

$route = new Zend_Controller_Router_Route_Regex('(\d+)-?(.*)',
                       array('controller' => 'controller_name', 'action' => 'action_name'),
                       array(1 => 'id'));
 
$ctrl->addRoute('my-route', $route);

Iterate over YouTube Channel with Zend_Gdata_YouTube

Read YouTube’s Feed in Zend App

The task is to read the entire Gdata from a YouTube’s channel. It may sound easy, but as you may know Gdata is based on the Atom publishing protocol, and it naturally gives you only the latest few items.

Of course Zend Framework has a large set of classes dealing with Google’s Gdata services, one of them is the Zend_Gdata_YouTube.

It gives you the power to iterate/paginate over the entire set of YouTube user’s/channel’s videos. In that example let’s assume you have a protected function doing the reading of a single portion:

protected function _process(Zend_Gdata_YouTube_VideoFeed $videoFeed)
{
 
    /* @var $entry Zend_Gdata_YouTube_VideoEntry */
    foreach ($videoFeed as $entry) {
        /* @var $published Zend_Gdata_App_Extension_Published */
        $published = $entry->getPublished();
        $thumbnails = $entry->getVideoThumbnails();
 
        // set date format
        $date = new Zend_Date($published->getText());
 
        // array
        $tags = $entry->getVideoTags();
 
        $data['title'] = $entry->getVideoTitle();
        $data['description'] = $entry->getVideoDescription();
        $data['date'] = $date->get('Y-MM-d hh:mm:ss');
        $data['thumb'] = @$thumbnails[3]['url'];
        $data['id'] = $entry->getVideoId();
        $data['flash_player'] = $entry->getFlashPlayerUrl();
        $data['tags'] = implode(',', $tags) . ', ' . $entry->getVideoCategory();
        // do whatever you want with the data
    }
 
}

The only thing left is to iterate over the “pages” of the feed. This can be done with getNextFeed() method.

public function readRssAction()
{
    $userId = 'some_user';
    $client = new Zend_Http_Client();
    $gdata = new Zend_Gdata_YouTube($client, 'my-app', null, $this->_apiKey);
    $videoFeed = $gdata->getUserUploads($userId);
 
    // save the last items
    $this->_process($videoFeed);
 
    try {
        while ($videoFeed = $videoFeed->getNextFeed()) {
            $this->_process($videoFeed);
        }
    } catch (Zend_Gdata_App_Exception $e) {
        echo $e->getMessage();
    }
}

You’ll get an exception when there is no more feeds to process!

To combine all this into a single controller and to complete the code here’s the entire source:

class YoutubeController extends Zend_Controller_Action
{
    protected $_apiKey = 'your_api_key_goes_here';
 
    protected function _process(Zend_Gdata_YouTube_VideoFeed $videoFeed)
    {
 
        /* @var $entry Zend_Gdata_YouTube_VideoEntry */
        foreach ($videoFeed as $entry) {
            /* @var $published Zend_Gdata_App_Extension_Published */
            $published = $entry->getPublished();
            $thumbnails = $entry->getVideoThumbnails();
 
            // set date format
            $date = new Zend_Date($published->getText());
 
            // array
            $tags = $entry->getVideoTags();
 
            $data['title'] = $entry->getVideoTitle();
            $data['description'] = $entry->getVideoDescription();
            $data['date'] = $date->get('Y-MM-d hh:mm:ss');
            $data['thumb'] = @$thumbnails[3]['url'];
            $data['id'] = $entry->getVideoId();
            $data['flash_player'] = $entry->getFlashPlayerUrl();
            $data['tags'] = implode(',', $tags) . ', ' . $entry->getVideoCategory();
            // do whatever you want with the data
        }
    }
 
    public function readRssAction()
    {
        $userId = 'some_user';
        $client = new Zend_Http_Client();
        $gdata = new Zend_Gdata_YouTube($client, 'my-app', null, $this->_apiKey);
        $videoFeed = $gdata->getUserUploads($userId);
 
        // save the last items
        $this->_process($videoFeed);
 
        try {
            while ($videoFeed = $videoFeed->getNextFeed()) {
                $this->_process($videoFeed);
            }
        } catch (Zend_Gdata_App_Exception $e) {
            echo $e->getMessage();
        }
    }
}

Note: here you’ve to substitute the API key with the one you’ve for your development.

Zend_Validate_Alnum Doesn’t Work Correctly

Zend Framework’s Zend_Validate

I’m used to validate the form in a typical Zend Framework application with Zend_Validate or course. It gives you the absolute power to use the build in classes for standard validation, as well as write your custom validation classes.

The problem I had was a bit odd! Zend_Validate_Alnum is supposed to filter all characters and numbers, but what about non Latin symbols!? The project I’m working on is multilingual and there are eight languages, including Arabic, Chinese and Russian! All these were incorrectly !not validated by Zend_Validate_Alnum, although there were only letters and numbers!

In fact whatever the string encoding was – it doesn’t seem to work! Perhaps I’m wrong, but so far this is the situation.

Recent Update

After few test I figured out the problem was not in Zend_Validate at all! My mistake the solution is:

iconv_set_encoding('input_encoding', 'UTF-8');
iconv_set_encoding('output_encoding', 'UTF-8');
iconv_set_encoding('internal_encoding', 'UTF-8');