• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Laravel Request外观设计模式-实践:优雅的设置全局变量

武飞扬头像
juejin
帮助43

场景和考虑

  • model层封装消费逻辑,需要区分是Android端的请求还是iOS端的请求,分别扣除不同账户的金币

  • 要精简代码逻辑,不想需要调用的地方都一层一层的传值到消费model,这样太繁琐了。

  • 优雅一点~

 

经过一番调研之后,找到了解决办法如下:

如何获得全局变量?

  • Request::get("deviceType") 获得了全局变量设备类型,即deviceType。
  • 这段代码是在model层中
if (Utility::isIos(Request::get("deviceType")) && $userVip['coinIos'] >= $costCoin) {
        $pass = 1;
        $consumeType = self::TYPE_CONSUME_COIN;
    } else if ($userVip['coinAndroid'] >= $costCoin) {
        $pass = 1;
        $consumeType = self::TYPE_CONSUME_COIN;
    }

如何设置全局变量?

  • 自定义customController,其他的controller都继承自定义的customController

  • 我们通过中间件middleware,获得请求request

  • 通过deviceAgent = $request->header('device-agent'); 获得请求的设备类型

  • 我们定义的device-agent:app版本_设备类型_设备名_系统版本,例如,1.0.0_ios_iphonex_13.0

  • 通过request->merge(["deviceType" => $this->_deviceType]);把设备类型,比如iOS或者Android merge到request中

  • 这样我们就能在任何接收到request请求的地方(包括model层),通过这种方式获得设备类型了:Request::get("deviceType")

下面上代码

<?php

namespace App\Http\Controllers;

use Validator;

class CustomController extends Controller
{
    protected $_appVersion = '';
    protected $_deviceType = '';
    protected $_deviceName = '';
    protected $_sysVersion = '';

    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            $deviceAgent = $request->header('device-agent');
            if (!empty($deviceAgent)) {
                $deviceArr = explode("_", $deviceAgent);
                if (count($deviceArr) == 4) {
                    $this->_appVersion = $deviceArr[0];
                    $this->_deviceType = $deviceArr[1];
                    $request->merge(["deviceType" => $this->_deviceType]);
                    $this->_deviceName = $deviceArr[2];
                    $this->_sysVersion = $deviceArr[3];
                }
            }

            return $next($request);
        });
    }
}

还有一个疑问:request是怎么传递到model层的?对model层有没有要求?为什么能直接用?

  • 经过一番调研之后发现,和model层没有关系。

  • 之所以能在model层中直接用,不需要通过controller传递request到model中,是因为我们使用的Request实现了外观设计模式:segmentfault.com/a/119000001…

 

  • 在model中甚至工具类中,只要我们使用Laravel内置的Facades下的Request,就可以取到值。

注意:别用错了Request;Laravel内置了很多种Request

use Illuminate\Support\Facades\Request;

Request::get("deviceType")

总结

  • Laravel的设计思想还是非常优雅的,外观设计模式值得再好好消化理解一下。

  • 我们通过上述的思路,优雅的规避了层层传值的问题,简化了代码复杂度。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanfbbac
系列文章
更多 icon
同类精品
更多 icon
继续加载