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

    • Ubuntu Administrator
      • on January 6, 2009
    • Login example with Zend_Auth
      • on January 5, 2009
    • 2008: Moments worth a note
      • on December 27, 2008
    • Styling the default Zend_Form layout
      • on December 23, 2008
    • Data filtering with PHP’s Filter extension
      • on December 15, 2008
    • MyUrl view helper for Zend Framework
      • on December 2, 2008
    • One day for Human Rights
      • on November 26, 2008
    • LAMP and SVN on Ubuntu 8.10
      • on November 24, 2008
    • TickTweet WordPress plug-in
      • on November 21, 2008
    • School’s out!
      • on November 13, 2008
  • Recent Comments

    • vasim
      • on January 2nd @ 3:45 pm
    • Jonathan Kushner
      • on January 2nd @ 4:48 am
    • BradGman
      • on December 31st @ 4:16 am
    • gsx
      • on December 30th @ 4:07 am
    • Robert Kubica
      • on December 28th @ 6:45 pm
    • abtris
      • on December 27th @ 2:30 pm
    • Christian
      • on December 27th @ 12:35 am
    • Fernando Bittencourt
      • on December 26th @ 3:07 pm
    • Josh
      • on December 26th @ 7:13 am
    • gimly
      • on December 21st @ 5:29 pm
  • Friends and Blogs

    • Andrew Taylor
    • Bojan Pejić
    • Eran Galperin
    • Graham Smith
    • Matthew Weier O’Phinney
    • Nebojša Radović
    • Nemanja Avramović
    • Nikola Krajačić
    • Nikola Plejić
    • Pádraic Brady
    • Rob Allen
    • Swizec
    • WeAreJustCreative
  • I use

    • 960 Grid System
    • jQuery
    • Notepad++
    • Zend Framework
  • Tags

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

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

    • 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 © 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