azmolla/app-context
最新稳定版本:1.0.0
Composer 安装命令:
composer require azmolla/app-context
包简介
Laravel App Context Helper — Safe global data accessor for application-wide settings.
README 文档
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
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-20