Zend Framework without MVC

The reasoning for using a framework such as Zend Framework (ZF for short) is to speed up the development process, make the application extensible, and make use of the design patterns such as MVC. I think MVC is great, and it separates the models, views and controllers and makes the entire development process very clean. If MVC is great, why would you use Zend Framework without MVC?

Zend Framework is a loosely coupled (or "glued") component library, which you may use independently without using the entire framework. For example, if you're trying to put up a quick A/B split testing "landing" pages for a website built with Zen Cart, which you'll use with Google Optimizer. Trying to setup a new ZF project on top of existing website that does not utilize ZF may be an overkill if you're just trying to put up a couple of web pages. Luckily, ZF can be used as a component library to build a couple of webpages without using the entire framework. In this article, I will share my experience in building a webpage containing HTML form posting it to itself using Zend Components.

Email Subscription Example

I am going to use a very simple example, perhaps too simple to even bother with a framework like ZF. This is just for an illustration purpose, so let's not debate whether it makes sense or not, but focus on how we can make use of the Zend component library to build a self-posting webpage. The webpage will have a HTML form with three input elements, namely "First Name", "Last Name" and "Email Address".

// Include library for ZendFramework
set_include_path(implode(PATH_SEPARATOR, array(
	'/home/sample/library',
	get_include_path()
)));

require_once 'Zend/Config.php';
require_once 'Zend/Form.php';
require_once 'Zend/Mail.php';
require_once 'Zend/View.php';

$$db_config = array(
    'database' => array(
        'adapter' => 'pdo_mysql',
        'params'  => array(
            'host'     => 'db.example.com',
            'username' => 'dbuser',
            'password' => 'secret',
            'dbname'   => 'mydatabase'
        )
    )
);

$config = new Zend_Config($db_config);
$form = new Zend_Form();
$form->addElement('text', 'fname', array('label' -> 'First Name');
$form->addElement('text', 'lname', array('label' -> 'Last Name');
$form->addElement('text', 'email', array('label' -> 'Email Address',
    'required' => true, 
    'filters' => array('StringToLower'),
    'validators' => array( array('NotEmpty', true, 
        array('message' => 'Enter your email address')),
        array('EmailAddress', true)))
    );

$form->setView(new Zend_View());

if ($_SERVER['REQUEST_METHOD'] == "POST")  {
    $db = Zend_Db::factory($config->database);
    ... logic to save posted data to a database.
} else {
    echo $form;
}

Comments

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.