PHP code example of spatie / laravel-visit

1. Go to this page and download the library: Download spatie/laravel-visit library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spatie / laravel-visit example snippets


return [
    /*
     * These classes are responsible for colorizing the output.
     */
    'colorizers' => [
        Spatie\Visit\Colorizers\JsonColorizer::class,
        Spatie\Visit\Colorizers\HtmlColorizer::class,
    ],

    /*
     * These stats will be displayed in the response block.
     */
    'stats' => [
        ...Spatie\Visit\Stats\DefaultStatsClasses::all(),
    ]
];

visit <your-url> --method=patch --payload='{"testKey":"testValue"}'

php artisan visit /api/user/me --user=1

php artisan visit /api/user/me [email protected]

namespace Spatie\Visit\Stats;

use Illuminate\Contracts\Foundation\Application;

abstract class Stat
{
    public function beforeRequest(Application $app)
    {
    }

    public function afterRequest(Application $app)
    {
    }

    abstract public function getStatResult(): StatResult;
}

namespace Spatie\Visit\Stats;

use Illuminate\Contracts\Foundation\Application;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\Stopwatch\StopwatchEvent;

class RuntimeStat extends Stat
{
    protected Stopwatch $stopwatch;

    protected ?StopwatchEvent $stopwatchEvent = null;

    public function __construct()
    {
        $this->stopwatch = new Stopwatch(true);
    }

    public function beforeRequest(Application $app)
    {
        $this->stopwatch->start('default');
    }

    public function afterRequest(Application $app)
    {
        $this->stopwatchEvent = $this->stopwatch->stop('default');
    }

    public function getStatResult(): StatResult
    {
        $duration = $this->stopwatchEvent->getDuration();

        return StatResult::make('Duration')
            ->value($duration . 'ms');
    }
}

// in config/stats.php

return [
    // ...
    
    'stats' => [
        App\Support\YourCustomStat::class,
        ...Spatie\Visit\Stats\DefaultStatsClasses::all(),
    ]
]
bash
php artisan vendor:publish --tag="visit-config"
bash
php artisan visit /your-page
bash
php artisan visit --route=contact
bash
php artisan visit /users/1 --method=delete
bash
php artisan visit /users --method=post --payload='{"testKey":"testValue"}'
bash
php artisan visit / --only-response
bash
php artisan visit / --only-stats
bash
php artisan visit / --no-color
bash
php artisan visit / --text
bash
php artisan visit / --filter="nested.secondName"