Laravel 101 - Setting Up

Installation

The Laravel framework is built using 'components', or packages or modules in other frameworks. Laravel uses Composer to manage these components and their dependencies.

Install Composer

Download and install the latest version of Composer.

$ curl -sS https://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /your/root/dir/composer.phar
Use it: php composer.phar

You can also install using curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin so that it'll be available globally. You might need to use sudo.

Add ~/.composer/vendor/bin to your PATH environment variable to ensure any executables provided by the packages can be executed without specifying the full path. For example, you can open up your ~/.bash_profile and add the following lines.

PATH=$PATH:~/.composer/vendor/bin
export PATH

You may also need to run source ~/.bash_profile to apply the new settings to your current shell.

Laravel Installer

Download the Laravel installer which provides you with an executable (laravel) that you can run to create a new Laravel application. This step can take several minutes.

composer global require "laravel/installer=~1.1"

Once the installer is installed, use it to create new applications.

laravel new myapp

This will create a directory named myapp with all dependencies installed.

Git

We will use Git for version control. The default installation already includes a pre-configured .gitignore, let's take a look inside.

/vendor
/node_modules
.env

The /vendor directory holds all your packages installed by Composer that're required for Laravel to work. Because all your package information are defined inside composer.json and set in composer.lock, you should not include the /vendor directory; instead, when deploying, you should pull the repository down and run composer install (or composer update) manually.

The same for the /node_modules directory, which houses all packages installed by the NPM package manager, should you use tools like Gulp. The .env entry will be explained in the next section.

So, we're all set, all we need to do is run:

git init
git add -A
git commit -m "Initial commit"

Configuration

All configuration files specific to your application resides inside the config directory.

The installation should have also provided you with an .env.example file, which contains some default configurations you need to modify to fit your system. You should keep the original and copy it as .env, and only modify this new file.

$ cp .env.example .env

Since each developer might have different settings for their environment, the .env file should not be included in version control. The .env.example file, however, contains the default configurations, and should be included for reference.

Application Key

Each application in Laravel has an application key, set inside the environment file .env. It is a randomly-generated, 32-character string used to encrypt user sessions and data. So the first thing to do set it to an new random string.

$ vi .env

Change the APP_KEY variable from SomeRandomString to an actual random string.

APP_KEY=C96E25D24EF6EA52B0899CA72E0566F9

Finishing up

That's it, Laravel is set. In the next post, we will look inside our application source code and give you an overview of the general structure of a Laravel application, and the role of each component inside.

comments powered by Disqus