CakePHP:查找助手中是否是移动浏览器(无法访问请求处理程序)

发布于 2024-11-28 05:26:58 字数 121 浏览 2 评论 0原文

我需要在 CakePHP 应用程序的助手中知道设备是否是移动的,我很想使用 $this->RequestHandler->isMobile(),但请求处理程序组件在助手中不可用。有什么想法吗?

谢谢!

I need to know in a helper in a CakePHP application if the device is mobile, I would love to use $this->RequestHandler->isMobile(), but the request handler component is not available in helpers. Any ideas?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦一生花开无言 2024-12-05 05:26:58

您可以导入该类并在框架中的任何位置使用它,如下所示:(

App::import('Component', 'RequestHandler'); // import class
$requestHandler = new RequestHandlerComponent(); // instantiate class
$isMobile = $requestHandler->isMobile(); // call method
var_dump($isMobile); // output: bool(true) or bool(false)

通过助手测试并为 Firefox 和 iPhone 提供正确的结果)

You can import the class and use it anywhere in the framework like so:

App::import('Component', 'RequestHandler'); // import class
$requestHandler = new RequestHandlerComponent(); // instantiate class
$isMobile = $requestHandler->isMobile(); // call method
var_dump($isMobile); // output: bool(true) or bool(false)

(Tested from helper and gives correct results for Firefox and iPhone)

偷得浮生 2024-12-05 05:26:58

此外,您在 Controller::helpers 属性中设置的任何选项都将传递给助手:

class AppController extends Controller {

    public $components = array(/*...*/, 'RequestHandler');

    public $helpers = array(/*...*/, 'MyHelper');

    public function beforeFilter() {
        $this->helpers['MyHelper']['mobile'] = $this->RequestHandler->isMobile();
    }

}

您可以在助手的构造函数中捕获选项数组:

class MyHelper extends AppHelper {

    protected $_defaultOptions = array('mobile' => false);

    public function __construct($options) {
        $this->options = array_merge($this->_defaultOptions, $options);
    }

}

Also, any options you set in the Controller::helpers property will be passed to the helper:

class AppController extends Controller {

    public $components = array(/*...*/, 'RequestHandler');

    public $helpers = array(/*...*/, 'MyHelper');

    public function beforeFilter() {
        $this->helpers['MyHelper']['mobile'] = $this->RequestHandler->isMobile();
    }

}

You can catch the options array in your helper's constructor:

class MyHelper extends AppHelper {

    protected $_defaultOptions = array('mobile' => false);

    public function __construct($options) {
        $this->options = array_merge($this->_defaultOptions, $options);
    }

}
烟柳画桥 2024-12-05 05:26:58

接受的答案建议在助手中使用组件,应该避免这种情况,因为组件仅在控制器中使用,并且会导致 Anupal 提到的错误。

简单的解决方案是使用 RequestHandlerComponent 使用的 CakeRequest 类。所以在你的助手中你可以这样做:-

App::uses('CakeRequest', 'Utility');
$isMobile = (new CakeRequest())->is('mobile');

The accepted answer suggests using a component inside a helper which should be avoided as components are for use solely in controllers and will result in errors as mentioned by Anupal.

The simple solution is to use the CakeRequest class that RequestHandlerComponent uses. So in your helper you can do:-

App::uses('CakeRequest', 'Utility');
$isMobile = (new CakeRequest())->is('mobile');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文