Insights

10 Laravel Folder Structure Best Practices

If you're working with Laravel, it's important to have a good folder structure. Here are 10 best practices to follow.

When it comes to developing applications with the Laravel framework, it is important to have a well-structured folder structure. This helps to keep the code organized and makes it easier to maintain.

In this article, we will discuss 10 best practices for structuring your Laravel project folders. We will look at how to organize your files, how to name your folders, and how to keep your codebase organized. By following these best practices, you can ensure that your project is well-structured and easy to maintain.

1. /app

This folder contains all of the application’s code, including models, controllers, views, and other files. It also houses configuration files for services such as caching, authentication, and routing.

The /app folder is where you should store your custom code, so it’s important to keep this folder organized. For example, if you have a lot of models, create subfolders within the /app/Models directory to organize them by type or purpose. This will make it easier to find what you’re looking for when you need to make changes or debug an issue.

2. /bootstrap

The /bootstrap folder is the first thing that gets loaded when your application starts up. This means that any code you put in this folder will be executed before anything else, making it a great place to store global functions and constants. It’s also a good place for setting up autoloaders and loading environment variables.

By keeping all of your bootstrapping code in one place, you can easily keep track of what’s happening during startup and make sure everything is running smoothly. Plus, if you ever need to debug an issue with your application, having all of your bootstrapping code in one place makes it much easier to find the source of the problem.

3. /config

This folder contains all of the configuration files for your application. This includes database connections, caching settings, and other important information that you need to keep track of.

The /config folder is also where you can store custom configurations for different environments. For example, if you have a development environment and a production environment, you can create separate config files for each one. This allows you to easily switch between them without having to manually change any settings.

Finally, this folder is also where you can store third-party packages’ configuration files. This makes it easy to manage multiple packages at once and ensure they are configured correctly.

4. /database

This folder contains all of the database related files, such as migrations, seeds, and factories. It also contains the configuration file for your database connection.

Having a well-structured /database folder helps keep your code organized and makes it easier to find what you need when working with databases. Additionally, having this folder structure in place allows you to quickly create new migrations, seeders, and factories without having to search through your entire project.

5. /public

This folder is the web root of your application, and it contains all the publicly accessible files. This includes images, CSS, JavaScript, and any other assets that are needed for the front-end of your website.

The /public folder also houses the index.php file, which is the entry point to your Laravel application. It’s important to keep this file secure by making sure it can’t be accessed directly from a browser. You should also make sure that only necessary files are stored in this folder, as anything else could potentially be exposed to the public.

6. /resources

This folder contains all the views, language files, and configuration files that are used to create your application. It’s important to keep this folder organized so you can easily find what you need when you need it.

For example, if you have multiple languages in your application, you should create a separate folder for each language. This way, you can quickly access the correct language file without having to search through a long list of files. Additionally, you should also organize your view files into folders based on their purpose. For instance, you could have one folder for authentication views, another for user profile views, etc.

By organizing your /resources folder properly, you’ll be able to quickly locate the files you need and make changes as needed. This will save you time and help ensure your application runs smoothly.

7. /routes

This folder contains all of the routes for your application. Routes are responsible for mapping URLs to specific controller actions, and they can also be used to define custom logic that should run when a certain URL is requested.

The /routes folder should contain all of the route files for your application. Each file should have a descriptive name that reflects its purpose, such as web.php or api.php. This will make it easier to find the right route file when you need to make changes. Additionally, each route file should be organized in a logical way so that related routes are grouped together.

8. /storage

This folder contains all of the files and folders related to your application’s storage. This includes things like session data, cache files, logs, and more.

The /storage folder is also where you should store any user-uploaded files. By keeping these files in a separate folder, it makes them easier to manage and secure. You can also use Laravel’s built-in file system to easily access and manipulate these files.

Finally, the /storage folder is also used for storing compiled views. This means that when a view is requested, Laravel will check the /storage/framework/views folder first before compiling the view from scratch. This helps improve performance by avoiding unnecessary compilation time.

9. /tests

This folder is where you store all of your automated tests. Automated testing is a great way to ensure that your code works as expected and helps prevent bugs from slipping into production.

The /tests folder should contain two subfolders: /unit and /feature. The /unit folder contains unit tests, which are used to test individual components or functions in isolation. The /feature folder contains feature tests, which are used to test the entire application end-to-end.

By keeping these folders separate, it’s easier to find and run specific tests when needed. It also makes it easier to maintain and update tests over time.

10. /vendor

This folder contains all of the third-party packages and libraries that your application uses. Keeping these files in a separate folder helps keep your codebase organized, as well as making it easier to update or replace any of the packages you’re using.

The /vendor folder should also contain any custom packages you’ve created for your application. This makes it easy to find them when needed, and keeps them from cluttering up the rest of your project’s folders.

Previous

7 SCCM Hardware Inventory Best Practices

Back to Insights
Next

10 Session Cookies Best Practices