Skip to content
Documentation · Integrations

Laravel

Laravel integration with goaop/goaop-laravel-bridge 2.0: auto-discovery, behavior classes from config or tags, and php artisan aop:warmup.

The bridge goaop/goaop-laravel-bridge 2.0 boots the engine early in the application lifecycle, so behaviors are attached to your classes as they load.

  • PHP 8.4 or newer
  • Laravel 12 or 13
  • goaop/framework ^4.0 (behaviors declared with attributes), installed with the bridge
Terminal window
composer require goaop/goaop-laravel-bridge

Package discovery registers the service provider automatically. Publish the configuration if you want to change it:

Terminal window
php artisan vendor:publish --tag=goaop-config
<?php
namespace App\Aspects;
use Go\Aop\Aspect;
use Go\Aop\Intercept\MethodInvocation;
use Go\Lang\Attribute\Around;
use Go\Lang\Attribute\Before;
use Illuminate\Support\Facades\Log;
class LoggingAspect implements Aspect
{
#[Before('execution(public App\Services\**->*(*))')]
public function logMethodCall(MethodInvocation $invocation): void
{
Log::debug('Calling ' . $invocation->getMethod()->getName());
}
#[Around('execution(public App\Services\PaymentService->charge(*))')]
public function measureCharge(MethodInvocation $invocation): mixed
{
$start = hrtime(true);
try {
return $invocation->proceed();
} finally {
Log::info('charge() took ' . (hrtime(true) - $start) / 1e6 . 'ms');
}
}
}

A behavior class is a plain class that implements Go\Aop\Aspect. Its behaviors are methods with #[Before], #[After], #[Around] or #[AfterThrowing].

You never register behavior classes with the engine yourself. During the boot phase, after all service providers have registered and before your application code runs, the bridge collects behavior classes from two sources.

The config list. List the class in config/go_aop.php:

config/go_aop.php
'aspects' => [
App\Aspects\LoggingAspect::class,
],

A listed class is registered as soon as the kernel starts, without loading the class. The Laravel container resolves it on the first call of one of its behaviors. Constructor injection works, and the behavior class may depend on services that only exist once the application has fully booted.

The goaop.aspect tag. Tag a service in one of your service providers:

app/Providers/AppServiceProvider.php
// in register()
$this->app->singleton(LoggingAspect::class, function ($app) {
return new LoggingAspect($app->make(LoggerInterface::class));
});
$this->app->tag([LoggingAspect::class], 'goaop.aspect');

Use the tag when a behavior class needs non-trivial construction, or when a package contributes behavior classes without touching your config. Tagged services are resolved when the bridge’s provider boots, because a Laravel tag can only be enumerated by resolving its services. Prefer the config list for behavior classes with heavy or boot-dependent constructors.

You can combine both sources; duplicates are registered once. A discovered class that doesn’t implement Go\Aop\Aspect fails with a descriptive exception. The Laravel container is attached to the kernel’s container as its PSR-11 delegate, so behaviors and the factories of behavior classes can look up application services through it.

Key options in config/go_aop.php. Each can be overridden through the environment.

Option Default Purpose
debug GOAOP_DEBUG → APP_DEBUG Recompile when sources change; keep it off in production
appDir base_path() Root directory the compiler may touch
cacheDir storage_path('framework/aop') (GOAOP_CACHE_DIR) Where compiled sources and proxies are cached
cacheFileMode 0770 (GOAOP_CACHE_PERMISSIONS, octal digits, e.g. "770") chmod for cache files
parsedFilesLimit 100 (GOAOP_PARSED_FILES_LIMIT) Parsed source files (about 0.2 MB each) kept in memory while compiling; the default of aop:warmup --cached-files
includePaths [app_path()] Only these directories are compiled
excludePaths [] Never compile these
features 0 (GOAOP_FEATURES) Bitmask of Go\Aop\Features flags: INTERCEPT_INCLUDES = 4, PREBUILT_CACHE = 64, PARTIAL_REFLECTION = 256
aspects [] Behavior classes to register

Function and new interception is driven by matching rules and needs no flag. php artisan about shows the current location of the framework’s cache and debug mode.

Terminal window
php artisan aop:warmup

Compilation happens lazily on the first load of each class. Run aop:warmup at deploy time to pre-generate all proxies; it is recommended with debug off. The command exits with a non-zero status when a file cannot be compiled, so a deploy script stops instead of shipping an incomplete cache. Pass --fail-fast to stop at the first error.

Terminal window
php artisan aop:warmup --workers=8
php artisan aop:warmup --cache-dir=/srv/app/releases/next/storage/framework/aop
php artisan aop:warmup --preload
  • --workers=N compiles in parallel worker processes. Each worker is your own artisan, so it boots with the same environment, configuration and behavior classes.
  • --cache-dir=DIR builds the cache of a new release next to the running one. The cache built this way is complete and location independent.
  • --preload writes an opcache.preload script, _preload.php in the cache directory being built, or the file given as --preload=FILE.

To ship compiled scripts with a container image, prime opcache.file_cache after the warm-up and after php artisan optimize, in the final image:

Terminal window
php artisan optimize
mkdir -p /var/cache/opcache && php -d opcache.enable_cli=1 -d opcache.file_cache_only=1 \
-d opcache.file_cache=/var/cache/opcache \
artisan aop:warmup-opcache --file-cache=/var/cache/opcache
Terminal window
composer install --no-dev --optimize-autoloader # runs package:discover, with an empty framework cache
php artisan optimize # the cached configuration carries GOAOP_FEATURES
php artisan aop:warmup

With GOAOP_FEATURES=64 (PREBUILT_CACHE) a request never compiles. A class below the include paths that the cache misses fails with a WeavingException, so run aop:warmup on every deployment. Laravel’s cache commands (package:discover, optimize, optimize:clear, and the config, event, route and view cache and clear commands) serve no request, so they load classes from their original source. That is why the order above works.

Once the configuration is cached, env() is not read any more. Run optimize (or config:cache) with the production environment, GOAOP_FEATURES included.

  • The compile step starts at the beginning of the boot phase. Classes loaded before that (very early bootstrap code, other packages’ register() internals) cannot be compiled.
  • Only classes under appDir and includePaths are considered. Vendor code is not compiled.
  • Exclude exception handlers through excludePaths: when a fatal error occurs, a compiled handler may not be loadable from a cold cache.

© 2026 Alexander Lisachenko