Atatus Laravel Middleware helps you to automatically capture of Laravel API calls and sends them to Atatus API analytics, which gives you a comprehensive Laravel API monitoring.

Add Atatus Laravel Middleware steps

  1. Make sure the Atatus PHP agent is installed on your host. If not, follow the installation steps to install the Atatus PHP agent.

  2. Install/Config the atatus middleware

    Option 1: Install the laravel package using composer.

    copy
    icon/buttons/copy
    composer require atatus/laravel-atatus
    

    Option 2: Add atatus/laravel-atatus to your project's composer.json file, and install composer.

  3. Add Atatus provider to service provider in config/app.php file.

    // In config/app.php
    
    'providers' => [
      /*
       * Application Service Providers...
       */
        Atatus\Middleware\AtatusLaravelServiceProvider::class,
    ];
    
  4. Add Atatus middleware in your App/Http/Kernel.php file

    // In App/Http/Kernel.php
    
    protected $middleware = [
      /*
       * The application's global HTTP middleware stack.
       *
       * These middleware are run during every request to your application.
       */
       \Atatus\Middleware\AtatusLaravel::class,
    ];
    
    

    If you only want to add tracking for APIs under specific route group, add to your route group, but be sure to remove from the global Middleware stack from above global list.

    // In App/Http/Kernel.php
    
    protected $middlewareGroups = [
      /**
       * The application's API route middleware group.
       */
       'api' => [
            //
            \Atatus\Middleware\AtatusLaravel::class,
        ],
    ];
    

    To track only certain routes, use route specific middleware setup.

  5. Publish the package config file.

    php artisan vendor:publish --provider="Atatus\Middleware\AtatusLaravelServiceProvider"
    
  6. Add configuration in config/atatus.php file.

    // In config/atatus.php
    
    return [
        'logBody' => true,
        // 'debug' => false,
        // 'configClass' => 'MyApp\\MyConfigs\\CustomAtatusConfig'
    ];
    

Configuration options

You can define Atatus configuration options in the config/atatus.php file.

// config/atatus.php
// Example

return [
    'logBody' => true,
    'debug' => false,
    'configClass' => 'MyApp\\MyConfigs\\CustomAtatusConfig'
];

Configuration class (Optional)

Because configuration hooks and functions cannot be placed in the config/atatus.php file, these reside in a PHP class that you create. Set the path to this class using the configClass option. You can define any of the following hooks:

  • identifyUserId
    Type: ($request, $response) => String Optional, a function that takes a $request and $response and return a string for userId. Atatus automatically obtains end userId via $request->user()['id'], In case you use a non standard way of injecting user into $request or want to override userId, you can do so with identifyUserId.

  • identifyCompanyId
    Type: ($request, $response) => String Optional, a function that takes a $request and $response and return a string for companyId.

  • maskRequestBody
    Type: $body => $body Optional, a function that takes a $body, which is an associative array representation of JSON, and returns an associative array with any information removed.

  • maskResponseBody
    Type: $body => $body Optional, same as above, but for Responses.

// Example config class

namespace MyApp\MyConfigs;

class CustomAtatusConfig
{

    public function identifyUserId($request, $response) {
      if (is_null($request->user())) {
        return null;
      } else {
        $user = $request->user();
        return $user['id'];
      }
    }

    public function identifyCompanyId($request, $response) {
      return "comp_acme_corporation";
    }

    public function maskRequestBody($body) {
      return $body;
    }

    public function maskResponseBody($body) {
      return $body;
    }

}