Showing posts with label ZendFramework. Show all posts
Showing posts with label ZendFramework. Show all posts

What is the Zend Framework ?

Zend Framework (ZF) is an open source, object-oriented web application framework implemented in PHP 5 and licensed under the New BSD License.

The Zend Framework is a simple, straightforward, open-source software framework for PHP 5 designed to eliminate the tedious details of coding and let you focus on the big picture. Its strength is in its highly-modular MVC design, making your code more reusable and easier to maintain.

Requirements for Zend framework

Zend Framework requires PHP 5.2.4 or later since version 1.7.0. 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 in these versions of PHP. Zend Framework 2.0 will require 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.

Building and running Zend Framework applications

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.[13]
Zend Studio is not free software, whereas the Zend Framework and Zend Server Community Edition are free.
Zend Server is compatible 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 preconfigured, free version of Eclipse PDT with Zend Debug is available on the Zend web site.

What is the Zend Framework ?

Zend_Controller_Front


1.getInstance() is used to retrieve a front controller instance

2.setControllerDirectory() is used to tell the dispatcher where to look for action controller class files

3.addControllerDirectory() is used to add a module name, it will set the directory for the default module -            overwriting it if it already exists.

4.getControllerDirectory() is used to get the current settings for the controller directory.

5.addModuleDirectory() allows you to pass the name of a directory containing one or more module directories.

6.getModuleDirectory() is used to determine the path to a particular module or the current module.

7.dispatch(Zend_Controller_Request_Abstract $request = null, Zend_Controller_Response_Abstract $response = null) does the heavy work of the front controller.
It may optionally take a request object and/or a response object.

8.Zend_Controller_Front::run($path) is a static method taking simply a path to a directory containing controllers.

9.resetInstance() can be used to clear all current settings.

10.setDefaultControllerName() and getDefaultControllerName() let you specify a different name to use for the default controller  and retrieve the current value.

11.setDefaultAction() and getDefaultAction() let you specify a different name to use for the default action  and retrieve the current value.

12.setRequest() and getRequest() let you specify the request class or object to use during the dispatch process and to retrieve the current object.

13.setRouter() getRouter() let you specify the router class or object to use during the dispatch process and to retrieve the current object.

14.setBaseUrl() and getBaseUrl() let you specify the base URL to strip when routing requests and to retrieve the current value.

15.setDispatcher() and getDispatcher() let you specify the dispatcher class or object to use during the dispatch process and retrieve the current object.

16.setResponse() and getResponse() let you specify the response class or object to use during the dispatch process and to retrieve the current object.

17.registerPlugin(Zend_Controller_Plugin_Abstract $plugin, $stackIndex = null) allows you to register plugin objects.

18.unregisterPlugin($plugin) let you unregister plugin objects. $plugin may be either a plugin object or a string denoting the class of plugin to unregister.

19.returnResponse($flag) is used to tell the front controller whether to return the response (TRUE) from dispatch(), or if the response should be automatically emitted (FALSE).

20.setParam($name, $value) allows you to set a single parameter of $name with value $value.

21.setParams(array $params) allows you to set multiple parameters at once using an associative array.

22.getParam($name) allows you to retrieve a single parameter at a time, using $name as the identifier.

23.getParams() allows you to retrieve the entire list of parameters at once.

24.clearParams() allows you to clear a single parameter , multiple named parameters, or the entire parameter stack.

Using Zend_Layout with the Zend Framework MVC

simple example of zend_layout.
First we define some defined default.


The next, we create LayoutController.


File layout in application\layouts\scripts\index.phtml

Read and write configuration files the format xml with Zend_Config


1. Read ini file.


 I have a configuration file /application/configs/db.ini as follows:



We use the class to read file Zend_Config_Xml db.xml


Output:



2.Write xml file




My01.xml output file:



Configuration multi modules in Zend Framework

1. Create the basic structure for the application


2. Create a detailed structure for each module (Admin & Default)


3. Configuration file application\index.php



4. Configuration file application\.htaccess



5.Install file contains application configuration application\configs\application.ini



6. Set the file contents application\layouts\layout.phtml


7. Create initialization files for all applications application\Bootstrap.phpl



8. Create files initializate for Admin module application\modules\admin\Bootstrap.php



9. IndexController inclusion of the Admin module - application\modules\admin\controllers\IndexController.php




10. Show indexAction content in the Admin module IndexController

- application\modules\admin\views\scripts\index\index.phtml


11. Create files initializate for Default module application\modules\default\Bootstrap.php



12.Inclusion of Default IndexController module - application\modules\default\controllers\IndexController.php



13. Display the contents of the Default IndexController indexAction in module

- application\modules\default\views\scripts\index\index.phtml

Read and write configuration files the format ini with Zend_Config


1. Read ini file.


 I have a configuration file /application/configs/db.ini as follows:



We use the class to read file Zend_Config_Ini db.ini


Output:



2.Write ini file




My01.ini output file:



Zend_Translate and Error message

For example: You build a form with Zend_Form, the only one Email address text box is simple and used to determine Zend_Validate_EmailAddress Email is valid:



When you run the function, you attempt to enter an invalid email example: 123; á @ 12222, and of course it's an error, a heap of errors like this:



But if you want all the messages above a certain language (as Default), why? For example, you want to report in Vietnamese? You will have to use Zend_Translate by way or that. But certainly one thing that you would like to be a headache.

After some research, I found a way that I was very pleased, and in this article I want to share with you. Note that I do on Zend 1:10:



And I wrote in my file vi.csv following language



After running the function, the resulting error is as follows:


Configs project ZendFramework

1.Create the tree directory looks like the following:


|-- application
|   |-- Bootstrap.php
|   |-- configs
|   |   `-- application.ini
|   |-- controllers
|   |   |-- ErrorController.php
|   |   `-- IndexController.php
|   |-- models
|   `-- views
|       |-- helpers
|       `-- scripts
|           |-- error
|           |   `-- error.phtml
|           `-- index
|               `-- index.phtml
|-- library
|-- public
|   |-- .htaccess
|   `-- index.php
`-- tests
|-- application
|   `-- bootstrap.php
|-- library
|   `-- bootstrap.php
`-- phpunit.xml


2.The Bootstrap
Your Bootstrap class defines what resources and components to initialize with file \application\Bootstrap.php.



3.Configuration 
The default configuration is placed in application/configs/application.ini, and contains some basic directives for setting your PHP environment, indicating the path to your bootstrap class, and the path to your action controllers.



4.Index.php
File start project in \public\index.php.



5.Action Controllers
Your application's action controllers contain your application workflow, and do the work of mapping your requests to the appropriate models and views.
The default IndexController is as follows:


6.Views
View scripts are placed in application/views/scripts/, where they are further categorized using the controller names. In the default case, we thus have the view scripts index/index.phtml
The following is what we install by default for the index/index.phtml view script:


7.Output

Zend_Controller

Zend_Controller is the heart of Zend Framework's MVC system. MVC stands for » Model-View-Controller and is a design pattern targeted at separating application logic from display logic.Zend_Controller_Front implements a » Front Controller pattern, in which all requests are intercepted by the front controller and dispatched to individual Action Controllers based on the URLrequested.
Create the Filesystem Layout
application/
    controllers/
        IndexController.php
    models/
    views/
        scripts/
            index/
                index.phtml
        helpers/
        filters/
html/
    .htaccess
    index.php
config/
     config.ini


*application: will the programming code of the application
- Controllers: source file contains the application processing

- Models: the files related to database handling

- View / scripts /: contains the directory. name of this folder will correspond to the Controller in the directory controllers. In each folder containing the. Phtml. Pages. Phtml is responsible for data output to the external interface

- Helpers: contains the file. Php. These files are simply expanding handler of the application, for example: handler involved that day in May in the library without ZF

- Filters: contains the file. Php. These files can also act as helpers in the directory file. But I often used to store the file has the function check condition of entry form.

*Html file containing the ZF startup (bootstrap), plus it can be a container imges, css, js ...
*Config file containing the application's configuration file



Multi layouts for Zend Framework


1. Create a directory \ templates as shown below:
2. Create index.phtml file in the folder \application\templates\admin with the following contents:


 3. Create index.phtml file in the folder \application\templates\default
with the following contents:

 4. Load layout in Admin modules: - Create function init () in
\application\modules\admin\controllers\IndexController.php
with the following contents:

 5. Load layout in Default modules. - Create function init () in
application\modules\default\controllers\IndexController.php
with the following contents:

19 steps to setting and configure the Zend Framework.



  • Step 1: Build system directories and files for the application 
  • Step 2: Declare the physical path to the application 
  • Step 3: Declare the application configuration in the configuration file 
  • Step 4: Declare the path to the folder containing the Zend Framework library 
  • Step 5: Go into the application layer Zend_Application 
  • Step 6: Create the class Bootstrap 
  • Step 7: Set the system error to the application 
  • Step 8: Declare path of initialization file (Bootstap.php) 
  • Step 9: Declare class Bootstrap 
  • Step 10: Declare the folder containing the application's Controller (appliction.ini) 
  • Step 11: Declare interface file name (layout.phtml) 
  • Step 12: Declare the path to the directory containing the main (layout) 
  • Step 13: Creating Zend_Application object and read the configuration file 
  • Step 14: Start the application 
  • Step 15: Create the file layout 
  • Step 16: Create IndexController 
  • Step 17: Create files for IndexController View 
  • Step 18: Build files. Htaccess 
  • Step 19: Run the application