azmolla/app-context

Laravel App Context Helper โ€” Safe global data accessor for application-wide settings.

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/azmolla/app-context

1.0.0 2025-10-20 04:38 UTC

This package is auto-updated.

Last update: 2025-10-20 04:40:41 UTC


README

A lightweight Laravel package that provides a safe global application context โ€” allowing you to access configuration data (like site name, logo, contact info, etc.) from anywhere in your application, with automatic caching and graceful null handling.

๐Ÿš€ Features

โœ… Retrieve global data safely without null errors โœ… Works from Database, Cache, or Config โœ… View-shared $appContext variable โœ… Helper function app_context() โœ… Supports auto-cache refresh โœ… Zero dependency, fast & memory-safe

๐Ÿ“ฆ Installation

composer require azmolla/app-context

Laravel will auto-discover the service provider.

โš™๏ธ Publish Config & Migration

To customize settings or use database mode:

php artisan vendor:publish --tag=app-context-config
php artisan vendor:publish --tag=app-context-migrations

Then run the migration:

php artisan migrate

This creates the app_context table in your database.

๐Ÿงฉ Configuration

return [
    'source' => env('APP_CONTEXT_SOURCE', 'database'), // database | cache | array
    'cache_key' => 'app_context',
    'cache_ttl' => 3600, // seconds
    'table' => 'app_context',
];

You can also use .env:

APP_CONTEXT_SOURCE=database

๐Ÿง  Usage

Option 1 โ€” Using Helper Function

app_context('site_name');          // returns value or null
app_context('site_name', 'MyApp'); // returns default if missing

Get all context values:

app_context(); // returns full array

Option 2 โ€” In Blade Templates

{{ $appContext->site_name }}
{{ $appContext->support_email ?? 'support@example.com' }}

The $appContext variable is automatically shared with all Blade views.

Option 3 โ€” In Controllers

use Illuminate\Support\Facades\Cache;

public function index()
{
    $context = app('app.context');
    $siteName = $context->site_name;

    return view('home', compact('siteName'));
}

๐Ÿงฐ Example Database Structure

id key value
1 site_name My Laravel App
2 contact_email hello@example.com
3 logo_url /uploads/logo.png

๐Ÿงฉ Extending

You can extend the class for custom logic:

class CustomContext extends \Azmolla\AppContext\Support\SafeAppContext
{
    public function getSiteTitle()
    {
        return strtoupper($this->site_name ?? 'DEFAULT SITE');
    }
}

๐Ÿงช Testing

The package includes a basic PHPUnit test to validate functionality:

composer test

๐Ÿชช License

MIT License (see LICENSE).

๐Ÿ‘จโ€๐Ÿ’ป Author

Abiruzzaman Molla Backend Developer LinkedIn ๐Ÿ“ง abiruzzaman.molla@gmail.com