Files
lotteryLaravel/app/Providers/AppServiceProvider.php

41 lines
1.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Providers;
use App\Models\Player;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
// 仅在通过 EnsurePlayerApi 后可用;未走中间件时为 null
Request::macro('lotteryPlayer', function (): ?Player {
/** @var Request $this */
return $this->attributes->get('lottery_player');
});
/**
* 【当前请求语言】zh / en / ne与 lottery.locales.supported 对齐。
* 由 NegotiateLotteryLocale 写入 attribute控制台等非 HTTP 或无该中间件时回落到 fallback。
*/
Request::macro('lotteryLocale', function (): string {
/** @var Request $this */
return (string) ($this->attributes->get('lottery_locale')
?? config('lottery.locales.fallback', 'en'));
});
}
}