Learning Management Platform for Written Tutorial Series.

Laravel 6 Installation & Configuration

Laravel 6 Installation & Configuration

In this tutorial, we are going to look at how to install Laravel, configure Laravel and explore the directories of Laravel. We will work with a windows machine in this tutorial but the knowledge can still be applied to other operating systems such as Linux based OS, Mac OS etc.

Topics to be covered

We will cover the following topics in this lesson

  • Pre-requisites for installing Laravel
  • What is Composer
  • Visual Studio Code
  • A Good Terminal
  • Git
  • How to Install Laravel
  • Laravel Application Structure
  • Laravel Configuration

Pre-requisites for installing Laravel

Before installing Laravel, ensure that you have the following programs already installed

  • PHP - you need to have at least version 7.2 or above of PHP installed on your computer. You can install PHP separately or you can install it as part of a package such as XAMPP
  • SQL Database - MySQL is the standard default and its the database engine that we will be working with as we proceed. But you can use other database engines such as SQLite, SQL Server etc.
  • Composer - this is a PHP package manager that is used to download PHP packages. Laravel is one of the packages that can be installed using composer.
  • Text editor to IDE - Needed to write code. Visual Studio Code is really popular, great and works on all platforms but you are free to choose what feels good to you.
  • Webserver - this is optional since PHP comes with a built-in server that we can work with during development. But you can also work with a real webserver like Apache or NGinx. The choice is yours.

What is Composer

As we started in the above section, it is a package manager that is used to install, uninstall and update existing packages. It is a command line utility program that allows us to type a command in the terminal and watch the magic happen. Composer downloads packages from Packgist.

You can download composer from Get Composer. We will not cover how to install Composer in this lesson. It is a straight forward process. Instruction on how to install it can be found here

Visual Studio Code

It is a cool text editor developed by Microsoft that can be used to write code for a number of languages like PHP, Python, C,C++, Java etc. Like we stated in the above section, you can use any text editor that you prefer. Visual Studio Code is cross-platform which means it works on Windows, MacOS and Ubuntu. One of the things that I like about Visual Studio Code is that it comes with a default nice dark theme which is consistent on all platforms and it has great performance. A market place of free extensions also exists. This allows you to activate more features.

You can download Visual Studio Code from the official; website Get Visual Studio Code

We will not cover how to download Visual Studio Code in this tutorial.

A Good Terminal

We will be working with the command line a lot through out these tutorial series. MacOS and Linux come with good terminals but on Windows the command prompt still works but personally I prefer something more mature. On windows, you can install Git which comes with Git bash.

Git

GitHub has lots of packages and some of the code of for the tutorials will be available on Github. Git will enable you to download the code for the tutorial series as well as code for other applications and packages.

You can download Git from Get Git

How to Install Laravel

In this section, we will look at how to install Laravel version 6 using composer. We will do it from the command line. I will install my project in the home directory inside a directory called Dev but feel free to use whatever directory that y6ou want.

Open the terminal

Run the following command

cd ~
mkdir Dev
cd Dev

We can now install Laravel like so

composer create-project laravel/laravel larashop 6.0.*

HERE,

  • composer invokes the program composer. create-project is the command that we use to create new projects. laravel/laravel tells composer to create a Laravel project. larashop is the name of our project. 6.0.* tells composer to fetch the latest version of Laravel 6.0

Executing the above command should give you results similar to the following

Composer Install Laravel 6

When the project is done install a directory larashop should exist.

Laravel Application Structure

We will now use Visual Studio Code to open the new folder. On windows, you can right-click on the directory larashop and Select the option Open with Code

Once you have opened the project directory, you should be able to see something similar to the following.

Visual Studio Code Laravel

As you can see from the above image, we have a lot of folders which can be overwhelming if you are a beginner. The good thing is we will be one with only a single directory 80% of the times.

The directory where we will be writing our code in 805 of the times is app

The following images shows the app directory expanded to reveal its contents

Laravel 6 Directories

Let's now explore the directories. Some of the directories mentioned below do not exist by default but we mention them for the sake of completion.

S/NDIRECTORYDESCRIPTION
1/appcontains all of your application code
2/app/Consolecontains all of your artisan commands
3/app/Eventscontains event classes
4/app/Exceptionscontains exception handling classes
5/app/Httpcontains controllers, filters, and requests
6/app/Jobscontains jobs that can be queued
7/app/Listenerscontains handler classes for events
8/app/PoliciesContains authorization policy classes
9/bootstrapcontains files required by the bootstrap framework
10/configcontains the application configuration files
11/databaseContains database migrations and seeds. It is also used to store the database for SQLite
12/publiccontains the front controllers and assets such as images, CSS, JavaScript etc.
13/resourcesContains views, localization files and raw assets
14/routesContains files that define routes for web, api, console etc.
14/storagecontains compiled blade templates, filed based sessions, etc.
15/testscontains automated unit tests
16/vendorcontains composer dependencies

Laravel Configuration

Now that we have explored the directories of Laravel, let's configure our application.

Application configuration

The configuration information is stored in the .env file in the project root. Laravel uses /config/app.php to check for configuration parameters defined in .env file.

In this section, we are going to;

Note: Laravel comes with sensible defaults out of the box. We won’t have to change some of the settings.

  • Set the debugging mode – this will make Laravel display detailed information when an error occurs. The detailed information is useful for debugging purposes.
  • Set the application URL – used by artisan command when running tasks
  • Locale – Laravel supports application localization. The default language is English. If your app won’t use English as the default language, then you can set a default language using this key.
  • Set the time zone – this setting is used for PHP date and date-time functions.
  • Application key – this value is used for encryption purposes.
  • Logging errors - this option allows us to customize how errors are logged in Laravel.

Debugging mode

Let’s start with the .env file.

Open .env file in the root directory

Locate the following lines

APP_DEBUG=true

By default, APP_DEBUG is set to true. If you want to turn it off, you can set it to false but for now, we will leave it set to true. When you deploy the application to production, APP_DEBUG should always be set to false

Let’s now look at how Laravel reads this value

Open the file /config/app.php

Locate the following code

'debug' => env('APP_DEBUG', false),

HERE,

  • 'debug' => env('APP_DEBUG', false), Laravel reads the value of the key APP_DEBUG from the .env file. If it is not found, Laravel will use falseas the fall-back value. The value is assigned to the debug key of the configuration array.

Time zone

Locate the following code

'timezone' => 'UTC',

HERE,

  • 'timezone' => 'UTC', sets the time zone to UTC. This is the default value

If you would like to have a different time zone, you can replace UTC with a value of your preferred time zone. You can fine a list of supported time zones here

Application Key

Locate the following code

'key' => env('APP_KEY'),

HERE,

  • 'key' => env('APP_KEY'), Laravel reads the application key from our .env configuration file. This key is automatically generated when you create a new Laravel application. It is used for encryption purposes and must be kept secret.

Application Name

The application name is used in a number of templates within the application. For example, when you send an email from the application using default templates, The name of the application is included in the email. The default value is Laravel so we can change it to a name that is specific to our application.

Open .env file

Locate the following line

APP_NAME=Laravel

Change it to the following

APP_NAME=Larashop

Other settings

You can explore /config/app.php to see the various settings that are available in it.

Authentication configuration

The authentication configuration file is located in /config/auth.php. We will leave the default values as they are. If you want you can change them to meet your requirements.

Database configuration

The database configuration file is located in /config/database.php. By default, MySQL will be used as the database engine. You can set it to a different database management system if you want but for this tutorial, we will leave the default value. The code for database configuration looks like the following

        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

HERE,

  • 'driver' => 'mysql', sets the database driver name to mysql
  • 'url' => env('DATABASE_URL'), reads the database URL from the .env file and assigns the value to the array key url.
  • 'host' => env('DB_HOST', '127.0.0.1'), sets the database host name or IP address based on the values set in the .emv file
  • 'port' => env('DB_PORT', '3306'), sets the database port which defaults to 3306. if you are running MySQL on a different port number then you can update this value in the .env file.
  • 'database' => env('DB_DATABASE', 'forge'), sets the database name
  • 'username' => env('DB_USERNAME', 'forge'), sets the database username. This should be a valid user name that exists in MySQL.
  • 'password' => env('DB_PASSWORD', ''), sets the database password. This should be a valid password that you use to login to MySQL.
  • other settings the rest of the settings are defaults that are rarely changed so we will not discuss them yet.

Summary

In this tutorial, we looked at;

  • The pre-requisites for installing Laravel. Composer, Web server, PHP and MySQL plus an IDE for coding
  • Laravel project directories structure
  • How to configure a new Laravel project.

What’s next?

Let’s get our hands dirty by developing a simple hello world application. We will continue building on this project. Use the next tutorial button to navigate to the next tutorial


...