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

Call to undefined method Cake\Controller\Component\RequestHandlerComponent::isMobile()

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

我收到错误消息:

Call to undefined method Cake\Controller\Component\RequestHandlerComponent::isMobile()

我做错了什么?

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

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

请注意,这仅在您添加了 mobile 检测器时才有效,因为它不是核心的一部分。如果您使用的是默认应用程序模板,那么它应该已经存在 in your dependencies, and applied 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();
});