spatie/yii-ray
Yii2 integration for Spatie Ray desktop debugger. Send dumps, arrays, HTML, queries, and more from your Yii2 app to Ray for faster debugging. Includes performance tools and the same Ray API used across PHP frameworks.
Install the Package
composer require spatie/yii-ray
Ensure spatie/ray is installed in your project (required for Ray functionality).
Configure Ray
Add the following to your Yii2 config/web.php or config/console.php:
'components' => [
'ray' => [
'enabled' => true,
'auth' => [
'id' => 'your-ray-app-id',
'secret' => 'your-ray-app-secret',
],
],
],
Retrieve your id and secret from myray.app.
First Use Case Trigger a debug dump in a controller or service:
use Spatie\Ray\Ray;
public function actionIndex()
{
$data = ['user' => User::find(1), 'posts' => Post::all()];
Ray::dump($data); // Sends to Ray
return $this->render('index');
}
Launch Ray on your desktop to view the output.
Debugging Data
Replace var_dump() or Yii::debug() with Ray’s fluent methods:
Ray::info('User loaded', ['id' => $user->id]); // Logs with metadata
Ray::dump($queryResult); // Inspects complex objects
Performance Profiling Measure execution time in critical paths:
Ray::startTiming('database_query');
$users = User::all();
$time = Ray::stopTiming('database_query');
Ray::info("Query took {$time}ms");
Error Handling Catch exceptions and log them to Ray:
try {
// Risky operation
} catch (\Exception $e) {
Ray::exception($e, ['context' => 'user_profile']);
}
Integration with Yii Components
Hook into Yii events (e.g., yii\base\Application::EVENT_BEFORE_ACTION) to log requests:
Yii::$app->on(Application::EVENT_BEFORE_ACTION, function () {
Ray::info('Request started', [
'uri' => Yii::$app->request->url,
'method' => Yii::$app->request->method,
]);
});
Conditional Debugging
Disable Ray in production by toggling enabled in config or using:
if (Yii::$app->get('ray')->isEnabled()) {
Ray::dump($data);
}
Authentication Errors
id/secret are invalid.myray.app and check for typos in config/web.php.Performance Overhead
Ray::dump() calls may slow down requests.dev environment) or batch logs.Yii Caching Conflicts
php yii cache/flush) or use Ray::flush() to reset the session.HTML/Markdown Rendering
Ray::image() for screenshots or simplify content for readability.http://localhost:8000 by default).mcp.myray.app (whitelist if behind a proxy).RAY_ID/RAY_SECRET in .env for security:
RAY_ID=your-app-id
RAY_SECRET=your-app-secret
Then reference them in config:
'auth' => [
'id' => getenv('RAY_ID'),
'secret' => getenv('RAY_SECRET'),
],
Custom Log Channels
Extend Ray to log to Yii’s yii\log\Logger:
Yii::$app->log->targets['ray'] = [
'class' => \Spatie\Ray\Yii\LogTarget::class,
];
Middleware Integration Create a middleware to log requests/responses:
public function handle($request, \Closure $next)
{
Ray::info('Incoming request', ['uri' => $request->url]);
$response = $next($request);
Ray::info('Response status', ['status' => $response->statusCode]);
return $response;
}
Ray in Tests Use Ray in PHPUnit tests for debugging:
public function testUserCreation()
{
$user = User::create(['name' => 'Test']);
Ray::dump($user); // Debug test data
$this->assertTrue($user->save());
}
Theming Customize Ray’s UI by passing themes:
Ray::setTheme('dark'); // or 'light', 'custom'
How can I help you explore Laravel packages today?