spatie/laravel-visit
Adds a php artisan visit command to quickly hit any route in your Laravel app and inspect the response. Shows colorized HTML or JSON output plus request results, with options like visiting by route name and logging in a user before the request.
Installation:
composer require spatie/laravel-visit
php artisan visit:install
config/visit.php) with default settings.First Use Case:
php artisan visit /dashboard
/dashboard route in your browser (if --browser flag is used) and displays a colorized HTML/JSON response in the terminal.Key Configurations:
config/visit.php for default settings like:
browser: Whether to open the page in the browser (true/false).auth: Default user to authenticate as (e.g., auth:user).headers: Custom headers to include in requests.timeout: Request timeout in seconds.Visiting Routes by Name:
php artisan visit admin.dashboard
Authentication:
config/visit.php:
'auth' => [
'user' => 'auth:user', // Uses the `auth:user` artisan command
],
php artisan visit /profile --auth=admin:password
Custom Headers:
config/visit.php:
'headers' => [
'X-Custom-Header' => 'value',
],
php artisan visit /api --header="Authorization: Bearer token"
Browser Integration:
php artisan visit /dashboard --browser
config/visit.browser = true).JSON Responses:
php artisan visit /api/users --json
Session Persistence:
--session to persist session data across requests:
php artisan visit /dashboard --session
Testing Workflows:
visit in CI/CD pipelines to visually inspect rendered pages or API responses.phpunit.xml as a post-test hook:
<postTestCommands>
<command>php artisan visit /dashboard --json > last-test-response.json</command>
</postTestCommands>
Debugging Middleware:
php artisan visit /admin --middleware=web
php artisan visit /admin --no-middleware
Customizing Output:
resources/views/vendor/visit/results.blade.php template.API Testing:
php artisan visit /api/v1/users --auth=api --header="Accept: application/json"
Localization:
php artisan visit /dashboard --locale=es
Session Conflicts:
--session, ensure no other processes (e.g., Laravel queues) interfere with session storage.php artisan session:clear
Middleware Overrides:
--no-middleware) may bypass critical security checks (e.g., CSRF). Use cautiously in production-like environments.Browser Flag Limitations:
--browser flag relies on the system’s default browser. On headless servers (e.g., CI), this will fail silently.--json or redirect output to a file for CI pipelines.Route Caching:
php artisan route:cache), ensure the visit command runs after caching or clears the cache first:
php artisan route:clear && php artisan visit /dashboard
Auth User Resolution:
auth:user command must resolve a valid user. If no user is found, the request will fail.php artisan auth:user separately to verify.Verbose Mode: Enable debug output for HTTP requests:
php artisan visit /dashboard --verbose
Inspect Headers: Dump request/response headers:
php artisan visit /dashboard --dump-headers
Network Issues:
timeout setting in config/visit.php (default: 30 seconds).'timeout' => 60,
Colorized Output Issues:
php artisan visit /dashboard --no-colors
Custom Commands:
Extend the VisitCommand class to add domain-specific logic. Publish the command:
php artisan vendor:publish --tag="visit-commands"
Then modify app/Console/Commands/VisitCommand.php.
Pre/Post-Request Hooks:
Use Laravel’s registering and registered events in EventServiceProvider to hook into the VisitCommand lifecycle.
Custom Response Handlers:
Override how responses are processed by modifying the handleResponse method in the published command class.
Environment-Specific Configs: Use Laravel’s environment configs to switch behaviors:
'auth' => env('VISIT_AUTH_USER', 'auth:user'),
Proxy Support:
Configure proxy settings in config/visit.php for behind-firewall setups:
'proxy' => [
'http' => 'tcp://proxy.example.com:8080',
'https' => 'tcp://proxy.example.com:8080',
],
How can I help you explore Laravel packages today?