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 \