Setting Up Your Laravel Project Structure and Essentials
Part of the series Empower and Enhance Your VitePress Blog with a Laravel API
Welcome to the Laravel part of this series! 👋
In the previous series, we set up a VitePress blog in Create a Blog with VitePress and Vue.js from Scratch. Now we are ready to build the Laravel API that will power its interactive features. You do not need to have read that series, but it provides useful context for what follows.
In this first article, we will create the Laravel project, explore its structure, and identify where the code we write throughout this series will live. By the end, you should feel comfortable finding your way around the application.
Setting Up the Project
Note
This article does not cover installing Laravel itself. For that, refer to the official documentation.
One of Laravel's strengths is its CLI, which helps you create a new project quickly. Run the following command:
laravel new mimram --pest # Mimram is the name of the projectNote
Starting with Laravel 12, the laravel new command no longer prompts for the testing framework. The --pest flag sets up Pest automatically.
The setup should look like this:
Note
For this article, I'm using version 12.x of Laravel.
_ _
| | | |
| | __ _ _ __ __ ___ _____| |
| | / _` | __/ _` \ \ / / _ \ |
| |___| (_| | | | (_| |\ 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.
┌ Which database will your application use? ───────────────────┐
│ › ● SQLite │
│ ○ MySQL │
│ ○ MariaDB │
│ ○ PostgreSQL │
│ ○ SQL Server (Missing PDO extension) │
└──────────────────────────────────────────────────────────────┘For this project, choose SQLite.
SQLite is a C library that stores data in a single database file on disk, making it portable and low-maintenance. It may not fit every use case, but it is an excellent choice for a simple blog that will not experience heavy traffic.
Note
To learn more about SQLite, I recommend High Performance SQLite by Aaron Francis. It is an excellent series for mastering SQLite.
Finally, say yes when asked whether you want to install the JavaScript dependencies and build the assets.
Perfect! We now have a fresh Laravel project in which to build our API.
Project Structure
Here's the project structure we will be working with:
- app
- Http/
- Models/
- Providers/
- bootstrap
- app.php
- cache/
- config
- app.php
- database.php
- mail.php
- database
- migrations/
- factories/
- seeders/
- public
- index.php
- css/
- js/
- resources
- views/
- css/
- js/
- routes
- web.php
- api.php
- storage
- app/
- framework/
- logs/
- tests
- Feature/
- Unit/
- vendor
Laravel Project Structure
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 is the heart of the project and where you will spend most of your time coding.
bootstrap
The bootstrap folder contains files that initialize the application. It includes app.php, which creates and configures the application.
The folder also contains the providers.php file, which loads the service providers. A service provider registers services, routes, and Laravel extensions such as macros and helpers. Every Laravel package has a service provider as its entry point, and your application providers live in app/Providers.
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 seeders. Migrations version-control your database schema. Model factories generate test data conveniently, and seeders populate your database, often by using those factories.
Model factories relate to models located in the app/Models folder.
node_modules
The node_modules folder contains the JavaScript dependencies installed by npm, pnpm, or yarn. It is generated automatically and should not be committed to version control.
public
The public folder stores the front controller and public assets such as JavaScript, CSS, and images. It is the directory served by the web server. The index.php file is the application's entry point and routes incoming requests to Laravel.
resources
The resources folder contains view files and raw assets such as CSS and JavaScript. In Laravel, views are PHP files called Blade templates. Blade provides an elegant syntax for PHP code and directives such as @if and @foreach.
Raw assets are compiled by Vite and stored in the public/build folder.
routes
The routes folder holds the application's routes. It includes web routes, API routes, and console routes. Each file manages a specific type of route: web.php handles browser-facing routes, while api.php handles API routes.
storage
The storage folder contains compiled Blade templates, file-based sessions, caches, logs, and other framework-generated files. It is divided into:
app: Application-generated files.framework: Framework-generated files.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 that your application behaves as expected and helps prevent regressions.
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 values, allowing you to configure the application without changing its code. It may include sensitive details such as database credentials and API keys.
.env.example
The .env.example file is a template for .env, providing the values needed to create a local configuration. Commit this file to version control so other developers know which variables are available.
.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. It provides commands for developing the application, running migrations, generating code, and executing tests.
composer.json
The composer.json file is Composer's configuration file. It lists PHP dependencies and project metadata such as the project name and description.
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:
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,viteLaravel is more than a single process during development. The web server handles requests, the Vite server serves assets, and the queue and log processes handle their respective tasks.
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 and makes installations reproducible. Commit this file to version control.
phpunit.xml
The phpunit.xml file configures PHPUnit, including its test suites, bootstrap file, and environment options.
vite.config.js
The vite.config.js file configures Vite, Laravel's frontend build tool, including the JavaScript and CSS entry points.
package.json
The package.json file is the npm configuration file. It lists JavaScript dependencies and scripts used by the project.
package-lock.json
The package-lock.json file, generated by npm, locks JavaScript dependencies to specific versions. Commit it to version control when using npm.
Final Words
This first step gives us the foundation for the rest of the series. You now know where Laravel keeps its application code, configuration, database files, routes, and tests. As we add features, these folders will become familiar.
Next, we will make that foundation easier to maintain by configuring formatting, automatic refactoring, static analysis, and testing.
Thanks for reading! My name is Estéban, and I love to write about web development and the human journey around it.
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!
Discussions
Add a Comment
You need to be logged in to access this feature.