1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
<?xml version="1.0" encoding="UTF-8"?>
<!-- EN-Revision: 20763 -->
<!-- Reviewed: 20763 -->
<sect1 id="zend.application.quick-start">
<title>Zend_Application Quick Start</title>
<para>
Es gibt zwei Wege um mit <classname>Zend_Application</classname> anzufangen, und diese
hängen davon ab, wie man das Projekt beginnt. In jedem Fall beginnt man immer mit der
Erstellung einer <classname>Bootstrap</classname>-Klasse und einer entsprechenden
Konfigurationsdatei.
</para>
<para>
Wenn man plant <classname>Zend_Tool</classname> zu verwenden, um das eigene Projekt zu
erstellen, sollte man unten weiterlesen. Wenn man <classname>Zend_Application</classname> zu
einem existierenden Projekt hinzufügen will, sollte man <link
linkend="zend.application.quick-start.manual">hier weiterlesen</link>.
</para>
<sect2 id="zend.application.quick-start.zend-tool">
<title>Verwenden von Zend_Tool</title>
<para>
Der schnellste Weg, um mit <classname>Zend_Application</classname> zu beginnen, ist die
Verwendung von <classname>Zend_Tool</classname> um das Projekt zu erstellen. Das
erstellt auch die <classname>Bootstrap</classname>-Klasse und die Datei.
</para>
<para>
Um ein Projekt zu erstellen, muß einfach das Kommando <command>zf</command> (auf *nix
Systemen) ausgeführt werden:
</para>
<programlisting language="sh"><![CDATA[
% zf create project newproject
]]></programlisting>
<para>
Oder unter Windows das Kommando <filename>zf.bat</filename>:
</para>
<programlisting language="dos"><![CDATA[
C:> zf.bat create project newproject
]]></programlisting>
<para>
Beides erstellt eine Projektstruktur, die wie folgt aussieht:
</para>
<programlisting language="text"><![CDATA[
newproject
|-- application
| |-- Bootstrap.php
| |-- configs
| | `-- application.ini
| |-- controllers
| | |-- ErrorController.php
| | `-- IndexController.php
| |-- models
| `-- views
| |-- helpers
| `-- scripts
| |-- error
| | `-- error.phtml
| `-- index
| `-- index.phtml
|-- library
|-- public
| `-- index.php
`-- tests
|-- application
| `-- bootstrap.php
|-- library
| `-- bootstrap.php
`-- phpunit.xml
]]></programlisting>
<para>
Im obigen Diagramm ist die Bootstrap unter
<filename>newproject/application/Bootstrap.php</filename> und sieht zuerst wie folgt
aus:
</para>
<programlisting language="php"><![CDATA[
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
}
]]></programlisting>
<para>
Sie werden auch bemerken, dass eine Konfigurationdatei unter
<filename>newproject/application/configs/application.ini</filename> erstellt wurde.
Diese hat den folgenden Inhalt:
</para>
<programlisting language="dosini"><![CDATA[
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
]]></programlisting>
<para>
Alle Einstellungen in dieser Konfigurationsdatei sind für die Verwendung mit
<classname>Zend_Application</classname> und der Bootstrap.
</para>
<para>
Eine andere Datei von Interesse ist die <filename>newproject/public/index.php</filename>
Datei, welche <classname>Zend_Application</classname> aufruft und diese ausführt.
</para>
<programlisting language="php"><![CDATA[
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH',
realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV',
(getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
: 'production'));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
]]></programlisting>
<para>
Um mit dem Quick Start weiterzumachen, springen Sie bitte <link
linkend="zend.application.quick-start.resources">auf das Ressource-Kapitel</link>.
</para>
</sect2>
<sect2 id="zend.application.quick-start.manual">
<title>Zend_Application in der eigenen Anwendung hinzufügen</title>
<para>
Die Basis von <classname>Zend_Application</classname> ist wirklich einfach:
</para>
<itemizedlist>
<listitem>
<para>
Eine Datei <filename>application/Bootstrap.php</filename> mit der Klasse
<classname>Bootstrap</classname> erstellen.
</para>
</listitem>
<listitem>
<para>
Eine Konfigurationsdatei
<filename>application/configs/application.ini</filename> mit der
Basiskonfiguration für <classname>Zend_Application</classname> erstellen.
</para>
</listitem>
<listitem>
<para>
Ändern von <filename>public/index.php</filename> um
<classname>Zend_Application</classname> anzupassen.
</para>
</listitem>
</itemizedlist>
<para>
Zuerst die eigene <classname>Bootstrap</classname>-Klasse erstellen. Erzeuge eine Datei
<filename>application/Bootstrap.php</filename> mit dem folgenden Inhalt:
</para>
<programlisting language="php"><![CDATA[
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
}
]]></programlisting>
<para>
Jetzt die Konfiguration erstellen. Für dieses Tutorial, verwenden wir eine
Konfiguration im <acronym>INI</acronym>-Stil; man kann natürlich genauso eine
<acronym>XML</acronym>- oder <acronym>PHP</acronym>-Konfigurationsdatei verwenden.
Erstelle eine Datei <filename>application/configs/application.ini</filename>, und füge
den folgenden Inhalt ein:
</para>
<programlisting language="dosini"><![CDATA[
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
]]></programlisting>
<para>
Jetz verändern wir das Gateway Skript <filename>public/index.php</filename>. Wenn die
Datei nicht existiert, erstellen wir sie; andernfalls ersetzen wir sie mit dem folgenden
Inhalt:
</para>
<programlisting language="php"><![CDATA[
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH',
realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV',
(getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
: 'production'));
// Typischerweise wird man das eigene library/ Verzeichnis zum include_path
// hinzufügen wollen, speziell wenn es die ZF Installation enthält
set_include_path(implode(PATH_SEPARATOR, array(
dirname(dirname(__FILE__)) . '/library',
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
]]></programlisting>
<para>
Es ist zu beachten, dass die Umgebungskonstante der Anwendung nach einer
Umgebungsvariablen "APPLICATION_ENV" sucht. Wir empfehlen diese im Web Server Environment
zu setzen. In Apache kann man diese entweder in der vhost Definition setzen, oder in der
Datei <filename>.htaccess</filename>. Wir empfehlen den folgenden Inhalt in der Datei
<filename>public/.htaccess</filename>:
</para>
<programlisting language="conf"><![CDATA[
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
]]></programlisting>
<note>
<title>Mehr über mod_rewrite lernen</title>
<para>
Die obigen Rewrite-Regeln erlauben es, auf jede Datei im DocumentRoot des eigenen
virtuellen Hosts zuzugreifen. Wenn es Dateien gibt, die man auf diesem Weg nicht
bereitstellen will, muss man in seinen Regeln restriktiver sein. Gehe zur
Apache WebSite und <ulink
url="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html">lerne mehr über
mod_rewrite</ulink>.
</para>
</note>
<para>
An diesem Punkt haben wir es geschafft und können damit beginnen, die Vorteile von
<classname>Zend_Application</classname> zu nutzen.
</para>
</sect2>
<sect2 id="zend.application.quick-start.resources">
<title>Hinzufügen und Erstellen von Ressourcen</title>
<para>
Wenn man den Anleitungen von oben gefolgt ist, dann verwendet die Bootstrap-Klasse
einen FrontController, und wenn sie gestartet wird, wird sie den FrontController
ausführen. Trotzdem wird man mit großer Wahrscheinlichkeit etwas mehr Konfiguration
als das benötigen.
</para>
<para>
In diesem Kapitel werden wir zwei Ressourcen zur Anwendung hinzufügen. Zuerst werden
wir Layouts erstellen, und dann werden wir das View Objekt anpassen.
</para>
<para>
Eine der von <classname>Zend_Application</classname> angebotenen Standardressourcen ist
die "layout" Ressource. Diese Ressource erwartet, dass man Konfigurationswerte definiert,
welche dann verwendet werden, um unsere Instanz von <classname>Zend_Layout</classname>
zu konfigurieren.
</para>
<para>
Um sie zu verwenden, müssen wir die Konfigurationsdatei aktualisieren.
</para>
<programlisting language="dosini"><![CDATA[
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
; ADD THE FOLLOWING LINES
resources.layout.layout = "layout"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
]]></programlisting>
<para>
Wenn man es nicht bereits getan hat, muß man das Verzeichnis
<filename>application/layouts/scripts/</filename> und die Datei
<filename>layout.phtml</filename> in diesem Verzeichnis erstellen. Ein gutes Layout zum
Starten ist das folgende (und ist mit der View Ressource verbunden, die als nächstes
besprochen wird):
</para>
<programlisting language="php"><![CDATA[
<?php echo $this->doctype() ?>
<html>
<head>
<?php echo $this->headTitle() ?>
<?php echo $this->headLink() ?>
<?php echo $this->headStyle() ?>
<?php echo $this->headScript() ?>
</head>
<body>
<?php echo $this->layout()->content ?>
</body>
</html>
]]></programlisting>
<para>
An diesem Punkt haben wir ein funktionierendes Layout.
</para>
<para>
Jetzt fügen wir eine eigene View Ressource hinzu. Wenn die View initialisiert wird,
will man den <acronym>HTML</acronym> DocType und einen Standardwert für den im
<acronym>HTML</acronym>-Kopf zu verwendenden Titel setzen. Das kann durch die Änderung
der <classname>Bootstrap</classname>-Klasse und dem Hinzufügen einer Methode gemacht
werden:
</para>
<programlisting language="php"><![CDATA[
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initView()
{
// View initialisieren
$view = new Zend_View();
$view->doctype('XHTML1_STRICT');
$view->headTitle('My First Zend Framework Application');
// View zum ViewRenderer hinzufügen
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer'
);
$viewRenderer->setView($view);
// Zurückgeben, damit sie von Bootstrap gespeichert werden kann
return $view;
}
}
]]></programlisting>
<para>
Diese Methode wird automatisch ausgeführt, wenn das Bootstrap der Anwendung ausgeführt
wird und stellt sicher, dass die View so initialisiert wird, wie die Anwendung das
benötigt.
</para>
</sect2>
<sect2 id="zend.application.quick-start.next-steps">
<title>Nächste Schritte mit Zend_Application</title>
<para>
Das oben erwähnte reicht, um mit <classname>Zend_Application</classname> zu beginnen und
das Bootstrap der eigenen Anwendung zu erstellen. Von hier an sollte man beginnen
Ressource-Methoden zu erstellen, oder für maximale Wiederverwendbarkeit,
Ressource-Plugin Klassen. Lesen Sie weiter, um mehr zu lernen!
</para>
</sect2>
</sect1>
|