更新(2014):これは2007年の記事ですが、驚いたことに、まだ需要があります。 このため、Habréへの投稿に関する新しいルールに従って更新し、コード例の構文強調表示を追加しました。 誰かが何かを追加または修正したい場合、Habraマークアップのソースコードが公開されています:
gist.github.com/dreikanter/2b4ee996d7a775e707d7翻訳者からの注釈
PHPは、最も広く使用されているWebアプリケーション開発言語の1つであり、最も物議を醸すものの1つです。 私はこの技術に否定的な態度を頻繁に見ましたが、この態度を引き起こす欠点は誰にとっても秘密ではありません。 ただし、PHPは積極的に進化しており、多くの点で徐々に改善されています。 私の意見では、その開発における重大なステップの1つは、Webアプリケーションの開発プロセスを体系化し、開発者を注文に慣れさせるために設計されたMVCフレームワークの出現です。開発中のソフトウェア(最後のステートメントは主観的であり、私が見たさまざまなソフトウェアソリューションのコードのみに基づいていることをすぐに明確にします)。
最近、MVCアーキテクチャに積極的に興味を持ち、PHP4のこのコンセプトで独自のフレームワークを実装しました。 ごく最近、私が長い間聞いていたZend Frameworkの最初の公式リリースが目を惹きましたが、すべての手がそれで遊ぶことができませんでした。 PHPには、他の同様のライブラリがありますが、この場合はブランドを引き付けました。
最初は、公式ドキュメントに失望を経験しましたが、これは良い参考になりましたが、チュートリアルではなく、導入資料の役割に適合しませんでした。 しかし、ほとんどすぐに、著者がWebアプリケーションの作成プロセスを段階的に分析する良い記事がありました。 それを理解するのに十分な資料があり、Zendのマナで意識的にナビゲートし始めることができます。 このテキストは、PHPでWebアプリケーションをプログラミングした経験はあるものの、Zend Frameworkに精通していないプログラマーを対象としています。
この記事は非常に膨大なので、誰の脳にも負担をかけないように、2つの部分に分けることにしました。 以下に、彼女の翻訳の最初の部分を示します。これは、MVCの概念を概説し、Zend Frameworkに実装されたWebアプリケーションの構造を分解します。 素材がhabrachitateliに興味がある場合は、続きを発行します。
紹介の最後に、小さな免責事項があります。 Ruby on RailsとPython on Djangoの愛するファン、私はあなたの宗教を尊重します:)どのフレームワークまたは言語が優れているかについてのコメントのトピックを作成しないでください。 これは、この投稿のトピックに関連するトピックではありません。 Zendでの実際のソフトウェア開発の経験について学ぶことは、おそらく私と多くの人々にとってはるかに興味深いでしょう。
投稿者: Rob Allen、
akrabat.comオリジナル: akrabat.com/zend-framework-tutorial翻訳:アレクサンダー・ムサエフ、
musayev.comこの資料は、Zend Frameworkを使用してデータベースを使用して簡単なアプリケーションを作成するという一般的なアイデアを提供するように設計されています。 上記の例は、Zend Frameworkバージョン1.0.0でテストされています。 ほとんどの場合、新しいバージョンでは動作しますが、古いバージョンでは動作しません。
アーキテクチャモデルビューコントローラ
従来の方法で記述されたPHPアプリケーションは、次の例のようになります。
<?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;
}
.