加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

从CakePHP组件到达CakeRequest对象

发布时间:2020-12-13 17:22:28 所属栏目:PHP教程 来源:网络整理
导读:我正试图从我的自定义组件到达CakeRequest对象.但我不能.在 cookbook里面我发现了这个: CakeRequest is the default request object used in CakePHP. It centralizes a number of features for interrogating and interacting with request data. On each
我正试图从我的自定义组件到达CakeRequest对象.但我不能.在 cookbook里面我发现了这个:

CakeRequest is the default request object used in CakePHP. It
centralizes a number of features for interrogating and interacting
with request data. On each request one CakeRequest is created and then
passed by reference to the various layers of an application that use
request data. By default CakeRequest is assigned to $this->request,
and is available in Controller,Views and Helpers. You can also access
it in Components by using the controller reference.

控制器参考意味着什么?我尝试了以下内容.有用.
但是如何在不将参数传递给组件的情况下到达CakeRequest?

// MyController.php
    public function foo(){
        $this->MyUtil->bar($this);
    }

// MyUtilComponent.php
    function bar(&$controller) {
        $a=$controller->request;
        print_r($a);
    }

解决方法

CakePHP组件具有 initialize回调,可用于存储对控制器的引用.将其添加到Component类:

public function initialize(Controller $controller) {
    $this->controller = $controller;
}

然后吧可以是:

function bar() {
    $a = $this->controller->request;
    print_r($a);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读