代码之家  ›  专栏  ›  技术社区  ›  Peter VARGA

调用未定义的方法Cake\Controller\Component\RequestHandlerComponent::isMobile()

  •  0
  • Peter VARGA  · 技术社区  · 4 年前

    我正在运行CakePHP 4.1.6并参考以下文档: https://book.cakephp.org/4/en/controllers/components/request-handling.html#RequestHandlerComponent::isMobile

    我收到错误消息:

    调用未定义的方法 Cake\Controller\Component\RequestHandlerComponent::isMobile()

    我做错什么了?

    1 回复  |  直到 4 年前
        1
  •  1
  •   ndm    4 年前

    这是应该删除的过时信息。这些方法不再可用,您应该改用 \Cake\Http\ServerRequest::is() :

    $isMobile = $this->request->is('mobile');
    

    请注意,只有添加了 mobile 探测器,因为它不是核心的一部分。如果您使用的是默认的应用程序模板,那么它应该已经存在了 in your dependencies ,并应用 in your bootstrap .

    composer.json :

    "require": {
        ...
        "mobiledetect/mobiledetectlib": "^2.8"
    },
    

    config/bootstrap.php :

    ServerRequest::addDetector('mobile', function ($request) {
        $detector = new \Detection\MobileDetect();
    
        return $detector->isMobile();
    });