03-Running-Symfony.txt 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. Chapter 3 - Running Symfony
  2. ===========================
  3. As you've learned in previous chapters, the symfony framework is a set of files written in PHP. A symfony project uses these files, so installing symfony means getting these files and making them available for the project.
  4. Symfony requires at least PHP 5.1. Make sure you have it installed by opening a command line and typing this command:
  5. > php -v
  6. PHP 5.2.5 (cli) (built: Nov 20 2007 16:55:40)
  7. Copyright (c) 1997-2007 The PHP Group
  8. Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
  9. If the version number is 5.1 or higher, then you're ready for the installation, as described in this chapter.
  10. Installing the Sandbox
  11. ----------------------
  12. If you just want to see what symfony is capable of, you'll probably go for the fast installation. In that case, you need the sandbox.
  13. The sandbox is a simple archive of files. It contains an empty symfony project including all the required libraries (symfony, lime, Creole, Propel, and Phing), a default application, and basic configuration. It will work out of the box, without specific server configuration or any additional packages.
  14. To install it, download the sandbox archive from [http://www.symfony-project.org/get/sf_sandbox_1_1.tgz](http://www.symfony-project.org/get/sf_sandbox_1_1.tgz). Unpack it under the root web directory configured for your server (usually `web/` or `www/`). For the purposes of uniformity, this chapter will assume you unpacked it to the directory `sf_sandbox/`.
  15. >**CAUTION**
  16. >Having all the files under the root web directory is fine for your own tests in a local host, but is a bad practice in a production server. It makes all the internals of your application visible to end users.
  17. Test your installation by executing the symfony CLI. Go to the new `sf_sandbox/` directory and type the following:
  18. > php symfony -V
  19. You should see the sandbox version number:
  20. symfony version 1.1.0 (/path/to/the/symfony/lib/dir/used/by/the/sandbox)
  21. Now make sure that your web server can browse the sandbox by requesting this URL:
  22. http://localhost/sf_sandbox/web/frontend_dev.php/
  23. You should see a congratulations page that looks like Figure 3-1, and it means that your installation is finished. If not, then an error message will guide you through the configuration changes needed. You can also refer to the "Troubleshooting" section later in this chapter.
  24. Figure 3-1 - Sandbox congratulations page
  25. ![Sandbox congratulations page](/images/book/F0301.jpg "Sandbox congratulations page")
  26. The sandbox is intended for you to practice with symfony on a local computer, not to develop complex applications that may end up on the Web. However, the version of symfony shipped with the sandbox is fully functional and equivalent to the one you can install via PEAR.
  27. To uninstall a sandbox, just remove the `sf_sandbox/` directory from your `web/` folder.
  28. Installing the symfony Libraries
  29. --------------------------------
  30. When developing an application, you will probably need to install symfony twice: once for your development environment and once for the host server (unless your host already has symfony installed). For each server, you will probably want to avoid duplication by keeping all the symfony files in a single place, whether you develop only one application or several applications.
  31. Since the symfony framework evolves quickly, a new stable version could very well be released only a few days after your first installation. You need to think of the framework upgrade as a major concern, and that's another reason why you should share one instance of the symfony libraries across all your symfony projects.
  32. When it comes to installing the libraries for a real application development, you have two alternatives:
  33. * The PEAR installation is recommended for most people. It can be easily shared and upgraded, and the installation process is straightforward.
  34. * The Subversion (SVN) installation is meant to be used only by advanced PHP developers, who want to take advantage of the latest patches, add features of their own, and/or contribute to the symfony project.
  35. Symfony integrates a few other packages:
  36. * lime is a unit testing utility.
  37. * Creole is a database abstraction engine. Just like PHP Data Objects (PDO), it provides an interface between your code and the database SQL code, and makes it possible to switch to another engine.
  38. * Propel is for ORM. It provides object persistence and query service.
  39. * Phing is a build system used by Propel to generate the model classes.
  40. Lime is developed by the symfony team. Creole, Propel, and Phing come from another team and are released under the GNU Lesser Public General License (LGPL). All these packages are bundled with symfony.
  41. >**TIP**
  42. >The symfony framework is licensed under a MIT license. All the copyrights for the bundled third party libraries can be found in the `COPYRIGHT` file and the associated licenses are stored in the `licenses/` directory.
  43. ### Installing the symfony PEAR Package
  44. The symfony PEAR package contains the symfony libraries and all its dependencies. It also contains a script that will extend your CLI to include the `symfony` command.
  45. The first step to install it is to add the symfony channel to PEAR, by issuing this command:
  46. > pear channel-discover pear.symfony-project.com
  47. To see the libraries available in this channel, type the following:
  48. > pear remote-list -c symfony
  49. Now you are ready to install the latest stable version of symfony. Issue this command:
  50. > pear install symfony/symfony
  51. downloading symfony-1.1.0.tgz ...
  52. Starting to download symfony-1.1.0.tgz (1,283,270 bytes)
  53. .................................................................
  54. .................................................................
  55. .............done: 1,283,270 bytes
  56. install ok: channel://pear.symfony-project.com/symfony-1.1.0
  57. That's it. The symfony files and CLI are installed. Check that the installation succeeded by calling the new `symfony` command line, asking for the version number:
  58. > symfony -V
  59. symfony version 1.1.0 (/path/to/the/pear/symfony/lib/dir)
  60. The symfony libraries are now installed in directories as follows:
  61. * `$php_dir/symfony/` contains the main libraries.
  62. * `$data_dir/symfony/` contains the web assets used by symfony default modules.
  63. * `$doc_dir/symfony/` contains the documentation.
  64. * `$test_dir/symfony/` contains symfony core unit and functional tests.
  65. The `_dir` variables are part of your PEAR configuration. To see their values, type the following:
  66. > pear config-show
  67. ### Checking Out symfony from the SVN Repository
  68. For production servers, or when PEAR is not an option, you can download the
  69. latest version of the symfony libraries directly from the symfony Subversion
  70. repository by requesting a checkout:
  71. > mkdir /path/to/symfony
  72. > cd /path/to/symfony
  73. > svn checkout http://svn.symfony-project.com/tags/RELEASE_1_1_0/ .
  74. >**TIP**
  75. >For the latest stable bug-fix release on the 1.1 branch (1.1.x) refer to
  76. >([http://www.symfony-project.org/installation/1_1](http://www.symfony-project.org/installation/1_1))
  77. The `symfony` command, available only for PEAR installations, is a call to the `/path/to/symfony/data/bin/symfony` script. So the following would be the equivalent to the `symfony -V` command for an SVN installation:
  78. > php /path/to/symfony/data/bin/symfony -V
  79. symfony version 1.1.0 (/path/to/the/svn/symfony/lib/dir)
  80. If you chose an SVN installation, you probably already have an existing symfony project. For this project to make use of the symfony files, you need to change the path defined in the project's `config/ProjectConfiguration.class.php` file, as follows:
  81. [php]
  82. <?php
  83. require_once '/path/to/symfony/lib/autoload/sfCoreAutoload.class.php';
  84. sfCoreAutoload::register();
  85. class ProjectConfiguration extends sfProjectConfiguration
  86. {
  87. // ...
  88. }
  89. Chapter 19 proposes other ways to link a project with a symfony installation (including symbolic links and relative paths).
  90. >**TIP**
  91. >Alternatively, you can also download the PEAR package. Refer to
  92. >([http://www.symfony-project.org/installation/1_1](http://www.symfony-project.org/installation/1_1))
  93. >for latest 1.1 release. You will have the same result as with a checkout.
  94. Setting Up an Application
  95. -------------------------
  96. As you learned in Chapter 2, symfony gathers related applications in projects. All the applications of a project share the same databases. In order to set up an application, you must first set up a project.
  97. ### Creating the Project
  98. Each symfony project follows a predefined directory structure. The symfony command line automates the creation of new projects by initiating the skeleton of the project, with the proper tree structure and access rights. So to create a project, simply create a new directory and ask symfony to make it a project.
  99. For a PEAR installation, issue these commands:
  100. > mkdir ~/myproject
  101. > cd ~/myproject
  102. > symfony generate:project myproject
  103. For an SVN installation, create a project with these commands:
  104. > mkdir ~/myproject
  105. > cd ~/myproject
  106. > php /path/to/symfony/data/bin/symfony generate:project myproject
  107. Symfony will create a directory structure that looks like this:
  108. apps/
  109. cache/
  110. config/
  111. data/
  112. doc/
  113. lib/
  114. log/
  115. plugins/
  116. test/
  117. web/
  118. >**TIP**
  119. >The `generate:project` task adds a `symfony` script in the project root directory. This PHP script does the same as the `symfony` command installed by PEAR, so you can call `php symfony` instead of `symfony` if you don't have native command-line support (for SVN installations).
  120. ### Creating the Application
  121. The project is not yet ready to be viewed, because it requires at least one application. To initialize it, use the `symfony generate:app` command and pass the name of the application as an argument:
  122. > php symfony generate:app frontend
  123. This will create a `frontend/` directory in the `apps/` folder of the project root, with a default application configuration and a set of directories ready to host the file of your website:
  124. apps/
  125. frontend/
  126. config/
  127. i18n/
  128. lib/
  129. modules/
  130. templates/
  131. Some PHP files corresponding to the front controllers of each default environment are also created in the project `web` directory:
  132. web/
  133. index.php
  134. frontend_dev.php
  135. `index.php` is the production front controller of the new application. Because you created the first application of the project, symfony created a file called `index.php` instead of `frontend.php` (if you now add a new application called `backend`, the new production front controller will be named `backend.php`). To run your application in the development environment, call the front controller `frontend_dev.php`. Note that for security reasons the development controller is available only for localhost by default. You'll learn more about these environments in Chapter 5.
  136. The `symfony` command must always be called from the project's root directory (`myproject/` in the preceding examples), because all the tasks performed by this command are project-specific.
  137. Configuring the Web Server
  138. --------------------------
  139. The scripts of the `web/` directory are the entry points to the application. To be able to access them from the Internet, the web server must be configured. In your development server, as well as in a professional hosting solution, you probably have access to the Apache configuration and you can set up a virtual host. On a shared-host server, you probably have access only to an `.htaccess` file.
  140. ### Setting Up a Virtual Host
  141. Listing 3-1 is an example of Apache configuration, where a new virtual host is added in the `httpd.conf` file.
  142. Listing 3-1 - Sample Apache Configuration, in `apache/conf/httpd.conf`
  143. <VirtualHost *:80>
  144. ServerName myapp.example.com
  145. DocumentRoot "/home/steve/myproject/web"
  146. DirectoryIndex index.php
  147. Alias /sf /$sf_symfony_data_dir/web/sf
  148. <Directory "/$sf_symfony_data_dir/web/sf">
  149. AllowOverride All
  150. Allow from All
  151. </Directory>
  152. <Directory "/home/steve/myproject/web">
  153. AllowOverride All
  154. Allow from All
  155. </Directory>
  156. </VirtualHost>
  157. In the configuration in Listing 3-1, the `$sf_symfony_data_dir` placeholder
  158. must be replaced by the actual path. For example, for a PEAR installation in
  159. *nix, you should type something like this:
  160. Alias /sf /usr/local/lib/php/data/symfony/web/sf
  161. >**NOTE**
  162. >The alias to the `web/sf/` directory is not mandatory. It allows Apache to
  163. find images, style sheets, and JavaScript files for the web debug toolbar,
  164. the admin generator, the default symfony pages, and the Ajax support.
  165. An alternative to this alias would be to create a symbolic link (symlink) or
  166. copy the `/path/to/symfony/data/web/sf/` directory to `myproject/web/sf/`.
  167. >**TIP**
  168. >If you have installed symfony via PEAR and can't find the symfony shared data
  169. >directory, look in the PEAR `data_dir` which is listed in the PEAR config:
  170. >
  171. > pear config-show
  172. Restart Apache, and that's it. Your newly created application can now be called
  173. and viewed through a standard web browser at the following URL:
  174. http://localhost/frontend_dev.php/
  175. You should see a congratulations page similar to the one shown earlier in Figure 3-1.
  176. >**SIDEBAR**
  177. >URL Rewriting
  178. >
  179. >Symfony uses URL rewriting to display "smart URLs"--meaningful locations that display well on search engines and hide all the technical data from the user. You will learn more about this feature, called routing, in Chapter 9.
  180. >
  181. >If your version of Apache is not compiled with the `mod_rewrite` module, check that you have the `mod_rewrite` Dynamic Shared Object (DSO) installed and the following lines in your `httpd.conf`:
  182. >
  183. > AddModule mod_rewrite.c
  184. > LoadModule rewrite_module modules/mod_rewrite.so
  185. >
  186. >For Internet Information Services (IIS), you will need `isapi/rewrite` installed and running. Check the symfony online cookbook for a detailed IIS installation guide.
  187. ### Configuring a Shared-Host Server
  188. Setting up an application in a shared host is a little trickier, since the host usually has a specific directory layout that you can't change.
  189. >**CAUTION**
  190. >Doing tests and development directly in a shared host is not a good practice. One reason is that it makes the application visible even if it is not finished, revealing its internals and opening large security breaches. Another reason is that the performance of shared hosts is often not sufficient to browse your application with the debug tools on efficiently. So you should not start your development with a shared-host installation, but rather build your application locally and deploy it to the shared host when it is finished. Chapter 16 will tell you more about deployment techniques and tools.
  191. Let's imagine that your shared host requires that the web folder is named `www/` instead of `web/`, and that it doesn't give you access to the `httpd.conf` file, but only to an `.htaccess` file in the web folder.
  192. In a symfony project, every path to a directory is configurable. Chapter 19 will tell you more about it, but in the meantime, you can still rename the `web` directory to `www` and have the application take it into account by changing the configuration, as shown in Listing 3-2. These lines are to be added to the end of the `config/ProjectConfiguration.class.php` file.
  193. Listing 3-2 - Changing the Default Directory Structure Settings, in `config/ProjectConfiguration.class.php`
  194. [php]
  195. class ProjectConfiguration extends sfProjectConfiguration
  196. {
  197. public function setup()
  198. {
  199. $this->setWebDir($this->getRootDir().'/www');
  200. }
  201. }
  202. The project web root contains an `.htaccess` file by default. It is shown in Listing 3-3. Modify it as necessary to match your shared host requirements.
  203. Listing 3-3 - Default `.htaccess` Configuration, Now in `myproject/www/.htaccess`
  204. Options +FollowSymLinks +ExecCGI
  205. <IfModule mod_rewrite.c>
  206. RewriteEngine On
  207. # uncomment the following line, if you are having trouble
  208. # getting no_script_name to work
  209. #RewriteBase /
  210. # we skip all files with .something
  211. #RewriteCond %{REQUEST_URI} \..+$
  212. #RewriteCond %{REQUEST_URI} !\.html$
  213. #RewriteRule .* - [L]
  214. # we check if the .html version is here (caching)
  215. RewriteRule ^$ index.html [QSA]
  216. RewriteRule ^([^.]+)$ $1.html [QSA]
  217. RewriteCond %{REQUEST_FILENAME} !-f
  218. # no, so we redirect to our front web controller
  219. RewriteRule ^(.*)$ index.php [QSA,L]
  220. </IfModule>
  221. You should now be ready to browse your application. Check the congratulation page by requesting this URL:
  222. http://www.example.com/frontend_dev.php/
  223. >**SIDEBAR**
  224. >Other Server Configurations
  225. >
  226. >Symfony is compatible with other server configurations. You can, for instance, access a symfony application using an alias instead of a virtual host. You can also run a symfony application with an IIS server. There are as many techniques as there are configurations, and it is not the purpose of this book to explain them all.
  227. >
  228. >To find directions for a specific server configuration, refer to the symfony wiki ([http://trac.symfony-project.org/](http://trac.symfony-project.org/)), which contains many step-by-step tutorials.
  229. Troubleshooting
  230. ---------------
  231. If you encounter problems during the installation, try to make the best out of the errors or exceptions thrown to the shell or to the browser. They are often self-explanatory and may even contain links to specific resources on the Web about your issue.
  232. ### Typical Problems
  233. If you are still having problems getting symfony running, check the following:
  234. * Some PHP installations come with both a PHP 4 and a PHP 5 command. In that case, the command line is probably `php5` instead of `php`, so try calling `php5 symfony` instead of the `symfony` command. You may also need to add `SetEnv PHP_VER 5` to your `.htaccess` configuration, or rename the scripts of the `web/` directory from `.php` to `.php5`. The error thrown by a PHP 4 command line trying to access symfony looks like this:
  235. Parse error, unexpected ',', expecting '(' in .../symfony.php on line 19.
  236. * The memory limit, defined in the `php.ini`, must be set to `32M` at least. The usual symptom for this problem is an error message when installing symfony via PEAR or using the command line.
  237. Allowed memory size of 8388608 bytes exhausted
  238. * The `zend.ze1_compatibility_mode` setting must be set to `off` in your `php.ini`. If it is not, trying to browse to one of the web scripts will produce an "implicit cloning" error:
  239. Strict Standards: Implicit cloning object of class 'sfTimer'because of 'zend.ze1_compatibility_mode'
  240. * The `log/` and `cache/` directories of your project must be writable by the web server. Attempts to browse a symfony application without these directory permissions will result in an exception:
  241. sfCacheException [message] Unable to write cache file"/usr/myproject/cache/frontend/prod/config/config_config_handlers.yml.php"
  242. * The include path of your system must include the path to the `php` command, and the include path of your `php.ini` must contain a path to PEAR (if you use PEAR).
  243. * Sometimes, there is more than one `php.ini` on a server's file system (for instance, if you use the WAMP package). Call `phpinfo()` to know the exact location of the `php.ini` file used by your application.
  244. >**NOTE**
  245. >Although it is not mandatory, it is strongly advised, for performance reasons, to set the `magic_quotes_gpc` and `register_globals` settings to `off` in your `php.ini`.
  246. ### Symfony Resources
  247. You can check if your problem has already happened to someone else and find solutions in various places:
  248. * The symfony installation forum ([http://www.symfony-project.org/forum/](http://www.symfony-project.org/forum/)) is full of installation questions about a given platform, environment, configuration, host, and so on.
  249. * The archives of the users mailing-list ([http://groups.google.fr/group/symfony-users](http://groups.google.fr/group/symfony-users)) are also searchable. You may find similar experiences to your own there.
  250. * The symfony wiki ([http://trac.symfony-project.org/#Installingsymfony](http://trac.symfony-project.org/#Installingsymfony)) contains step-by-step tutorials, contributed by symfony users, about installation.
  251. If you don't find any answer, try posing your question to the symfony community. You can post your query in the forum, the mailing list, or even drop to the `#symfony` IRC channel to get feedback from the most active members of the community.
  252. Source Versioning
  253. -----------------
  254. Once the setup of the application is done, starting a source versioning (or version control) process is recommended. Source versioning keeps track of all modifications in the code, gives access to previous releases, facilitates patching, and allows for efficient team work. Symfony natively supports CVS, although Subversion ([http://subversion.tigris.org/](http://subversion.tigris.org/)) is recommended. The following examples show the commands for Subversion, and assume that you already have a Subversion server installed and that you wish to create a new repository for your project. For Windows users, a recommended Subversion client is TortoiseSVN ([http://tortoisesvn.tigris.org/](http://tortoisesvn.tigris.org/)). For more information about source versioning and the commands used here, consult the Subversion documentation.
  255. The following example assumes that `$SVNREP_DIR` is defined as an environment variable. If you don't have it defined, you will need to substitute the actual location of the repository in place of `$SVNREP_DIR`.
  256. So let's create the new repository for the `myproject` project:
  257. > svnadmin create $SVNREP_DIR/myproject
  258. Then the base structure (layout) of the repository is created with the `trunk`, `tags`, and `branches` directories with this pretty long command:
  259. > svn mkdir -m "layout creation" file:///$SVNREP_DIR/myproject/trunk file:///$SVNREP_DIR/myproject/tags file:///$SVNREP_DIR/myproject/branches
  260. This will be your first revision. Now you need to import the files of the project except the cache and log temporary files:
  261. > cd ~/myproject
  262. > rm -rf cache/*
  263. > rm -rf log/*
  264. > svn import -m "initial import" . file:///$SVNREP_DIR/myproject/trunk
  265. Check the committed files by typing the following:
  266. > svn ls file:///$SVNREP_DIR/myproject/trunk/
  267. That seems good. Now the SVN repository has the reference version (and the history) of all your project files. This means that the files of the actual `~/myproject/` directory need to refer to the repository. To do that, first rename the `myproject/` directory--you will erase it soon if everything works well--and do a checkout of the repository in a new directory:
  268. > cd ~
  269. > mv myproject myproject.origin
  270. > svn co file:///$SVNREP_DIR/myproject/trunk myproject
  271. > ls myproject
  272. That's it. Now you can work on the files located in ~`/myproject/` and commit your modifications to the repository. Don't forget to do some cleanup and erase the `myproject.origin/` directory, which is now useless.
  273. There is one remaining thing to set up. If you commit your working directory to the repository, you may copy some unwanted files, like the ones located in the `cache` and `log` directories of your project. So you need to specify an ignore list to SVN for this project. You also need to set full access to the `cache/` and `log/` directories again:
  274. > cd ~/myproject
  275. > chmod 777 cache
  276. > chmod 777 log
  277. > svn propedit svn:ignore log
  278. > svn propedit svn:ignore cache
  279. The default text editor configured for SVN should launch. If this doesn't happen, make Subversion use your preferred editor by typing this:
  280. > export SVN_EDITOR=<name of editor>
  281. > svn propedit svn:ignore log
  282. > svn propedit svn:ignore cache
  283. Now simply add all files from the subdirectories of `myproject/` that SVN should ignore when committing:
  284. *
  285. Save and quit. You're finished.
  286. Summary
  287. -------
  288. To test and play with symfony on your local server, your best option for installation is definitely the sandbox, which contains a preconfigured symfony environment.
  289. For a real development or in a production server, opt for the PEAR installation or the SVN checkout. This will install the symfony libraries, and you still need to initialize a project and an application. The last step of the application setup is the server configuration, which can be done in many ways. Symfony works perfectly fine with a virtual host, and it is the recommended solution.
  290. If you have any problems during installation, you will find many tutorials and answers to frequently asked questions on the symfony website. If necessary, you can submit your problem to the symfony community, and you will get a quick and effective answer.
  291. Once your project is initiated, it is a good habit to start a version-control process.
  292. Now that you are ready to use symfony, it is time to see how to build a basic web application.