-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResolveMerchant.php
More file actions
42 lines (35 loc) · 1.51 KB
/
Copy pathResolveMerchant.php
File metadata and controls
42 lines (35 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
namespace App\Http\Middleware;
use App\Models\User;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* يحدد "أي تاجر" لأي طلب عام (بدون تسجيل دخول) عبر جزء {merchant} من الرابط،
* مثال: /stores/{merchant}/products
*
* {merchant} ممكن يكون store_slug (المفضّل للروابط العامة المنشورة) أو رقم
* الـ id مباشرة (fallback). لو ما لقى تاجر مطابق يرجع 404 صريح بدل ما يكمل
* صامتاً ويخلي MerchantScope يرجع صفر نتائج بدون تفسير واضح.
*
* بعد ما يتحدد التاجر، يعبّي app('current_merchant_id') اللي يعتمد عليه
* MerchantScope وBelongsToMerchant::creating() لعزل/تعبئة merchant_id
* تلقائياً على كل استعلام وإنشاء بهذا الطلب.
*/
class ResolveMerchant
{
public function handle(Request $request, Closure $next): Response
{
$identifier = $request->route('merchant');
$merchant = is_numeric($identifier)
? User::find($identifier)
: User::where('store_slug', $identifier)->first();
if (!$merchant) {
abort(404, 'المتجر غير موجود');
}
app()->instance('current_merchant_id', $merchant->id);
app()->instance('current_merchant', $merchant);
$request->attributes->set('merchant', $merchant);
return $next($request);
}
}