1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.application.quick-start">
4 <title>Zend_Application Quick Start</title>
7 There are two paths to getting started with
8 <classname>Zend_Application</classname>, and they depend on how you start your
9 project. In each case, you always start with creating a
10 <classname>Bootstrap</classname> class, and a related configuration file.
14 If you plan on using <classname>Zend_Tool</classname> to create your project,
15 continue reading below. If you will be adding
16 <classname>Zend_Application</classname> to an existing project, you'll want to
17 <link linkend="zend.application.quick-start.manual">skip ahead</link>.
20 <sect2 id="zend.application.quick-start.zend-tool">
21 <title>Using Zend_Tool</title>
24 The quickest way to start using <classname>Zend_Application</classname> is to use
25 <classname>Zend_Tool</classname> to generate your project. This will also create
26 your <classname>Bootstrap</classname> class and file.
30 To create a project, execute the <command>zf</command> command (on *nix systems):
33 <programlisting language="sh"><![CDATA[
34 % zf create project newproject
38 Or the Windows <filename>zf.bat</filename> command:
41 <programlisting language="dos"><![CDATA[
42 C:> zf.bat create project newproject
46 Both will create a project structure that looks like the following:
49 <programlisting language="text"><![CDATA[
54 | | `-- application.ini
56 | | |-- ErrorController.php
57 | | `-- IndexController.php
78 In the above diagram, your bootstrap is in
79 <filename>newproject/application/Bootstrap.php</filename>, and looks like
80 the following at first:
83 <programlisting language="php"><![CDATA[
84 class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
90 You'll also note that a configuration file,
91 <filename>newproject/application/configs/application.ini</filename>, is
92 created. It has the following contents:
95 <programlisting language="dosini"><![CDATA[
97 phpSettings.display_startup_errors = 0
98 phpSettings.display_errors = 0
99 includePaths.library = APPLICATION_PATH "/../library"
100 bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
101 bootstrap.class = "Bootstrap"
102 resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
104 [staging : production]
106 [testing : production]
107 phpSettings.display_startup_errors = 1
108 phpSettings.display_errors = 1
110 [development : production]
111 phpSettings.display_startup_errors = 1
112 phpSettings.display_errors = 1
116 All settings in this configuration file are for use with
117 <classname>Zend_Application</classname> and your bootstrap.
121 Another file of interest is the
122 <filename>newproject/public/index.php</filename> file, which invokes
123 <classname>Zend_Application</classname> and dispatches it.
126 <programlisting language="php"><![CDATA[
127 // Define path to application directory
128 defined('APPLICATION_PATH')
129 || define('APPLICATION_PATH',
130 realpath(dirname(__FILE__) . '/../application'));
132 // Define application environment
133 defined('APPLICATION_ENV')
134 || define('APPLICATION_ENV',
135 (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
138 /** Zend_Application */
139 require_once 'Zend/Application.php';
141 // Create application, bootstrap, and run
142 $application = new Zend_Application(
144 APPLICATION_PATH . '/configs/application.ini'
146 $application->bootstrap()
151 To continue the quick start, please <link
152 linkend="zend.application.quick-start.resources">skip to the
153 Resources section</link>.
157 <sect2 id="zend.application.quick-start.manual">
158 <title>Adding Zend_Application to your application</title>
161 The basics of <classname>Zend_Application</classname> are fairly simple:
167 Create an <filename>application/Bootstrap.php</filename> file, with the
168 class <classname>Bootstrap</classname>.
174 Create an <filename>application/configs/application.ini</filename>
175 configuration file with the base configuration necessary for
176 <classname>Zend_Application</classname>.
182 Modify your <filename>public/index.php</filename> to utilize
183 <classname>Zend_Application</classname>.
189 First, create your <classname>Bootstrap</classname> class. Create a file,
190 <filename>application/Bootstrap.php</filename>, with the following contents:
193 <programlisting language="php"><![CDATA[
194 class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
200 Now, create your configuration. For this tutorial, we will use an
201 <acronym>INI</acronym> style configuration; you may, of course, use an
202 <acronym>XML</acronym> or <acronym>PHP</acronym> configuration file as well. Create
203 the file <filename>application/configs/application.ini</filename>, and provide the
207 <programlisting language="dosini"><![CDATA[
209 phpSettings.display_startup_errors = 0
210 phpSettings.display_errors = 0
211 includePaths.library = APPLICATION_PATH "/../library"
212 bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
213 bootstrap.class = "Bootstrap"
214 resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
216 [staging : production]
218 [testing : production]
219 phpSettings.display_startup_errors = 1
220 phpSettings.display_errors = 1
222 [development : production]
223 phpSettings.display_startup_errors = 1
224 phpSettings.display_errors = 1
228 Now, let's modify your gateway script,
229 <filename>public/index.php</filename>. If the file does not exist, create
230 it; otherwise, replace it with the following contents:
233 <programlisting language="php"><![CDATA[
234 // Define path to application directory
235 defined('APPLICATION_PATH')
236 || define('APPLICATION_PATH',
237 realpath(dirname(__FILE__) . '/../application'));
239 // Define application environment
240 defined('APPLICATION_ENV')
241 || define('APPLICATION_ENV',
242 (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
245 // Typically, you will also want to add your library/ directory
246 // to the include_path, particularly if it contains your ZF installed
247 set_include_path(implode(PATH_SEPARATOR, array(
248 dirname(dirname(__FILE__)) . '/library',
252 /** Zend_Application */
253 require_once 'Zend/Application.php';
255 // Create application, bootstrap, and run
256 $application = new Zend_Application(
258 APPLICATION_PATH . '/configs/application.ini'
260 $application->bootstrap()
265 You may note that the application environment constant value looks
266 for an environment variable "APPLICATION_ENV". We recommend setting
267 this in your web server environment. In Apache, you can set this
268 either in your vhost definition, or in your <filename>.htaccess</filename>
269 file. We recommend the following contents for your
270 <filename>public/.htaccess</filename> file:
273 <programlisting language="conf"><![CDATA[
274 SetEnv APPLICATION_ENV development
277 RewriteCond %{REQUEST_FILENAME} -s [OR]
278 RewriteCond %{REQUEST_FILENAME} -l [OR]
279 RewriteCond %{REQUEST_FILENAME} -d
280 RewriteRule ^.*$ - [NC,L]
281 RewriteRule ^.*$ index.php [NC,L]
285 <title>Learn about mod_rewrite</title>
288 The above rewrite rules allow access to any file under your
289 virtual host's document root. If there are files you do not want
290 exposed in this way, you may want to be more restrictive in your
291 rules. Go to the Apache website to <ulink
292 url="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html">learn
293 more about mod_rewrite</ulink>.
298 At this point, you're all set to start taking advantage of
299 <classname>Zend_Application</classname>.
303 <sect2 id="zend.application.quick-start.resources">
304 <title>Adding and creating resources</title>
307 If you followed the directions above, then your bootstrap class
308 will be utilizing a front controller, and when it is run, it will
309 dispatch the front controller. However, in all likelihood, you'll
310 need a little more configuration than this.
314 In this section, we'll look at adding two resources to your
315 application. First, we'll set up your layouts, and then we'll
316 customize your view object.
320 One of the standard resources provided with
321 <classname>Zend_Application</classname> is the "layout" resource. This
322 resource expects you to define configuration values which it will
323 then use to configure your <classname>Zend_Layout</classname> instance.
327 To use it, all we need to do is update the configuration file.
330 <programlisting language="dosini"><![CDATA[
332 phpSettings.display_startup_errors = 0
333 phpSettings.display_errors = 0
334 bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
335 bootstrap.class = "Bootstrap"
336 resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
337 ; ADD THE FOLLOWING LINES
338 resources.layout.layout = "layout"
339 resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
341 [staging : production]
343 [testing : production]
344 phpSettings.display_startup_errors = 1
345 phpSettings.display_errors = 1
347 [development : production]
348 phpSettings.display_startup_errors = 1
349 phpSettings.display_errors = 1
353 If you haven't already, create the directory
354 <filename>application/layouts/scripts/</filename>, and the file
355 <filename>layout.phtml</filename> within that directory. A good starting
356 layout is as follows (and ties in with the view resource covered
360 <programlisting language="php"><![CDATA[
361 <?php echo $this->doctype() ?>
364 <?php echo $this->headTitle() ?>
365 <?php echo $this->headLink() ?>
366 <?php echo $this->headStyle() ?>
367 <?php echo $this->headScript() ?>
370 <?php echo $this->layout()->content ?>
376 At this point, you will now have a working layout.
380 Now, we'll add a custom view resource. When initializing the view,
381 we'll want to set the <acronym>HTML</acronym> DocType and a default value for the title
382 to use in the <acronym>HTML</acronym> head. This can be accomplished by editing your
383 <classname>Bootstrap</classname> class to add a method:
386 <programlisting language="php"><![CDATA[
387 class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
389 protected function _initView()
392 $view = new Zend_View();
393 $view->doctype('XHTML1_STRICT');
394 $view->headTitle('My First Zend Framework Application');
396 // Add it to the ViewRenderer
397 $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
400 $viewRenderer->setView($view);
402 // Return it, so that it can be stored by the bootstrap
409 This method will be automatically executed when you bootstrap the
410 application, and will ensure your view is initialized according to
411 your application needs.
415 <sect2 id="zend.application.quick-start.next-steps">
416 <title>Next steps with Zend_Application</title>
419 The above should get you started with <classname>Zend_Application</classname>
420 and creating your application bootstrap. From here, you should start
421 creating resource methods, or, for maximum re-usability, resource
422 plugin classes. Continue reading to learn more!