Zend - Installation And Sample Project Creation

Preview:

Citation preview

Zend Framework IntroductionZend Framework Introductionbyby Susheel Kumar Sharma

Zend Framework Zend Framework IntroductionIntroduction

Open Source Framework

100% Object-Oriented Code

Use-At-Will

 MVC Pattern

MVCMVC

Model - data, web services, feeds, etc.

View - The display returned to the user.

Controller - Manages the request environment, and determines what happens.

MVC Cont..MVC Cont..

MVC MVC Cont…Cont…

Controller <-> ViewController and View can interact

Controller <-> ModelController can pull data from the model for decisioning, or push data to the model

View <- ModelView can access the model to retrieve data, but not write to it.

Installation Zend Framework Installation Zend Framework Cont..Cont..

By Installing Zend Server

Manual Installation

Installation Zend Framework Installation Zend Framework Cont..Cont..

Manual Installation

1.Check php is available from any directory

C:\Users\compare>php –I

2.If not, set path for php.exe in System Environment Variable a)Find your php installation directory - This can

generally be found in one of the following places: C:\php C:\xampp\php

b)Go to -> My Computer -> (Right Click)Properties -> Advanced -> Environment Variables -> System Variables ->

(Select)Path -> Edit -> Variable Value(Paste) -> Ok

Installation Zend Framework Installation Zend Framework Cont..Cont..

3.Copy zf.bat, zf.php from ZendFramework\bin and paste zf.bat, zf.php to C:\php\ or C:\xampp\php\

4.Set up Zend Framework library to system PHP include_path a) Open php.ini in an editor b) Find include_path c) Add Zend Framework library e.g. include_path = ".;G:\xampp\php\pear\;G:\data\ZendFramework\library“

5. Run command C:\Users\compare>zf show versionZend Framework Version: 1.11.2

Create Create ProjectProject

1.Go to directory where you want to create project e.g.

C:\Users\compare>cd ..

C:\Users>cd ..

C:\>G:

G:\>cd xampp\htdocs

G:\xampp\htdocs>

2. Run command

G:\xampp\htdocs> zf create project trainingtestproject

Creating project at G:/xampp/htdocs/trainingtestproject

Note: This command created a web project, for more information setting up your V

HOST, please see docs/README

Create Project Cont..Create Project Cont..

3. Project directory structure

trainingtestproject|-- 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

Create Project Create Project Cont..Cont..

4.Copy Zend directory from

ZendFramework\library\Zend

and paste to

trainingtestproject\library\Zend(Optional)

Main ArtifactsMain Artifacts

Bootstrap

Configuration

Action Controllers

Views

The BootstrapThe Bootstrap

Bootstrap class defines what resources and components to initialize

By default, Zend Framework's Front Controller is initialized

It uses the application/controllers/ as the default directory in which to look for action controllers

The class looks like the following:

// application/Bootstrap.phpclass Bootstrap extends Zend_Application_Bootstrap_Bootstrap {}

ConfigurationConfiguration

Zend Framework is itself configuration less, need to configure application.

The default configuration is placed in application/configs/application.ini

Contains some basic directives for setting PHP environment 

It looks as follows:

Configuration Cont..Configuration Cont..

; application/configs/application.ini[production]phpSettings.display_startup_errors = 0phpSettings.display_errors = 0includePaths.library = APPLICATION_PATH "/../library"bootstrap.path = APPLICATION_PATH "/Bootstrap.php"bootstrap.class = "Bootstrap"appnamespace = "Application"resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"resources.frontController.params.displayExceptions = 0[staging : production] [testing : production]phpSettings.display_startup_errors = 1phpSettings.display_errors = 1[development : production]phpSettings.display_startup_errors = 1phpSettings.display_errors = 1

Action ControllersAction Controllers

action controllers contain application workflow, do mapping requests to the appropriate models and views.

action controller should have one or more methods ending in “Action”

 methods may then be requested via the web

Zend Framework URLs follow the schema /controller/action

"controller" maps to the action controller name (minus the "Controller" suffix) and "action" maps to an action method (minus the "Action" suffix)

need an IndexController, which is a fallback controller and which also serves the home page of the site

ErrorController, which is used to indicate things such as HTTP 404 errors (controller or action not found) and HTTP 500 errors (application errors)

Action Controllers Cont..Action Controllers Cont..

The default IndexController is as follows:// application/controllers/IndexController.phpclass IndexController extends Zend_Controller_Action{    public function init()    {        /* Initialize action controller here */    }    public function indexAction()    {        // action body    }}

Action Controllers Cont..Action Controllers Cont..

ErrorController is as follows:

// application/controllers/ErrorController.phpclass ErrorController extends Zend_Controller_Action{    public function errorAction()    {        $errors = $this->_getParam('error_handler');        switch ($errors->type) {         

  case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:         

  case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLE

R:        

  case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:

                // 404 error -- controller or action not found

                $this->getResponse()->setHttpResponseCode(404);

                $this->view->message = 'Page not found';

                break;

            default:

                // application error

                $this->getResponse()->setHttpResponseCode(500);

                $this->view->message = 'Application error';

                break;

        }

         $this->view->exception = $errors->exception;

        $this->view->request   = $errors->request;

    }

}

ViewsViews

Written in plain old PHP

View scripts are placed in application/views/scripts/

They are further categorized using the controller names

We have an IndexController and an ErrorController

Thus we have corresponding  index/ and error/ 

subdirectories within view scripts directory

We thus have the view scripts index/index.phtml 

and error/error.phtml

The following is what we install by default for the index/index.phtml view script:

Views Cont..Views Cont..

<!-- application/views/scripts/index/index.phtml --><style>    a:link,    a:visited{color: #0398CA;}    span#zf-name{color: #91BE3F;}    div#welcome{color: #FFFFFF;background-image: url(http://framework.zend.com/images/bkg_header.jpg); width:  600px;height: 400px;border: 2px solid #444444; overflow: hidden;text-align: center;}    div#more-information{background-image: url(http://framework.zend.com/images/bkg_body-bottom.gif);        height: 100%;} </style>

<div id="welcome">

    <h1>Welcome to the <span id="zf-name">Zend Framework!</span><h1 />

    <h3>This is your project's main page<h3 />

    <div id="more-information">

        <p>

            <img src="http://framework.zend.com/images/PoweredBy_ZF_4LightBG.png" />

        </p>

         <p>

            Helpful Links: <br />

            <a href="http://framework.zend.com/">Zend Framework Website</a> |

            <a href="http://framework.zend.com/manual/en/">Zend Framework

                Manual</a>

        </p>

    </div>

</div>

Views Cont..Views Cont..

The error/error.phtml view script :<!-- application/views/scripts/error/error.phtml --><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN";    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd><html xmlns="http://www.w3.org/1999/xhtml"><head>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  <title>Zend Framework Default Application</title></head><body>  <h1>An error occurred</h1>  <h2><?php echo $this->message ?></h2>   <?php if ('development' == $this->env): ?>   <h3>Exception information:</h3>  <p> 

    <b>Message:</b> <?php echo $this->exception->getMessage() ?>

  </p>

   <h3>Stack trace:</h3>

  <pre><?php echo $this->exception->getTraceAsString() ?>

  </pre>

   <h3>Request Parameters:</h3>

  <pre><?php echo var_export($this->request->getParams(), 1) ?>

  </pre>

  <?php endif ?>

 </body>

</html>

Create a virtual Create a virtual host(Optional)host(Optional)

Open httpd.conf file in any editor, Some common locations: /etc/httpd/httpd.conf (Fedora, RHEL, and

others), C:\Program Files\Zend\Apache2\conf (Zend Server on Windows machines)/ G:\xampp\apache\conf

 First, ensure that the NameVirtualHost is defined; set it to a value of "*:80"

define a virtual host:<VirtualHost *:80>    ServerName quickstart.local    DocumentRoot /path/to/quickstart/public    SetEnv APPLICATION_ENV "development"    <Directory /path/to/quickstart/public>        DirectoryIndex index.php        AllowOverride All        Order allow,deny        Allow from all    </Directory></VirtualHost>

Create a virtual host(Optional) Cont..Create a virtual host(Optional) Cont..

<VirtualHost *:80>    ServerName  trainingtestproject.local    DocumentRoot /path/to/quickstart/public    SetEnv APPLICATION_ENV "development"    <Directory /path/to/quickstart/public>        DirectoryIndex index.php        AllowOverride All        Order allow,deny        Allow from all    </Directory></VirtualHost>

Create a virtual host(Optional) Cont..Create a virtual host(Optional) Cont..

 DocumentRoot setting specifies the public subdirectory of project; this means that only files under that directory can ever be served directly by the server

AllowOverride, Order, and Allow directives; these are to allow to use htacess files within project

SetEnv directive is setting an environment variable for virtual host; this variable will be picked up in the index.php and used to set the APPLICATION_ENV constant for our Zend Framework application

Add an entry in hosts file located at : On *nix-like systems, this is usually /etc/hosts;

On windows : C:\WINDOWS\system32\drivers\etc

127.0.0.1 trainingtestproject.local

Reference :

http://framework.zend.com/

http://framework.zend.com/manual/en/

Recommended