Today we will setup the development environment, install Symfony2 and display a page of the application in a web browser. First of all, you need to check that your computer has a friendly working environment for web development. At a minimum, you need a web server (Apache, for instance), a database engine (MySQL), and PHP 5.3.2 or later.
Download and install Symfony2
Prepare a directory on your web server where you want to install the new project. We will name this directory jobeet as the name of our future website. Then go to http://symfony.com/download, choose “Symfony Standard” (the version with vendors) and download it (at the moment of writing this tutorial, the latest Symfony version was 2.0.11). After downloading, unpack the archive and copy the contents of “Symfony” dir to your project directory. You should now have something like this:
Web Server Configuration
To configure Apache for your new project, locate and open the httpd.conf configuration file and add the following configuration at the end:
<VirtualHost *:80> ServerName jobeet.local DocumentRoot /home/dragos/work/jobeet/web DirectoryIndex app.php ErrorLog /var/log/apache2/jobeet-error.log CustomLog /var/log/apache2/jobeet-access.log combined <Directory "/home/dragos/work/jobeet/web"> AllowOverride All Allow from All </Directory> </VirtualHost>
Change the above configuration to reflect your setup and then restart your web server.
This is the standard way to define a new virtual host in Apache. Depending on your server configuration or Apache version, some things might be slightly different. For example in Ubuntu you have to create a new file in the folder /etc/apache2/sites-enabled/ named jobeet with the above content.
The domain name jobeet.local used in the Apache configuration has to be declared locally. If you run a Linux system, it has to be done in the /etc/hosts file. If you run Windows, this file is located in the C:\WINDOWS\system32\drivers\etc\ directory. Add in the following line:
Test the Symfony2 Installation
After restarting Apache, open a browser window and type in http://jobeet.local/app_dev.php. You should see the following page:
To avoid some headaches further down the line, check that your configuration can run a Symfony2 project smoothly by requesting the following URL: http://jobeet.local/config.php. Make sure you don’t have any Major Problems listed, and follow the optional recommendations listed (if any).
Just like Symfony 1.x, Symfony2 comes with the console component tool that you will use for different tasks. To see a list of things it can do for you type at the command prompt:
php app/console list
Creating the Application Bundle
As you know, a Symfony2 project is made up of bundles. Even the Symfony framework is a bundle. To create a new bundle for our application run from the command line:
php app/console generate:bundle --namespace=Ens/JobeetBundle --format=yml
The generator will ask you some questions before generating the bundle. Here are the questions and the answers (all except one are the default answers):
Bundle namespace [Ens/JobeetBundle]: Ens/JobeetBundle
Bundle name [EnsJobeetBundle]: EnsJobeetBundle
Target directory [/home/dragos/work/jobeet/src]: /home/dragos/work/jobeet/src
Configuration format (yml, xml, php, or annotation) [yml]: yml
Do you want to generate the whole directory structure [no]? yes
Do you confirm generation [yes]? yes
Confirm automatic update of your Kernel [yes]? yes
Confirm automatic update of the Routing [yes]? yes
Clear the cache after generating the new bundle with:
php app/console cache:clear --env=prod php app/console cache:clear --env=dev
The new Jobeet bundle can be now found in the src directory of out project: src/Ens/JobeetBundle. The bundle generator made a DefaultController with an index action. You can access this in your browser: http://jobeet.local/hello/jobeet or http://jobeet.local/app_dev.php/hello/jobeet
As Symfony 1.x, Symfony2 also has different environments. If you look in the project’s web directory, you will see two php files: app.php and app_dev.php. These files are called front controllers; all requests to the application are made through them. The app.php file is for production environment and app_dev.php is used by web developers when they work on the application in the development environment. The development environment will prove very handy because it will show you all the errors and warnings and the Web Debug Toolbar – the developer’s best friend.
That’s all for today. See you on the next day of this tutorial when we will talk about what exactly the Jobeet website will be about!