Laravel is an elegant PHP framework designed to simplify the development of modern web applications. When you create a new Laravel project, it comes with a well-organized directory structure that encourages best practices and a clear separation of concerns. Let’s dive into the main directories and their purposes.
A fresh Laravel installation contains the following key directories:
project-root/ |-- app/ |-- bootstrap/ |-- config/ |-- database/ |-- public/ |-- resources/ |-- routes/ |-- storage/ |-- tests/ |-- vendor/
This is the heart of your application where most of your logic resides. The app/ directory contains subfolders for various application components:
Console/: Contains custom Artisan commands.
Events/: Defines events that your application can listen for.
Exceptions/: Handles exceptions and custom error reporting.
Http/:
Controllers/: Houses your application’s controllers.
Middleware/: Stores middleware for filtering HTTP requests.
Models/: The default location for your Eloquent models.
Observers/: Holds observer classes for handling model events.
Policies/: Contains authorization logic for specific models.
Providers/: Stores service providers that bootstrap various parts of the application.
This directory contains the application’s initial bootstrap files. The most notable file here is app.php, which initializes the Laravel framework.
Additionally, it contains a cache/ folder for storing framework-generated cache files to improve performance.
As the name suggests, this directory contains all configuration files for your application. These files allow you to manage settings for services, database connections, mail, caching, and more. For example:
app.php for core application settings.
database.php for database configurations.
mail.php for email service settings.
This directory manages all database-related files:
factories/: Contains factory classes for seeding test data.
migrations/: Stores migration files for defining your database schema.
seeders/: Houses seeders for populating your database with sample data.
This directory is the entry point for web requests and contains the front-facing files of your application. Key contents include:
index.php: The primary file that handles all incoming requests.
Static assets such as images, CSS, and JavaScript files.
This directory stores application resources that will be compiled or used to generate views. Subdirectories include:
views/: Contains Blade template files for your application’s front-end.
lang/: Holds language files for localization.
css/ and js/ (inside sass/ and js/): Default folders for your CSS and JavaScript assets.
This is where your application’s routing logic resides. Laravel provides separate files for different route types:
web.php: Handles routes for web requests (usually with sessions and cookies).
api.php: Handles routes for API requests (stateless).
console.php: Defines custom Artisan commands.
channels.php: Defines event broadcasting channels.
This directory contains files generated by your application and the framework itself:
app/: Stores application-generated files.
framework/: Stores framework caches, sessions, and compiled views.
logs/: Contains log files for debugging and error tracking.
All application tests are stored here. By default, it includes directories for feature and unit tests:
Feature/: Tests that evaluate the functionality of the entire application stack.
Unit/: Tests that validate specific functions or classes.
This directory contains all the third-party packages installed via Composer. You should never modify this folder directly.
When developing in Laravel, understanding the purpose of each directory helps you organize your code logically. For example:
Place business logic in models within app/Models.
Write custom routes in routes/web.php or routes/api.php.
Keep reusable components, such as layouts, in resources/views.
Follow Laravel conventions: Laravel has a predefined structure for a reason—stick to it unless absolutely necessary.
Use subfolders for scalability: For larger projects, consider creating subfolders within Controllers or Models to group related files.
Leverage artisan commands: Use commands like php artisan make:model, php artisan make:controller, etc., to automatically place files in the correct locations.
By mastering Laravel’s directory structure, you’ll gain the confidence to navigate, maintain, and scale your projects effectively. Stay tuned for Day 4, where we’ll dive into configuring Laravel to suit your development needs!
The above is the detailed content of Day Exploring Laravel's Directory Structure. For more information, please follow other related articles on the PHP Chinese website!