Directory Structure
Introduction
The default Laravel Hyperf application structure follows by Laravel. It can provide you a great starting point for both large and small applications. You're free to organize your application however you like. But some directories such as bootstrap
, config
, database
and runtime
should remain due to the core design in Hyperf.
Here's how you directory structure looks like:
├── app
│ ├── Console
│ │ └── Commands
│ ├── Events
│ ├── Exceptions
│ ├── Http
│ │ ├── Controllers
│ │ ├── Middleware
│ │ └── Requests
│ ├── Listeners
│ ├── Models
│ └── Providers
├── bin
├── bootstrap
├── config
├── database
│ ├── factories
│ ├── migrations
│ └── seeders
├── lang
│ ├── en
│ ├── zh_CN
│ └── zh_TW
├── routes
├── runtime
│ └── container
├── storage
│ ├── app
| ├── framework
│ └── logs
└── tests
├── Feature
└── Unit
The Root Directory
The App Directory
The app
directory contains the core code of your application. We'll explore this directory in more detail soon; however, almost all of the classes in your application will be in this directory.
The Bootstrap Directory
The bootstrap
directory contains the app.php
file which bootstraps the framework.
The Bin Directory
The bin
directory contains the hyperf.php
file which defines BASE_PATH
and running application loaded from bootstrap.app
.
The Runtime Directory
This directory stores files generated by Hyperf during runtime. It houses a container
directory which contains framework generated files for caching the scanning result.
The Config Directory
The config
directory, as the name implies, contains all of your application's configuration files. It's a great idea to read through all of these files and familiarize yourself with all of the options available to you.
Warning
There's additional autoload
directory inside config
in Hyperf. This folder is removed in Laravel Hyperf. All the config files are flattened as Laravel does.
hyperf.php
is preserved for the compatibility of root configs in Hyperf. All the configs here will not include any prefixes. And all the configs in config/config.php
are moved to config/app.php
in Laravel Hyperf.
The Database Directory
The database
directory contains your database migrations, model factories, and seeds. If you wish, you may also use this directory to hold an SQLite database.
The Resources Directory
The resources
directory contains your views as well as your raw, un-compiled assets such as CSS or JavaScript.
The Lang Directory
The lang
directory contains the translation files. Within this directory, there may be subdirectories for each language supported by the application. This is the approach Laravel Hyperf uses to manage translation strings for built-in features such as validation error messages.
The Routes Directory
The routes
directory contains all of the route definitions for your application. By default, three route files are included with Laravel Hyperf: web.php
, api.php
and console.php
.
The web.php
file contains routes that serves common requests such as views, files in Laravel Hyperf.
The api.php
file contains routes that are intended to be stateless, so requests entering the application through these routes are intended to be authenticated and will not have access to session state.
Info
Middleware groups are applied to web.php
and api.php
, but are kept in empty by default. You can configure them manually if in need.
The console.php
file is where you may define all of your closure based console commands. Each closure is bound to a command instance allowing a simple approach to interacting with each command's IO methods. Even though this file does not define HTTP routes, it defines console based entry points (routes) into your application. You may also schedule tasks in the console.php
file.
The Storage Directory
The storage
directory contains your logs, compiled Blade templates, file caches, and other files generated by the framework. This directory is segregated into app
, framework
, and logs
directories. The app
directory may be used to store any files generated by your application. The framework
directory is used to store caches (and framework generated files in the future). Finally, the logs
directory contains your application's log files.
The storage/app/public
directory may be used to store user-generated files, such as profile avatars, that should be publicly accessible. You should create a symbolic link at public/storage
which points to this directory.
The Tests Directory
The tests
directory contains your automated tests. Example Pest or PHPUnit unit tests and feature tests are provided out of the box. Each test class should be suffixed with the word Test
. You may run your tests using the /vendor/bin/pest
or /vendor/bin/phpunit
commands.
Note
Pest package is not installed by default. You can install it by running:
composer require pestphp/pest --dev
The App Directory
The majority of your application is housed in the app
directory. By default, this directory is namespaced under App
and is autoloaded by Composer using the PSR-4 autoloading standard.
The app
directory contains a variety of additional directories such as Console
, Http
, and Providers
. Think of the Console
and Http
directories as providing an API into the core of your application. The HTTP protocol and CLI are both mechanisms to interact with your application, but do not actually contain application logic. In other words, they are two ways of issuing commands to your application. The Console
directory contains all of your Artisan commands, while the Http
directory contains your controllers, middleware, and requests.
Note
Many of the classes in the app
directory can be generated by Hyperf via commands. To review the available commands, run the php hyperf list make
command in your terminal.
The Console Directory
The Console
directory contains all of the custom Hyperf commands for your application. These commands may be generated using the make:command
command. This directory also houses your console kernel, which is where your custom Hyperf commands are registered and your scheduled tasks are defined.
The Events Directory
The Events
directory houses event classes. Events may be used to alert other parts of your application that a given action has occurred, providing a great deal of flexibility and decoupling.
The Exception Directory
The Exceptions
directory contains your application's exception handler and is also a good place to place any exceptions thrown by your application. If you would like to customize how your exceptions are logged or rendered, you should modify the Handler class in this directory.
The Http Directory
The Http
directory contains your controllers, middleware, and form requests. Almost all of the logic to handle requests entering your application will be placed in this directory.
The Listeners Directory
The Listeners
directory contains the classes that handle your events. Event listeners receive an event instance and perform logic in response to the event being fired. For example, a UserRegistered
event might be handled by a SendWelcomeEmail
listener. If you execute the make:listener
command, listener classes will be placed in this directory.
The Models Directory
The Models
directory contains all of your Eloquent model classes. The Eloquent ORM included with Laravel Hyperf provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.
The Providers Directory
The Providers
directory contains all of the service providers for your application. Service providers bootstrap your application by binding services in the service container, registering events, or performing any other tasks to prepare your application for incoming requests.
In a fresh Laravel Hyperf application, this directory will already contain several providers. You are free to add your own providers to this directory as needed.