LoadDistData.php 947 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use Illuminate\Http\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use App\Helpers\SiteCache;
  7. /**
  8. * Class LoadDistData 用中间件来获取distm网店信息
  9. */
  10. class LoadDistData
  11. {
  12. /**
  13. * Handle an incoming request.
  14. *
  15. * @param
  16. */
  17. public function handle(Request $request, Closure $next): Response
  18. {
  19. // 获取请求的域名
  20. $domain = getHost();
  21. // 先检查 custom_domain,如果没有找到,再检查 secondary_domain
  22. $dist=SiteCache::getDist($domain);
  23. // 如果找不到匹配的数据,直接返回404响应
  24. if (!$dist) {
  25. abort(404, 'site not found.');
  26. }
  27. // 将找到的 dist 数据添加到请求中,方便后续使用
  28. $request->attributes->set('dist', $dist);
  29. //保存到共享地方供其它地方用
  30. return $next($request);
  31. }
  32. }