Setting Up Your Laravel Project Structure and Essentials

Part of the series Empower and Enhance Your VitePress Blog with a Laravel API

Hello! 👋

I'm thrilled to have you here. This series is a follow-up to Create a Blog with VitePress and Vue.js from Scratch, where we explored setting up a Vitepress blog. Feel free to check it out, although it's not a prerequisite for following this series.

In today's session, we will set up our project from scratch and tour the project structure. By the end of this article, you'll have a solid understanding of the folder organization, key components of a Laravel project, and how to navigate it effectively.

Setting Up the Project

Note

This article does not cover the installation of Laravel. For that, please refer to the official documentation.

One great feature of Laravel is its CLI tool, which helps you create a new project effortlessly. Run the following command:

bash
laravel new mimram --pest # Mimram is the name of the project

Note

Starting with Laravel 12, the laravel new command no longer prompts for the testing framework. Using the --pest flag will automatically set up Pest as the testing framework.

The setup should look like this:

Note

For this article, I'm using version 12.x of Laravel.

txt
   _                               _
  | |                             | |
  | |     __ _ _ __ __ ___   _____| |
  | |    / _` |  __/ _` \ \ / / _ \ |
  | |___| (_| | | | (_| |\ V /  __/ |
  |______\__,_|_|  \__,_| \_/ \___|_|

 ┌ Which starter kit would you like to install? ────────────────┐
 │ › ● None                                                     │
 │   ○ React                                                    │
 │   ○ Vue                                                      │
 │   ○ Livewire                                                 │
 └──────────────────────────────────────────────────────────────┘

Since we have an existing frontend, select the None option.

Next, it will ask about the database our application should use.

txt
 ┌ Which database will your application use? ───────────────────┐
 │ › ● SQLite                                                   │
 │   ○ MySQL                                                    │
 │   ○ MariaDB                                                  │
 │   ○ PostgreSQL                                               │
 │   ○ SQL Server (Missing PDO extension)                       │
 └──────────────────────────────────────────────────────────────┘

For this project, let's choose SQLite.

SQLite is a C library that directly interacts with a single database file on disk, making it portable and low-maintenance. While it may not fit every use case, it's an excellent choice for a simple blog that won't experience heavy traffic.

Note

To learn more about SQLite, I recommend High Performance SQLite by Aaron Francis. It's an excellent series for mastering SQLite.

Finally, say yes when asked if you want to install the JavaScript dependencies and build the assets.

Perfect! We now have a fresh Laravel project to start building our API.

Project Structure

Here's the project structure we will be working with:

    Laravel Project Structure

  • app
  • bootstrap
  • app.php
  • config
  • app.php
  • database.php
  • mail.php
  • database
  • public
  • index.php
  • resources
  • routes
  • web.php
  • api.php
  • storage
  • tests

Let's dive into all the folders and files.

app

The app folder houses the core code of your application, including controllers, models, and services. It's the heart of your project and where you'll spend most of your time coding.

bootstrap

The bootstrap folder contains files that initialize the application. It includes the app.php file responsible for loading the project.

The folder also holds the providers file, which loads all service providers. A service provider registers additional services, routes, and extends Laravel through macros and helpers. Every Laravel package has a service provider as its entry point. All your service providers live in the app/Providers folder.

config

The config folder contains all the configuration files. Here, you can find settings for the database, mail, cache, and more.

database

The database folder stores database migrations, model factories, and seeds. Migrations version-control your database schema. Model factories generate large amounts of test data conveniently, and seed files populate your database with test data using model factories.

Model factories relate to models located in the app/Models folder.

node_modules

The node_modules folder keeps all JavaScript dependencies. It’s automatically generated by npm or yarn when you install dependencies.

public

The public folder stores the front controller and assets like JavaScript, CSS, and images. It’s served by the web server. The index.php file, the application’s entry point, routes all requests. Only this file is publicly accessible, with other files loaded by index.php.

resources

The resources folder contains view files, raw assets like CSS and JavaScript, and views. In Laravel, views are specialized PHP files called Blade templates. Blade is a templating engine with an elegant syntax, allowing for PHP code and directives like @if and @foreach in your views.

Raw assets are compiled by Vite and stored in the public/build folder.

routes

The routes folder holds all application routes. It includes web routes, API routes, console routes, and more. Each file manages specific route types, with web.php handling user-facing routes and api.php managing API routes.

storage

The storage folder contains compiled Blade templates, file-based sessions, caches, and framework-generated files. It’s divided into:

  1. app: Application-generated files.
  2. framework: Framework-generated files.
  3. logs: Application log files.

Look for the laravel.log file in storage/logs for error logs and stack traces.

tests

The tests folder houses automated tests. Laravel includes Pest, a testing framework built on PHPUnit, for writing unit and feature tests. Testing ensures your application behaves as expected and prevents breaking changes.

Throughout this series, we will write tests for our application, showcasing Laravel's ease of use in test writing.

vendor

The vendor folder holds all PHP dependencies, automatically generated by Composer. It is akin to the node_modules folder for JavaScript but for PHP.

Root Files

Here’s a list of files in the project root:

.editorconfig

The .editorconfig file helps maintain consistent coding styles for multiple developers across editors and IDEs. It defines styles like indentation, line endings, and character encoding.

.env

The .env file contains environment-specific configuration variables, configuring the application without code changes. It includes sensitive details like database credentials and API keys.

.env.example

The .env.example file serves as a template for the .env file, offering a sample configuration to create a new .env file. Include this file in version control to aid fellow developers setting up their environments.

.gitattributes

The .gitattributes file defines attributes for paths in your repository, controlling Git’s handling of line endings, merge strategies, and other behaviors for specific files or directories.

.gitignore

The .gitignore file specifies files and directories Git should ignore, preventing sensitive data and unnecessary files from being committed.

README.md

The README.md file provides a project overview, covering setup, usage, and other essential details. It’s often the first reference for new developers on the project.

artisan

The artisan file is Laravel's command-line interface, providing commands for developing your application, running migrations, generating code, and executing tests.

composer.json

The composer.json file is the Composer configuration file, outlining PHP project dependencies and metadata like the project name and description. It enables Composer to manage dependencies.

This file includes a scripts key for running composer run commands. For instance, composer run test executes application tests. Among these scripts, the dev script launches development necessities:

bash
npx concurrently -c \"#93c5fd,#c4b5fd,#fb7185,#fdba74\" \"php artisan serve\" \"php artisan queue:listen --tries=1\" \"php artisan pail --timeout=0\" \"npm run dev\" --names=server,queue,logs,vite

Laravel is more than a simple script; multiple processes must run for development, ensuring everything functions correctly. The web server handles requests, while the Vite server serves assets, and the queue manages jobs and logs in the terminal.

The composer run dev command launches these processes in parallel using the concurrently npm package.

composer.lock

The composer.lock file, generated by Composer, locks dependencies to specific versions, ensuring consistency during setup. Commit this file to version control.

phpunit.xml

The phpunit.xml file configures PHPUnit, Laravel's testing framework, detailing test suite settings, bootstrap file, and options.

vite.config.js

The vite.config.js file configures Vite, Laravel’s frontend build tool, defining frontend asset building and bundling settings like JavaScript, CSS, and resources.

package.json

The package.json file is the npm configuration file, outlining JavaScript project dependencies and metadata, used by npm for dependency management.

package-lock.json

The package-lock.json file, generated by npm, locks dependencies to specific versions, ensuring consistent installation across setups. Commit this file to version control.

Final Words

The first episode may feel boring but it’s essential.

You now have a clearer understanding of a Laravel project and its files structure. Familiarity with this structure is crucial because you'll navigate through it all the time. Initially intimidating, it becomes powerful and flexible once you understand it.

Next episode, we'll set up essentials like formatting, linting, and automatic refactoring. Expect more excitement ahead.

PP

Thanks for reading! My name is Estéban, and I love to write about web development.

I've been coding for several years now, and I'm still learning new things every day. I enjoy sharing my knowledge with others, as I would have appreciated having access to such clear and complete resources when I first started learning programming.

If you have any questions or want to chat, feel free to comment below or reach out to me on Bluesky, X, and LinkedIn.

I hope you enjoyed this article and learned something new. Please consider sharing it with your friends or on social media, and feel free to leave a comment or a reaction below—it would mean a lot to me! If you'd like to support my work, you can sponsor me on GitHub!

Continue readingPint, Rector, Larastan, and Pest: Essentials for Success

Reactions

Discussions

Add a Comment

You need to be logged in to access this feature.