Technology & News

Home » Our Blog » Setting Up Your First Laravel Project: A Comprehensive Guide for Beginners
July 10, 2024

Introduction

Laravel is a popular PHP framework designed to make web development easier and more efficient. It’s known for its elegant syntax, robust features, and extensive documentation. If you’re new to Laravel and web development in general, setting up your first Laravel project can seem daunting. This blog post aims to guide you through the process step by step, ensuring that even beginners can follow along and get their first Laravel project up and running.

What You Need Before You Start

Before you start setting up Laravel, you’ll need to ensure that your development environment is ready. Here’s a list of prerequisites:

  1. PHP: Laravel is a PHP framework, so you’ll need PHP installed on your computer. Laravel requires PHP 7.3 or higher.
  2. Composer: Composer is a dependency manager for PHP. Laravel uses Composer to manage its dependencies.
  3. Web Server: You can use a local development server like XAMPP, WAMP, or MAMP, or use Laravel’s built-in development server.
  4. Text Editor/IDE: A good text editor or Integrated Development Environment (IDE) like Visual Studio Code, PhpStorm, or Sublime Text will make your development process easier.

Step-by-Step Guide to Setting Up Laravel

1. Install PHP

First, ensure you have PHP installed on your system. You can download it from the official PHP website. If you’re using a local development server like XAMPP or MAMP, PHP will be included.

To check if PHP is installed, open your terminal (Command Prompt on Windows, Terminal on macOS or Linux) and type:

php -v

You should see the PHP version information if PHP is installed.

2. Install Composer

Next, install Composer. Composer is a tool for managing dependencies in PHP. You can download it from the official Composer website.

To verify Composer installation, type:

composer -v

You should see the Composer version information if Composer is installed.

3. Create a New Laravel Project

With PHP and Composer installed, you can now create a new Laravel project. Open your terminal and navigate to the directory where you want to create your project. Then, run the following command:

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

Replace my_first_project with the desired name for your project.

Composer will download and install Laravel and its dependencies. This may take a few minutes.

4. Navigate to Your Project Directory

Once the installation is complete, navigate to your project directory:

cd my_first_project

5. Start the Development Server

Laravel includes a built-in development server. To start it, run:

php artisan serve

You should see a message indicating that the development server has started:

Laravel development server started: <http://127.0.0.1:8000>

Open your web browser and go to http://127.0.0.1:8000. You should see the Laravel welcome page, indicating that your Laravel project is set up correctly.

Understanding the Laravel Directory Structure

Now that your Laravel project is up and running, let’s take a look at the directory structure. Understanding the structure of a Laravel project will help you navigate and manage your project more effectively.

  1. app: This directory contains the core code of your application. It includes subdirectories for controllers, models, and other essential components.
  2. bootstrap: This directory contains the files needed to bootstrap the Laravel application.
  3. config: This directory contains configuration files for your application.
  4. database: This directory contains database migrations, model factories, and seeds.
  5. public: This is the public directory that serves as the entry point for your application. It contains the index.php file and assets like images, JavaScript, and CSS files.
  6. resources: This directory contains your views (HTML templates), language files, and raw assets.
  7. routes: This directory contains all the route definitions for your application.
  8. storage: This directory is used for storing logs, compiled Blade templates, file uploads, and other generated files.
  9. tests: This directory contains automated tests for your application.
  10. vendor: This directory contains the Composer dependencies.

Configuring Your Laravel Application

After setting up your Laravel project, you might need to configure it according to your needs. Here are some common configurations:

1. Environment Configuration

Laravel uses an environment configuration file named .env. This file contains various settings for your application, such as database credentials, application environment, and debug mode.

Open the .env file in your text editor. You’ll see something like this:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:randomkeygeneratedbylaravel
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=your_username
DB_PASSWORD=your_password

Update the database settings with your own credentials if you’re using a database.

2. Database Configuration

If you’re using a database, you’ll need to set it up and configure Laravel to use it. Laravel supports various database systems like MySQL, PostgreSQL, SQLite, and SQL Server.

Update the database settings in the .env file with your database information.

3. Generating Application Key

Laravel requires an application key for encryption. This key is generated during the installation, but if you need to regenerate it, you can run:

php artisan key:generate

Creating Your First Laravel Route

Routes define the paths that your application responds to. To create your first route, open the routes/web.php file and add a new route:

Route::get('/hello', function () {
    return 'Hello, World!';
});

Save the file and go to http://127.0.0.1:8000/hello in your web browser. You should see the message “Hello, World!”.

Creating Your First Laravel Controller

Controllers are used to handle the logic for your routes. To create a new controller, run the following command:

php artisan make:controller HelloController

This will create a new controller file in the app/Http/Controllers directory. Open the HelloController.php file and add the following code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HelloController extends Controller
{
    public function greet()
    {
        return 'Hello from the controller!';
    }
}

Next, update the routes/web.php file to use the new controller:

Route::get('/hello', [App\Http\Controllers\HelloController::class, 'greet']);

Save the file and go to http://127.0.0.1:8000/hello in your web browser. You should see the message “Hello from the controller!”.

Conclusion

Setting up your first Laravel project may seem overwhelming at first, but with the right guidance, it becomes a manageable and enjoyable process. By following this step-by-step guide, you should now have a working Laravel project, understand its directory structure, and know how to configure it according to your needs.

Laravel’s rich feature set and elegant syntax make it an excellent choice for web development, whether you’re a beginner or an experienced developer. As you continue to explore Laravel, you’ll discover more powerful tools and features that can help you build robust and scalable web applications.

Happy coding!

    Leave a Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Terms of Service
    Privacy Policy
    © Copyright 2024 Misah Software Solutions Townsville