gpt4 book ai didi

php - 从 CakePHP 组件到达 CakeRequest 对象

转载 作者:可可西里 更新时间:2023-10-31 22:56:33 25 4
gpt4 key购买 nike

我正在尝试从我的自定义组件访问 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.

Controller 引用是什么意思?我尝试了以下。它有效。
但是,如何在不将参数 this 传递给组件的情况下到达 CakeRequest 呢?

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

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

最佳答案

CakePHP 组件有一个 initialize可用于存储对 Controller 的引用的回调。将此添加到您的组件类:

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

那么 bar 可以是:

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

关于php - 从 CakePHP 组件到达 CakeRequest 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13996527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com