Getting Started with Laravel

Tutorial 3 of 5

Laravel Tutorial: Getting Started with Laravel

1. Introduction

Goal

In this tutorial, we will be introducing Laravel, a robust and popular PHP framework for web application development. The aim of this tutorial is to help you install Laravel, understand its directory structure, and create a simple web application using Laravel.

Learning Outcomes

By the end of this tutorial, you will:
* Have Laravel installed on your local system
* Understand the Laravel directory structure
* Be able to create a basic web application using Laravel

Prerequisites

Before starting this tutorial, you should:

  • Have a basic understanding of PHP
  • Have PHP, Composer (PHP package manager), and a database (MySQL, SQLite, PostgreSQL) installed on your system

2. Step-by-Step Guide

Installing Laravel

  1. Open your terminal and navigate to the directory where you want to install Laravel.
  2. Run the following command to create a new Laravel project:
composer global require laravel/installer

This command will install Laravel on your system.

Understanding the Laravel Directory Structure

After successfully installing Laravel, navigate to your Laravel project directory. Here, you'll notice several directories and files. The most important ones are:

  • app: Contains the core code of your application.
  • public: This is the document root of your application. It starts your Laravel app via the index.php file.
  • resources: Contains your views (HTML/CSS/JS), raw assets, and localization files.
  • routes: Contains all the route definitions for your application.
  • config: Contains all the configuration files for your application.

Creating a Simple Web Application

Let's create a simple "Hello, World!" web application.

  1. Open the .env file in the root directory and update your database details.
  2. Open the routes/web.php file. This is where we define our web routes for our application.
  3. Delete everything in this file and replace it with the following code:
Route::get('/', function () {
    return 'Hello, World!';
});

This code defines a route that returns "Hello, World!" when the root URL (/) of your application is accessed.

3. Code Examples

Let's look at a practical example of creating a controller and a route:

  1. To create a controller, run the following command in your terminal:
php artisan make:controller HelloWorldController

This command will create a new controller in app/Http/Controllers/HelloWorldController.php.

  1. Open HelloWorldController.php, and add the following code:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HelloWorldController extends Controller
{
    public function index()
    {
        return 'Hello, World!';
    }
}

Here, we've defined a controller method index that returns "Hello, World!".

  1. Next, define a route that points to this controller method. Open routes/web.php and add the following code:
Route::get('/', 'HelloWorldController@index');

This code tells Laravel to execute index method of HelloWorldController when the root URL (/) is accessed.

4. Summary

In this tutorial, we have installed Laravel, explored its directory structure, and created a simple "Hello, World!" web application using a controller and a route.

For further learning, consider exploring more about Laravel's MVC (Model-View-Controller) architecture, database migrations, and routing.

5. Practice Exercises

  1. Create a new Laravel project and familiarize yourself with its directory structure.
  2. Create a new route that points to a function returning "Welcome to Laravel!".
  3. Create a new controller with a method that returns a view.

Remember, practice is the key to mastering any programming framework or language. Happy coding!