loading...
Cover image for Laravel 8 Tutorial #1: Setup the Project

Laravel 8 Tutorial #1: Setup the Project

ericnanhu profile image Eric Hu Originally published at techjblog.com Updated on ・6 min read

You can download the source code of this tutorial here: https://www.techjblog.com/index.php/laravel-tutorial-for-beginners/

Laravel is a very powerful framework that follows the MVC structure. It is designed for web developers who need a simple, elegant yet powerful toolkit to build a fully-featured website.

This tutorial explains the basic use of this framework by building a simple blogging system. But before we start, there are some software we need to install.

Install Necessary Tools

XAMPP

XAMPP is a free and open-source cross-platform web server solution stack package developed by Apache Friends. It contains MySQL, phpMyAdmin, PHP, and Apache server.

img

It combines everything you need for PHP web development in one place and spares you the time for installing and configuring each of them individually.

DOWNLOAD XAMPP

Composer

img

Composer is a dependency manager for PHP, and it helps you install Laravel and all the other dependencies that you need for building a website. Remember, you need to install XAMPP or PHP first, or the composer installer will not be able to find php.exe.

DOWNLOAD COMPOSER

PhpStrom

img

PhpStorm is an IDE designed for PHP projects, as the name suggests. It is a commercial, cross-platform IDE for PHP, built by the Czech Republic-based company JetBrains. PhpStorm provides an editor for PHP, HTML, and JavaScript with on-the-fly code analysis, error prevention, and automated refactorings for PHP and JavaScript code.

The good news is, if you are a student, you will be qualified for a free copy with your student email. However, you do need to verify your student status every year.

DOWNLOAD PHPSTORM

Create A New Project

First, let us create a new empty PHP project in PhpStorm. In this tutorial, we’ll call it “Laravel_8_Tutorial”.

img

Now, we need to install Laravel for our project. Open “Terminal”, it should go to the root directory of the project. Run the following commands.

Download the Laravel installer:

composer global require laravel/installer
Enter fullscreen mode Exit fullscreen mode

This command will automatically install the latest version of the laravel installer.

Next, we can create a new Laravel application in the project directory:

laravel new
Enter fullscreen mode Exit fullscreen mode

If you are planning on creating multiple apps, then put each app into an independent folder, for example:

laravel new blog
laravel new gallery
Enter fullscreen mode Exit fullscreen mode

This installation process will take a few minutes to complete.

imgDownload Laravel Installer

imgCreate A New App

Once it is done, this is what you should get:

img

Application Structure

Here I will briefly introduce the directories that we need to use in this tutorial since some might be a little confusing for beginners. If you are interested, you can read the Official Documentation, which explains the function of each directory in detail.

The Root Directory

app Directory

This folder contains the entire source code of our project. It includes events, middleware, exceptions, and so on. But, for now, we only care about models and controllers.

config Directory

As the name impies, it stores all configuration files for our project.

database Directory

The database directory is where we put all the seeds and migration files. They determine the structure of the database. If you wish, you may also use this directory to hold an SQLite database. But I don’t recommend using SQLite for huge projects.

public Directory

This directory holds the index.php file, which is the entry point for all requests. Also, we need to put all static files (CSS and JS) into this folder.

routes Directory

The route directory contains all URL declarations for our project. By default, there are four route files: web.php, api.php, console.php and channels.php. For this tutorial, we only need to understand web.php.

resources Directory

This folder stores all the views and uncompiled assets such as LESS, SASS, or JavaScript.

The App Directory

Http/Controllers Directory

This is where we put all the controllers for our project. All of the logic to handle requests entering your application will be placed in this directory.

Models Directory

Laravel 8 now has an individual folder for models.

The Models directory contains all of your Eloquent model classes. The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding “Model” which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

Project Configuration

All the environment configuration in Laravel are stored in the .env file in the root directory. To make our project function properly, there are some changes we need to make.

App URL

First, let’s define the URL for our project. Since we are in development, the URL should be http://localhost.

APP_URL=http://localhost
Enter fullscreen mode Exit fullscreen mode

Database

If you installed XAMPP, we can use the MySQL database that comes with the software. We need to create a new databse for our project first. Open XAMPP control panel:

img

Click on the “Admin” button for MySQL. This will take us to phpMyAdmin, which is the administration panel for MySQL databases.

img

We create a new database here, and then, we tell Laravel how to access this database. Go back to the .env file, and find the database section (the one with the DB_ prefix).

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_8_tutorial
DB_USERNAME=root
DB_PASSWORD=
Enter fullscreen mode Exit fullscreen mode

Application Key

Setting an application key is essential to the safety of our project. If the application key is not set, your user sessions and other encrypted data will not be secure.

The following command will set the application key for you:

php artisan key:generate
Enter fullscreen mode Exit fullscreen mode

File Storage

This configuration is not about the environment, but it is still very important. In order to make sure Laravel will find the media files (images, videos…) we uploaded, we need to create a symbolic link in the public directory with this command:

php artisan storage:link
Enter fullscreen mode Exit fullscreen mode

Start the Server

Finally, it’s time to start our server. We have two choices, we can run the built-in local development server:

php artisan serve
Enter fullscreen mode Exit fullscreen mode

Or we can use the Apache server from XAMPP, but you might need to change the “DocumentRoot” setting in the configuration file. Open the XAMPP control panel again. Click the “Config” button and open httpd.conf:

img

Find DocumentRoot and change its path to the public folder of your project.

img

Once you are done, restart the server. Open the browser and go to http://localhost/. You should see the Laravel welcome page.

img

Next, we’ll take a look at routing in Laravel.

Related Articles

How to Make Your Server More Secure

Laravel Tutorial For Beginners

Django Tutorial For Beginners

Build A Unit Converter with Vue.js

Discussion

pic
Editor guide