Tag Archives: framework

Redirecting with Zend Framework

Many of the web applications need to be redirected to some given URI sooner or later. In that scenario like redirecting to the profile page after login, which is very common to almost every app, you need to change the URI of the browser after the login form is validated. You can make this by simply using the built in header function in PHP. In other terms the most popular method to redirect in Zend Framework, don’t know why, but this is the _forward method of the controller actions:

$this->_forward('action-name');

The problem here is that thus you get to the given action name, or controller/action but the URI of the browser stays as it is from the caller action.

If you’d like to redirect, like the header function will do it you should use the following construction:

$this->_helper->redirector->gotoRoute(array(
   'controller'=> 'contrlname',
   'action' =>'actionname'));

Zend Framework – quick tutorial (part 2)

Directory Layout and Bootstrapping.

When someone starts with the learning of a framework, first he begins to read various articles to understand the basic rules to work with.

There are a lot of tutorials how exactly to start, and there is also an official quick start guide, but beside this there are too much advices how should you directory layout look like.

For me the following directory layout is working well. I don’t remember exactly where I’ve found it, but I think many people use it.

  • application
    • admin
    • controllers
    • models
    • views
  • content
    • controllers
    • models
    • views
  • layouts
  • bootstrap.php
  • config.ini
  • library
  • Zend
  • public
  • index.php

Almost half of the sources “how to start” recommend all of the initialization should be in index.php in the public folder. In fact all the logic of the public/private folders is that we don’t like to show anything on the web, and that’s why index.php contains only the following peace of code (it’s recommended to leave the file open from the ?> closing tag):
<?php
require '../application/bootstrap.php';

Anything else, all the initialization is in bootstrap.php:

error_reporting (E_ALL | E_STRICT);

ini_set (‘display_startup_errors’, 1);
ini_set (‘display_errors’, 1);

set_include_path (‘../library’ . PATH_SEPARATOR . get_include_path());

require_once “Zend/Loader.php”;
Zend_Loader::registerAutoload();

$front = Zend_Controller_Front::getInstance();

$front->throwExceptions(true);

$front->setControllerDirectory(array(‘default’ => ‘../application/content/controllers’,
‘admin’   => ‘../application/admin/controllers’));

$front->dispatch();
We set the error reporting to be ON, which si good when you develop any application and to collect all error messages. Than add the library folder in the include path with that line of code:

set_include_path(‘../library’ . PATH_SEPARATOR . get_include_path());

Next step is to start the __autoload functionality built in Zend Framework:

require_once “Zend/Loader.php”;
Zend_Loader::registerAutoload();

Finally start the front controller which implements the singleton pattern and point to the two main directories – admin and content. I used them for administration panel and front end, but you can used as many as you application needs:
$front->setControllerDirectory(array('default' => '../application/content/controllers',
'admin'   => '../application/admin/controllers'));

Related Links

Zend_Db tutorial

Zend Framework – quick tutorial (part 1)

Introduction

I’ve decided to write a quick tutorial about the way I work with Zend Framework. The problems I have, and the way I get through their solution. I think it will be useful for anyone common with the OOP design patterns and PHP programming, who wants to start with Zend Framework.

Introduction

In my practice as programmer I’ve noticed, that almost 90% of the sites we’ve build, have the same problems to solve and similar milestones.

At first why we need a framework? Because most of the programmers start from the beginning with the design of abstraction layers, and the time for the design is absolutely wasted when there are already that kind of abstraction given from almost every framework. Many of them have a big community of programmers around and cover a large scale of programming issues.

The obvious conclusion here is that there are not much sense in loosing that time. In the other part learning and using a framework with the scale of Zend Framework also requires a lot of time, but there’s no need of learning every part of it before you really start. That can happen later on.

That’s what happened in my case. Before two years I’ve designed and build my own framework, which does a lot of good work to organize the working process in the company I worked than. Than I start using a content management systems (CMS), mainly Joomla!, but recently noticed, that our clients don’t need that many functionality as Joomla! offers. They did not create a “menu” or add “template”.

With the idea to create web systems with my own CMS I decided to use Zend Framework. Not just like that, but mostely because the other PHP frameworks like CakePHP or Symfony looked to me more like CMS, something that I don’t want, because I’d like to build my own steping on the base of framework which gives those abstraction layers like db connection, acl etc.

With this tutorial I’ll describe the path I passed trough using the Zend Framework, and how it can be basicaly used.

part 2 – directory layout and bootstrapping