• Subscribe to the RSS feed!
  • Subscribe by Email
  • home
  • blog
  • dev
  • Recent Posts

    • Toggler
      • on February 4, 2010
    • Book review - jQuery 1.3 with PHP
      • on January 6, 2010
    • 2009 in a few words
      • on January 2, 2010
    • Bad Firebug!
      • on December 21, 2009
    • Posterous
      • on December 2, 2009
    • Chaining routes in Zend Framework
      • on November 27, 2009
    • Zend Framework bug hunt days
      • on November 22, 2009
    • Zend Framework 1.8 Web Application Development book review
      • on November 17, 2009
    • A book review
      • on October 11, 2009
    • Playing with Zend_Navigation and routes
      • on August 9, 2009
  • Recent Comments

    • Aryashree Pritikrishna
      • on January 28th @ 9:10 am
    • Michl
      • on January 15th @ 10:09 am
    • Robert
      • on January 2nd @ 1:36 pm
    • Ivan
      • on January 2nd @ 1:33 pm
    • Keith Pope
      • on January 1st @ 11:57 am
    • Jani Hartikainen
      • on December 29th @ 8:55 am
    • johnjbarton
      • on December 22nd @ 1:01 am
    • Robert
      • on December 21st @ 11:55 pm
    • René Silva
      • on December 21st @ 11:47 pm
    • Robert van Drunen
      • on December 21st @ 6:37 pm
  • Tags

    • php
    • framework
    • zend
    • example
    • random
    • about
    • site
    • ubuntu
    • blog
    • introduction
    • book
    • wordpress
    • linux
    • apache
    • lamp
    • setup
    • review
    • open source
    • svn
    • comic
  • Categories

    • Blablabla
    • Development
    • Free time
    • Places on the web
    • Programming
    • Software
  • Archives

    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • August 2009
    • May 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
  • Find me on

    • DZone
    • Google Code
    • Google Reader
    • Last.fm
    • StumbleUpon
    • Twitter
    • Vimeo
  • Friends and Blogs

    • Andrew Taylor
    • Andy Sowards
    • Bojan Pejić
    • Eran Galperin
    • Graham Smith
    • Jani Hartikainen
    • Jasper Tandy
    • Matthew Turland
    • Matthew Weier O’Phinney
    • Miff
    • Miloš Ćuković
    • Nebojša Radović
    • Nemanja Avramović
    • Nemanja Tobić
    • Nikola Krajačić
    • Nikola Plejić
    • Pádraic Brady
    • Rob Allen
    • Swizec Teller
    • Vladimir Stanković
    • WeAreJustCreative
    • Željko Stevanović
  • I use

    • 960 Grid System
    • jQuery
    • Notepad++
    • Subversion
    • Trac
    • Vim
    • Zend Framework

Starting with Zend Framework - part 2

by Robert Basic on October 20th, 2008

This post is the second part of my introductory text on Zend Framework, Starting with Zend Framework. This time I cover the basics about controllers, actions, view scripts and view helpers. On request routing and the Front Controller I will write one (or more) big post(s), so this part won’t be explained now. I will also skip explaining the models; they deserve their own post :)

If anyone is into writing a guest-post on models, let me know!

The Controllers

The Controllers are the heart of every MVC based application. They control the execution of the application, what to do with the data, what to show the user, what to write to the database, etc. The Controllers that you will write all the time, are called Action Controllers. These Controllers subclass the Zend_Controller_Action abstract class. Every application module must have a default Controller, which will be accessed if no specific Controller is requested. The default name for this default Controller is Index. Examples of the IndexController and FooController:

<?php

// The IndexController class must be placed in the controllers folder
// and saved as IndexController.php
class IndexController extends Zend_Controller_Action
{
    public function init()
    {
    }

    public function indexAction()
    {
    }
}

// The FooController class must be placed in the controllers folder
// and saved as FooController.php
class FooController extends Zend_Controller_Action
{
    public function init()
    {
    }

    public function indexAction()
    {
    }

    public function barAction()
    {
    }

    public function someRandomFunctionDoingSomeFunkyStuff()
    {
    }
}

The Controllers must contain at least the indexAction() function; the others are arbitrary. I always have an init() function, in which I setup the cache object, call up the models, etc. Controller names that are not in the “default” module, must be prefixed with the Title-cased name of the module and an underscore:

<?php

// An example of the IndexController in the
// dummy module
// The file name remains IndexController.php!!!
class Dummy_IndexController extends Zend_Controller_Action
{
}

// An example of the FooController in the
// dummy module
// The file name remains FooController.php!!!
class Dummy_FooController extends Zend_Controller_Action
{
}

The actions

Actions are methods of the Controller class. Use them to do some specific task: show users, list news, insert to database (the actual INSERT SQL statement should be in the model), etc. As stated before, every Controller must have an index action — this one is called if no specific action is requested. By default the view object is instantiated, so if you don’t turn it off, you must create a view script with the same name as the action (without the “Action” word) in the views/scripts/foo/ folder.

Assigning variables to the view scripts is simple:

public function indexAction()
{
    $this->view->someVariable = "some value...";
}

The view scripts

View scripts are, well, for viewing. This is the only place where you should have statements like echo and print. The default templating engine is PHP itself, but it’s possible to change it to something like Smarty. I leave PHP; it has everything for templating, so why would I change it? The default file extension for view scripts is “phtml” — but as with everything, this can also be changed :)

Getting variables that are assigned from the action:

// Output: some value...
<?= this->someVariable ?>

The view helpers

The view helpers are simple classes that help in view scripts with things like formatting dates, creating links, etc. Here’s an example view helper that I use to show dates in “Serbian” format:

File name: views/helpers/SrDateFormat.php
<?php
/**
* View helper for returning dates in Serbian format
* dd.mm.yyyy.
*
*/
class Zend_View_Helper_SrDateFormat
{
    public function srDateFormat($dateToFormat)
    {
        return date('d.m.Y.', strtotime($dateToFormat));
    }
}

Usage is quite simple:

// somewhere in some view script...
<?= $this->srDateFormat($someDateToShow); ?>

Bringing it all together

Just for an overview, here is an example of a Foo Controller in the Dummy module with index and bar actions and their view scripts.

<?php
// File name: application/dummy/controllers/FooController.php
class Dummy_FooController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $this->view->sayHello = "Hi there!";
    }

    public function barAction()
    {
        $this->view->sayHelloAgain = "Hi here :)";
    }
}

And the view scripts:


<h1>Saying hello</h1>
<?= $this->sayHello ?>


<h1>Saying hello again</h1>
<?= $this->sayHelloAgain ?>

So if you direct your browser to “http://example.com/dummy/foo/” or to “http://example.com/dummy/foo/bar” you should get the “Saying hello” or the “Saying hello again” page…

This would be my introductory text to Zend Framework. Hope it’s not confusing and is easy to follow. I just want to help newcomers to ZF help settling in easily :) For a tutorial application with ZF, I recommend Rob Allen’s Zend Framework tutorial.

In the coming days/weeks I’ll write a detailed post about the Front Controller, so if you wish, grab the feed or subscribe by E-mail to stay tuned.

Cheers!

Share this post:
  • Digg
  • description
  • del.icio.us
  • StumbleUpon
  • Facebook
  • Reddit
  • TwitThis
  • Google
  • E-mail this story to a friend!
Other posts you might be interested in: Starting with Zend Framework, A Zend_Captcha example, Wordpress paging navigation, Wordpress as CMS tutorial, Bad Firebug!, or just wonder on through the archives...
If you liked this post, you can buy me a cup of coffee!
Tags: example, framework, introduction, php, zend.
Categories: Development, Programming, Software.
Subscribe to the feed.

Comments: 5

Grab the comments feed

  • Mojah

  • October 21st, 2008

This is a very nice introduction to the Zend Framework, with an easy-to-understand explanation. (Ps; perhaps you should lighten the text-color just a wee bit, to make the contrast with the background bigger).

  • Robert Basic’s Blog: Starting with Zend Framework - part 2 : Dragonfly Networks

  • October 21st, 2008

[...] Basic has posted the second part of his introduction to the Zend Framework series (here’s part [...]

  • ozofeliz

  • October 21st, 2008

I like zend framework cause it’s not so rigid than rails or cakephp. You can use the part that you like, for example, the soap server.

  • Tims Blog » Blog Archive » Sans Halloween

  • October 24th, 2008

[...] starting with zend framework part 2 [...]

  • Anonymous

  • October 29th, 2008

Thanks your message has very much helped me:)

Leave a Reply

 

Robert Basic © 2008 — 2010
Design & graphics by: Livia Radvanski
Coded by: Robert Basic
Home page last updated on November 30th, 2009.
Frameworks used: Zend Framework, jQuery, 960 Grid System
Blog is powered by Wordpress
Subscribe: Entries — RSS & Comments — RSS