Documentation/ja_JP/book/1.0/03-Running-Symfony (diff)

You must first sign up to be able to contribute.

Changes from Version 1 of Documentation/ja_JP/book/1.0/03-Running-Symfony

heihachiro (IP:
02/08/07 12:25:23 (11 years ago)



  • Documentation/ja_JP/book/1.0/03-Running-Symfony

    v0 v1  
     4Chapter 3 - Running Symfony 
     7As 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. 
     9Being a PHP 5 framework, symfony requires PHP 5. Make sure you have it installed by opening a command line and typing this command: 
     11    > php -v 
     13    PHP 5.2.0 (cli) (built: Nov 2 2006 11:57:36) 
     14    Copyright (c) 1997-2006 The PHP Group 
     15    Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies 
     17If the version number is 5.0 or higher, then you're ready for the installation, as described in this chapter. 
     19Installing the Sandbox 
     22If 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. 
     24The sandbox is a simple archive of files. It contains an empty symfony project including all the required libraries (symfony, pake, 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. 
     26To install it, download the sandbox archive from ``. 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/`. 
     29>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. 
     31Test your installation by executing the symfony CLI. Go to the new `sf_sandbox/` directory and type the following on a *nix system: 
     33    > ./symfony -V 
     35On Windows, issue this command: 
     37    > symfony -V 
     39You should see the sandbox version number: 
     41    symfony version 1.0.0 
     43Now make sure that your web server can browse the sandbox by requesting this URL: 
     45    http://localhost/sf_sandbox/web/frontend_dev.php/ 
     47You 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. 
     51   [4]: 
     53Figure 3-1 - Sandbox congratulations page 
     55The 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. 
     57To uninstall a sandbox, just remove the `sf_sandbox/` directory from your `web/` folder. 
     59Installing the Symfony Libraries 
     62When 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. 
     64Since 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. 
     66When it comes to installing the libraries for a real application development, you have two alternatives: 
     68  * The PEAR installation is recommended for most people. It can be easily shared and upgraded, and the installation process is straightforward. 
     69  * 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. 
     71Symfony integrates a few other packages: 
     73  * pake is a CLI utility. 
     74  * lime is a unit testing utility. 
     75  * 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. 
     76  * Propel is for ORM. It provides object persistence and query service. 
     77  * Phing is a CLI for Propel. 
     79Pake and lime are 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. 
     81### Installing the Symfony PEAR Package 
     83The 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. 
     85The first step to install it is to add the symfony channel to PEAR, by issuing this command: 
     87    > pear channel-discover 
     89To see the libraries available in this channel, type the following: 
     91    > pear remote-list -c symfony 
     93Now you are ready to install the latest stable version of symfony. Issue this command: 
     95    > pear install symfony/symfony 
     97    downloading symfony-1.0.0.tgz ... 
     98    Starting to download symfony-1.0.0.tgz (1,283,270 bytes) 
     99    ................................................................. 
     100    ................................................................. 
     101    .............done: 1,283,270 bytes 
     102    install ok: channel:// 
     104That'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: 
     106    > symfony -V 
     108    symfony version 1.0.0 
     111>If you prefer to install the most recent beta, which has the latest bug fixes and enhancements, type `p`ear install symfony/symfony-beta instead. Beta releases are not completely stable and are generally not recommended for production environments. 
     113The symfony libraries are now installed in directories as follows: 
     115  * `$php_dir/symfony/` contains the main libraries. 
     116  * `$data_dir/symfony/` contains the skeleton of symfony applications; default modules; and configuration, i18n data, and so on. 
     117  * `$doc_dir/symfony/` contains the documentation. 
     118  * `$test_dir/symfony/` contains unit tests. 
     120The _dir variables are part of your PEAR configuration. To see their values, type the following: 
     122    >pear config-show 
     124### Checking Out Symfony from the SVN Repository 
     126For production servers, or when PEAR is not an option, you can download the latest version of the symfony libraries directly from the symfony Subversion repository by requesting a checkout: 
     128    > mkdir /path/to/symfony 
     129    > cd /path/to/symfony 
     130    > svn checkout . 
     132The `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: 
     134    > php /path/to/symfony/data/bin/symfony -V 
     136    symfony version 1.0.0 
     138If 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 two variables defined in your project's `config/config.php` file, as follows: 
     140    [php] 
     141    <?php 
     143    $sf_symfony_lib_dir  = '/path/to/symfony/lib/'; 
     144    $sf_symfony_data_dir = '/path/to/symfony/data/'; 
     146Chapter 19 proposes other ways to link a project with a symfony installation (including symbolic links and relative paths). 
     149>Alternatively, you can also download the PEAR package (``) and unpack it somewhere. You will have the same result as with a checkout. 
     151Setting Up an Application 
     154As 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. 
     156### Creating the Project 
     158Each 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. 
     160For a PEAR installation, issue these commands: 
     162    > mkdir ~/myproject 
     163    > cd ~/myproject 
     164    > symfony init-project myproject 
     166For an SVN installation, create a project with these commands: 
     168    > mkdir ~/myproject 
     169    > cd ~/myproject 
     170    > php /path/to/symfony/data/bin/symfony init-project myproject 
     172The `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. 
     174Symfony will create a directory structure that looks like this: 
     176    apps/ 
     177    batch/ 
     178    cache/ 
     179    config/ 
     180    data/ 
     181    doc/ 
     182    lib/ 
     183    log/ 
     184    plugins/ 
     185    test/ 
     186    web/ 
     189>The `init-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). 
     191### Creating the Application 
     193The project is not yet ready to be viewed, because it requires at least one application. To initialize it, use the `symfony init-app` command and pass the name of the application as an argument: 
     195    > symfony init-app myapp 
     197This will create a `myapp/` 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: 
     199    apps/ 
     200      myapp/ 
     201        config/ 
     202        i18n/ 
     203        lib/ 
     204        modules/ 
     205        templates/ 
     207Some PHP files corresponding to the front controllers of each default environment are also created in the project `web` directory: 
     209    web/ 
     210      index.php 
     211      myapp_dev.php 
     213`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 `myapp.php` (if you now add a new application called `mynewapp`, the new production front controller will be named `mynewapp.php`). To run your application in the development environment, call the front controller `myapp_dev.php`. You'll learn more about these environments in Chapter 5. 
     215Configuring the Web Server 
     218The 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. 
     220### Setting Up a Virtual Host 
     222Listing 3-1 is an example of Apache configuration, where a new virtual host is added in the `httpd.conf` file. 
     224Listing 3-1 - Sample Apache Configuration, in `apache/conf/httpd.conf` 
     226    <VirtualHost *:80> 
     227      ServerName 
     228      DocumentRoot "/home/steve/myproject/web" 
     229      DirectoryIndex index.php 
     230      Alias /sf /$sf_symfony_data_dir/web/sf 
     231      <Directory "/$sf_symfony_data_dir/web/sf"> 
     232        AllowOverride All 
     233        Allow from All 
     234      </Directory> 
     235      <Directory "/home/steve/myproject/web"> 
     236        AllowOverride All 
     237        Allow from All 
     238      </Directory> 
     239    </VirtualHost> 
     241In the configuration in Listing 3-1, the `/path/to/symfony/data` placeholder must be replaced by the actual path. For example, for a PEAR installation in *nix, you should type something like this: 
     243        Alias /sf /usr/local/lib/php/data/symfony/web/sf 
     246>The alias to the `web/sf/` directory is not mandatory. It allows Apache to find images, style sheets, and JavaScript files for the web debug toolbar, the admin generator, the default symfony pages, and the Ajax support. An alternative to this alias would be to create a symbolic link (symlink) or copy the `/path/to/symfony/data/web/sf/` directory to `myproject/web/sf/`. 
     248Restart Apache, and that's it. Your newly created application can now be called and viewed through a standard web browser at the following URL: 
     250    http://localhost/myapp_dev.php/ 
     252You should see a congratulations page similar to the one shown earlier in Figure 3-1. 
     255>URL Rewriting 
     257>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. 
     259>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`: 
     262>    AddModule mod_rewrite.c 
     263>    LoadModule rewrite_module modules/ 
     266>For Internet Information Services (IIS), you will need `isapi/rewrite` installed and running. Check the symfony online documentation for a detailed IIS installation guide. 
     268### Configuring a Shared-Host Server 
     270Setting 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. 
     273>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. 
     275Let'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. 
     277In 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 application `config.php` file. 
     279Listing 3-2 - Changing the Default Directory Structure Settings, in `apps/myapp/config/config.php` 
     281    [php] 
     282    $sf_root_dir = sfConfig::get('sf_root_dir'); 
     283    sfConfig::add(array( 
     284      'sf_web_dir_name' => $sf_web_dir_name = 'www', 
     285      'sf_web_dir'      => $sf_root_dir.DIRECTORY_SEPARATOR.$sf_web_dir_name, 
     286      'sf_upload_dir'   => $sf_root_dir.DIRECTORY_SEPARATOR.$sf_web_dir_name.DIRECTORY_SEPARATOR.sfConfig::get('sf_upload_dir_name'), 
     287    )); 
     289The 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. 
     291Listing 3-3 - Default `.htaccess` Configuration, Now in `myproject/www/.htaccess` 
     293    Options +FollowSymLinks +ExecCGI 
     295    <IfModule mod_rewrite.c> 
     296      RewriteEngine On 
     298      # we skip all files with .something 
     299      RewriteCond %{REQUEST_URI} \..+$ 
     300      RewriteCond %{REQUEST_URI} !\.html$ 
     301      RewriteRule .* - [L] 
     303      # we check if the .html version is here (caching) 
     304      RewriteRule ^$ index.html [QSA] 
     305      RewriteRule ^([^.]+)$ $1.html [QSA] 
     306      RewriteCond %{REQUEST_FILENAME} !-f 
     308      # no, so we redirect to our front web controller 
     309      RewriteRule ^(.*)$ index.php [QSA,L] 
     310    </IfModule> 
     312    # big crash from our front web controller 
     313    ErrorDocument 500 "<h2>Application error</h2>symfony applicationfailed to start properly" 
     315You should now be ready to browse your application. Check the congratulation page by requesting this URL: 
     320>Other Server Configurations 
     322>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. 
     324>To find directions for a specific server configuration, refer to the symfony wiki (http://www.`m/trac/wiki`), which contains many step-by-step tutorials. 
     329If 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. 
     331### Typical Problems 
     333If you are still having problems getting symfony running, check the following: 
     335  * 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: 
     337    Parse error, unexpected ',', expecting '(' in .../symfony.php on line 19. 
     339  * The memory limit, defined in the `php.ini`, must be set to `16M` at least. The usual symptom for this problem is an error message when installing symfony via PEAR or using the command line. 
     341    Allowed memory size of 8388608 bytes exhausted 
     343  * 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: 
     345    Strict Standards: Implicit cloning object of class 'sfTimer'because of 'zend.ze1_compatibility_mode' 
     347  * 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: 
     349    sfCacheException [message] Unable to write cache file"/usr/myproject/cache/frontend/prod/config/config_config_handlers.yml.php" 
     351  * 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). 
     352  * Sometimes, there is more than one `php.ini` on a server's file system (for instance, if you use the WAMP package), so it is difficult to know which `php.ini` is used. Make the setting changes described in the previous items in all the `php.ini` files that you can find. 
     356   [5]: 
     359>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`. 
     363   [6]: 
     365### Symfony Resources 
     367You can check if your problem has already happened to someone else and find solutions in various places: 
     369  * The symfony installation forum (``) is full of installation questions about a given platform, environment, configuration, host, and so on. 
     370  * The archives of the users mailing-list (``) are also searchable. You may find similar experiences to your own there. 
     371  * The symfony wiki (``) contains step-by-step tutorials, contributed by symfony users, about installation. 
     373If 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. 
     375Source Versioning 
     378Once 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 ( 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.`). For more information about source versioning and the commands used here, consult the Subversion documentation. 
     380The 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`. 
     382So let's create the new repository for the `myproject` project: 
     384    > svnadmin create $SVNREP_DIR/myproject 
     386Then the base structure (layout) of the repository is created with the `trunk`, `tags`, and `branches` directories with this pretty long command: 
     388    > svn mkdir -m "layout creation" file:///$SVNREP_DIR/myproject/trunkfile:///$SVNREP_DIR/myproject/tags file:///$SVNREP_DIR/myproject/branches 
     390This will be your first revision. Now you need to import the files of the project except the cache and log temporary files: 
     392    > cd ~/myproject 
     393    > rm -rf cache/* 
     394    > rm -rf log/* 
     395    > svn import -m "initial import" . file:///$SVNREP_DIR/myproject/trunk 
     397Check the committed files by typing the following: 
     399    > svn ls file:///$SVNREP_DIR/myproject/trunk/ 
     401That 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: 
     403    > cd ~ 
     404    > mv myproject myproject.origin 
     405    > svn co file:///$SVNREP_DIR/myproject/trunk myproject 
     406    > ls myproject 
     408That'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. 
     410There 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: 
     412    > cd ~/myproject 
     413    > chmod 777 cache 
     414    > chmod 777 log 
     415    > svn propedit svn:ignore log 
     416    > svn propedit svn:ignore cache 
     418The default text editor configured for SVN should launch. If this doesn't happen, make Subversion use your preferred editor by typing this: 
     420    > export SVN_EDITOR=<name of editor> 
     421    > svn propedit svn:ignore log 
     422    > svn propedit svn:ignore cache 
     424Now simply add all files from the subdirectories of `myproject/` that SVN should ignore when committing: 
     426    * 
     428Save and quit. You're finished. 
     433To test and play with symfony on your local server, your best option for installation is definitely the sandbox, which contains a preconfigured symfony environment. 
     435For 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. 
     437If 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. 
     439Once your project is initiated, it is a good habit to start a version-control process. 
     441Now that you are ready to use symfony, it is time to see how to build a basic web application. 
     445[wiki:Documentation/ja_JP/ 目次]