Zend Framework QuickStart — Creating A Project

This article is not something supernatural and is suited more for those who are beginning to learn
The Zend Framework. It just so happened that I started learning with version
1.7, but shortly after the beginning of knowledge of this tool I was taken into the army, and programming was not engaged. Now to return to their homes remained a month, and I had some free time.
At the beginning of studying any instrument, as I noticed myself always want to have it on hand to immediately if possible
to dig deeper and not to pre-read a bunch of documentation.
So I had with Zend Framework when it was still version 1.7
Had preciate a few articles and re-create a bunch of directories,
to configure a working Zend Framework project. Actually this article is a kind of translation of the article Zend Framework Quick Start | Create Your Project to version Zend Framework 1.10.5
With some corrections and notes.
After reading it I think you will easily create a working Zend Framework
the project and immediately goes to the study in practice.

In General, if all someone article seem to taste, take
for the translation of all the articles series Quick Start.

So there you go...


Installing Zend Framework


In order to create your project, first you need to download and extract Zend Framework.
I will not go into the details of horse racing, let me just say that
it lies where the here

The simplest way to Zend Framework, including customized PHP is to install Zend Server.
Zend Server includes installers for these operating sisem, such as: Mac OSX, Windows, Fedora Core, and Ubuntu.
Additionally, Zend Server also has a universal installation package compatible with most Linux distribution.
After installing Zend Server a, you can find the Framework in the directory /usr/local/zend/share/ on the operating systems MacOS X and Linux or in the directory C:\Program Files\Zend\ZendServer\share\ZendFramework in the Windows operating system.
In addition, you can download the latest version of the Zend Framework as an archive and then unpack it.

Create project


note: In the directory where you installed the Zend Framework is a subdirectory of the bin content scripts:
zf.sh and zf.bat for Unix-based OS and Windows OS respectively. Remember the absolute path to the script.
Below, you will use the command zf. For it to work properly, replace the path to the command to the absolute path for your operating system. In Unix systems you can use alias.
For example:
alias zf.sh=path/to/ZendFramework/bin/zf.sh
If you have any problems using zf script, use rukovodstvo

Open a terminal window (In Windows laponite "start"-> Run->"cmd") In the console, navigate to the directory in which you want to create your project. Then, use the script zf to create project.
For example:
the zf create project quickstart
The result of this command will create a standard Zend framework project glucouse standard controllers and scripts.
The directory tree will look as follows:
quickstart
|-- 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

Further, if you do not specify the path to the Zend Framework scripts in include_path in your php.ini file, it is recommended to either create a link or copy the scripts Zend framework directory to the library/ of your project.
In Unix systems this can be done as follows:
# Symlink:
% cd library; ln-s path/to/ZendFramework/library/Zend
# Copy:
% cd library; cp-r path/to/ZendFramework/library/Zend
Now when the project is created you can proceed to the analysis of such concepts as
Loader (Bootstraper), Configuration (Configuration), action Controllers (Action Controllers) and Scenario View (Views).

The Bootstraper (Loader)


Class Bootstraper defines what resources and components will initialize your project. Default initialized in Front Controller that uses the application/controllers/ as the default directory to search for the action controllers (which we discuss later), This class looks like the following:

    // application/Bootstrap.php

    class Bootstrap extends Zend_Application_Bootstrap_Bootstrap

    { the

  1. }
* This source code was highlighted with Source Code Highlighter.


Configuration (Configuration)


At this point your project in Zend Framework is already more or less configured. However, in the future you will need to configure your app to your taste. The main configuration file is by default stored in the file: application/configs/application.ini contains the Directive settings to your PHP environment (such as enabling and otsloenie error report). In addition to this file is specified
the classpath of the bootstrap (Bootstrap), and the path to the action controllers (Action Controllers).
This file looks as follows:
; application/configs/application.ini
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.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 = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

Here are a few things you need to know about the configuration file. When using the ini configuration file you can use constants. APPLICATION_PATH is a constant. Also this file has several sections: production, staging, testing and development. The last three sections inherit the settings from the section 'production'. This division into categories is a good way to guarantee the available settings for each stage of development.

action Controllers (Action Controllers)


Action controllers are the main part of the application and responsible for processing queries that select the data from the models (models) and transfer them to view (views).
Controller actions must have one or more methods ending with the word Action, these methods will be run when you request any page in your application. The default URL in Zend Framework follow the following pattern: "controller/action" where controller — current controller and the action — the current action. For example: mysite/simple/test. Here the Controller is simple and the applicable test.
Usually Zend Framework application should have two controller actions is IndexController controller which is the controller for umolchaniu page or home page of your website and ErrorController designed to handle errors like HTTP 404 (Page not found) or HTTP 500 (application Error).
IndexController present by default after you create your project Zend framework looks like the following:

    // application/controllers/IndexController.php

    class IndexController extends Zend_Controller_Action

    {

    public function init()

    {

    /* Initialize action controller here */ the

  1. }
  2. public function indexAction()

    {

    // action body the

  3. }
  4. the
  5. }
* This source code was highlighted with Source Code Highlighter.

ErrorController created by default looks like this:

    // application/controllers/ErrorController.php

    {

    public function errorAction()

    { the

  1. $errors = $this->_getParam('error_handler');
  2. switch ($errors->type) {

    case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:

    case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:

    case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:

    // 404 error -- controller or action not found the

  3. $this->getResponse()->setHttpResponseCode(404);
  4. the
  5. $this->view->message = 'Page not found';
  6. break;

    default:

    // application error the

  7. $this->getResponse()->setHttpResponseCode(500);
  8. the
  9. $this->view->message = 'Application error';
  10. break; the

  11. }
  12. the
  13. $this->view->exception = $errors- > exception;
  14. the
  15. $this->view->request = $errors->request;
  16. the
  17. }
  18. the
  19. }
* This source code was highlighted with Source Code Highlighter.

It should be noted that IndexController has virtually any code, while ErrorController refers to the representation of "view".

Scripts view (views)


Scenario view in Zend Framework by default use a simple php syntax. Scripts from the directory application/views/scripts/, where they are divided into directories, depending on controller action. So for example in our project there are controllers IndexController and ErrorController, respectively the scenarios for them are in the subdirectories index and error/ In these subdirectories contain files like *.phtml who are scripts for action. So by default, we have a scenario of the form: index/index.phtml and error/error.phtml. For Index of controller Error Controller, respectively.
Scripts can contain any text. In addition, you can use php tags <?php ?> to insert them in PHP directives.
It looks like the script for the action index controller indexController:

    <!-- application/views/scripts/index/index.phtml -->

    <style>

    a:link,

    a:visited

    { the

  1. color: #0398CA;
  2. the
  3. }
  4. the
  5. span#zf-name
  6. { the

  7. color: #91BE3F;
  8. the
  9. }
  10. the
  11. div#welcome
  12. { the

  13. color: #FFFFFF;
  14. the
  15. background-image: url(http://framework.zend.com/images/bkg_header.jpg);
  16. the
  17. width: 600px;
  18. the
  19. height: 400px;
  20. the
  21. border: 2px solid #444444;
  22. the
  23. overflow: hidden;
  24. the
  25. text-align: center;
  26. the
  27. }
  28. the
  29. div#more-information
  30. { the

  31. background-image: url(http://framework.zend.com/images/bkg_body-bottom.gif);
  32. the
  33. height: 100%;
  34. the
  35. }
  36. </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/manual/en/">Zend Framework the

  37. Manual</a>
  38. </p>

    </div>

    </div>

* This source code was highlighted with Source Code Highlighter.


And it looks like the script for the error controller (Controller Error)
Note that it contains some PHP instructions.

    <!-- application/views/scripts/error/error.phtml -->

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"; the

  1. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
  2. <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->the 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->requests>getParams(), 1) ?>

    </pre>

    <?php endif ?>

    </body>

    </html>

* This source code was highlighted with Source Code Highlighter.


create a Virtual Host (Virtual Host)


In this example we use the server Apache. Zend Framework works fine with other servers such as Microsoft IIS, lighthttpd, nginx etc, but most developers should at least be familiar with Apache.
To create a virtual host you need to know the location of your file httpd.conf
Here is a list of directories where is the configuration file for the Apache server

/etc/httpd/httpd.conf (Fedora, RHEL, and others)
/etc/apache2/httpd.conf (Debian, Ubuntu, and others)
/usr/local/zend/etc/httpd.conf (Zend Server on *nix operating systems)
C:\Program Files\Zend\Apache2\conf (Zend Server on Windows)


In your file httpd.conf (or httpd-vhosts.conf on some systems), you will need to do two things. First, check that the NameVirtualHost is defined; usually "*:80".
Then create a virtual host for your project:
<VirtualHost quickstart: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



There are a few things you should know. The Directive DocumentRoot is a public/ directory of your project; AllowOverride, Order and Allow Directive must be set so as to allow the use of .htacess files within our project. During development, it is a good practice since it eliminates the need for constant server restart when changing some of the directives within your project. However, production versions of these guidelines must be configured to ban the use of this file. Thirdly you should set the Directive SetEnv. Here we set the environment variable for your virtual host; which will be subsequently included in the index.php and used, you'll get an answer to the constant APPLICATION_ENV for our Zend Framework application. In production version, you can omit this Directive.
Finally, you will need to add your virtual host to hosts On *nix systems, it is usually /etc/hosts; in Windows, you will find it in C:\WINDOWS\system32\drivers\etc.
Regardless of the operating system, the hosts file will look something like the following:
127.0.0.1 quickstart.local

Now start your Web server (or restart it), and can continue to study Zend Framework.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Why I left Google Zurich

2000 3000 icons ready — become a sponsor! (the table of orders)

New web-interface for statistics and listen to the calls for IP PBX Asterisk