Yii 2 SDK
The Nadi Yii 2 SDK provides integration with Yii 2 applications, automatically capturing exceptions and providing Yii 2-specific context.
Requirements
- PHP 8.1 or higher
- Yii 2.0.45+
- Composer
Installation
Install the package via Composer:
composer require nadi-pro/nadi-yii2Component Configuration
Add the Nadi component to your web application config in config/web.php:
// config/web.php
return [
'bootstrap' => [
// ... other bootstrap components
'nadi',
],
'components' => [
// ... other components
'nadi' => [
'class' => \Nadi\Yii2\NadiComponent::class,
'enabled' => true,
'driver' => 'log',
],
],
];For console applications, add the same configuration to config/console.php:
// config/console.php
return [
'bootstrap' => [
// ... other bootstrap components
'nadi',
],
'components' => [
'nadi' => [
'class' => \Nadi\Yii2\NadiComponent::class,
'enabled' => true,
'driver' => 'log',
],
],
];Configuration
Add the following to your .env:
NADI_ENABLED=true
NADI_DRIVER=logCredentials in nadi.yaml
API credentials (apiKey and appKey) are configured in runtime/nadi/nadi.yaml for the Shipper agent, not in .env. See Shipper Configuration for details.
Additional configuration options on the component:
'nadi' => [
'class' => \Nadi\Yii2\NadiComponent::class,
'enabled' => true,
'driver' => 'log',
'scrubFields' => [
'password',
'password_confirmation',
'credit_card',
'cvv',
'ssn',
'api_key',
'secret',
],
],Shipper Setup
The shipper binary monitors runtime/nadi/ for log files and forwards them to the Nadi API. Set up Supervisord to run the shipper as a background process.
Create a supervisor config file:
Supervisor Config Path
The supervisor config path and file extension vary by OS:
| OS | Path | Extension |
|---|---|---|
| Ubuntu / Debian | /etc/supervisor/conf.d/ | .conf |
| AlmaLinux / CentOS / RHEL / Fedora | /etc/supervisord.d/ | .ini |
| macOS (Homebrew) | /opt/homebrew/etc/supervisor.d/ | .conf |
sudo nano /etc/supervisor/conf.d/nadi-shipper.confAdd the configuration:
[program:nadi-shipper-your-app]
process_name=%(program_name)s
command=/path/to/project/vendor/bin/shipper --config=/path/to/project/runtime/nadi/nadi.yaml
directory=/path/to/project
autostart=true
autorestart=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/path/to/project/runtime/logs/shipper.log
stdout_logfile_maxbytes=10MB
stdout_logfile_backups=3
stopwaitsecs=3600Naming Convention
Use kebab-case for both the config filename and program name. Avoid repeated dashes.
Good: nadi-shipper-my-app.conf, [program:nadi-shipper-my-app]Bad: nadi-shipper--my-app.conf, [program:nadi-shipper--my-app]
Apply the configuration:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start nadi-shipper-your-appBasic Usage
Automatic Exception Capturing
Once installed, Nadi automatically captures all unhandled exceptions. No additional code is required.
// This exception will be automatically captured
throw new \Exception('Something went wrong');Manual Exception Capturing
You can also capture exceptions manually:
try {
// Your code
} catch (\Exception $e) {
\Yii::$app->nadi->captureException($e);
// Handle the exception
}Capturing Messages
Log messages without an exception:
\Yii::$app->nadi->captureMessage('User performed an important action', 'info');Available levels: debug, info, warning, error, fatal
Adding Context
User Context
Identify the current user:
\Yii::$app->nadi->setUser([
'id' => \Yii::$app->user->id,
'email' => \Yii::$app->user->identity->email,
'name' => \Yii::$app->user->identity->name,
]);Tags
Add tags for filtering:
\Yii::$app->nadi->setTag('subscription', 'premium');
\Yii::$app->nadi->setTags([
'feature' => 'checkout',
'version' => '2.1.0',
]);Extra Data
Attach additional data:
\Yii::$app->nadi->setExtra('order_id', $order->id);
\Yii::$app->nadi->setExtras([
'cart_items' => count($cartItems),
'total' => $cartTotal,
]);What's Captured
The Yii 2 SDK automatically captures:
| Data | Description |
|---|---|
| Exception | Type, message, code, file, line |
| Stack Trace | Full trace with file paths and line numbers |
| Request | URL, method, headers, input (filtered) |
| User | Authenticated user (if configured) |
| Session | Session data (filtered) |
| Environment | App environment, PHP version, Yii version |
| Route | Controller, action, parameters |
| Git | Commit hash (if available) |
Filtering Sensitive Data
Configure which request fields to exclude:
'nadi' => [
'scrubFields' => [
'password',
'password_confirmation',
'credit_card',
'cvv',
'ssn',
'api_key',
'secret',
],
],Next Steps
- PHP SDK — Advanced Usage - Advanced features available in all PHP SDKs
- PHP SDK — Transporters - Configure transport options
- PHP SDK — Sampling - Control event volume
- Shipper - Shipper agent documentation