How to manage Laravel projects with Git and BitBucket

laravel-logoGit, Laravel and BitBucket might be very popular in open source development world so I will not mention them again here. The purpose of this tutorial is pretty simple: we want to store all our project files into a remote BitBucket repository while can still maintain Laravel updates and other vendors’ libraries via git. So, I write this short tutorial will guide how to manage Laravel projects with Git and BitBucket.

  • First, for quick start, we will use the code base of Laravel 4 Bootstrap Starter Site as initial project source code. We will develop needed changes based on this and then use BitBucket to store latest source code for the project. So, we need to have an account at BitBucket to start.
  • Create a new git repository on BitBucket (called my-website) and clone it to the project folder:

    [bash]git clone https://[email protected]/USERNAME/website.git website[/bash]

    . This step to ensure that the default git repository (called origin) is pointed to our BitBucket repo.

  • Download latest code from Laravel 4 Bootstrap Starter Site and move tham all to the new folder:

    [bash]wget https://github.com/andrew13/Laravel-4-Bootstrap-Starter-Site/archive/master.zip
    unzip master.zip
    mv master/* website
    mv master/.git* website[/bash]

  • Push the initial code to BitBucket:

    [bash]cd website
    git add .
    git commit -m "Initial commit"
    git push[/bash]

  • Add Laravel repo to current repo and pull changes from it:

    [bash]git remote add laravel git://github.com/laravel/laravel.git
    git fetch laravel
    git merge laravel/master[/bash]

  • Check current status by

    [bash]git status[/bash]

  • Finally, install all Laravel environment using composer (Remember that you will need to have an account at github):

    [bash]composer install[/bash]

  • Remember to set necessary permission for Laravel before accessing it for the first time (remember to use sudo if you are in a VM box powered by vagrant and PuPHPet):

    [bash]chmod -R 777 app/storage
    chmod -R 777 public/assets/compiled[/bash]

  • OK, now we need to create database and associated user:

    [bash]mysql -u root -p
    create database DBNAME;
    grant all privileges on DBNAME.* to DBUSERNAME@localhost identified by ‘DBPASSWORD’;
    FLUSH PRIVILEGES;
    exit;[/bash]

  • Then edit bootstrap/start.php and make your $env variable look like:

    [php]$env = $app->detectEnvironment(array(
    ‘local’ => array(‘local.*’,’*.local’),
    ‘staging’ => array(‘*.staging-server.com’),
    ‘production’ => array(‘*.com’),
    ));[/php]

  • Now create new folders in app/config, named after your different environments:

    [bash]cd app/config/
    mkdir local staging production
    cp database.php local
    cp app.php local
    cp database.php staging
    cp app.php staging
    cp database.php production
    cp app.php production
    [/bash]

    . Remember to add those folders to the repository root directory .gitignore to not tracking by git:

    [bash]/app/config/local/*
    /app/config/staging/*
    /app/config/production/*
    /public/assests/compiled/*[/bash]

    . Commit it right after that:

    [bash]git add .gitignore
    git commit -a -m "exclude environment configuration"
    git push[/bash]

  • Next, add needed configuration files (app.php and database.php, remember that this needs to be redone every environments) to app/config/local
  • Finally, run migrations and seeds (Remember to add necessary tables by php artisan migrate:make create_mynecessary_table before running migration):

    [bash]php artisan migrate
    php artisan db:seed[/bash]

Notes regarding setting up Laravel starter project

  1. basset library from http://jasonlewis.me/code/basset is a little out-dated and will cause error on the project. To fix it, simply download the improved version from https://github.com/Marwelln/basset and overwrite the current one.
  2. In the remote server, in order to perform git command lines (such as git fetch, git pull, etc.), remember to open outgoing port 9418.
  3. In case some new controllers were added but laravel cannot recognize it, regenerate auto-load by

    [bash]composer dump-autoload[/bash]

Simple way for empty project

[bash]composer create-project laravel/laravel myproject –prefer-dist
cd myproject
git init
git remote add origin https://[email protected]/USERNAME/myproject-website.git
[/bash]

Leave a comment

Your email address will not be published. Required fields are marked *

2 thoughts on “How to manage Laravel projects with Git and BitBucket”