[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/doc/ -> 03-Running-Symfony.txt (source)

   1  Chapter 3 - Running Symfony
   2  ===========================
   3  
   4  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.
   5  
   6  Being a PHP 5 framework, symfony requires PHP 5. Make sure you have it installed by opening a command line and typing this command:
   7  
   8      > php -v
   9  
  10      PHP 5.2.0 (cli) (built: Nov 2 2006 11:57:36)
  11      Copyright (c) 1997-2006 The PHP Group
  12      Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies
  13  
  14  If the version number is 5.0 or higher, then you're ready for the installation, as described in this chapter.
  15  
  16  Installing the Sandbox
  17  ----------------------
  18  
  19  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.
  20  
  21  The 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.
  22  
  23  To install it, download the sandbox archive from [http://www.symfony-project.com/get/sf_sandbox.tgz](http://www.symfony-project.com/get/sf_sandbox.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/`.
  24  
  25  >**CAUTION**
  26  >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.
  27  
  28  Test your installation by executing the symfony CLI. Go to the new `sf_sandbox/` directory and type the following on a *nix system:
  29  
  30      > ./symfony -V
  31  
  32  On Windows, issue this command:
  33  
  34      > symfony -V
  35  
  36  You should see the sandbox version number:
  37  
  38      symfony version 1.0.0
  39  
  40  Now make sure that your web server can browse the sandbox by requesting this URL:
  41  
  42      http://localhost/sf_sandbox/web/frontend_dev.php/
  43  
  44  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.
  45  
  46  Figure 3-1 - Sandbox congratulations page
  47  
  48  ![Sandbox congratulations page](/images/book/F0301.jpg "Sandbox congratulations page")
  49  
  50  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.
  51  
  52  To uninstall a sandbox, just remove the `sf_sandbox/` directory from your `web/` folder.
  53  
  54  Installing the Symfony Libraries
  55  --------------------------------
  56  
  57  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.
  58  
  59  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.
  60  
  61  When it comes to installing the libraries for a real application development, you have two alternatives:
  62  
  63    * The PEAR installation is recommended for most people. It can be easily shared and upgraded, and the installation process is straightforward.
  64    * 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.
  65  
  66  Symfony integrates a few other packages:
  67  
  68    * pake is a CLI utility.
  69    * lime is a unit testing utility.
  70    * 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.
  71    * Propel is for ORM. It provides object persistence and query service.
  72    * Phing is a CLI for Propel.
  73  
  74  Pake 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.
  75  
  76  ### Installing the Symfony PEAR Package
  77  
  78  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.
  79  
  80  The first step to install it is to add the symfony channel to PEAR, by issuing this command:
  81  
  82      > pear channel-discover pear.symfony-project.com
  83  
  84  To see the libraries available in this channel, type the following:
  85  
  86      > pear remote-list -c symfony
  87  
  88  Now you are ready to install the latest stable version of symfony. Issue this command:
  89  
  90      > pear install symfony/symfony
  91  
  92      downloading symfony-1.0.0.tgz ...
  93      Starting to download symfony-1.0.0.tgz (1,283,270 bytes)
  94      .................................................................
  95      .................................................................
  96      .............done: 1,283,270 bytes
  97      install ok: channel://pear.symfony-project.com/symfony-1.0.0
  98  
  99  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:
 100  
 101      > symfony -V
 102  
 103      symfony version 1.0.0
 104  
 105  >**TIP**
 106  >If you prefer to install the most recent beta, which has the latest bug fixes and enhancements, type `pear install symfony/symfony-beta` instead. Beta releases are not completely stable and are generally not recommended for production environments.
 107  
 108  The symfony libraries are now installed in directories as follows:
 109  
 110    * `$php_dir/symfony/` contains the main libraries.
 111    * `$data_dir/symfony/` contains the skeleton of symfony applications; default modules; and configuration, i18n data, and so on.
 112    * `$doc_dir/symfony/` contains the documentation.
 113    * `$test_dir/symfony/` contains unit tests.
 114  
 115  The _dir variables are part of your PEAR configuration. To see their values, type the following:
 116  
 117      > pear config-show
 118  
 119  ### Checking Out Symfony from the SVN Repository
 120  
 121  For 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:
 122  
 123      > mkdir /path/to/symfony
 124      > cd /path/to/symfony
 125      > svn checkout http://svn.symfony-project.com/tags/RELEASE_1_0_0/ .
 126  
 127  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:
 128  
 129      > php /path/to/symfony/data/bin/symfony -V
 130  
 131      symfony version 1.0.0
 132  
 133  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 two variables defined in your project's `config/config.php` file, as follows:
 134  
 135      [php]
 136      <?php
 137  
 138      $sf_symfony_lib_dir  = '/path/to/symfony/lib/';
 139      $sf_symfony_data_dir = '/path/to/symfony/data/';
 140  
 141  Chapter 19 proposes other ways to link a project with a symfony installation (including symbolic links and relative paths).
 142  
 143  >**TIP**
 144  >Alternatively, you can also download the PEAR package ([http://pear.symfony-project.com/get/symfony-1.0.0.tgz](http://pear.symfony-project.com/get/symfony-1.0.0.tgz)) and unpack it somewhere. You will have the same result as with a checkout.
 145  
 146  Setting Up an Application
 147  -------------------------
 148  
 149  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.
 150  
 151  ### Creating the Project
 152  
 153  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.
 154  
 155  For a PEAR installation, issue these commands:
 156  
 157      > mkdir ~/myproject
 158      > cd ~/myproject
 159      > symfony init-project myproject
 160  
 161  For an SVN installation, create a project with these commands:
 162  
 163      > mkdir ~/myproject
 164      > cd ~/myproject
 165      > php /path/to/symfony/data/bin/symfony init-project myproject
 166  
 167  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.
 168  
 169  Symfony will create a directory structure that looks like this:
 170  
 171      apps/
 172      batch/
 173      cache/
 174      config/
 175      data/
 176      doc/
 177      lib/
 178      log/
 179      plugins/
 180      test/
 181      web/
 182  
 183  >**TIP**
 184  >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).
 185  
 186  ### Creating the Application
 187  
 188  The 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:
 189  
 190      > symfony init-app myapp
 191  
 192  This 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:
 193  
 194      apps/
 195        myapp/
 196          config/
 197          i18n/
 198          lib/
 199          modules/
 200          templates/
 201  
 202  Some PHP files corresponding to the front controllers of each default environment are also created in the project `web` directory:
 203  
 204      web/
 205        index.php
 206        myapp_dev.php
 207  
 208  `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.
 209  
 210  Configuring the Web Server
 211  --------------------------
 212  
 213  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.
 214  
 215  ### Setting Up a Virtual Host
 216  
 217  Listing 3-1 is an example of Apache configuration, where a new virtual host is added in the `httpd.conf` file.
 218  
 219  Listing 3-1 - Sample Apache Configuration, in `apache/conf/httpd.conf`
 220  
 221      <VirtualHost *:80>
 222        ServerName myapp.example.com
 223        DocumentRoot "/home/steve/myproject/web"
 224        DirectoryIndex index.php
 225        Alias /sf /$sf_symfony_data_dir/web/sf
 226        <Directory "/$sf_symfony_data_dir/web/sf">
 227          AllowOverride All
 228          Allow from All
 229        </Directory>
 230        <Directory "/home/steve/myproject/web">
 231          AllowOverride All
 232          Allow from All
 233        </Directory>
 234      </VirtualHost>
 235  
 236  In 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:
 237  
 238          Alias /sf /usr/local/lib/php/data/symfony/web/sf
 239  
 240  >**NOTE**
 241  >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/`.
 242  
 243  Restart Apache, and that's it. Your newly created application can now be called and viewed through a standard web browser at the following URL:
 244  
 245      http://localhost/myapp_dev.php/
 246  
 247  You should see a congratulations page similar to the one shown earlier in Figure 3-1.
 248  
 249  >**SIDEBAR**
 250  >URL Rewriting
 251  >
 252  >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.
 253  >
 254  >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`:
 255  >
 256  >
 257  >    AddModule mod_rewrite.c
 258  >    LoadModule rewrite_module modules/mod_rewrite.so
 259  >
 260  >
 261  >For Internet Information Services (IIS), you will need `isapi/rewrite` installed and running. Check the symfony online documentation for a detailed IIS installation guide.
 262  
 263  ### Configuring a Shared-Host Server
 264  
 265  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.
 266  
 267  >**CAUTION**
 268  >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.
 269  
 270  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.
 271  
 272  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 application `config.php` file.
 273  
 274  Listing 3-2 - Changing the Default Directory Structure Settings, in `apps/myapp/config/config.php`
 275  
 276      [php]
 277      $sf_root_dir = sfConfig::get('sf_root_dir');
 278      sfConfig::add(array(
 279        'sf_web_dir_name' => $sf_web_dir_name = 'www',
 280        'sf_web_dir'      => $sf_root_dir.DIRECTORY_SEPARATOR.$sf_web_dir_name,
 281        'sf_upload_dir'   => $sf_root_dir.DIRECTORY_SEPARATOR.$sf_web_dir_name.DIRECTORY_SEPARATOR.sfConfig::get('sf_upload_dir_name'),
 282      ));
 283  
 284  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.
 285  
 286  Listing 3-3 - Default `.htaccess` Configuration, Now in `myproject/www/.htaccess`
 287  
 288      Options +FollowSymLinks +ExecCGI
 289  
 290      <IfModule mod_rewrite.c>
 291        RewriteEngine On
 292  
 293        # we skip all files with .something
 294        RewriteCond %{REQUEST_URI} \..+$
 295        RewriteCond %{REQUEST_URI} !\.html$
 296        RewriteRule .* - [L]
 297  
 298        # we check if the .html version is here (caching)
 299        RewriteRule ^$ index.html [QSA]
 300        RewriteRule ^([^.]+)$ $1.html [QSA]
 301        RewriteCond %{REQUEST_FILENAME} !-f
 302  
 303        # no, so we redirect to our front web controller
 304        RewriteRule ^(.*)$ index.php [QSA,L]
 305      </IfModule>
 306  
 307      # big crash from our front web controller
 308      ErrorDocument 500 "<h2>Application error</h2>symfony applicationfailed to start properly"
 309  
 310  You should now be ready to browse your application. Check the congratulation page by requesting this URL:
 311  
 312      http://www.example.com/myapp_dev.php/
 313  
 314  >**SIDEBAR**
 315  >Other Server Configurations
 316  >
 317  >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.
 318  >
 319  >To find directions for a specific server configuration, refer to the symfony wiki ([http://www.symfony-project.com/trac/wiki](http://www.symfony-project.com/trac/wiki)), which contains many step-by-step tutorials.
 320  
 321  Troubleshooting
 322  ---------------
 323  
 324  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.
 325  
 326  ### Typical Problems
 327  
 328  If you are still having problems getting symfony running, check the following:
 329  
 330    * 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:
 331  
 332          Parse error, unexpected ',', expecting '(' in .../symfony.php on line 19.
 333  
 334    * 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.
 335  
 336          Allowed memory size of 8388608 bytes exhausted
 337  
 338    * 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:
 339  
 340          Strict Standards: Implicit cloning object of class 'sfTimer'because of 'zend.ze1_compatibility_mode'
 341  
 342    * 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:
 343  
 344          sfCacheException [message] Unable to write cache file"/usr/myproject/cache/frontend/prod/config/config_config_handlers.yml.php"
 345  
 346    * 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).
 347    * 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.
 348  
 349  >**NOTE**
 350  >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`.
 351  
 352  ### Symfony Resources
 353  
 354  You can check if your problem has already happened to someone else and find solutions in various places:
 355  
 356    * The symfony installation forum ([http://www.symfony-project.com/forum/](http://www.symfony-project.com/forum/)) is full of installation questions about a given platform, environment, configuration, host, and so on.
 357    * 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.
 358    * The symfony wiki ([http://www.symfony-project.com/trac/wiki#Installingsymfony](http://www.symfony-project.com/trac/wiki#Installingsymfony)) contains step-by-step tutorials, contributed by symfony users, about installation.
 359  
 360  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.
 361  
 362  Source Versioning
 363  -----------------
 364  
 365  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.
 366  
 367  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`.
 368  
 369  So let's create the new repository for the `myproject` project:
 370  
 371      > svnadmin create $SVNREP_DIR/myproject
 372  
 373  Then the base structure (layout) of the repository is created with the `trunk`, `tags`, and `branches` directories with this pretty long command:
 374  
 375      > svn mkdir -m "layout creation" file:///$SVNREP_DIR/myproject/trunk file:///$SVNREP_DIR/myproject/tags file:///$SVNREP_DIR/myproject/branches
 376  
 377  This will be your first revision. Now you need to import the files of the project except the cache and log temporary files:
 378  
 379      > cd ~/myproject
 380      > rm -rf cache/*
 381      > rm -rf log/*
 382      > svn import -m "initial import" . file:///$SVNREP_DIR/myproject/trunk
 383  
 384  Check the committed files by typing the following:
 385  
 386      > svn ls file:///$SVNREP_DIR/myproject/trunk/
 387  
 388  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:
 389  
 390      > cd ~
 391      > mv myproject myproject.origin
 392      > svn co file:///$SVNREP_DIR/myproject/trunk myproject
 393      > ls myproject
 394  
 395  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.
 396  
 397  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:
 398  
 399      > cd ~/myproject
 400      > chmod 777 cache
 401      > chmod 777 log
 402      > svn propedit svn:ignore log
 403      > svn propedit svn:ignore cache
 404  
 405  The default text editor configured for SVN should launch. If this doesn't happen, make Subversion use your preferred editor by typing this:
 406  
 407      > export SVN_EDITOR=<name of editor>
 408      > svn propedit svn:ignore log
 409      > svn propedit svn:ignore cache
 410  
 411  Now simply add all files from the subdirectories of `myproject/` that SVN should ignore when committing:
 412  
 413      *
 414  
 415  Save and quit. You're finished.
 416  
 417  Summary
 418  -------
 419  
 420  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.
 421  
 422  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.
 423  
 424  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.
 425  
 426  Once your project is initiated, it is a good habit to start a version-control process.
 427  
 428  Now that you are ready to use symfony, it is time to see how to build a basic web application.


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7