/* drucafe */

Friday, October 25, 2013

Installing Symfony2 Hello World on Windows XAMPP

This is not strictly about Drupal, but rather about installing Symfony2. Drupal 8 will use Symfony2 as a framework, so I'd like to take a look at Symfony itself and install a simple "hello world" project on Windows 7 XAMPP. The installation of Symfony with Drupal 8 will perhaps look differently.


The place to start with Symfony2 is:
I generally followed the installation steps from http://net.tutsplus.com/tutorials/php/diving-into-symfony-2-2/.
  • install Git on Windows
  • install Composer
  • install Symfony
The prerequisite on Windows is to have XAMPP with php 5.3.8 or higher.

Install GIT on Windows

Git provides a console with helpful Linux-like commands and syntax coloring. This console will be used during the installation and then often during the project development.

Go to page http://git-scm.com/downloads and download the Git for Windows:


Run the downloaded exe file, which is a standard Windows installer. After installing Git, it will create a program group in the Start menu:


It will also add three options to the right-click menu in Windows Explorer:


The last option, "Git Bash", will be used a lot in this post and I will call it just a "console".

Install Composer

Create a Windows directory for the Symfony projects. I created the directory in i:\xampp\htdocs\symfony. In Windows Explorer, right click inside this directory and choose Git Bash to open the command window here.
Quick test of php.ini
In the bash window, type php --ini to see which initialization file your system is using. If the answer is sh: php: command not found, then there is a problem with php installation on your system, so fix it.

Normally, it will show something like this:


Note the "Loaded Configuration File" name - in case of problems, this is the file where php parameters are set. I will use this information later, when fixing the problem with openssl.dll setting.
Installation of Composer
In the bash console, enter command: curl -s http://getcomposer.org/installer | php. It will create a file composer.phar in the current directory, in other words, it will install the Composer. Other ways to install Composer on Windows  are described here: http://getcomposer.org/doc/00-intro.md#installation-windows.

Check the installation by entering php composer.phar -V in the bash console. Actually, in this console, a shorter version (without "php") also works: composer.phar -V.

Install Symfony

Ensure that the bash console is open in the directory where you want to install Symfony. On my system it was i:\xampp\htdocs\symfony. Enter command:
php composer.phar create-project symfony/framework-standard-edition helloworld/, which means creating a new symfony project in directory "helloworld".

On my system, the command failed with Runtime Exception "You must enable the openssl extension to download files via https". The remedy was to add the line extension=php_openssl.dll to the php.ini file (see "Quick test of php.ini" above for the location of the php.ini file).

Once the openssl error had been fixed, the installation went on for around 5 minutes, downloading various packages etc. (Quick stats: Symfony 2.5 has 7000+ files and occupies almost 50MB on disk). Near the end, the installer typed a few prompts asking for details about database, mailer, locale and a secret token. Here is the log:

$ php composer.phar create-project symfony/framework-standard-edition helloworld
Installing symfony/framework-standard-edition (v2.3.6)
  - Installing symfony/framework-standard-edition (v2.3.6)
    Downloading: 100%

Created project in helloworld
Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing jdorn/sql-formatter (v1.2.9)
    Downloading: 100%

  - Installing psr/log (1.0.0)
    Downloading: 100%

  - Installing twig/twig (v1.14.1)
    Downloading: 100%

  - Installing doctrine/lexer (v1.0)
    Downloading: 100%

  - Installing doctrine/annotations (v1.1.2)
    Downloading: 100%

  - Installing doctrine/collections (v1.1)
    Downloading: 100%

  - Installing doctrine/cache (v1.2.0)
    Downloading: 100%

  - Installing doctrine/inflector (v1.0)
    Downloading: 100%

  - Installing doctrine/common (v2.4.1)
    Downloading: 100%

  - Installing symfony/symfony (v2.3.6)
    Downloading: 100%

  - Installing symfony/icu (v1.0.0)
    Downloading: 100%

  - Installing doctrine/dbal (2.3.4)
    Downloading: 100%

  - Installing doctrine/doctrine-bundle (v1.2.0)
    Downloading: 100%

  - Installing twig/extensions (v1.0.0)
    Downloading: 100%

  - Installing kriswallsmith/assetic (v1.1.2)
    Downloading: 100%

  - Installing symfony/assetic-bundle (v2.3.0)
    Downloading: 100%

  - Installing monolog/monolog (1.6.0)
    Downloading: 100%

  - Installing symfony/monolog-bundle (v2.3.0)
    Downloading: 100%

  - Installing incenteev/composer-parameter-handler (v2.0.0)
    Downloading: 100%

  - Installing doctrine/orm (2.3.4)
    Downloading: 100%

  - Installing swiftmailer/swiftmailer (v5.0.2)
    Downloading: 100%

  - Installing symfony/swiftmailer-bundle (v2.3.4)
    Downloading: 100%

  - Installing sensio/distribution-bundle (v2.3.4)
    Downloading: 100%

  - Installing sensio/framework-extra-bundle (v2.3.4)
    Downloading: 100%

  - Installing sensio/generator-bundle (v2.3.4)
    Downloading: 100%

kriswallsmith/assetic suggests installing leafo/lessphp (Assetic provides the integration with the lessphp LESS compiler)
kriswallsmith/assetic suggests installing leafo/scssphp (Assetic provides the integration with the scssphp SCSS compiler)
kriswallsmith/assetic suggests installing ptachoire/cssembed (Assetic provides the integration with phpcssembed to embed data uris)
kriswallsmith/assetic suggests installing leafo/scssphp-compass (Assetic provides the integration with the SCSS compass plugin)
monolog/monolog suggests installing mlehner/gelf-php (Allow sending log messages to a GrayLog2 server)
monolog/monolog suggests installing raven/raven (Allow sending log messages to a Sentry server)
monolog/monolog suggests installing doctrine/couchdb (Allow sending log messages to a CouchDB server)
monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required))
monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server)
Writing lock file
Generating autoload files
Creating the "app/config/parameters.yml" file.
Some parameters are missing. Please provide them.
database_driver (pdo_mysql):
database_host (127.0.0.1):
database_port (null):
database_name (symfony):symhelloworld
database_user (root):
database_password (null):password
mailer_transport (smtp):
mailer_host (127.0.0.1):
mailer_user (null):
mailer_password (null):
locale (en):
secret (ThisTokenIsNotSoSecretChangeIt):somerandomletters
Clearing the cache for the dev environment with debug true
Installing assets using the hard copy option
Installing assets for Symfony\Bundle\FrameworkBundle into web/bundles/framework
Installing assets for Acme\DemoBundle into web/bundles/acmedemo
Installing assets for Sensio\Bundle\DistributionBundle into web/bundles/sensiodistribution

When finished, a new directory is created i:\xampp\htdocs\symfony\helloworld for the "helloworld" project with the following content:

Post installation check

Check the installation of the Symfony project by going in a web browser to the Symfony config page at: http://localhost/symfony/helloworld/web/config.php (the address should reflect the folder where you installed Symfony). The page shows some suggestions how to improve performance etc.



If the address http://localhost/symfony/helloworld/web/config.php is unavailable, check your httpd.conf file (in directory I:\xampp\apache\conf). There should be a line:
DocumentRoot "I:/xampp/htdocs".

Click on the "Bypass configuration and go to the Welcome page" link on the previous screen. Or, go directly to http://localhost/symfony/helloworld/web/app_dev.php to see the Symfony application welcome screen. That's not the "hello world" application yet, this is a standard welcome page.




Install a HelloWorld bundle

A bundle is like a Drupal module. I'll install a bundle that displays "Hello world" on the screen. To install a bundle, I followed the procedure described here:
http://symfony.com/doc/current/book/page_creation.html#before-you-begin-create-the-bundle

The bundle installation is done via the console. you just have to type the command:
app/console generate:bundle --namespace=Drucafe/HelloWorld --format=yml
Below is the log of my installation of a HelloWorld bundle. It was automatic (or "promptomatic") and the only cases I had to type something was to correct the bundle namespace (which must end with "Bundle") and say "yes" to automatically generate the directory structure. Apart from that, I pressed Enter on all prompts to accept the defaults.

User1@MYWORKSTATION /I/xampp/htdocs/symfony/helloworld
$ app/console generate:bundle --namespace=Drucafe/HelloWorld --format=yml

  Welcome to the Symfony2 bundle generator

 The namespace must end with Bundle.

Your application code must be written in bundles. This command helps
you generate them easily.

Each bundle is hosted under a namespace (like Acme/Bundle/BlogBundle).
The namespace should begin with a "vendor" name like your company name, your
project name, or your client name, followed by one or more optional category
sub-namespaces, and it should end with the bundle name itself
(which must have Bundle as a suffix).

See http://symfony.com/doc/current/cookbook/bundles/best_practices.html#index-1 for more
details on bundle naming conventions.

Use / instead of \  for the namespace delimiter to avoid any problem.

Bundle namespace [Drucafe/HelloWorld]:
 The namespace must end with Bundle.
Bundle namespace [Drucafe/HelloWorld]: Drucafe/HelloWorldBundle

In your code, a bundle is often referenced by its name. It can be the
concatenation of all namespace parts but it's really up to you to come
up with a unique name (a good practice is to start with the vendor name).
Based on the namespace, we suggest DrucafeHelloWorldBundle.

Bundle name [DrucafeHelloWorldBundle]:

The bundle can be generated anywhere. The suggested default directory uses
the standard conventions.

Target directory [I:/xampp/htdocs/symfony/helloworld/src]:

To help you get started faster, the command can generate some
code snippets for you.

Do you want to generate the whole directory structure [no]? yes

  Summary before generation

You are going to generate a "Drucafe\HelloWorldBundle\DrucafeHelloWorldBundle" bundle
in "I:/xampp/htdocs/symfony/helloworld/src/" using the "yml" format.

Do you confirm generation [yes]?

  Bundle generation

Generating the bundle code: OK
Checking that the bundle is autoloaded: OK
Confirm automatic update of your Kernel [yes]?
Enabling the bundle inside the Kernel: OK
Confirm automatic update of the Routing [yes]?
Importing the bundle routing resource: OK

  You can now start using the generated code!

During the installation, the following things were done by the installer:
  • a new bundle directory at I:\xampp\htdocs\symfony\helloworld\src\Drucafe\HelloWorldBundle was created, with structure as pictured below
  • bundle routing file with a new bundle route (web path) hello/{name} was created at I:\xampp\htdocs\symfony\helloworld\src\Drucafe\HelloWorldBundle\Resources\config\routing.yml
  • a new bundle controller file I:\xampp\htdocs\symfony\helloworld\src\Drucafe\HelloWorldBundle\Controller\DefaultController.php contains an default action indexAction, which renders a page with "Hello, {name}" text.
  • the Symfony kernel file I:\xampp\htdocs\symfony\helloworld\app\AppKernel.php file was changed and now contains a line with DrucafeHelloWorldBundle bundle registration
  • the Symfony routing file
    I:\xampp\htdocs\symfony\helloworld\app\config\routing.yml was changed and now contains a reference to the new bundle routing file
Structure of the new bundle folder:

The whole picture:

The controller

The DrucafeHelloWorldBundle controller looks like this, out of the box:

namespace Drucafe\HelloWorldBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller {
    public function indexAction($name) {
        return $this->render('DrucafeHelloWorldBundle:Default:index.html.twig', array('name' => $name));
    }
}

We can change it to return the "Hello, world" content:

namespace Drucafe\HelloWorldBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller {
    public function indexAction($name) {
      return new Response('Hello, ' . $name . '!');
    }
}

Checking the bundle installation in a browser

As a result, after typing into the web browser address bar:
http://localhost/symfony/helloworld/web/app_dev.php/hello/world
a page is rendered with "Hello, world" content.

The following will also work:
http://localhost/symfony/helloworld/web/app.php/hello/world
http://localhost/symfony/helloworld/web/hello/world
But in order for these to work, it's necessary to clear the cache:
app/console cache:clear --env=prod --no-debug



1 comment: