In this article, we'll walk through the building blocks of Laravel and how can we use Laravel to set up a small project.
Laravel is a robust and elegant PHP web application framework that has gained immense popularity among developers for its simplicity, versatility, and powerful features. Over the years, Laravel has become the go-to PHP framework for both big and small projects.
Prerequisites: Getting Started with Laravel
Before diving into Laravel development, we must ensure we have all the necessary tools and software installed. Here's what we'll need:
-
PHP. Laravel runs on PHP, so the first step is to ensure you have PHP installed on your system. If you're not sure whether PHP is installed, open a terminal or command prompt and type
php -v
. If PHP is installed, you'll see a version number. If not, you'll need to install it.To install PHP in your machine we have a couple of options:
Local installation. You can install PHP directly on your computer. Visit the PHP downloads page to get the latest version for your operating system.
Laravel Homestead. For a more streamlined setup, especially if you're new to PHP development, consider using Laravel Homestead. Homestead is a pre-packaged Vagrant box that provides a complete development environment for Laravel. You can find installation instructions here.
-
Composer. Laravel uses Composer for dependency management. Composer is the default PHP dependency manager, and is widely used and well documented.
-
Laravel Installer. Laravel can be globally installed on our system using the Laravel Installer, a convenient command-line tool that streamlines and simplifies the process of creating new Laravel projects. To install it globally on your system, follow these steps:
- Open a terminal or command prompt.
- Run the following Composer command:
composer global require laravel/installer
Once installation is complete, make sure Composer's global bin directory is in your system's "PATH" so you can run the
laravel
command from anywhere.If you prefer a more lightweight alternative to Homestead, you might consider Laravel Herd. Herd is a Docker-based local development environment specifically tailored for Laravel projects. You can learn more about Herd and how to set it up here.
By setting up your development environment with PHP, Composer, and the Laravel Installer (or exploring options like Homestead or Herd), you'll be well-prepared to embark on your Laravel journey. In the following sections, we'll go through the process of creating a new Laravel project, exploring its directory structure, and configuring the development environment.
Setting Up a New Laravel Project
Now that we have our development environment ready, it's time to create a new Laravel project. To create a new, "empty" project, we can use the following terminal command:
composer create-project --prefer-dist laravel/laravel project-name
project-name
should be replaced with the actual project name. This command will download the latest version of Laravel and set up a new project directory with all the necessary files and dependencies.
Directory Structure: Navigating A Laravel Project
Upon creating a new Laravel project, we'll find ourselves in a well-organized directory structure. Understanding this structure is crucial for effective Laravel development. Here are some of the key directories and their purposes:
- app. This directory houses our application's core logic, including controllers, models, and service providers.
- bootstrap. Laravel's bootstrapping and configuration files reside here.
- config. Configuration files for various components of our application can be found here, allowing us to find and customize settings like database connections and services from a single point in the project.
- Database. In this directory, we'll define our database migrations and seeds to be used by Laravel's Eloquent ORM. Eloquent simplifies database management.
- public. Publicly accessible assets like CSS, JavaScript, and images belong here. This directory also contains the entry point for our application, the
index.php
file. - resources. Our application's raw, uncompiled assets, such as Blade templates, Sass, and JavaScript, are stored here.
- routes. Laravel's routing configuration is managed in this directory.
- storage. Temporary and cache files, as well as logs, are stored here.
- vendor. Composer manages our project's dependencies in this directory. All downloaded libraries will be in this directory.
Configuration: Database Setup and Environment Variables
To configure our database connection, we need to open the .env
file in the project's root directory. Here, we can specify the database type, host, username, password, and database name. Thanks to Eloquent ORM, Laravel supports multiple database connections, making it versatile for various project requirements.
Understanding the .env file
The .env
file is where we define environment-specific configuration values, such as database connection details, API keys, and other settings. Let's take a look at a simple example of what you might find in a .env
file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_database
DB_USERNAME=my_username
DB_PASSWORD=my_password
In this example:
DB_CONNECTION
specifies the type of database driver we're using (such as MySQL, PostgreSQL, SQLite).DB_HOST
specifies the host where our database server is located.DB_PORT
specifies the port on which the database server is running.DB_DATABASE
specifies the name of the database we want to connect to.DB_USERNAME
andDB_PASSWORD
specify the username and password required to access the database.
Using environment variables
It's crucial to keep sensitive information like database credentials secure. Laravel encourages the use of environment variables to achieve this. Instead of hardcoding our credentials in the .env
file, we can reference them in our configuration files.
For example, in our Laravel configuration files (located in the config/
directory), we can reference the database configuration like this:
'mysql' => [
'driver' => env('DB_CONNECTION', 'mysql'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
// ...
],
Here, the env()
function retrieves the value of the specified environment variable from the .env
file. If the variable isn't found, it falls back to a default value provided as the second argument.
By using configuration files, we can store sensitive data in a more secure location and easily switch configurations between environments (like development and production).
With our Laravel project created, directories organized, and database configured, we're ready to start building our web application. In the following sections, we'll focus on routing, controllers, and working with Blade templates for our frontend views.
The post A Beginner’s Guide to Setting Up a Project in Laravel appeared first on SitePoint.