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

    • Ze Balkanic Tweetup
      • on May 31, 2009
    • Moblin, Linux for netbooks
      • on May 21, 2009
    • Back
      • on
    • Happy birthday, dear magician…
      • on May 10, 2009
    • Wordpress as CMS tutorial
      • on March 14, 2009
    • New blog - Try Open Source
      • on March 11, 2009
    • Online resources for Zend Framework
      • on March 3, 2009
    • pywst - setting up web projects quickly
      • on February 22, 2009
    • Full Circle Magazine
      • on February 8, 2009
    • Trac on Ubuntu
      • on January 27, 2009
  • Recent Comments

    • Jason Gilmore
      • on June 23rd @ 5:04 am
    • slawek
      • on June 11th @ 1:29 am
    • igor
      • on June 7th @ 9:56 pm
    • Swizec
      • on June 1st @ 11:18 pm
    • Robert
      • on June 1st @ 8:12 pm
    • Eniac
      • on June 1st @ 2:17 pm
    • -1-
      • on May 31st @ 11:04 pm
    • Robert
      • on May 31st @ 10:54 pm
    • Swizec
      • on May 31st @ 10:27 pm
    • blackshtef
      • on May 31st @ 8:14 pm
  • 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
  • Tags

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

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

    IMG_0185.JPG
    IMG_0147.JPG
    Catholic church, tallest one in Vojvodina
    IMG_0167.JPG

  • Archives

    • May 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008

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!
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 — 2009
Design & graphics by: Livia Radvanski
Coded by: Robert Basic
Home page last updated on January 3rd, 2009.
Frameworks used: Zend Framework, jQuery, 960 Grid System
Blog is powered by Wordpress
Subscribe: Entries — RSS & Comments — RSS