Last Updated: January 19, 2020
·
27.77K
· nekwebdev

Git and Laravel 4 practical use

Hello World!

Once again, a little tip related to Laravel 4 aimed to help me keep track of what I am doing and hopefully help others.

My end project is to redo an e-commerce site built on Joomla/VirtueMart that is slowly driving me nuts. I have no real dead lines besides my patience so I figured I could use the occasion to learn a PHP framework and maybe a JS one along the way.

After I started documenting myself I figured I did not want to start from scratch and found andrew13/Laravel-4-Bootstrap-Starter-Site.

This would be my base, and since it had a blogging featured already, which I don't need for my end project, I figured I could make a sort of base template for future projects, basically it would need:

  • Authentication
  • Pages/Blogs/Comments
  • Products pages/Shopping cart functionality
  • Classifieds
  • Admin panels to control all of this

And then I would just use the parts of the code needed for the specific project.
Most of this is already built it the bootstrap starter site, and I will just have to twist around what's already there to make the new functions I want.

I want to use git to version control my project and easily keep up to date with the bootstrap starter site code and the base laravel code.

Quite an intro! Let's get on with it.

The final objective is to have a local repository of the project that can pull from the Bootstrap-Starter-Site any updates to the code, and also pull updates from Laravel to keep up to date. It then needs to push to a staging webser, a Linode in my case. Once the code is production ready it gets pushed from the linode to the production server.

1. Setting up our Git remotes

First let's get a fresh copy of the Bootstrap-Starter-Site:

git clone git://github.com/andrew13/Laravel-4-Bootstrap-Starter-Site.git mysite

Now let's setup our different remotes:

cd mysite
git remote rename origin starter
git remote add laravel git://github.com/laravel/laravel.git
git remote add staging ssh://git@bitbucket.org/username/mysite.git

First we renamed the origin remote we initially cloned from to starter and then added our two other remotes, the official laravel repository and our private staging repository. My linode will then pull from this staging repository. This allows me to have a backup of my most current repository.

For now I will not worry about Git tracking option since I will be specifying the remotes in the git pull push and fetch requests.

2. Our first merge

It will most likely require resolving conflicts, let's start by changing a git setting:

git config merge.conflictstyle diff3

What this does is that it will add conflict markers on the files that have the common ancestor info, quite useful sometimes:

<<<<<<<
changes made on my branch
|||||||
the common ancestor version
=======
changes made on the branch i'm merging
>>>>>>>

So let's see how that goes:

git fetch laravel
git merge laravel/master
git status

git status will list the conflicting files, now edit them in your favorite editor. Once a file conflict is resolved mark it using git add filename.
You can keep checking what files still have conflicts with git status. Once they are all resolved give it a quick commit:

git commit -m "merge conflicts fixed"

So now our local master branch is a copy of the Bootstrap-Starter-Site but is updated to the latest Laravel code, cool let's make sure it works and setup our different environments.

3. Setting environments

Let's start it off by finishing up the install of Laravel:

composer install

While composer is doing it's thing let's quickly create our local database and it's user:

mysql -u root -p
create database mysite;
grant all privileges on mysite.* to username@localhost identified by 'password';
exit;

Done, so much faster than using phpmyadmin ;)

You want to edit bootstrap/strat.php and make your $env variable look something along those lines:

$env = $app->detectEnvironment(array(

    'local' => array('local.*','*.local'),
    'staging' => array('*.staging-server.org'),
    'production' => array('*.com'),
));

Change it what ever suits you, remember that you can also use the machine hostname to specify the environment, not just parts of the url. You will need to specify hostname for using artisan without the --env argument.

Now create new folders in app/config, named after your different environments. We don't want the files in those folders to be managed by git, we also don't want the compiled assets, so let's edit the repository root directory .gitignore and add:

/app/config/local/*
/app/config/staging/*
/app/config/production/*
/public/assests/compiled/*

Remember, commit often:

git add .gitignore
git commit -a -m "setup environments in bootstrap/start.php"

This time I manually added .gitignore, but used the -a argument for the commit which will automatically add bootstrap/start.php that we modified earlier for us. Cool!

Now create an app.php in the app/config/local folder, we will only need a couple of lines in it:

<?php

return array(
    'url' => 'http://mysite.local',
    'timezone' => 'UTC',
    'key' => 'GENERATE A KEY AND PASTE IT HERE',
    'providers' => array(
    /* Uncomment for use in development */
    //        'Way\Generators\GeneratorsServiceProvider', // Generators
    //        'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider', // IDE Helpers
    ),
);

Edit it as you see fit. Do the same for the app/config/local/database.php file:

<?php

return array(

    'connections' => array(

        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'mysite',
            'username'  => 'username',
            'password'  => 'password',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    ),
);

Finally do this for every other config file you edit, such as mail.php
At this point a git status should not show any changes or you did something wrong in the .gitignore file.

This rounds it up for now. Might add more very soon.

4. Get laravel running

We need to make certain folders writable by the webserver. The first one is a requirement of laravel, the second one from Basset, one of our plugins that needs to compile and write the css to the server when running under specific environments.

chmod -R 777 app/storage
chmod -R 777 public/assets/compiled

Run your migrations and seeds:

php artisan migrate
php artisan db:seed

Navigate to your local webserver and it should display a home page of blogs!

4 Responses
Add your response

good

over 1 year ago ·

Thank you, this was very helpful, especially the part about git.

over 1 year ago ·

Using domain names for environment configs has been dropped in 4.1, use hostnames instead. Overwriting providers like you do in app/config/local/app.php won't work - the arrays are only merged at the top level. You'll need to add the full list of providers.

over 1 year ago ·

Thanks, this was really helpful. I prefer using Laravel and recently I've tried this new Laravel based CMS - Doptor CMS. It's great! www.doptor.org

over 1 year ago ·