Zend Framework - Php Tutorials

Zend Framework  - php tutorials

Zend Framework (ZF) is an open source, object-oriented web application framework implemented in PHP 5 and licensed under the New BSD License. The Framework is basically a collection of professional PHP based packages. The framework uses various packages by the use of Composer as part of its package dependency managers; some of them are PHP unit for testing all packages, Travis CI for continuous Integration Services. Zend Framework provides to users a support of the Model View Controller (MVC) in combination with Front Controller solution. MVC implementation in Zend Framework has five main areas. The router and dispatcher functions to decided which controller to run based on data from URL, and controller functions in combination with the mode l and view to develop and create the final web page.

Zend Framework  - php tutorials
Licensing

Zend Framework is licensed under the Open Source Initiative (OSI)-approved New BSD License. For ZFv1 all code contributors must sign a Contributor License Agreement (CLA) based on the Apache Software Foundation’s CLA. The licensing and contribution policies were established to prevent intellectual property issues for commercial ZF users, according to Zend's Andi Gutmans. ZF2 is CLA free. There is also a longterm support available for the framework (long term support or LTS) for a total duration of 3 years. In order to do this one need to modify the Composer requirement for Zend Framework.

This will result in modification of composer.json file and will pin the semantic version 2.4.0 which will ensure you get updates specifically from 2.4 series. If user want to use a different LTS then they need to specify X.Y.) version.

Zend Framework  - php tutorials
Installation

The Zend Framework documentation shows simple and easy to manage steps for the installation of the framework. The first step is the addition of the /library folder to your include path. A recommended advice; move library folder to another shared location. The steps to include the Zend Framework is first to download the lates version, secondly download lates nightly snapshot, use a>>Subversion() client, exporting, and external definitions. For downloading various options are available such as .zip and .tar.gz formate. The Snapshots are Zend Framework's bundled documentation which considers Subversion() client. After installation it is required that you must set up your include path to Zend in order to access the repository. According to Zend's documentation the repository can be found at http://framework.zend.com/svn/framework/standard/trunk.

Zend Framework  - php tutorials
Anatomy of Zend Framework

The framework is has a diverse structure and based on directories such as application, library, public, and tests. This is based because Zend is inclined towards keeping separation of different parts of application individually. The functionality of application, library and public directory is used as per the request of the user.

Recommended Directory Structure

The recommended directory structure as shown by Zend Framework is given as:

This directory contains all the nodes which are necessary to run the application and web server has no direct access to it. The Framework further enahances by separation the business, display and control logic through separate directories which contains MVC framework. Zend explanation also shows that the application directory contains the application directory and will house the MVC system with configuration, service and bootstrap file. The config/ directory has application wide configurtions. controllers/, model/, and views/ these are the default controller, view controller directories in the framework. These are simple and approachable layouts to start your application. Controller/helpers/ here the actions helpers are stored and they will be namedspaced as Controller_Helper_ for default modules or <module?_Controller_Helper in different modules. The module structure is also similar to that of the aforementioned directory structures and are:

Zend Framework  - php tutorials
Creating Project Structure

Zend framework supports command line input to create structure of directories. We will use command line interface to start creating the directory structure for our project. This will give you complete structural understanding of directories. The interface supports and provides Zend_Tool interface giving a whole host of command functionalities.

  1. Open the command line interface, and change the hellozend directory.
  2. Windows users type: bin\zf\bat create project
  3. Linux/Mac users type: bin\zf.sh create project

This procedure will create Zend Framework project in a your own specified location. After running Zend_Toll it will create the basic application skeleton. This will not only create directory structure but also all the basic elements of the MVC framework. In order to get Apache functionalities the virtual host settings will be as:

The basic directory structure created will be somewhat as mentioned in the aforementioned directory structure of Zend Framework with similar explanation. There is another aspect of Zend-Tool which is automatically initialized during installation is bootstrapping. Here the basic purpose is to initialize the request of page by developer. The main entery here created by Zend Framework is the Index file. Index file provides function to handle user request.This is the main entry point for all requests. Following shows the functionalities.

  1. Application-path: defines the path to application directory
  2. Application_Env: changes the application behavior depending on various factors such as how the application is used.
  3. getenv(): checks system environment.
  4. Initialize Zend-Application application: includes Zend-Application and create an instance of it.
  5. Call bootstrap() method coupled with run() method starting MVC.

In general Zend-Tool creates many important directory structures. This system is built upon Rapid Application Development technology. As a general rule of support the framework focuses on coding and project structures instead of focusing on smaller parts.

  • Project directory structure
  • Controllers
  • Actions
  • Views
  • Bootstrap file

Controllers

Controller is the main entry to Zend Framework application. The front controller handler is main hub for accepting requests and running the accurate actions as requested by the commands. The whole process of requesting and reacting is routing and dispatching (which basically means calling correct methods in a class) which determines the functionality of the code. This is implemented by Zend_Controller_Router_- Interface. The router functionality is to find which actions need to be run and on contrary dispatcher runs those requested actions. The controller in Zend Framework is connected in a diverse array of structural directories, which provides a support to efficient routing. The main entry point and the command controller is the Zend_Controller_Front, this works as a foundation which delegates the work received and sent. The request is shaped and encapsulated with an instance of Zend Controller Request HTTP, as a provider of access to HTTP requests. The HTTP hold all the supergl obls of the framework ($_GET, $_POST, $_COOKIE, $_SERVER, and $_ENV) with their relevant paths. Moreover, the controller also provides getParam() functions which enables collection of requested variables.

Actions

Actions are important functionalities. Controllers does not function without Actions. For this purpose we create another method which has action appended in its name and automatically the front controller will recognize it as an action. The Action has init() method which shows its private nature and not accessible by anyone. Following commands are run so that Zend_Tool can create action for us. Through the use of standard dispatcher all functions are named after the action's name and followed by word "Action" appended. This leads to controller action class containing methods like indexAction(), viewAction(), editAction(), and deleteAction().

Windows users:

  bin\zf.bat create actions about index  

Linux and Mac users:

  bin/zf.ch create action about index  

An example of forms and actions:

The Standard Router

Standard router is an important Front_Controller tool. Here the main decisions are made in order what module, controller and action are being requested. These are all processed here. The following are defaults structure.

  1. Module
  2. Controller
  3. Actions

The request follows a pattern first information is taken from URL endpoint of HTTP. URI is the end point of the request. URL structure follows as: http://domain.com/moduleName/controllerName/actionName

The default router code example:

Utility Methods

The Zend Framework also provides some utility methods. Following are some utility methods provided in the framework.

_forward()
it is used to call action
_forward{$action, $controller = null, $module = null, array $params = null}
$actions
string, action required
$controller
optional string parameter and is place where controller is in.
$module
string, has module in which we have the controller.
$params
array, user parameter

Another method is the redirect utility method. This is the opposite of aforementioned -forward() method. _redirect() performs HTPP in redirection in creation of a new request. _redirect() methods accepts two arguments namely $url, and $options.

Furthermore, Action Helpers are also a way to provide extra functionalities within the framework. Action helpers are useful when there is a need to provide functionality between controllers.

During initialization phase of IndexController and ContactController, viewReader is called and noRender flag is called on the view object. The lack of this process creates an error in our application.

View Directories

Zend Framework provides the view framework to our project and controller and actions are automatically provided to our application. Inside the Zend Framework in view folder we observe the following folders.

  1. View
  2. Helpers
  3. Scrips
  4. Contacts
  5. errors
  6. index

In order to create a view we follow:

View Sample:

Zend Framework  - php tutorials

Zend Technologies, co-founded by PHP core contributors Andi Gutmans and Zeev Suraski, is the corporate sponsor of Zend Framework. Technology partners include IBM, Google, Microsoft, Adobe Systems, and StrikeIron.

Zend Framework  - php tutorials
Requirements

Zend Framework version 1.7 requires PHP 5.2.4 or later. Previous versions required PHP 5.1.4 or later, although the ZF Programmer's Reference Guide strongly recommended PHP 5.2.3 or later for security and performance improvements included i these versions of PHP. Zend Framework 2.0 requires PHP 5.3.3 or later. PHPUnit 3.0 or later is required to run the unit tests shipped with Zend Framework. Many components also require PHP extensions. Below is a table showing PHP used in Zend Framework.

Zend Framework  - php tutorials
Features

Zend Framework features include:

  • All components are fully object-oriented PHP 5 and are E_STRICT compliant, which helps in the development of building tests and writing codes in a bug-free and crash-proof application manner.
  • Use-at-will architecture with loosely coupled components and minimal interdependencies
  • Extensible MVC implementation supporting layouts and PHP-based templates by default
  • Support for multiple database systems and vendors, including MariaDB, MySQL, Oracle, IBM DB2, Microsoft SQL Server, PostgreSQL, SQLite, and Informix Dynamic Server
  • Email composition and delivery, retrieval via mbox, Maildir, POP3 and IMAP4
  • Flexible caching sub-system with support for many types of backends, such as memory or a file system.
  • With the help of RPC(Remote Procedure Call) and REST(Representational State Transfer) services, Zend Apigility helps developers to create APIs, Authentication of APIs, Documentation of APIs, Easy Modification

Zend Framework  - php tutorials
Development of Zend Framework Apps

Zend Framework applications can run on any PHP stack that fulfills the technical requirements. Zend Technologies provides a PHP stack, Zend Server (or Zend Server Community Edition), which is advertised to be optimized for running Zend Framework applications. Zend Server includes Zend Framework in its installers, along with PHP and all required extensions. According to Zend Technologies, Zend Server provides improved performance for PHP and especially Zend Framework applications through opcode acceleration and several caching capabilities, and includes application monitoring and diagnostics facilities. Zend Studio is an IDE that includes features specifically to integrate with Zend Framework. It provides an MVC view, MVC code generation based on Zend_Tool (a component of the Zend Framework), a code formatter, code completion, parameter assist, and more. Zend Studio is not free software, whereas the Zend Framework and Zend Server Community Edition are free. Zend Server is compatibl e with common debugging tools such as Xdebug. Other developers may want to use a different PHP stack and another IDE such as Eclipse PDT which works well together with Zend Server. A pre configured, free version of Eclipse PDT with Zend Debug is available on the Zend web site.

Zend Framework  - php tutorials
Code, Documentation, and Test Standards

Code contributions to Zend Framework are subject to rigorous code, documentation, and test standards. All code must meet ZF’s coding standards and unit tests must reach 80% code coverage before the corresponding code may be moved to the release branch.

Simple Cloud API

On September 22, 2009, Zend Technologies announced that it would be working with technology partners including Microsoft, IBM, Rackspace, Nirvanix, and GoGrid along with the Zend Framework community to develop a common API to cloud application services called the Simple Cloud API. This project is part of Zend Framework and will be hosted on the Zend Framework website, but a separate site called simplecloud.org has been launched to discuss and download the most current versions of the API.The Simple Cloud API and several Cloud Services are included in Zend Framework. The adapters to popular cloud services have reached production quality.

Hello World: File by File

In order to create Hello World program, there are multiple steps including:

First create four files within the directory structure. These files are bootstap file, an Apache Control file (.htaccess), a controller file and a view controller for the view.

Secondly we also need to develop a copy of Zend Framework. With the growth of complexity, additional code is required which will provide the functionality and that is relative small and focuses on the benefits of MVC system. If we look at the process in mode details we can observe the bootstrap file is initialization in one or another form.

At first the environment is correctly ensured that there are no errors and followed by date and time for tracking functionality. In order to setup date and time we can follow many procedures for example we can call the method data_default_timezone_set() and Zend assumes that default directory will include the phd path. The Framework does not depend on any specific file, but helper classes are helpful in this case. Following are some examples:

Zend_Loader::loadClass() the main purpose here is to correct file for the supplied class name.

Following this the underscores are convereted into directory specific structures. As a result, the code lines Zend_Loader::loadClass('Zend_Controller_Front'); and include_once 'Zend/Controller/Front.php'; shows similar results.

Zend_Debug::dump() functions in terms of debugging information and is focused on formatted var_dump() output. Finally the bootstrap runs the front controller and initialize it. The design patter used by Zend_Controller_Front, implements is the Singleton design and getInstance() is used to get this functionality.

Current Development

Zend Framework 3.0 was released on June 28, 2016. It includes new components like a JSON RPC server, a XML to JSON converter, PSR-7 functionality, and compatibility with PHP 7. Zend Framework 3.0 runs up to 4 times faster than Zend Framework 2, and the packages have been decoupled to allow for greater reuse. The contributors of Zend Framework are actively encouraging the use of Zend Framework version 3.x. The stated end of life for Zend Framework 1 is 2016-09-28, and for Zend Framework 2 is 2018-03-31. The first development release of Zend Framework 2.0 was released on August 6, 2010. Changes made in this release were the removal of require_once statements, migration to PHP 5.3 namespaces, a refactored test suite, a rewritten Zend\Session, and the addition of the new Zend\Stdlib. The second development release was on November 3, 2010. The first stable release of Zend Framework 2.0 was released 5 September 2012.

0 komentar: