PHP code example of laravel-ready / license-server
1. Go to this page and download the library: Download laravel-ready/license-server 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/ */
laravel-ready / license-server example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use LaravelReady\LicenseServer\Traits\Licensable;
class Product extends Model
{
use HasFactory, Licensable;
protected $table = 'products';
protected $fillable = [
'name',
'description',
'price',
'image',
...
];
...
}
// get licensable product
$product = Product::find(1);
$user = User::find(1);
// add license to domain
$license = LicenseService::addLicense($product, 'example.com', $user->id);
// add license to user
$license = LicenseService::addLicense($product, null, $user->id);
// with expiration in days (see configs for defaults)
$license = LicenseService::addLicense($product, null, $user->id, 999);
// with lifetime license (see configs for defaults)
$license = LicenseService::addLicense($product, null, $user->id, null, true);
// with trial license (see configs for defaults)
$license = LicenseService::addLicense($product, null, $user->id, null, false, true);
// license key in uuid format
$licenseKey = "46fad906-bc51-435f-9929-db46cb4baf13";
// check license status
$licenseStatus = LicenseService::checkLicenseStatus($licenseKey);
// license key in uuid format
$licenseKey = "46fad906-bc51-435f-9929-db46cb4baf13";
// check license status
$licenseStatus = LicenseService::setLicenseStatus($licenseKey, "suspended");
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Event;
use LaravelReady\LicenseServer\Models\License;
use LaravelReady\LicenseServer\Events\LicenseChecked;
class LicenseController extends Controller
{
/**
* Custom license validation
*
* @param Request $request
* @param License $license
*
* @return \Illuminate\Http\JsonResponse
*/
public function licenseValidate(Request $request, License $license)
{
// this controller is works under 'auth:sanctum' and 'ls-license-guard' middleware
// in this case 'auth()->user()' will be is our license
$_license = $license->select(
'domain',
'license_key',
'status',
'expiration_date',
'is_trial',
'is_lifetime'
)->where([
['id', '=', auth()->user()->id],
['is_trial', '!=', true]
])->first();
$data = $request->input();
// event will be fired after license is checked
// this part depends to your logic, you can remove it or change it
Event::dispatch(new LicenseChecked($_license, $data));
$_license->appent_some_data = 'some data and date now -> ' . now();
return $_license;
}
}
/**
* Custom controllers for License Server
*/
'controllers' => [
/**
* License validation controller
*
* You can use this controller to handle license validating
*
* See the documentation for more information.
*
*/
'license_validation' => [
App\Http\Controllers\LicenseController::class,
'licenseValidate'
]
]
namespace App\Listeners;
use LaravelReady\LicenseServer\Events\LicenseChecked;
class LicenseCheckedListener
{
public function __construct()
{
//
}
public function handle(LicenseChecked $event)
{
// $event->license,
// $event->data,
}
}
/**
* Event listeners for License Server
*/
'event_listeners' => [
/**
* License checked event listener
*
* You can use this event to do something when a license is checked.
* Also you can handle custom data with this listener.
*
* See the documentation for more information.
*
* Default: null
*/
'license_checked' => App\Listeners\LicenseCheckedListener::class,
],