Writing your first Laravel app - Part One

Writing your first Laravel app - Part One

Rai Omido

In this tutorial, we are going to learn how to build applications with Laravel by example.

We shall be using Laravel version 8, as it the current version as per the time of writing this tutorial.

Throughout the tutorial, I'll walk you through the creation of a blog.

It will consist of two parts;

a) A public site that lets people view posts

b) An admin site that lets you create and manage posts

# Creating a Laravel project

To create a Laravel project, you need to ensure you have installed Composer on your computer. Composer is the package manager for PHP.

~Download requirements

Since we will be dealing with databases in this tutorial, we will need to install a few modules to aid us along the way.

We shall use XAMPP for now. This is simply a software package that will install for us PHP, an apache server, and a MySQL database.

Download XAMP on the Apache Friends download page.

~Create a Laravel project

Once you have composer and XAMPP installed, navigate to the directory where you want to install your project and run the following command;

composer create-project --prefer-dist laravel/laravel blog

If you still have problems, please visit the Laravel Installation Guide to read more.

# The Laravel directory structure

Let's have a quick rundown of what our application is all about.

Inside the blog directory you just created, you'll find a number of directories as follows;

~The App directory

This directory contains most of your application's logic. Most of the classes you create for your application will live here.

~The Bootstrap directory

This contains a bootstrap.php file that bootstraps (loads) your entire Laravel application.

It also contains a cache directory that houses cached files generated by the framework for performance optimization. 

~The config directory

As the name suggests, this directory contains configuration files for your application.

~The database directory

This contains your application's migrations, model factories, and seeds.

~The public directory

This contains an index.php file that acts as the entry point of all requests entering your app and configures autoloading. It also houses your application's assets such as images, CSS, and JavaScript.

~The resources directory

This houses your application's view files, plus uncompiled assets. It also houses your language files.

~The routes directory

This houses your application's route definitions. There are a number of files inside this directory. Let's have a look at each of them.

web.php file contains routes that have access to session state, CSRF protection, and cookie encryption.

api.php file contains routes that are intended to be stateless. These routes have access to rate limiting.

console.php contains Closure based console commands.

channels.php is where you define your event broadcasting channels.

~The storage directory

The storage directory houses compiled Blade templates, file based sessions, file caches, and other files generated by the framework. It also contains several directories;

The app directory is intended to house any files generated by your application.

The framework directory is used to house framework generated files.

The logs directory contains your application's log files.

The app/public directory houses your application's publicly available user generated files such as profile avatars and more.

~The tests directory

This contains your application's automated tests.

~The vendor directory

This directory contains your application's composer dependencies.

# The development server

Now that we have a good idea of what our application entails, it's time to test that our application actually works. Laravel ships with a minimal server that you can use to run your application in development.

To Launch your application using the development server, run the following command;

php artisan serve

You'll see the following output in the command line.

Starting Laravel development server: http://127.0.0.1:8000
[Fri Sep 11 11:54:01 2020] PHP 7.4.9 Development Server (http://127.0.0.1:8000) started

You can now visit the link (http://127.0.0.1:8000/) in your browser to see your new Laravel application.

# Create your first view

Open the resources/views directory, create an index.blade.php file, and add the code below.

<h1>Howdy! This is the home page.</h1>

This is a simple view in Laravel.

Now to call the view, we need to map it to a URL. To do this, we need to create a Route.

Open the routes/web.php file and replace the code;

Route::get('/', function () {
return view('welcome');
});

With the following code;

Route::get('/', function () {
return view('index');
});

Now refresh your browser and you should see our new view displayed.

# What's next?

After installing our application, the next step is to explore the database. See here part two tutorial.