首页 > 解决方案 > 调用未定义的方法 Cake\Controller\Component\RequestHandlerComponent::isMobile()

问题描述

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

我收到错误消息:

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

我究竟做错了什么?

标签: cakephp

解决方案


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

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

请注意,这仅在您添加了mobile检测器时才有效,因为它不是核心的一部分。如果您使用的是默认应用程序模板,那么它应该已经存在于您的依赖项中,并应用在您的引导程序中。

composer.json

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

config/bootstrap.php

ServerRequest::addDetector('mobile', function ($request) {
    $detector = new \Detection\MobileDetect();

    return $detector->isMobile();
});

推荐阅读