PHP code example of techparse / offline-sync
1. Go to this page and download the library: Download techparse/offline-sync 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/ */
techparse / offline-sync example snippets
use Techparse\OfflineSync\Traits\Syncable;
class Task extends Model
{
use Syncable;
// Optional: customize sync behavior
protected $syncResourceName = 'tasks';
protected $syncExcluded = ['internal_notes'];
}
'resource_mapping' => [
'tasks' => \App\Models\Task::class,
'users' => \App\Models\User::class,
],
// Operations are automatically queued when offline
$task = Task::create(['title' => 'My Task']);
// Manual sync (optional)
use Techparse\OfflineSync\Facades\OfflineSync;
OfflineSync::sync(['tasks']);
// Check status
$status = OfflineSync::getStatus();
// ['pending_count' => 5, 'is_syncing' => false, 'last_sync' => '...']
// These are automatically queued when offline
$task = Task::create(['title' => 'New Task']);
$task->update(['completed' => true]);
$task->delete();
use Techparse\OfflineSync\Facades\OfflineSync;
// Bidirectional sync (push + pull)
OfflineSync::sync(['tasks', 'users']);
// Push only (local → server)
OfflineSync::push(['tasks']);
// Pull only (server → local)
OfflineSync::pull(['users']);
// Get pending items
$pending = OfflineSync::getPending();
$pendingTasks = OfflineSync::getPending('tasks');
// Purge old synced items (older than 7 days)
OfflineSync::purgeOldItems(7);
'conflict_resolution' => [
// Default strategy for all resources
'default_strategy' => 'server_wins',
// Per-resource strategies
'per_resource' => [
'tasks' => 'last_write_wins',
'users' => 'server_wins',
],
],
// app/Providers/AppServiceProvider.php
public function boot(): void
{
$token = $this->app->make(Request::class)->bearerToken();
if ($token) {
config(['offline-sync.security.headers' => [
'Authorization' => 'Bearer ' . $token,
]]);
}
}
use Techparse\OfflineSync\Http\Controllers\SyncController;
Route::middleware('auth:sanctum')->prefix('sync')->group(function () {
Route::post('/push', [SyncController::class, 'push']);
Route::get('/pull/{resource}', [SyncController::class, 'pull']);
Route::get('/status', [SyncController::class, 'status']);
Route::get('/ping', [SyncController::class, 'ping']);
});
use Techparse\OfflineSync\Events\SyncCompleted;
Event::listen(SyncCompleted::class, function ($event) {
Log::info("Synced {$event->synced} items in {$event->durationMs}ms");
});
bash
php artisan native:plugin:register techparse/offline-sync
bash
php artisan vendor:publish --tag=offline-sync-config
bash
php artisan migrate
bash
# Push local changes to server
php artisan sync:push
# Push specific resources
php artisan sync:push tasks users
# Pull remote changes
php artisan sync:pull tasks users
# Check queue status
php artisan sync:status
# Clear queue
php artisan sync:clear
php artisan sync:clear --failed