Tag Archives: Web application frameworks

Retrieving YouTube Channel’s Videos with Zend_Gdata_YouTube

Zend_Gdata

Zend Framework gives you the possibility to interact with Gdata services, which are provided by most of the Goolge services. You can find more on the docs page of Zend_Gdata. The basic principle is that you can connect a service with you API key, given by Google. What I’m about to show you is how do connect such a service.

In theory Zend_Gdata depends on Zend_Http_Client and you’ve to connect it with an instance of this class.

// 1. setup API key
$apiKey = 'your_api_key_here';
 
// 2. retrieve videos via GData Atom
$client = new Zend_Http_Client();
$gdata = new Zend_Gdata_YouTube(null, 'my-app', null, $apiKey);

Note that you’ve to replace your API key in the chunk above.

Now once that you’ve connected the YouTube service you can iterate through the entries from the video feed.

$videoFeed = $gdata->getUserUploads('username');
foreach ($videoFeed as $entry) {
    echo $entry->getTitle();
}

The thing is that you can modify a bit the code above. As ZF docs says if you don’t setup a Zend_Http_Client a default with default configuration, so you can omit this and simple write:

// 1. setup API key
$apiKey = 'your_api_key_here';
 
// 2. retrieve videos via GData Atom
$gdata = new Zend_Gdata_YouTube(null, 'my-app', null, $apiKey);

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

Escaping strings in a Zend Framework view. Prevent unclosed tags!

In most of the tutorials about Zend Framework the simple theory is described how all these MVC things may work together. However beyond that the things become more and more tricky and not everything is done by the MVC pattern itself.

What I mean is that reading most of the tutorials about ZF I found describing the simple relationship between controllers and views just by setting up a view member variable in the controller and than simply call that variable in the view of that particular controller action.

Setup controller’s view variable. In this example I used the default ZF IndexController – indexAction:

public IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $this->view->greeting = ‘Hello World!’;
    }
}

where afterwards you can call this member variable in the index.phtml view in the index view scripts.

Note: In a typical Zend Framework installation all this is setup into the application folder (either web visible or not), where the IndexController.php file containing the code above is placed into the application/controllers and its indexAction stores its view in the application/views/scripts/index/index.phtml

In that situation the view file, the previously mentioned index.phtml should access this “greeting” variable in something like:

<div><?php echo $this->greeting ?></div>

Note that here you miss the ->view-> part of the chain. That’s because the view’s parsed and now this contains everything that the controller’s view member object contains.

All that is pretty cool and it works great until you start accessing and working with different type of data escaped or not, coming from various sources.

Natively comes the problem of escaping bad strings and the single question is why should I do that? To give you a simple example I’ll describe the above code in a different manner.

Imagine you should setup a browser specific title depending of the controller/action you call. To do that is simple but there comes the tricky part. In the example above we can just change the code a bit.

In the controller you can setup the browserTitle variable:

<?php
class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $this->view->browserTitle = “Welcome”;
    }
}

Than normally in the view you may have something like that:

<html>
<head>
<title><?php echo $this->browserTitle ?></title>
...

Everything until that looks normal and you don’t need to escape whatever as it appears. Even if the browserTitle contains something with quotes:

...
$this->view->browserTitle = ‘Hello ”World”’;
...

This will result in the browser’s source view as:

...
<title>Hello “World”</title>
...

see the image below:


But what if you setup a meta title tag:

<meta name=”title” content=”Hello “World”” />

for the browser that’s an error as shown on the image:

There’s why you simply should be careful when dumping variables all over the page and to use the native ZF escape function.

$this->escape()

In the example above you simply can call the escape function into the view:

<html>
<head>
<title><?php echo $this->browserTitle ?></title>
<meta name=”title” content=”<?php echo $this->escape($this->browserTitle) ?>” />

That will prevent the browser to “crash” and of course and more important will improve the site’s SEO.

Cool jQuery 1.4 cheat sheet! Nice work!

jQuery 1.4 relased! Where’s the cheat sheet?!

If you’re familiar with the new jQuery version 1.4. released recently, I’d suppose you’d like to get its fresh cheat sheet.

Such has been published on http://labs.impulsestudios.ca/jquery-cheat-sheet or you can find it here.

Using Zend Framework validators – Zend_Validate_Db_RecordExists

What are validators?

In very very breve these are methods which can validate some data, usually user input, against some specific rules. Imagine there’s a web form that is always checked for empty fields or some fields that may contain valid e-mail addresses. This is so common that became everyday routine to almost all of us. However smart developers make abstractions that help them reuse all this functionality. Even smarter developers make use of frameworks. And for those of you, using Zend Framework, there’s no need to write most of the common used validators, simply because they come with the framework itself.

Technically you’ve various validators in zend, such as Zend_Validate_Alnum, Zend_Validate_Email or Zend_Validate_Regex. All these are extremely useful when it comes to automatic, bullet proof validation, but I’m going to talk more about one specific validator.

Zend_Validate_Db_RecordExists

Although the implementation is nothing more than just a chunk of code and doesn’t pretend to be difficult, the idea of such validator is genius indeed. It really helps you do some amazing job.

Image you have to check some database record existence. Then comes in help this validator. In fact I’m pretty sure almost everyone has experience with such kind of task. Simply because the registration process almost always requires it. When you try to register new user you more often check for the username existence. Although you may solve the problem with other technique by catching the exception the database is throwing for duplicate entries, this should be assumed just as an example.

I’m pretty sure this validator can be really useful in many occasions!