Middlewares
Apiato middlewares are just Laravel Middlewares, and they function in the exact same way as Laravel middlewares. However, they come with additional rules and conventions specific to Apiato.
To generate new middlewares
you may use the apiato:generate:middleware
interactive command:
php artisan apiato:generate:middleware
Rulesβ
- All container-specific Middlewares:
- MUST be placed in the
app/Containers/{Section}/{Container}/Middlewares
directory. - MUST be registered in their respective container's
App\Containers\{Section}\{Container}\Providers\MiddlewareServiceProvider
class.
- MUST be placed in the
- All general Middlewares:
- MUST be placed in the
app/Ship/Middlewares
directory. - MUST be registered in the
App\Ship\Kernels\HttpKernel
class.
- MUST be placed in the
- All non-Laravel or third-party package Middlewares MUST extend the
App\Ship\Parents\Middlewares\Middleware
class.- The parent extension SHOULD be aliased as
ParentMiddleware
.
- The parent extension SHOULD be aliased as
Folder Structureβ
The highlighted sections showcase middleware registration points:
app
βββ Containers
β βββ Section
β βββ Container
β βββ Middlewares
β β βββ DemoMiddleware.php
β β βββ ...
β βββ Providers
β βββ MiddlewareServiceProvider.php
β βββ ...
βββ Ship
βββ Kernels
β βββ HttpKernel.php
βββ Middlewares
βββ AnotherMiddleware.php
βββ ...
Code Exampleβ
Middlewares are defined exactly as you would define them in Laravel.
Registering Middlewareβ
The registration process for a middleware varies depending on its intended scope within the application. Different places are designated for different levels of middleware usage.
In essence, the decision of where to register a middleware boils down to two key factors: the scope of middleware usage and the logical location for its registration.
Container Middlewaresβ
If a middleware usage is specific to a container,
it must be registered in the App\Containers\{Section}\{Container}\Providers\MiddlewareServiceProvider
class.
use ...
use App\Ship\Parents\Providers\MiddlewareServiceProvider as ParentMiddlewareServiceProvider;
class MiddlewareServiceProvider extends ParentMiddlewareServiceProvider
{
protected array $middlewares = [];
protected array $middlewareGroups = [];
protected array $middlewarePriority = [];
protected array $middlewareAliases = [];
}
To generate a middleware service provider
you may use the apiato:generate:provider
interactive command:
php artisan apiato:generate:provider
Remember to also register the MiddlewareServiceProvider
in the container's MainServiceProvider
:
use ...
use App\Ship\Parents\Providers\MainServiceProvider as ParentMainServiceProvider;
class MainServiceProvider extends ParentMainServiceProvider
{
protected array $serviceProviders = [
// ... Other service providers
MiddlewareServiceProvider::class,
];
}
General Middlewaresβ
General middlewares must be registered in the App\Ship\Kernels\HttpKernel
class.
Third Party Middlewaresβ
When dealing with third-party packages that require middleware registration in the App\Ship\Kernels\HttpKernel
class,
you should follow these guidelines:
-
Specific Container Usage: If the package is used within a particular container, register its middleware in that container
App\Containers\{Section}\{Container}\Providers\MiddlewareServiceProvider
class. -
Framework-wide Usage: If the package is generic and used throughout the entire application, you can register its middleware in the
App\Ship\Kernels\HttpKernel
class.
Middleware Registration Flowβ
If you want to understand the middleware registration process, here is a breakdown of the registration flow.
Consider the following folder structure:
app
βββ Containers
β βββ Section
β βββ Container
β βββ Middlewares
β β βββ DemoMiddleware.php ββΊββ
β β βββ ... β
β βββ Providers βΌ
β βββ MiddlewareServiceProvider.php ββββββΊββββββ
β βββ MainServiceProvider.php ββregisteredβinβββ
β βββ ...
βββ Ship
βββ Kernels
β βββ HttpKernel.php ββregisteredβinβββ
β βββ ... β
βββ Middlewares β
βββ AnotherMiddleware.php ββββββΊβββββ
βββ ...
The following diagram illustrates the registration flow of middlewares in the above folder structure: