This article is not supernatural and rather suitable for those who are starting to learn
Zend Framework. It so happened that I myself began to study with the version
1.7, but shortly after the beginning of the knowledge of this tool, I was taken to the army, and I did not do programming for a year. Now, before returning to their homeland, there is a month left, as well as free time.
At the beginning of the study of any instrument, as I noted on my own, I always want to have it on hand, so that, whenever possible,
dig out and not pre-read a bunch of documentation.
So I had it with the Zend Framework, when it was still version 1.7
I had to read several articles and recreate a bunch of directories,
to set up a working Zend Framework project. Actually this article is a kind of translation of the article
Zend Framework Quick Start | Create Your Project for
Zend Framework version
1.10.5With some corrections and notes.
After reading it, I think you can easily create a working Zend Framework
project and immediately proceed to the study in practice.
In general, then if someone else seems to like the article, I will take
for the translation of all articles in the Quick Start series.
So let's go ...
')
Installing Zend Framework
In order to create your project, first you need to download and unzip the Zend Framework.
I will not go into the details of the race, I will only say that
he lies somewhere
hereThe easiest way to get the Zend Framework, including customized PHP, is to install
Zend Server .
Zend Server has installers for operating systems such as: Mac OSX, Windows, Fedora Core, and Ubuntu.
In addition, Zend Server also has a universal installation package compatible with most Linux distributions.
After installing Zend Server, you can find the Framework in the
/ usr / local / zend / share / directory on MacOS X and Linux operating systems or in the
C: \ Program Files \ Zend \ ZendServer \ share \ ZendFramework directory on the Windows operating system.
In addition, you can download the latest version of Zend Framework as an archive and then unpack it.
Creating a project
Note: In the directory where Zend Framework is installed, the bin subdirectory contains the contents of the scripts:
zf.sh and zf.bat for Unix-based OS and Windows OS, respectively. Remember the absolute path to these scripts.
The zf command will be used below. For it to work correctly, replace the command path with the absolute path for your operating system. In Unix systems, alias can be used.
For example:
alias zf.sh=path/to/ZendFramework/bin/zf.sh
If you have problems using the zf script, use the guide.Open a terminal window (On Windows, go to "Start" -> "Run" -> "cmd") In the console, go to the directory where you want to create your project. Next, use the
zf script to create the project.
For example:
zf create project quickstart
As a result of executing this command, a standard ZendFramework project will be created including standard controllers and view scripts.
The directory tree will look like this:
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 did not specify the path to the Zend Framework scripts in the
include_path of your php.ini file, it is recommended to either create a link or copy the ZendFramework scripts to the
library / directory of your project.
On 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 go on to analyze such concepts as
Loader (Bootstraper), Configuration (Configuration), Action Controllers (Action Controllers) and View Scripts (Views).
The Bootstraper (Loader)
The
Bootstraper class defines which resources and components will be initialized in your project. By default, the
Front Controller is initialized, which uses the
application / controllers / directory as the default directory to search for action controllers (we will discuss them later). This class looks something like this:
- // application / Bootstrap.php
- class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
- {
- }
* This source code was highlighted with Source Code Highlighter .
Configuration
At the moment, your project on the Zend Framework is more or less configured. However, in the future, in any case, you will need to configure your application to your taste. The main configuration file is stored by default in the file:
application / configs / application.ini and contains the basic directives for configuring your PHP environment (for example, enabling and disabling error reporting). In addition, this file contains
the path to the bootstrap class (Bootstrap), and the path to the action controllers (Action Controllers).
This file looks like this:
; 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. In addition, several sections are defined in this file: production, staging, testing, and development. The last three sections inherit settings from the 'production' section. This categorization is a good way to ensure that settings are available at every development stage.
Action Controllers
Action controllers are the main part of the application and are responsible for processing requests, selecting data from models (models) and transferring them to views (views).
The action controller must have one or several methods ending with the word
“Action” ; these methods will be launched when a page of your application is requested. By default, the Zend Framework URLs follow the following pattern:
"/ controller / action" where
controller is the current controller and
action is the current action. For example:
mysite / simple / test . Here the Controller is
simple and the action is
test .
Typically, the Zend Framework application should have two action controllers —
IndexController — the controller that is the default controller or home page of your site and the
ErrorController that is designed to handle errors such as HTTP 404 (Page not found) or HTTP 500 (Application error).
IndexController present by default after creating a ZendFramework project looks like this:
- // application / controllers / IndexController.php
- class IndexController extends Zend_Controller_Action
- {
- public function init ()
- {
- / * Initialize action controller here * /
- }
- public function indexAction ()
- {
- // action body
- }
- }
* This source code was highlighted with Source Code Highlighter .
ErrorController created by default looks like this:
- // application / controllers / ErrorController.php
- class 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_CONTROLLER:
- 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;
- }
- }
* This source code was highlighted with Source Code Highlighter .
It should be noted that
IndexController has virtually no code, while
ErrorController refers to the
view view .
View Scripts (views)
View scripts in the Zend Framework by default use simple php syntax. View scripts are located in the
application / views / scripts / directory, where they are distributed into directories depending on the action controller. So for example in our project there are Controllers
IndexController and
ErrorController , respectively, the type scripts for them are in the subdirectories
index / and
error / These subdirectories contain files like
* .phtml , which are the type scripts for actions. So by default we have scripts of the form:
index / index.phtml and
error / error.phtml . For
Index controller and
Error Controller respectively.
View scripts can contain any text you like. In addition, you can use php tags
<? Php?> To insert PHP directives into them.
This is the view script for the
index indexcontroller's
index action:
- <! - 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 >
* This source code was highlighted with Source Code Highlighter .
And this is what the view script for the error controller looks like (Error Controller)
Note that it contains some PHP instructions.
- <! - 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 >
* This source code was highlighted with Source Code Highlighter .
Creating a Virtual Host (Virtual Host)
In this example, we will use the
Apache server. 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
httpd.conf file.
Here is a list of directories where the Apache server configuration file is located.
/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
httpd.conf file (or httpd-vhosts.conf on some systems), you will need to do two things. First, check that NameVirtualHost is defined; usually "*: 80".
Then we 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
DocumentRoot directive indicates the
public / directory of your project;
AllowOverride, Order and Allow directives must be configured to allow the use of
.htacess files within our project. During development, this is a good practice, as there is no need to constantly restart the server when changing certain directives within your project. However, in the production version of these directives must be configured to prohibit the use of this file. Third, you must set the
SetEnv directive. Here we set the environment variable for your virtual host; which will later be included in
index.php and used to set the
APPLICATION_ENV constant for our Zend Framework application. In the production version, you can omit this directive.
Finally, you will need to add your
virtual host to the
hosts file. On * nix systems, it is located - 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 this:
127.0.0.1 quickstart.local
Now start your Web server (or restart it), and you can continue exploring the Zend Framework.