You can create a terminable Middleware and declare in the array of global middleware. Inside the method "terminate" you can add the logic to encrypt. Also you have to add another Middleware to decrypt the request.
DecryptRequest
<?php
namespace Illuminate\Session\Middleware;
use Closure;
class DecryptRequest
{
public function handle($request, Closure $next)
{
// Logic to decrypt
return $next($request);
}
}
EncryptResponse
<?php
namespace Illuminate\Session\Middleware;
use Closure;
class EncryptResponse
{
public function handle($request, Closure $next)
{
return $next($request);
}
public function terminate($request, $response)
{
// Logic to encrypt response.
}
}
Thank you agCepeda for suggesting middleware way of doing it, but what I want is how to encrypt the response of my api? Is it good practice to encrypt my response using Encrypt class the lumen/laravel provides? Or is there any other approach for this, can you suggest something on this?