Zend Framework without mod_rewrite in Windows

Filed Under (General) by Wenbert on 05-06-2008

Tagged Under : , , ,

Rob Allen posted an entry in his blog about using Zend Framework in an IIS on WIndows 2003 Server.

Some of our Zend Framework applications have to run on IIS without ISAPI_Rewrite installed. In these cases we need urls of the form http://www.example.com/index.php?module=mod&controller=con&action=act. I couldn’t get this to work out of the box with Zend Framework 1.5, so wrote my own router called App_Controller_Router_Route_RequestVars.

This code obviously only supports what I needed and I’ve only tested it on IIS for Windows 2003 Server, so you may need to tweak to make it do what you want! Feel free to share any fixes :)

Zend Framework and Dojo Javascript Toolkit Partners

Filed Under (General) by Wenbert on 22-05-2008

Tagged Under : , , ,

This has been out like crazy in the PHP Community. All my RSS Feeds for PHP have like 2 or 3 links related to the ZF and Dojo partnership so you have probably heard of this post.

I have known about Dojo a few years back before MVC frameworks were going mainstream. I think Dojo was the  obvious choice for the Zend Framework Team. Dojo is very mature and it has lots of components. I have no idea how bloated it is though. Personally, I would have selected jQuery because I use it :P and second, I love it’s small size and functionality. It is about time is tag along with the others and start using Dojo too :D

Zend Framework How To: Creating your own validator for confirm passwords and emails

Filed Under (General) by Wenbert on 14-05-2008

Tagged Under : , ,

Last night I was working on the registration page of my pet-project. I am using Zend_Form for it but I had difficulties creating a validator to confirm 2 fields in the form. I chatted around #zftalk and checked the ZF Documentation but I could not understand anything. I needed a concrete working example.

Today I found this. It has more or less a complete coverage with sample source-code and some explanation.

My “register user” form consists of four elements:

  • Email
  • Confirm email
  • Confirm password
  • Password

Exactly what I was looking for ;)

Twitter moving away from Ruby on Rails

Filed Under (General) by Wenbert on 03-05-2008

Tagged Under : , , ,

This may be a rumor but Techcrunch is reporting that Twitter is abandoning Ruby on Rails due to scalability issues. I have read a few articles a while back about people having problems with Ruby on Rails. That is why I was hesitant to use Ruby on Rails on some of my web applications (at work). I was hesitant so therefore never got to learn it either :(.

Personally, I think the best bet for Twitter would be PHP. I would trust my life with PHP especially with scalability issues. Techcrunch also mentioned Java, but I doubt Java would work for them. Remember Friendster a few years back? It had a .jsp extension in the URL and it was almost unusable. It was dead slow.

The developers of Twitter can write a clone within a day and no one would notice it. They should use the Zend Framework :P Anyways, PHP is FTW!

Zend_View Helper Tutorial

Filed Under (General) by Wenbert on 30-04-2008

Tagged Under : , , ,

Zend DevZone has a new article up.

What is a View Helper?

A View Helper is simply a class that follows particular naming conventions, When attached to a view object, you can call the helper as if it were a method of the view object itself. The View object retains helper instances, which means that they retain states between calls.

Common use cases for view helpers include:

* Accessing models
* Performing complex or repeatable display logic
* Manipulating and formatting model data
* Persisting data between view scripts

Mixing PHP Variables with Javascript (Zend Framework and jQuery)

Filed Under (General) by Wenbert on 23-04-2008

Tagged Under : , , ,

Sometimes, I “mix” PHP variables with Javascript. For example, I have something like this:

function deletenote(notes_id)
{
    if(!confirm("Delete note? You will not be able to undo this action."))
        return false;
       
    $.post("< ?=$this->baseUrl?>/notes/deletenote",{
        skeedl_notes_id: notes_id
    }, function(data){
        $(‘#notes_id’+data).fadeOut();
    });
}
 

Note the $this->baseUrl, I need it like that. The thing is, I have that code in my view file (.phtml - in Zend Framework, this is parsed like a normal PHP file). If I remove all the Javascript in my view file and place it in a .js file, the $this->baseUrl will not be parsed. To go around this, I create a hidden input element in my view file with $this->baseUrl echoed as the value. Like this:

// … in my .phtml file …
// rest of the php and html code goes here
< input type="hidden" id="base_url" value="<?=$this->baseUrl?>" />
// rest of the php and html code goes here
 

Then in my .js file (assuming that you have already included this file in your header, or somewhere else), I have something like this:

//This is using jQuery, but you can use document.getElementById(’base_url’).value if you like
function deletenote(notes_id)
{
    if(!confirm("Delete note? You will not be able to undo this action."))
        return false;
       
    $.post($(‘base_url’).val()+"/notes/deletenote",{
        skeedl_notes_id: notes_id
    }, function(data){
        $(‘#notes_id’+data).fadeOut();
    });
}
 

What it does is that jQuery gets the value of the hidden form element. The value of this hidden form element is from a PHP Variable.

So there you go, nothing special. Just something that works and I just wanted to share.

Zend Framework Blog Application Example / Tutorial

Filed Under (General) by Wenbert on 23-04-2008

Tagged Under : ,

This is useful to those who are trying to kick start a Zend Framework application.

Starting any new application is like walking into a shop and being dazzled by the displays. You want everything but finally realise you only have so much resources to spend. So you isolate the specifics you must have, and focus on those. That’s why I bought a Classic iPod, and not the much flashier iTouch (crap all storage anyway).

If we drill down the typical blog application we get a very short list of must-haves.

1. Authentication for Authors
2. Authorisation to create/edit/delete/read entries
3. Methods for adding new entries, and modifying them
4. Publishing entries as RSS and Atom
5. Permalinks unique for each entry (SEO friendly of course)
6. Commenting system
7. Spam detection for new comments
8. Perhaps, trackback detection

Click here for the entire post. I haven’t read the entire post yet - I just wanted to share it but I am assuming that it uses the new ZF1.5 stuff like Zend_Form and other cool features.

UPDATE: Here is the second part of the tutorial.

UPDATE2: Today (4-25-2008) Pádraic Brady just announced the link to his Subversion repository for the series of Zend Framework Blog Application Example. The SVN link is found here.

Serialize + base64_encode an array for a URL

Filed Under (General) by Wenbert on 08-04-2008

Tagged Under : , , ,

This is an alternative for passing a bunch of variables in the URL. The variables might be used for pagination scripts (limits, etc.), sorting results and others. Generally, I would do something like this for sorting results:

http://localhost/reports?sort=username&sort_type=ASC
 

That will work fine in most cases, but what if you had tons of variables to pass to the URL? Using POST would be out of the question since I would want that URL to be “bookmark-able”. So the answer would be serialize() and base64_encode().
I would do something like this:

//In this example, I am using Zend Framework. The code below is found in my Controller file.
$myOptions = array();
$myOptions[’sort_by’] = ‘username’;
$myOptions[‘display_images’] = ‘yes’;
$myOptions[‘edittable_fields’] = ‘no’;
$x = base64_encode(serialize($myOptions));
$this->_redirect(‘/report?q=’.$x);
 

On the “receiving” end of the applications — the View file, I would do something like this:

if(isset($_GET[‘q’])) {
    $q = unserialize(base64_decode($_GET[‘q’])); //this reverses the serialize() and base64_encode()
}
Zend_Debug::Dump($q); //This would output the same array (found in the Controller file) with the values included.
 

This is useful in many other scenarios. And this is just one way of doing it — pretty dirty in my own opinion but the serialize() + base64_encode() method has saved my neck more than once.

If you have a better solution, then please post a comment below. ;)

Zend_Db Changing Fetch Mode

Filed Under (General) by Wenbert on 04-04-2008

Tagged Under : ,

I love this, since I do not have to type [’ and ‘] at the end of each field. Typing -> is easier!

   $db = Zend_Registry::get(‘db’);
   $db->setFetchMode(Zend_Db::FETCH_OBJ);
   $result = $db->fetchAll("SELECT * FROM user_roles");
   foreach($result AS $row) {
      echo $row->fieldA; //instead of the usual $row[’fieldA’]
      echo $row->fieldB; //instead of the usual $row[’fieldA’]
   }
 

This is also documented in my wiki.

Zend Framework: View File downloaded as Excel File

Filed Under (General) by Wenbert on 02-04-2008

Tagged Under : ,

This is basically a back-up post from my previous posts regarding exporting of data from a database to an excel file. Since Excel is able to read HTML tables, we can directly export a “view” file into an excel file by changing the headers of the request.

< ?php
header("Pragma: public");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: pre-check=0, post-check=0, max-age=0");
header("Pragma: no-cache");
header("Expires: 0");
header("Content-Transfer-Encoding: none");
header("Content-Type: application/vnd.ms-excel;");
header("Content-type: application/x-msexcel");
header("Content-Disposition: attachment; filename=report2_opendebitsummary".date(‘Ymd’).".xls");
?>

< html>
< body>
< table border="1">
< tr>
    < th>Employee ID< / th>
    < th>Employee Name< / th>
< /tr>
< ?php
foreach ($data AS $row) :
?>
< tr>
    < td>< ?=$row[‘employee_id’]?>< / td>
    < td>< ?=$row[‘employee_name’]?>< / td>
< / tr>
< ?php
endforeach;
?>
< / table>
< / body>
< / html>
 

It is important that you disable layouts and other stuff that will directly change the output of the view file.

Subscribe to Rss Feed : Rss