1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="learning.quickstart.create-project">
4 <title>Create Your Project</title>
7 In order to create your project, you must first download and extract Zend Framework.
10 <sect2 id="learning.quickstart.create-project.install-zf">
11 <title>Install Zend Framework</title>
14 The easiest way to get Zend Framework along with a complete <acronym>PHP</acronym> stack
15 is by installing <ulink url="http://www.zend.com/en/products/server-ce/downloads">Zend
16 Server</ulink>. Zend Server has native installers for Mac OSX, Windows, Fedora Core,
17 and Ubuntu, as well as a universal installation package compatible with most Linux
22 After you have installed Zend Server, the Framework files may be found
23 under <filename>/usr/local/zend/share/ZendFramework</filename> on Mac OSX and Linux,
24 and <filename>C:\Program Files\Zend\ZendServer\share\ZendFramework</filename> on
25 Windows. The <constant>include_path</constant> will already be configured to include
30 Alternately, you can <ulink url="http://framework.zend.com/download/latest">Download the
31 latest version of Zend Framework</ulink> and extract the contents; make a note of where
36 Optionally, you can add the path to the <filename>library/</filename> subdirectory of
37 the archive to your <filename>php.ini</filename>'s <constant>include_path</constant>
42 That's it! Zend Framework is now installed and ready to use.
46 <sect2 id="learning.quickstart.create-project.create-project">
47 <title>Create Your Project</title>
50 <title>zf Command Line Tool</title>
53 In your Zend Framework installation is a <filename>bin/</filename> subdirectory,
54 containing the scripts <filename>zf.sh</filename> and <filename>zf.bat</filename>
55 for Unix-based and Windows-based users, respectively. Make a note of the absolute
60 Wherever you see references to the command <command>zf</command>, please substitute
61 the absolute path to the script. On Unix-like systems, you may want to use your
62 shell's alias functionality: <command>alias
63 zf.sh=path/to/ZendFramework/bin/zf.sh</command>.
67 If you have problems setting up the <command>zf</command> command-line tool, please
68 refer to <link linkend="zend.tool.framework.clitool">the
74 Open a terminal (in Windows, <command>Start -> Run</command>, and then use
75 <command>cmd</command>). Navigate to a directory where you would like to start a
76 project. Then, use the path to the appropriate script, and execute one of the following:
79 <programlisting language="shell"><![CDATA[
80 % zf create project quickstart
84 Running this command will create your basic site structure, including your initial
85 controllers and views. The tree looks like the following:
88 <programlisting language="text"><![CDATA[
93 | | `-- application.ini
95 | | |-- ErrorController.php
96 | | `-- IndexController.php
118 At this point, if you haven't added Zend Framework to your
119 <constant>include_path</constant>, we recommend either copying or symlinking it into
120 your <filename>library/</filename> directory. In either case, you'll want to either
121 recursively copy or symlink the <filename>library/Zend/</filename> directory of your
122 Zend Framework installation into the <filename>library/</filename> directory of your
123 project. On unix-like systems, that would look like one of the following:
126 <programlisting language="shell"><![CDATA[
128 % cd library; ln -s path/to/ZendFramework/library/Zend .
131 % cd library; cp -r path/to/ZendFramework/library/Zend .
135 On Windows systems, it may be easiest to do this from the Explorer.
139 Now that the project is created, the main artifacts to begin understanding are the
140 bootstrap, configuration, action controllers, and views.
144 <sect2 id="learning.quickstart.create-project.bootstrap">
145 <title>The Bootstrap</title>
148 Your <classname>Bootstrap</classname> class defines what resources and components to
149 initialize. By default, Zend Framework's <link linkend="zend.controller.front">Front
150 Controller</link> is initialized, and it uses the
151 <filename>application/controllers/</filename> as the default directory in which to look
152 for action controllers (more on that later). The class looks like the following:
155 <programlisting language="php"><![CDATA[
156 // application/Bootstrap.php
158 class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
164 As you can see, not much is necessary to begin with.
168 <sect2 id="learning.quickstart.create-project.configuration">
169 <title>Configuration</title>
172 While Zend Framework is itself configurationless, you often need to configure your
173 application. The default configuration is placed in
174 <filename>application/configs/application.ini</filename>, and contains some basic
175 directives for setting your <acronym>PHP</acronym> environment (for instance, turning
176 error reporting on and off), indicating the path to your bootstrap class (as well as its
177 class name), and the path to your action controllers. It looks as follows:
180 <programlisting language="ini"><![CDATA[
181 ; application/configs/application.ini
184 phpSettings.display_startup_errors = 0
185 phpSettings.display_errors = 0
186 includePaths.library = APPLICATION_PATH "/../library"
187 bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
188 bootstrap.class = "Bootstrap"
189 appnamespace = "Application"
190 resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
191 resources.frontController.params.displayExceptions = 0
193 [staging : production]
195 [testing : production]
196 phpSettings.display_startup_errors = 1
197 phpSettings.display_errors = 1
199 [development : production]
200 phpSettings.display_startup_errors = 1
201 phpSettings.display_errors = 1
205 Several things about this file should be noted. First, when using
206 <acronym>INI</acronym>-style configuration, you can reference constants directly and
207 expand them; <constant>APPLICATION_PATH</constant> is actually a constant. Additionally
208 note that there are several sections defined: production, staging, testing, and
209 development. The latter three inherit settings from the "production" environment. This
210 is a useful way to organize configuration to ensure that appropriate settings are
211 available in each stage of application development.
215 <sect2 id="learning.quickstart.create-project.action-controllers">
216 <title>Action Controllers</title>
219 Your application's <emphasis>action controllers</emphasis> contain your application
220 workflow, and do the work of mapping your requests to the appropriate models and views.
224 An action controller should have one or more methods ending in "Action"; these methods
225 may then be requested via the web. By default, Zend Framework URLs follow the schema
226 <constant>/controller/action</constant>, where "controller" maps to the action
227 controller name (minus the "Controller" suffix) and "action" maps to an action method
228 (minus the "Action" suffix).
232 Typically, you always need an <classname>IndexController</classname>, which is a
233 fallback controller and which also serves the home page of the site, and an
234 <classname>ErrorController</classname>, which is used to indicate things such as
235 <acronym>HTTP</acronym> 404 errors (controller or action not found) and
236 <acronym>HTTP</acronym> 500 errors (application errors).
240 The default <classname>IndexController</classname> is as follows:
243 <programlisting language="php"><![CDATA[
244 // application/controllers/IndexController.php
246 class IndexController extends Zend_Controller_Action
249 public function init()
251 /* Initialize action controller here */
254 public function indexAction()
262 And the default <classname>ErrorController</classname> is as follows:
265 <programlisting language="php"><![CDATA[
266 // application/controllers/ErrorController.php
268 class ErrorController extends Zend_Controller_Action
271 public function errorAction()
273 $errors = $this->_getParam('error_handler');
275 switch ($errors->type) {
276 case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
277 case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
278 case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
280 // 404 error -- controller or action not found
281 $this->getResponse()->setHttpResponseCode(404);
282 $this->view->message = 'Page not found';
286 $this->getResponse()->setHttpResponseCode(500);
287 $this->view->message = 'Application error';
291 $this->view->exception = $errors->exception;
292 $this->view->request = $errors->request;
298 You'll note that (1) the <classname>IndexController</classname> contains no real code,
299 and (2) the <classname>ErrorController</classname> makes reference to a "view" property.
300 That leads nicely into our next subject.
304 <sect2 id="learning.quickstart.create-project.views">
308 Views in Zend Framework are written in plain old <acronym>PHP</acronym>. View scripts
309 are placed in <filename>application/views/scripts/</filename>, where they are further
310 categorized using the controller names. In our case, we have an
311 <classname>IndexController</classname> and an <classname>ErrorController</classname>,
312 and thus we have corresponding <filename>index/</filename> and
313 <filename>error/</filename> subdirectories within our view scripts directory. Within
314 these subdirectories, you will then find and create view scripts that correspond to each
315 controller action exposed; in the default case, we thus have the view scripts
316 <filename>index/index.phtml</filename> and <filename>error/error.phtml</filename>.
320 View scripts may contain any markup you want, and use the <emphasis><?php</emphasis>
321 opening tag and <emphasis>?></emphasis> closing tag to insert <acronym>PHP</acronym>
326 The following is what we install by default for the
327 <filename>index/index.phtml</filename> view script:
330 <programlisting language="php"><![CDATA[
331 <!-- application/views/scripts/index/index.phtml -->
348 background-image: url(http://framework.zend.com/images/bkg_header.jpg);
351 border: 2px solid #444444;
358 background-image: url(http://framework.zend.com/images/bkg_body-bottom.gif);
364 <h1>Welcome to the <span id="zf-name">Zend Framework!</span><h1 />
365 <h3>This is your project's main page<h3 />
366 <div id="more-information">
368 <img src="http://framework.zend.com/images/PoweredBy_ZF_4LightBG.png" />
372 Helpful Links: <br />
373 <a href="http://framework.zend.com/">Zend Framework Website</a> |
374 <a href="http://framework.zend.com/manual/en/">Zend Framework
382 The <filename>error/error.phtml</filename> view script is slightly more interesting as
383 it uses some <acronym>PHP</acronym> conditionals:
386 <programlisting language="php"><![CDATA[
387 <!-- application/views/scripts/error/error.phtml -->
388 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN";
389 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
390 <html xmlns="http://www.w3.org/1999/xhtml">
392 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
393 <title>Zend Framework Default Application</title>
396 <h1>An error occurred</h1>
397 <h2><?php echo $this->message ?></h2>
399 <?php if ('development' == $this->env): ?>
401 <h3>Exception information:</h3>
403 <b>Message:</b> <?php echo $this->exception->getMessage() ?>
406 <h3>Stack trace:</h3>
407 <pre><?php echo $this->exception->getTraceAsString() ?>
410 <h3>Request Parameters:</h3>
411 <pre><?php echo var_export($this->request->getParams(), 1) ?>
420 <sect2 id="learning.quickstart.create-project.vhost">
421 <title>Create a virtual host</title>
424 For purposes of this quick start, we will assume you are using the <ulink
425 url="http://httpd.apache.org/">Apache web server</ulink>. Zend Framework works
426 perfectly well with other web servers -- including Microsoft Internet Information
427 Server, lighttpd, nginx, and more -- but most developers should be famililar with Apache
428 at the minimum, and it provides an easy introduction to Zend Framework's directory
429 structure and rewrite capabilities.
433 To create your vhost, you need to know the location of your
434 <filename>httpd.conf</filename> file, and potentially where other configuration files
435 are located. Some common locations:
441 <filename>/etc/httpd/httpd.conf</filename> (Fedora, RHEL, and others)
447 <filename>/etc/apache2/httpd.conf</filename> (Debian, Ubuntu, and others)
453 <filename>/usr/local/zend/etc/httpd.conf</filename> (Zend Server on *nix
460 <filename>C:\Program Files\Zend\Apache2\conf</filename> (Zend Server on Windows
467 Within your <filename>httpd.conf</filename> (or <filename>httpd-vhosts.conf</filename>
468 on some systems), you will need to do two things. First, ensure that the
469 <varname>NameVirtualHost</varname> is defined; typically, you will set it to a value of
470 "*:80". Second, define a virtual host:
473 <programlisting language="apache"><![CDATA[
475 ServerName quickstart.local
476 DocumentRoot /path/to/quickstart/public
478 SetEnv APPLICATION_ENV "development"
480 <Directory /path/to/quickstart/public>
481 DirectoryIndex index.php
490 There are several things to note. First, note that the <varname>DocumentRoot</varname>
491 setting specifies the <filename>public</filename> subdirectory of our project; this
492 means that only files under that directory can ever be served directly by the server.
493 Second, note the <varname>AllowOverride</varname>, <varname>Order</varname>, and
494 <varname>Allow</varname> directives; these are to allow us to use
495 <filename>htacess</filename> files within our project. During development, this is a
496 good practice, as it prevents the need to constantly restart the web server as you make
497 changes to your site directives; however, in production, you should likely push the
498 content of your <filename>htaccess</filename> file into your server configuration and
499 disable this. Third, note the <varname>SetEnv</varname> directive. What we are doing
500 here is setting an environment variable for your virtual host; this variable will be
501 picked up in the <filename>index.php</filename> and used to set the
502 <constant>APPLICATION_ENV</constant> constant for our Zend Framework application. In
503 production, you can omit this directive (in which case it will default to the value
504 "production") or set it explicitly to "production".
508 Finally, you will need to add an entry in your <filename>hosts</filename> file
509 corresponding to the value you place in your <varname>ServerName</varname> directive. On
510 *nix-like systems, this is usually <filename>/etc/hosts</filename>; on Windows, you'll
511 typically find it in <filename>C:\WINDOWS\system32\drivers\etc</filename>. Regardless of
512 the system, the entry will look like the following:
515 <programlisting language="text"><![CDATA[
516 127.0.0.1 quickstart.local
520 Start your webserver (or restart it), and you should be ready to go.
524 <sect2 id="learning.quickstart.create-project.checkpoint">
525 <title>Checkpoint</title>
528 At this point, you should be able to fire up your initial Zend Framework application.
529 Point your browser to the server name you configured in the previous section; you should
530 be able to see a welcome page at this point.