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

Laravel Tutorial #1: Setup the Project

ericnanhu profile image Eric Hu Originally published at techjblog.com ・4 min read

Laravel Tutorial #1: Setup the Project

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 apps we need to install.

Preparation

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 has combined 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

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.

Download PhpStorm

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

Create A New Project in PhpStorm

First, create a new empty PHP project in PhpStorm. In this tutorial, I’ll name it “Laravel_Blog”.

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

Create a new Laravel app:

laravel new

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

laravel new blog
laravel new gallery

This 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 only 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

Here, you will find all Eloquent models. The models do not have an individual folder, but developers can place them in one if they choose.

Http/Controllers Directory

And finally, 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.

Start the Server

Now we can run the built-in local development server:

php artisan serve

Or we can use the Apache server from XAMPP, but you might need to change the “DocumentRoot” setting in the configuration file.

img

Find DocumentRoot and change its path to the public folder of your project. Once you are done, restart the server. Open the browser and go to http://localhost/

img

Next, we’ll talk about URLs, routes, and the MVC (Model-View-Controller) architecture that is commonly used by most web frameworks.

Next Post: Laravel Tutorial #2: Routes, Views, Controllers and Models

Posted on by:

Discussion

pic
Editor guide