Skip to content

Instantly share code, notes, and snippets.

@pitchinnate
Last active June 24, 2019 10:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pitchinnate/7cd71518fc24114a454becb14cc4e17f to your computer and use it in GitHub Desktop.
Save pitchinnate/7cd71518fc24114a454becb14cc4e17f to your computer and use it in GitHub Desktop.
Laravel Windows Environment Setup

Requirements

In order to run this locally you will need the following tools/programs:

Global Installs

For php/composer install the following packages globally so they can be used anywhere:

composer global require "phpunit/phpunit"
composer global require "laravel/installer"
composer global require "squizlabs/php_codesniffer=*"

Aliases

If you are using cmder I highly recommend setting up some aliases that will make your life a lot easier. {CmderDirectory}\config\user-aliases.cmd add the following lines:

gs=git status
aa=git add --all
push=git push
pull=git pull --rebase
test=phpunit
testreport=php -c .\php-xdebug.ini C:\Users\{USERNAME}\AppData\Roaming\Composer\vendor\phpunit\phpunit\phpunit --coverage-html ./storage/logs/phpunit
testu=phpunit --testsuite unit
testf=phpunit --testsuite functional
testa=phpunit --testsuite functional
cleandb=php artisan migrate:refresh && php artisan db:seed
cleancode=phpcbf ./

Running Package Managers

From the project directory you will need to make sure you have all frontend dependencies (npm) as well as backend dependencies (composer). The following commands will need to ran:

composer install
npm install

Local Environment - Environment Variables

First you will need to create your local version of your .env file. This goes in the root directory of the project code. This allows you to set environment variables without having to worry about what web server you are using. You can copy the contents of /.env.example to your /.env file.

Your the first thing you need to do is set your APP_KEY. This can be done automatically after you create your .env file by running the following command:

php artisan key:generate

Local Environment - Database

Make sure you update your database credentials in your .env file to connect to your local database server. Set your database as an empty database then use migrations to create all tables.

Database Migrations and Seeds

To run database migrations simple run the artisan command migrate from the root directory of the project. If you want to use seed data run the artisan command db:seed command from the root directory of the project.

php artisan migrate
php artisan db:seed

Local Environment - Web Server

You have two options for running a web server locally. You can either run the server from the command line or you can use something like WAMP Server (http://www.wampserver.com/en/). I personally use Wamp Server 64bit with PHP 7.0.

Running Web Server via Command Line

From the project directory you will simply need to run php artisan serve. By default it will be running on localhost:8000, so you should be able to go to http://localhost:8000 in your browser.

Running Web Server via Wamp Server

First I normally setup a host record for a dev domain name, something like mroc.local. Open up your windows host file which can be found at C:/Windows/System32/drivers/etc/hosts. Add a line that looks like this:

127.0.0.1    mroc.local

In your wamp directory look for a directory called /alias, I normally create a vhost in here by creating a sites.conf file. Mine looks like this:

<VirtualHost *:80>
	ServerName mroc.local
	DocumentRoot c:/wamp64/www/mroc/public
</VirtualHost>

Modify your DocumentRoot if your code is in a different location. After you have added this make sure you restart apache for it to pick up the new .conf file. After this you should be able to visit http://mroc.local in your browser.

Code format

Our goal is to follow the PSR guides for php code. PSR-1 and PSR-2 are the main PSR rules you should worry about for most code. You can view the guides here http://www.php-fig.org/psr/

Automated Code Formatting

Other developers have created tools that can automatically format code to the PSR standards. If you go to https://github.com/squizlabs/PHP_CodeSniffer you can find instructions on installing phpcs (PHP CodeSniffer) and phpcbf (PHP Code Beautifier and Fixer). phpcs runs and just lets you know what is wrong with your code, phpcbf will actually automatically fix your code so I normally run this. You can find the instructions for installation on the link above but normally you can just run:

composer global require "squizlabs/php_codesniffer=*"

Once the two scripts are installed you can simply run the following command from the main project directory:

phpcbf ./

There is a phpcs.xml file in the project directory that allows us to exclude certain directories and also specify the rules we are following. The file currently has the PSR-1 and PSR-2 rules included.

To keep code clean and well formated please try to remember to run phpcbf before pushing code up.

Optional Alias

I seem to always mix up the cbf part of the command so I simply add an alias like we do above like so:

cleancode=phpcbf ./
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment