cakephp - 调用未定义的方法 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()
我究竟做错了什么?
解决方案
那是应该删除的过时信息。这些方法不再可用,您应该改用\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();
});
推荐阅读
- node.js - 获取仅由登录用户创建的数据
- php - 如何更改 phpSpreadSheet 版本?
- azure - 部署到 Azure 应用程序时如何在 docker compose 中包含启动脚本
- python - 如何从 yaml 解析任意布尔逻辑
- python - Python - 一个目录中的嵌套文件在另一个目录中导入嵌套文件
- react-native - ReactNative 中整个应用程序的背景图像
- python-3.x - groupby 与熊猫数据框中的条件
- forms - 使用 Thymeleaf 获取内部对象的验证错误的问题
- java - 如何在 Java 中按下按钮使新的随机图像出现?
- python - 使用 for 循环遍历列表,检查字符是否存在。如果存在,将角色放入同一位置的新列表中