Update (2014): This is the 2007 article, which, to my surprise, is still in demand. For this reason, I updated it in accordance with the new rules for the design of posts on Habré, and added syntax highlighting for code examples. If someone wants to add or fix something, the source code with habr-markup is posted in open access:
gist.github.com/dreikanter/2b4ee996d7a775e707d7Annotation from the translator
PHP is one of the most widely spoken web application development languages, and yet one of the most controversial. I have often seen a negative attitude towards this technology, and the disadvantages that provoke this attitude are no secret. However, PHP is actively evolving and in many ways is gradually getting better. One of the major steps of its development, in my opinion, is the emergence of MVC frameworks, designed to systematize the process of developing web applications and to accustom to the order of developers, who are often not strong enough will power, with all the freedom provided by the language, to preserve a competent and beautiful infrastructure of the software being developed (at once I will clarify that the last statement is subjective and based solely on the code of various software solutions that I have seen).
Recently, I actively became interested in the MVC architecture and made my own implementation of the framework on this concept for PHP4. Most recently, the first official release of the Zend Framework, which I heard about a long time ago, was caught my eyes, but all hands did not get to play with it. For PHP, there are other similar libraries, but in this case attracted the brand.
')
At first I experienced some disappointment in the official documentation, which turned out to be a good reference, but simply no tutorial, and therefore did not fit the role of introductory material. But almost immediately there was a good article in which the author step by step examines the process of creating a web application. There is enough material to understand it, you can begin to consciously navigate in the mana from Zend. The text is aimed at programmers with some experience in programming web applications in PHP, but not familiar with the Zend Framework.
The article is quite voluminous, so I decided to divide it into two parts in order not to overload the brain to anyone. Below I give the first part of its translation, in which the concept of MVC is outlined in general terms and the structure of a web application implemented on the Zend Framework is analyzed. If the material is interested in hack readers, I will publish a sequel.
At the end of the introduction, a small disclaimer. Dear lovers of Ruby on Rails and Python on Django, I respect your religion :) Please do not develop a theme in the comments about which framework or language is better. This is not at all related to the topic of this post topic. It will be much more interesting for me and probably many now to learn about the experience of real software development on Zend.
Posted by: Rob Allen,
akrabat.comOriginal: akrabat.com/zend-framework-tutorialTranslation: Alexander Musayev,
musayev.comThe material is designed to give an overview of the use of the Zend Framework to create the simplest applications using databases. These examples were tested on the Zend Framework version 1.0.0. Most likely, they will work with later versions, but not with earlier ones.
Architecture Model-View-Controller
A PHP application written in the traditional way may be somewhat similar to the following example:
<?php
include "common-libs.php";
include "config.php";
mysql_connect($hostname, $username, $password);
mysql_select_db($database);
?>
<?php include "header.php"; ?>
<h1>Home Page</h1>
<?php
$sql = "SELECT * FROM news";
$result = mysql_query($sql);
?>
<table>
<?php while ($row = mysql_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['date_created']; ?></td>
<td><?php echo $row['title']; ?></td>
</tr>
<?php } ?>
</table>
<?php include "footer.php"; ?>
, , . . («») . . :
Zend Framework -- (Model-View-Controller). , . .
Zend Framework :
- PHP 5.1.4 ;
- -, mod_rewrite.
Apache.
Zend Framework
Zend Framework ZIP TAR.GZ
framework.zend.com/download/stable.
Zend Framework , . , , , Apache. , Zend Framework .
,
zf-tutorial
-. , URL
localhost/zf-tutorial
( , ).
-:
zf-tutorial/
/application
/controllers
/models
/views
/filters
/helpers
/scripts
/library
/public
/images
/scripts
/styles
, , . , CSS ,
public
.
,
ZendFramework-1.0.0.zip
(
.tar.gz
) .
ZendFramework-1.0.0
.
library/Zend
zf-tutorial/library
.
zf-tutorial/library
Zend
.
Zend_Controller
Zend Framework («») URL. ,
index.php
, (bootstrapper). .
.htaccess
,
zf-tutorial
.
zf-tutorial/.htaccess:
RewriteEngine on
RewriteRule .* index.php
php_flag magic_quotes_gpc off
php_flag register_globals off
RewriteRule
URL, , «
index.php
URL». PHP. ,
php.ini
, . ,
php_flag
.htaccess
mod_php
. PHP CGI FastCGI,
php.ini
.
, , , JavaScript CSS,
index.php
. ,
public
, Apache, .
.htaccess
public
.
zf-tutorial/public/.htaccess:
RewriteEngine off
,
.htaccess
zf-tutorial/application
zf-tutorial/library
, .
zf-tutorial/application /.htaccess, zf-tutorial/library/.htaccess:
Deny from all
web . (. : , ,
public
, ).
,
.htaccess
, Apache (
httpd.conf
)
AllowOverride
All
.
.htaccess
(Jayson Minard) « PHP-: ( 2)» (http://devzone.zend.com/node/view/id/119). .
index.php
zf-tutorial/index.php
:
<?php
error_reporting(E_ALL|E_STRICT);
date_default_timezone_set('Europe/London');
set_include_path('.'.PATH_SEPARATOR . './library'
.PATH_SEPARATOR.'./application/models/'
.PATH_SEPARATOR.get_include_path());
include "Zend/Loader.php";
Zend_Loader::loadClass('Zend_Controller_Front');
$frontController = Zend_Controller_Front::getInstance();
$frontController->throwExceptions(true);
$frontController->setControllerDirectory('./application/controllers');
$frontController->dispatch();
, PHP
?>
, . . . ,
header()
, .
.
error_reporting(E_ALL|E_STRICT);
date_default_timezone_set('Europe/London');
(,
php.ini
display_errors
on
). , PHP 5.1+. .
set_include_path('.'.PATH_SEPARATOR.'./library'
.PATH_SEPARATOR.'./application/models/'
.PATH_SEPARATOR.get_include_path());
include "Zend/Loader.php";
Zend Framework ,
include_path
, . ,
include_path
, .
Zend/Loader.php
Zend_Loader
, Zend Framework.
Zend_Loader::loadClass('');
Zend_Loader::loadClass()
. : «_» «/»,
.php
. ,
Zend_Controller_Front
Zend/Controller/Front.php
. ,
Zend_Loader::loadClass()
.
, , — . , , -, URL . URL
index.php
, URI .
URL
Request
, ,
$frontController->setBaseUrl()
.
:
$frontController = Zend_Controller_Front::getInstance();
$frontController->setControllerDirectory('./application/controllers');
$frontController->throwExceptions(true);
, , , . -,
_exceptions
( Response object). , URL, HTTP-, - .
. , -, Zend Framework, , , () . , , .
, , :
$frontController->dispatch();
localhost/zf-tutorial
, , :
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (index)' in...
, -. , , .
-
, -. , CD, . , , :
Fieldname | Type | Null? | Notes |
---|
id | Integer | No | Primary key, Autoincrement |
artist | Varchar(100) | No | |
title | Varchar(100) | No | |
, :
, , Zend Framework. , - «».
. . «» action. , MVC . , ( ) — «».
, , — «». , URL
localhost/zf-tutorial/news/view
news
, —
view
. . news,
current
,
archived
view
( , ).
Zend Framework , , .
Zend Framework -,
index
. URL
localhost/zf-tutorial/news
(
index
news
). - , - —
index
. , URL
localhost/zf-tutorial
index
index
.
, Zend Framework, -, . , — . , , . , :
| | |
---|
| Index | Index |
| Index | Add |
| Index | Edit |
| Index | Delete |
. Zend Framework
{_}Controller
.
{_}Controller.php
, . : - , — . -
{_}Action
. , .
, -
IndexController
zf-tutorial/application/controllers/IndexController.php
.
zf-tutorial/application/controllers/IndexController.php:
<?php
class IndexController extends Zend_Controller_Action
{
function indexAction()
{
echo "<p>in IndexController::indexAction()</p>";
}
function addAction()
{
echo "<p>in IndexController::addAction()</p>";
}
function editAction()
{
echo "<p>in IndexController::editAction()</p>";
}
function deleteAction()
{
echo "<p>in IndexController::deleteAction()</p>";
}
}
. , URL:
URL | Displayed text |
---|
localhost/zf-tutorial | in IndexController::indexAction() |
localhost/zf-tutorial/index/add | in IndexController::addAction() |
localhost/zf-tutorial/index/edit | in IndexController::editAction() |
localhost/zf-tutorial/index/delete | in IndexController::deleteAction() |
- , -. - , « » .
.
Zend Framework, , —
Zend_View
. , , -.
Zend_View
:
$view = new Zend_View();
$view->setScriptPath('/path/to/view_files');
echo $view->render('view.php');
, -, , . , , - .
Zend Framework « » (action helpers).
Zend_Controller_Action_Helper_ViewRenderer
view
(
$this->view
), . -
Zend_View
,
views/scripts/{_}/{_}.phtml
(- , ).
, - Response, , , HTTP , . , - .
, , , , , — . , .
IndexController
.
zf-tutorial/application/contollers/IndexController.php:
<?php
class IndexController extends Zend_Controller_Action
{
function indexAction()
{
$this->view->title = "My Albums";
}
function addAction()
{
$this->view->title = "Add New Album";
}
function editAction()
{
$this->view->title = "Edit Album";
}
function deleteAction()
{
$this->view->title = "Delete Album";
}
}
title
view
. , — , .
-.
zf-tutorial/application/views/scripts/index/index.phtml:
<html>
<head>
<title><?php echo $this->escape($this->title); ?></title>
</head>
<body>
<h1><?php echo $this->escape($this->title); ?></h1>
</body>
</html>
zf-tutorial/application/views/scripts/index/add.phtml:
<html>
<head>
<title><?php echo $this->escape($this->title); ?></title>
</head>
<body>
<h1><?php echo $this->escape($this->title); ?></h1>
</body>
</html>
zf-tutorial/application/views/scripts/index/edit.phtml:
<html>
<head>
<title><?php echo $this->escape($this->title); ?></title>
</head>
<body>
<h1><?php echo $this->escape($this->title); ?></h1>
</body>
</html>
zf-tutorial/application/views/scripts/index/delete.phtml:
<html>
<head>
<title><?php echo $this->escape($this->title); ?></title>
</head>
<body>
<h1><?php echo $this->escape($this->title); ?></h1>
</body>
</html>
, .
HTML-
HTML . :
header.phtml
footer.phtml
, .
zf-tutorial/application/views/scripts/header.phtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title><?php echo $this->escape($this->title); ?></title>
</head>
<body>
<div id="content">
( , .)
zf-tutorial/application/views/scripts/footer.phtml:
</div>
</body>
</html>
.
zf-tutorial/application/views/scripts/index/index.phtml:
<?php echo $this->render('header.phtml'); ?>
<h1><?php echo $this->escape($this->title); ?></h1>
<?php echo $this->render('footer.phtml'); ?>
zf-tutorial/application/views/scripts/index/add.phtml:
<?php echo $this->render('header.phtml'); ?>
<h1><?php echo $this->escape($this->title); ?></h1>
<?php echo $this->render('footer.phtml'); ?>
zf-tutorial/application/views/scripts/index/edit.phtml:
<?php echo $this->render('header.phtml'); ?>
<h1><?php echo $this->escape($this->title); ?></h1>
<?php echo $this->render('footer.phtml'); ?>
zf-tutorial/application/views/scripts/index/delete.phtml:
<?php echo $this->render('header.phtml'); ?>
<h1><?php echo $this->escape($this->title); ?></h1>
<?php echo $this->render('footer.phtml'); ?>
, - , CSS. , , URL , CSS-.
getBaseUrl()
Request
. URL.
,
IndexController::init()
.
zf-tutorial/application/controllers/IndexController.php:
...
class IndexController extends Zend_Controller_Action
{
function init()
{
$this->view->baseUrl = $this->_request->getBaseUrl();
}
function indexAction()
{
...
CSS-
header.phtml
:
zf-tutorial/application/views/scripts/header.phtml:
...
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title><?php echo $this->escape($this->title); ?></title>
<link rel="stylesheet" type="text/css" media="screen"
href="<?php echo $this->baseUrl;?>/public/styles/site.css" />
</head>
...
, , , .
zf-tutorial/public/styles/site.css:
body,html {
font-size: 100%;
margin: 0;
font-family: Verdana, Arial, Helvetica, sans-serif;
color: #000;
background-color: #fff;
}
h1 {
font-size:1.4em;
color: #800000;
background-color: transparent;
}
#content {
width: 770px;
margin: 0 auto;
}
label {
width: 100px;
display: block;
float: left;
}
#formbutton {
margin-left: 100px;
}
a {
color: #800000;
}
.