The Anatomy of a Laravel Project

Laravel is a popular PHP web framework that makes it easy to build beautiful and powerful web applications. In this article, we'll take a look at the different folders and files that make up a Laravel project. We'll also discuss what to include in a build configuration.

If you’ve read this far, you’re probably a DeployBot user and familiar with version control systems, CI/CD, and other related topics. If not, we’ve compiled several beginner’s guides: Laravel, Digital Ocean, Ruby on Rails, Docker, Craft CMS, Ghost CMS, Google Web Starter Kit, Grunt or Gulp, Slack, Python, Heroku and many more.

Learn how to get started with DeployHQ here.

The Laravel Directory Structure

A Laravel project is typically made up of the following directories:

  • app: This directory contains the core application logic of your project, including controllers, models, and views.
  • bootstrap: This directory contains bootstrap files, such as app.php and bootstrap/app.php, which configure the application and register service providers.
  • config: This directory contains configuration files for various aspects of your application, such as the database connection and caching.
  • database: This directory contains your database files, such as migrations and seeds.
  • public: This directory contains assets that are publicly accessible, such as CSS, JavaScript, and images.
  • resources: This directory contains various application resources, such as views, layouts, and language files.
  • routes: This directory contains your application's routes, which define how URLs are mapped to controllers and actions.
  • storage: This directory contains application storage, such as uploaded files and cached data.
  • tests: This directory contains your application's unit and feature tests.
  • vendor: This directory contains third-party packages that are used by your application.

Build Configuration

A build configuration is a file that contains instructions for building your application for production. This typically includes tasks such as compiling assets, minifying code, and running tests. Laravel provides a variety of tools for building your application, such as npm and gulp.

1. Setting Up Your Environment:

Before we dive in, make sure you have PHP and Composer installed on your machine. Composer is a dependency manager for PHP and helps install Laravel's required packages. You can find installation guides for both PHP and Composer online.

2. Creating a New Project:

Open your terminal and navigate to the directory where you want your project to reside. Then, run the following command:

laravel new project-name

Replace "project-name" with your desired project name. This will create a fresh Laravel installation in a new directory.

3. Configuring the Database:

Laravel interacts with databases to store information. By default, it uses MySQL. Edit the .env file in your project root and configure your database credentials (DB_CONNECTION, DB_HOST, DB_DATABASE, etc.).

4. Starting the Development Server:

Now that everything is set up, fire up the development server using:

php artisan serve

This will start a local server, typically accessible at http://localhost:8000 in your web browser.

5. Building Your First Route and View:

Let's create a simple route to display a welcome message. Open the routes/web.php file and add:

Route::get('/', function () { return view('welcome'); });

This defines a route for the root path (/) that returns a view named "welcome". Create a corresponding Blade template file at resources/views/welcome.blade.php with some basic HTML content.

6. Accessing Your Application:

Visit http://localhost:8000 in your browser. You should see the content from your welcome Blade template! Congratulations, you've built your first Laravel application!

This is just a starting point. Laravel offers a vast ecosystem of features and tools to help you build complex and scalable web applications. Refer to the official Laravel documentation https://laravel.com/docs/11.x/installation for a deeper dive and explore its extensive capabilities.

Conclusion

By understanding the anatomy of a Laravel project, you'll be well on your way to building your own web applications. DeployBot can help you automate the deployment process for your Laravel applications, so you can focus on what you do best: building great software.