Pages

Monday, August 19, 2013

How to set and get session in Zend Framework

<?php
// Create a session object
$userDetails = new Zend_Session_Namespace(‘userDetails’);
// Set a value in name variable in userDetails object
$name = “just4you”;
$userDetails->__set(‘name’, $name);
// get value from userDetals session object.
$name = $userDetails->__get(‘name’);
?>

Redirect in Zend Framework

$this->_redirect(‘actionName’);
// it will redirect another action in the same controller
$this->_helper->redirector(‘actionName’, ‘controllerName’);
// it will redirect another controller on given action.

Friday, August 2, 2013

A Little Bit More About Controller

Zend_Controller_Front implements the Singleton pattern, that means it can be initialized in the project only once.

When you call a method dispatch(), the manager goes into a loop of dispatching passing three events:
Routing defines what module, controller and the event can be called. If other ways are not written, then: site.ru/modules/controller/action/var1/value1/var2/value2 /.

You can also override the paths through Zend_Controller_Route, but more on that in the next article.
Dispatching – checking for the called module, controller, and events and call events.
Answer – the view rendering.

Making the Structure of the ZendFramework Project

Create two directories in the root of the application:

application – here all our software modules of the project will be stored
public -- where will be available all share files.
Also create index.php and .htaccess in the root, in which immediately add rules of redirects:

RewriteEngine on
RewriteRule .* index.php

It’s necessary to add similar .htaccess file, in the folder “public”.

Now lets create 3 folders in “application” directory: configs, library, modules.

configs — here is configuration files of the project.
library — for additional libraries.
modules — and it’s for modules of our application.

Zend Framework File and Folder Structure

<project name>/
    application/
        configs/
            application.ini
        controllers/
            helpers/
        forms/
        layouts/
            filters/
            helpers/
            scripts/
        models/
        modules/
        services/
        views/
            filters/
            helpers/
            scripts/
        Bootstrap.php
    data/
        cache/
        indexes/
        locales/
        logs/
        sessions/
        uploads/
    docs/
    library/
    public/
        css/
        images/
        js/
        .htaccess
        index.php
    scripts/
        jobs/
        build/
    temp/
    tests/

What is Bootstrap File in Zend Framework and how it works?

Bootstrap is a setup process of different framework component to make easily accessible/available in application. It is very important to setup all require component of framework.

ZendFramework Installation

Download zend from http://framework.zend.com/downloads/latest

Extract the downloaded folder into www directory

What is MVC (Model View Controller)



A Model:- which represents the underlying, logical structure of data in a software application and the high-level class associated with it. This object model does not contain any information about the user interface.

A View:- which is a collection of classes representing the elements in the user interface (all of the things the user can see and respond to on the screen, such as buttons, display boxes, and so forth)

A Controller:-  which represents the classes connecting the model and the view, and is used to communicate between classes in the model and view.

ZendFramework System Requirements

Zend Framework has the following requirements:
PHP 5.2.4 (or higher).
A web server supporting mod_rewrite or similar functionality.

Positive and Negative Of ZendFramework

There are no ideal solutions and ZF is not the exception. It has its both positive and negative sides, which we’ll discuss in this part.
Let’s start with the negative:

1. Heavy, version 1.8.a for exaple is 17 MB (well, not be afraid of), although this is not so much. If you’re scared this size, see the size of many commercial CMS. Almost of them rolled over for 10 MBs and it is not surprising, cause they have a lot of functions. It is the similar story with ZF. It provides a lot of opportunities, but it’s not the fact that you can use them all.
2. Requires a lot of time to study. Actually, this all is individually here. Some may mark time a year, while it will be enough for others just a few days and they are ready to write their first applications.
3. Resource-intensive. To be honest, I have not seen more than one hosting provider that would kick the bucket from ZF.

That’s all the negative sides. Now the positive:

1. ZF is a set of classes, most of which are not tied to each other, so that it can be used as the spare parts in other projects and completely. There is all you need in today’s Web projects.
2. The presence of a component to work with third-party services.
3. Object-oriented approach.
4. MVC pattern based.
5. Well documented.

So, it’s enough, no more theory and let’s go straight to practice. We estimate the possibility of the giant, so to speak:) Inorder to work with ZF, we need ZendFramework (you can download the latest version from here, HTTP server with the mod_rewrite support, PHP at least version 5.2, and MySQL 5. Download the latest stable version. By the way I almost forgot, you can download ZF in two different assemblies:

Full
Minimal

Full Package contains the Dojo Toolkit, and the demos to work with a skeleton. Because you are just beginning to study this framework, I recommend to download this version.

Minimal Package contains only ZendFramework library.

Extract the folder with the core (/ library / Zend), ZF is better to keep a few levels higher, so that don’t produce the files for each project, in my example, it turned out in this folder D: \ library \ ZF \ 1.7.8 \ Zend \

Why to use Zend Framework?

The major aim for any CMF – reduce time for a project’s working up. Using this product, you can achieve following:

1. Permanent structure of your projects. The problem of majority projects, making from nothing, – absence of a permanent structure. First, you put necessary function in one module, in the next project you realized you’d made a mistake and decided to change everything. As a result, the more you create a website / web-applications, the more you improve your knowledge, well then you make changes in the structure of new projects. But now try to imagine that you will have to go back to support/revision of your oldest project. I’m sure you’ll just get entangled or waste much of time for the code’s investigation. When you create a web-based application frameworks, this problem disappears, because you’re always guided by rules. This approach can guarantee that, if the oldest project’s revision is necessary, you will always know its structure and can finish it off easily.

2. Increase the speed of development. There is a set of classes in ZF that allow to realize a lot of typical actions. You’ll not need to create one more bicycle cause you have the whole Kamaz of ready-made bicycles.

3. Increase the level of safety. Everybody makes mistakes and no one is safety. It is difficult to argue, but with the help of ZF you can significantly reduce the error rate in the project. Because it’s really to make a lot of typical actions with the help of built-in classes, it means you have to write less code. And less code, less errors.

What is zend framework or introduction to zend framework?

Zend  framework is an open source , object oriented web application framework which provides an advanced Model-View-Controller(MVC) implementation to the application. It is also known as component based libarary. It was first founded in 1999 by two Israel programmer Andi Gutmans and Zeev Suraski. The first version was 1.0 and released in 2007 and latest version is 2.0.