Module 1: The Foundation

Setting up your environment and creating your first Laravel project.

Welcome to Your First Step!

Welcome to the first module of your Laravel journey! Every great structure needs a solid foundation, and in web development, that foundation is your local development environment. This module is dedicated to getting you set up with all the necessary tools and creating your very first Laravel application. We'll move step-by-step, ensuring you understand the 'why' behind each action. By the end, you'll have a working Laravel project running on your computer.


1Setting Up Your Local Environment

Before you can write a single line of Laravel code, you need a server environment on your local machine. This environment needs three key components: a web server (like Apache or Nginx), a database (like MySQL), and the PHP programming language itself. Instead of installing these individually, we can use a pre-packaged software stack.

Using XAMPP (Windows/Linux) or MAMP (Mac)

XAMPP and MAMP are the most beginner-friendly ways to get started. They are free software packages that bundle everything you need into a single, easy-to-install application. They give you a graphical interface to start and stop your server and manage your database.

  1. Download: Go to the official Apache Friends website and download the XAMPP installer for your operating system (Windows, Linux, or OS X). Mac users might prefer MAMP.
  2. Install: Run the installer and follow the on-screen instructions. We recommend accepting the default settings.
  3. Start Services: Open the XAMPP Control Panel. You will see a list of services. For Laravel, you need to start the Apache and MySQL modules. You should see their status turn green.

Using Docker and Laravel Sail

For those comfortable with the command line, Docker is a modern and powerful approach. Laravel's official tool, Sail, provides a simple command-line interface for interacting with a Docker-based Laravel environment. This isolates your project's environment in "containers," preventing conflicts with other software on your machine.

To use this method, you first need to install Docker Desktop. Once Docker is installed and running, Laravel's own installer (which we'll cover next) can automatically set up Sail for you. This is the recommended method for experienced developers as it closely mirrors modern production deployments.


2Installing Composer: PHP's Package Manager

Think of Composer as a manager for your project's code libraries. Laravel itself, and all the packages it depends on, are managed through Composer. It's a critical tool in modern PHP development.

  1. Download: Visit the official Composer website: getcomposer.org.
  2. Install: Follow the installation instructions for your operating system. For Windows, there's a handy `Composer-Setup.exe` that will automatically find your PHP installation (from XAMPP) and set up your system's PATH variable. For Mac/Linux, you'll run a few commands in your terminal.
  3. Verify Installation: To check if Composer is installed correctly, open a new terminal or command prompt and type:
composer --version

If it's working, you'll see the Composer version number printed on the screen. If you get an error, revisit the installation steps, ensuring the PATH is set correctly.


3Creating Your First Laravel Project

With your environment and Composer ready, it's time for the exciting part! You can create a new Laravel project with a single Composer command. This command downloads the latest version of Laravel and all its dependencies into a new folder.

Open your terminal or command prompt, navigate to the directory where you store your projects (for XAMPP users, this is typically the `htdocs` folder inside your XAMPP installation directory), and run the following command:

composer create-project laravel/laravel my-first-app

Let's break that down:

  • composer create-project is the command to create a new project from a package.
  • laravel/laravel is the official Laravel application skeleton package.
  • my-first-app is the name of the directory that will be created for your project. You can change this to whatever you like!

This process will take a few minutes as Composer downloads all the necessary files. Grab a coffee and watch the magic happen!


4Running Your Application

Laravel comes with a built-in development server powered by PHP, managed by a tool called Artisan. Artisan is Laravel's command-line interface, and you will use it constantly. To start the server, first, navigate into your new project's directory:

cd my-first-app

Now, run the `serve` command:

php artisan serve

Your terminal will show a message like `Server running on [http://127.0.0.1:8000]`. Open this URL in your web browser. You should be greeted by the beautiful Laravel welcome page!


5A Quick Tour of the Directory Structure

Opening your new `my-first-app` folder in a code editor like VS Code can be overwhelming. There are a lot of files and folders! Don't worry, you only need to focus on a few key directories for now:

  • /app: This is the heart of your application. It contains your Models, Controllers, and other core PHP business logic. You'll spend most of your time here.
  • /routes: This folder contains all the route definitions for your application. The `web.php` file is where you define the URLs for your website.
  • /resources/views: This is where your front-end view files (your HTML) live. Laravel uses the Blade templating engine, so files will have a `.blade.php` extension.
  • /public: This is the only folder that should be accessible from the web. It contains your front-end assets like CSS, JavaScript, and images, as well as the main `index.php` file that boots up the application.
  • .env: This is your environment configuration file. It's where you'll store important settings like your database credentials and application keys.

We will explore each of these in much greater detail in the upcoming modules.