Laravel | License Server | PHP |
---|---|---|
9.x | 1.2.x | ^8.0 |
10.x | 2.x | ^8.1 |
11.x | 3.x | ^8.2 |
License Server package, which is a Laravel package that allows you to manage your Laravel applications license. You can use it with any product or service. License Server comes with the agnostic license management system, which allows you to manage your licenses in a simple and easy way. Just add license relation to any product model then you can work in your logic.
This package requires license-connector package. License Connector is client implementation for License Server. Package for the client makes a request to License Server and gets a response.
This package requires PHP 8.0 or higher. Also these extesnions are required:
If the above extensions already installed, you can enable them with php.ini
.
This package requires Laravel 8.x or higher. Other versions are ignored.
Get via composer
composer require laravel-ready/license-server
Publish migrations and migrate
# publish migrations
php artisan vendor:publish --tag=license-server-migrations
# apply migrations
php artisan migrate --path=/database/migrations/laravel-ready/license-server
Configs are very important. You can find them in license-server.php file. You should read all configs and configure for your needs.
# publish configs
php artisan vendor:publish --tag=license-server-configs
Every license must-have product, because we need to know what it is licensed for. The client application will send this information to the License Server. Then we can check if the license is valid for given the product.
Product model can be any model that you want to be licensed. Add Licensable trait to your product model.
<?php
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',
...
];
...
}
Add in your namespace list:
use LaravelReady\LicenseServer\Services\LicenseService;
and product model
use App\Models\Product;
First, we need to know licensing models. This package supports two types of licensing models: to Domain and to User. Both of them are valid. If you want to add license to domain, you must pass domain
parameter. If you want to add license to user, you must pass userId
parameter. Also, when you pass both of them, you will get domain license.
// 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);
LaravelReady\LicenseServer\Models\License
model.LicenseService::getLicenseByKey(string $licenseKey)
LicenseService::getLicenseByUserId(int $userId, string $licenseKey = null)
LicenseService::getLicenseByDomain(string $domain, string $licenseKey = null)
// license key in uuid format
$licenseKey = "46fad906-bc51-435f-9929-db46cb4baf13";
// check license status
$licenseStatus = LicenseService::checkLicenseStatus($licenseKey);
Returns "active", "inactive", "suspended", "expired", "invalid-license-key" and "no-license-found".
// license key in uuid format
$licenseKey = "46fad906-bc51-435f-9929-db46cb4baf13";
// check license status
$licenseStatus = LicenseService::setLicenseStatus($licenseKey, "suspended");
You can only set active
, inactive
, suspended
status.
If you want to use own license validation controller you can integrate it easily.
Then you need to register this custom controller in your config/license-server.php
file.
You can send custom data with connector and on the license server-side, you can catch this custom data. First you need to create a listener for this event.
php artisan make:listener LicenseCheckedListener --event=LicenseChecked
Add class LicenseChecked
with LaravelReady\LicenseServer\Events\LicenseChecked
namespace. You can retrieve custom data from event.
<?php
namespace App\Listeners;
use LaravelReady\LicenseServer\Events\LicenseChecked;
class LicenseCheckedListener
{
public function __construct()
{
//
}
public function handle(LicenseChecked $event)
{
// $event->license,
// $event->data,
}
}
Finally, you need to register this listener in your config/license-server.php
file.
Ready to use API is included with simple resource methods. API endpoint is /api/license-server/licenses
.
License Server uses a cache to store the public tld list. See tld list at https://data.iana.org/TLD/tlds-alpha-by-domain.txt
The TLD list cache is will be stored at the storage/license-server/iana-tld-list.txt
file and do not edit this file.
In development you may use domain like example.test
etc. but you won't pass domain validation because the test
is not valid tld.