CakePHP在控制器之间传递值 [英] CakePHP passing values between controllers
问题描述
我想将值从一个控制器传递到另一个。
例如,我有一个会议控制器,我想创建一个新的事件。
我想将会议ID传递给事件,以确保这两个对象是关联的。
我想使用beforeFilter方法存储在ivar $会议中。
I want to pass values from one controller to another. For example I have a Conference controller and I want to create a new Event. I want to pass the Conference id to the event to make sure those two objects are associated. I would like to store in the ivar $conference using beforeFilter method.
这是我的事件控制器中的beforeFilter函数
Here is my beforeFilter function in the Events controller
public function beforeFilter() {
parent::beforeFilter();
echo '1 ' + $this->request->id;
echo '2 ' + $this->request['id'];
echo $this->request->params['id'];
if(isset( $this->request->params['id'])){
$conference_id = $this->request->params['id'];
}
else{
echo "Id Doesn't Exist";
}
}
每当我将url更改为:
Whenever I change url to something like:
http://localhost:8888/cake/events/id/3
或
http://localhost:8888/cake/events/id:3
我收到一个错误说未定义id。
I am getting an error saying that id is not defined.
当您通过url传递数据时,我应该如何处理?
How should I proceed?
推荐答案
可以通过
$this->passedArgs['variable_name'];
例如,如果您的网址是:
For example if your URL is:
http://localhost/events/id:7
然后使用此行访问该ID
Then you access that id with this line
$id = $this->passedArgs['id'];
当您访问通过url接受参数的控制器函数时,您可以使用这些参数,变量,例如你的url看起来像这样
When you access a controller function that accepts parameters through url, you can use those parameters just as any other variable, like for example say your url looks like this
http://localhost/events/getid/7
然后您的控制器函数应如下所示:
Then your controller function should look like:
public function getid($id = null){
// $id would take the value of 7
// then you can use the $id as you please just like any other variable
}
这篇关于CakePHP在控制器之间传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!