Debugging Helpers
Rhapsody includes a set of global helper functions to simplify debugging and inspecting variables during development. These helpers are available everywhere – in controllers, models, commands, and even in your view files (though not recommended).
Available Helpers
dd(...$vars) – Dump and Die
This is the most commonly used debugging tool. It prints one or more variables in a beautifully formatted way and then stops script execution immediately.
// In a controller
public function index(Request $request): Response
{
$user = $this->userModel->findById(1);
dd($user, $request->getBody()); // Execution stops here
}
Output (using Symfony VarDumper) will look like:
App\Models\User {#123 ▼
+db: PDO {#45 ...}
#user_id: "1"
#name: "John Doe"
#email: "john@example.com"
}
array:2 [▼
"name" => "Jane"
"email" => "jane@example.com"
]
d(...$vars) – Dump (without dying)
Same as dd(), but execution continues afterwards. Useful when you want to inspect intermediate values without breaking the page.
$result = processData($input);
d($result); // Dump and continue
// More code runs...
dump(...$vars) – Alias for VarDumper
If Symfony VarDumper is installed (it comes with filp/whoops), this provides the same pretty output as dd() but without terminating the script. It’s an alias for Symfony\Component\VarDumper\VarDumper::dump().
How It Works
The helpers are defined in core/helpers.php, which is automatically loaded via Composer. They use the Symfony VarDumper component (already a dependency of Whoops) for advanced formatting – including syntax highlighting, expandable arrays/objects, and proper recursion handling.
If VarDumper is not available, the helpers fall back to simple var_dump().
Best Practices
- Never commit
dd()ord()to production code. They are strictly for local development. - Use
dd($user)instead of multiplevar_dump()calls – it’s cleaner and safer. - You can pass any number of arguments:
dd($user, $posts, $_SERVER); - In Twig templates, you can use
&lcurl;{ dump(user) }}if you add the Twig extension, but it’s not enabled by default. For quick debugging in views, consider dumping from the controller instead.
APP_ENV=production, the dump() function still works but may expose sensitive data. Remove all debugging calls before deploying.