rekowski.info David Rekowski's random stuff

Installing symfony 2 on uberspace

2012-07-31

How can I install the symfony framework on uberspace?

Keywords

symfony, composer, twig, symfony2

Background

After installing composer the next step is to install the symfony framework.

Install symfony 2

The symfony download page suggests doing the following:

$ php composer.phar create-project symfony/framework-standard-edition path/

Since we installed composer in our $PATH and removed the .phar, we can simply run

$ composer create-project symfony/framework-standard-edition ~/html/project

It takes a moment to clone the repos...

Installing symfony/framework-standard-edition (dev-master 3315eb0465e935dda19d23b4fd978705e9049fc6) - Installing symfony/framework-standard-edition (dev-master) Cloning master <snip>lots of install/clone output</snip> symfony/symfony suggests installing doctrine/data-fixtures (1.0.*) monolog/monolog suggests installing mlehner/gelf-php (Allow sending log messages to a GrayLog2 server) doctrine/orm suggests installing symfony/yaml (If you want to use YAML Metadata Mapping Driver) 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) symfony/assetic-bundle suggests installing symfony/twig-bundle (2.1.*) Generating autoload files 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 Symfony\Bundle\WebProfilerBundle into web/bundles/webprofiler Installing assets for Sensio\Bundle\DistributionBundle into web/bundles/sensiodistribution

Basically, that's it. From here on, we can follow the Book, although it is outdated in regard to symfony2 vendor handling via composer.json since it states php bin/vendors which no longer exists.

Setting up symfony

Since http://<yourhost>/project/web/config.php is only available on localhost and we don't want to mess around with lynx, we're going to edit the configuration manually.

We need to configure the database settings. From here on I assume we are in the project folder, whenever a path is given. Thus, in app/config/parameters.yml we need to alter the database_name, database_user and database_password parameters.

Checking the setup

Let's try to access http://<yourhost>/project/web/app_dev.php. The result:

You are not allowed to access this file. Check app_dev.php for more information.

A closer look at the indicated file reveals, that it is only accessible from localhost. I tried to use an SSH tunnel, but I failed at that. Until we find a solution, let's add our current IP in app_dev.php. To find out your IP, go to http://www.whatismyip.com. Add the IP to the array containing 127.0.0.1:

// snip || !in_array(@$_SERVER['REMOTE_ADDR'], array( '', '127.0.0.1', '::1', )) // snip

Keep in mind, though, that you have to update the IP whenever your ISP changes yours.

Now we can access the dev environment. Had we done this before, we could have used the config.php instead of manual configuration, but so be it.

So accessing the app in development mode works and we can gain insight into the symfony routing by running php app/console router:debug, which gives us a list of registered routes.

Routing in production

Regarding the router:debug output, we should keep in mind, that the symfony console considers the environment, which - correct me if I'm wrong - on the command line is by default dev. Thus, if we want to see which routes are defined for production, we add the --env=prod parameter:

$ php app/console --env=prod router:debug [router] Current routes Name Method Pattern

Boom! No routes. In order to make a route available for production, we need to edit app/config/routing.yml. For example, let's add the demo controller:

_demo: resource: "@AcmeDemoBundle/Controller/DemoController.php" type: annotation prefix: /demo

But sure enough, this is not sufficient, as we see running router:debug:

$ php app/console --env=prod router:debug [Symfony\Component\Config\Exception\FileLoaderLoadException] Cannot import resource "@AcmeDemoBundle/Controller/DemoController.php" from "/var/www/virtual/user/html/project/app/config/routing.yml". Make sure the "AcmeDemoBundle" bundle is correctly registered and loaded in the application kernel class. [InvalidArgumentException] Bundle "AcmeDemoBundle" does not exist or it is not enabled. Maybe you forgot to add it in the registerBundles() function of your AppKernel.php file? router:debug [name]

Which means the demo bundle is only available in the dev environment. We can change this by moving the AcmeDemoBundle in app/AppKernel.php from the test/dev conditional section into the bundles array.

new Acme\DemoBundle\AcmeDemoBundle(),

Next, we clear the cache to make sure it's not stale:

php app/console --env=prod cache:clear

Now we find another error message in app/logs/prod.log:

"Route "_welcome" does not exist." Which means we need to copy it to the routing.yml as well.

And sure enough, after another cache:clear run, we can call http://<yourhost>/project/web/demo/hello/world

Debugging reminder

Some generic steps for finding an error in symfony:

  1. check app/logs/<env>.log
  2. empty the cache with php app/console --env=<env> cache:clear
  3. check the routing with php app/console router:debug

Next steps

You can now continue to develop your symfony app. For further information, check the symfony book or the symfony cookbook.

Have fun!