Laravel - Application Structure
Laravel - Application Structure
Section titled “Laravel - Application Structure”A standard Laravel application has a well-organized directory structure designed to be intuitive for both small and large applications. Here’s an overview of the key directories:
Root Directory
Section titled “Root Directory”The root directory of a fresh Laravel installation contains several important folders and files:
app- This is where the core code of your application lives, including models, controllers, service providers, and more. You’ll spend most of your time in this directory.bootstrap- Contains files that bootstrap the framework and configure autoloading. Thecachesub-directory is used for framework-generated cache files for performance optimization.config- Holds all of your application’s configuration files. It’s a good idea to familiarize yourself with these files and their available options.database- Contains your database migrations, model factories, and seeds. Migrations allow you to version control your database schema.public- This is the document root for your application and the only directory that should be publicly accessible from your web server. It contains the entry pointindex.phpand your compiled assets (like CSS, JavaScript, images) managed by tools like Vite.resources- Contains your views (Blade templates), raw uncompiled assets (like JavaScript/CSS source files if not using Vite’s default structure), and language files for localization.routes- All route definitions for your application are here. It includesweb.php(for web interface routes),api.php(for API routes),console.php(for custom Artisan commands), andchannels.php(for event broadcasting channels).storage- Contains compiled Blade templates, file-based sessions, file caches, application-generated logs, and other files generated by the framework. Thestorage/app/publicdirectory can be used to store user-generated files, like avatars, that should be publicly accessible (after runningphp artisan storage:link).tests- Contains your automated tests, including unit tests and feature tests. PHPUnit is the default testing framework.vendor- Contains your Composer dependencies. You typically won’t modify files in this directory directly..env- (Environment file) This file, at the root of your project, contains environment-specific configurations like database credentials, API keys, and application settings. It’s not committed to version control.
The app Directory
Section titled “The app Directory”The app directory is the heart of your application. It’s namespaced under App by default and is autoloaded by Composer using the PSR-4 autoloading standard. Here’s a breakdown of some common sub-directories within app:
Console- Contains your custom Artisan commands. TheKernel.phpfile in this directory is where your custom commands are registered.Events- Stores event classes. Events can be used to decouple various aspects of your application, allowing other parts to react when a specific action occurs.Exceptions- Contains your application’s custom exception handler (Handler.php) and is a good place to put any custom exception classes.Http- This is where your controllers, middleware, and form requests reside.-
Controllers- Handle incoming HTTP requests and return responses.
-
Middleware- Provide a convenient mechanism for filtering HTTP requests entering your application (e.g., authentication, CSRF protection).
-
Requests- (Often called Form Requests) Custom request classes that encapsulate validation and authorization logic.
Jobs- Contains queueable jobs for your application. Jobs can be pushed onto a queue to be processed asynchronously in the background, improving application responsiveness.Listeners- Contains listener classes that handle events. Listeners receive an event instance and perform logic in response to that event.Mail- Contains your mailable classes. Each class represents an email that can be sent by your application, often using Blade templates for the email content.Models- Contains your Eloquent model classes. Each model typically represents a database table and allows you to interact with that table’s data.Notifications- Stores notification classes for sending short informational messages across various channels (e.g., email, Slack, SMS).Policies- Contains authorization policy classes. Policies organize authorization logic around a particular model or resource.Providers- Contains your application’s service providers. Service providers are central to bootstrapping a Laravel application, registering service container bindings, event listeners, middleware, and even routes.Rules- A good place to store custom validation rule objects.
Understanding this structure will help you navigate and build Laravel applications more effectively. While Laravel provides this default structure, you are free to organize your application differently if it suits your needs, as long as Composer can autoload your classes.