Showing posts with label Symfony2. Show all posts
Showing posts with label Symfony2. Show all posts

Thursday, August 21, 2014

Set up a Symfony2 dev environment in 10 minutes with Vagrant

Let's keep it simple. It is just a quick draft how can you set up a Symfony2 development environment in a few minutes.

I'll use Vagrant (you'll need version 1.5 at least) and my dev box to create the desired environment.

The box main components are: Ubuntu14.04, Nginx1.6, Php5.5Fpm, Xdebug, Composer, MySql5.5, NodeJs0.10.30, Redis and it has PhpMyAdmin, Webgrind and RedisCommander.

So the steps are:
  • Create the Vagrantfile
    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    
    VAGRANTFILE_API_VERSION = "2"
    
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
      config.vm.box = "adammbalogh/lemp"
    
      config.vm.network "private_network", ip: "192.168.33.10"
      config.vm.synced_folder "app/", "/var/www/app/"
    
      config.vm.provider "virtualbox" do |vb|
        vb.customize ["modifyvm", :id, "--memory", "1512"]
        vb.customize ["modifyvm", :id, "--cpuexecutioncap", "85"]
      end
    end
    
    
  • mkdir app
  • vagrant up
  • vagrant ssh
  • cd /var/www/app
  • composer create-project symfony/framework-standard-edition . '~2.5'
  • nano app/AppKernel.php (and add these two methods)
    public function getCacheDir()
    {
        return '/tmp/symfony/cache/' . $this->environment;
    }
    
    public function getLogDir()
    {
        return '/tmp/symfony/log/' . $this->environment;
    }
    
  • nano web/app_dev.php (and delete these lines)
    if (isset($_SERVER['HTTP_CLIENT_IP'])
        || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
        || !(in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1',$
    ) {
        header('HTTP/1.0 403 Forbidden');
        exit('You are not allowed to access this file. Check '.basename(__FIL$
    }
    
  • app/console cache:clear --env=dev (to test it)

Monday, May 12, 2014

Symfony2 - DataFixtures

Fixtures are used to load a controlled set of data into a database. This data can be used for testing or could be the initial data required for the application to run smoothly (from symfony.com)
So, the DataFixture Bundle is a great way to help initialize your application, e.g. set up an admin user. You can use it all through the development phase continuously, it has an append option. Without the append argument your database will be erased before the fixtures get injected.

If you plan to use this bundle for testing, you can mix it up with the Faker library to create various test data.

When you work on a not so large project it's a good enough bundle to solve your problems. But if the project is a large one and you want to use a more sophisticated way to make fixtures you should use the Alice library! (it has a Bundle for Symfony2 -> AliceBundle)

Alice calls itself an Expressive fixtures generator. It has a lot of handy tools in its toolbox.

You can create fixtures in a config file! Let's see a Static one:
Nelmio\Entity\User:
    user0:
        username: bob
        fullname: Bob
        birthDate: 1980-10-10
        email: bob@example.org
        favoriteNumber: 42
    user1:
        username: alice
        fullname: Alice
        birthDate: 1978-07-12
        email: alice@example.org
        favoriteNumber: 27

Nelmio\Entity\Group:
    group1:
        name: Admins
Take a look at a Fixture Range config:
Nelmio\Entity\User:
    user{1..10}:
        username: bob
        fullname: Bob
        birthDate: 1980-10-10
        email: bob@example.org
        favoriteNumber: 42
The above fixture configuration ends up 10 users, from user1 to user10.

It integrates with the Faker library initially and has a several other helpful features.