Getting Started with Laravel 11: Installation, Configuration and Directory Structure

Welcome to our in-depth Laravel 11 tutorial! In this episode, we're diving into the installation, configuration, and directory structure of Laravel 11. Whether you're new to web development or a seasoned professional, Laravel offers a powerful and elegant framework to help you build your applications. 🌟
In this comprehensive tutorial, we’ll explore the essentials of getting started with Laravel 11. Laravel is a popular PHP web framework known for its elegant syntax, rich feature set, and vibrant community. Whether you’re a novice learning the ropes or an experienced developer, this guide will help you understand the ins and outs of Laravel setup, configuration, and its directory structure.
Why Choose Laravel?
Laravel is often described as a "progressive" framework, meaning it grows with you. With Laravel, you get the best of both worlds: a simple learning curve for beginners and powerful tools for advanced developers. Key features include:
- Expressive Syntax: Making your code not only functional but also beautiful.
- Robust Tools: For dependency injection, unit testing, queues, and real-time events.
- Scalability: Built-in support for fast, distributed cache systems like Redis.
- Community Support: Thousands of developers contribute to and support the Laravel ecosystem.
Installation
Before diving into Laravel, ensure your local machine has PHP and Composer installed.
Step 1: Installing Composer
Composer is a dependency manager for PHP that you need to manage the Laravel framework.
# Downloading and installing Composer
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
mv composer.phar /usr/local/bin/composer
Step 2: Creating a New Laravel Project
You can create a new project using Composer
:
# Create new Laravel project using Composer
composer create-project laravel/laravel example-app
Or, via the globally installed Laravel installer:
# Globally install the Laravel installer
composer global require laravel/installer
# Create a new Laravel project
laravel new example-app
Step 3: Running the Laravel Development Server
Navigate into your project directory and start the server:
# Navigate to your project directory
cd example-app
# Start the development server
php artisan serve
Your Laravel application will now be accessible at http://localhost:8000
.
Initial Configuration
The configuration files for Laravel are stored in the config
directory. Most configurations come pre-set but you may need to adjust some according to your requirements.
Environment-Based Configuration
Environment-based configurations are stored in the .env
file located at the root of your application. This file should not be committed to source control and is used to handle different configurations for local and production environments.
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:YOUR_KEY_HERE
APP_DEBUG=true
APP_URL=http://localhost
Working with Databases
Laravel supports a variety of database drivers including MySQL, PostgreSQL, and SQLite. If you want to use MySQL, update your .env
configuration file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
Run your database migrations to create the necessary tables:
# Running database migrations
php artisan migrate
Docker Installation Using Sail
Laravel Sail provides a great starting point using Docker:
On macOS:
# Create a new Laravel project
curl -s "https://laravel.build/example-app" | bash
# Navigate and start Sail
cd example-app
./vendor/bin/sail up
On Windows:
Ensure you have Docker Desktop and WSL2 installed. Run the following from Windows Terminal:
# Create a new Laravel project
curl -s https://laravel.build/example-app | bash
# Navigate and start Sail
cd example-app
./vendor/bin/sail up
On Linux:
If Docker Compose is already installed:
# Create a new Laravel project
curl -s https://laravel.build/example-app | bash
# Navigate and start Sail
cd example-app
./vendor/bin/sail up
Choosing Your Sail Services
When creating a new Laravel application via Sail, you can specify which services to include:
# Specify services in the create command
curl -s "https://laravel.build/example-app?with=mysql,redis" | bash
Directory Structure
Understanding the directory structure of Laravel is crucial for organizing and managing your project effectively. Here's a quick overview:
- Root Directory: Contains composer.json, .env file, and the public directory.
- app Directory: Houses core code including models, controllers, and middleware.
- bootstrap Directory: Contains files for bootstrapping the framework.
- config Directory: All configuration files.
- database Directory: Contains database migrations, model factories, and seeds.
- public Directory: The entry point for all requests, contains assets like images and JavaScript.
- resources Directory: Views, raw assets such as CSS and JavaScript.
- routes Directory: Definitions for application routes.
- storage Directory: Logs, compiled templates, etc.
- tests Directory: Automated tests.
- vendor Directory: Composer dependencies.
For a more detailed understanding, refer to the Laravel Directory Structure Documentation.
Next Steps
By now, you should have a fully functional Laravel development environment. Here are the used resources to further your Laravel knowledge
In conclusion, Laravel 11 provides a robust framework for building web applications with ease and elegance. Its progressive nature means it grows with you, offering a vast array of tools and a vibrant community to support your development journey.
Discuss Your Project with Us
We're here to help with your web development needs. Schedule a call to discuss your project and how we can assist you.
Let's find the best solutions for your needs.