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
Requires
- php: ^8.0|^8.1|^8.2|^8.3
- illuminate/support: ^9.0|^10.0|^11.0|^12.0
- laravel/framework: ^9.0|^10.0|^11.0|^12.0
Requires (Dev)
- phpunit/phpunit: ^11.5
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