我正在使用链接在下一页的表单中命名我需要的参数。这是链接代码:
echo $this->Html->link('Email', array('controller' => 'emails', 'action' => 'add', 'contact_email' => $model), array('class' => 'button add'));
这样做的目的是将电子邮件保存到数据库,然后发送电子邮件(两者都有效)。
我想返回他们点击链接时所在的页面,但是在他们经历了两个以上的页面之后不知道如何访问该模型和ID ...
这是add.ctp
<div class="universities form">
<?php echo $this->Form->create('Email');?>
<fieldset>
<legend><?php __('Add Email'); ?></legend>
<?php
echo $this->Form->input('subject');
echo $this->Form->input('email_text');
echo $this->Form->hidden('email', array('value' => $this->params['named']['contact_email']));
echo $this->Form->hidden('user_from', array('value' => $this->Session->read('User.id')));
echo $this->Form->hidden('created', array('value' => date("Y-m-d")));
echo $this->Form->hidden('modified', array('value' => date("Y-m-d")));
echo $this->Form->hidden('model', array('value' => $this->params['named']['model']));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
真正的问题 - 重定向的位置?
$this->redirect(array('controller' => $this->data['Email']['model'], 'action' => 'view', $this->data['model']['id']));
实施答案一后,我在重定向时收到了这些错误(电子邮件保存并成功发送,因此只是重定向问题)。
Notice (8): Undefined property: Email::$enabled [CORE/cake/libs/controller/component.php, line 142]
Code | Context
$component =& $this->_loaded[$name];
if ($component->enabled === true && method_exists($component, 'beforeRedirect')) {
Component::beforeRedirect() - CORE/cake/libs/controller/component.php, line 142
Controller::redirect() - CORE/cake/libs/controller/controller.php, line 678
EmailsController::add() - APP/controllers/emails_controller.php, line 54
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 204
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171
[main] - APP/webroot/index.php, line 83
Warning: mkdir() [http://php.net/function.mkdir]: Permission denied in /Users/jwg2s/Sites/fundvista/cake/libs/folder.php on line 498
Warning (2): Cannot modify header information - headers already sent by (output started at /Users/jwg2s/Sites/fundvista/cake/libs/debugger.php:673) [CORE/cake/libs/controller/controller.php, line 742]
Code | Context
header - [internal], line ??
Controller::header() - CORE/cake/libs/controller/controller.php, line 742
Controller::redirect() - CORE/cake/libs/controller/controller.php, line 721
EmailsController::add() - APP/controllers/emails_controller.php, line 54
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 204
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171
[main] - APP/webroot/index.php, line 83
Warning: mkdir() [http://php.net/function.mkdir]: Permission denied in /Users/jwg2s/Sites/fundvista/cake/libs/folder.php on line 498
答案 0 :(得分:2)
我建议使用以下方法构建完整的返回URI:
$this->params['controller']
$this->params['action'];
$this->params['pass'];
所以它看起来像这样:
$returnUrl = $this->params['controller'] . '/' . $this->params['action'] . '/' . implode('/', $this->params['pass']);
// let's also replace the slashes with, say, underscores
$returnUrl = str_replace('/', '_', $returnUrl);
echo $this->Html->link('Email', array('controller' => 'emails', 'action' => 'add', 'contact_email' => $model, 'returnUrl' => $returnUrl), array('class' => 'button add'));
在add.ctp
中echo $this->Form->hidden('returnUrl', array('value' => $this->params['named']['returnUrl']));
并在电子邮件的控制器中
$this->redirect('/' . str_replace('_', '/', $this->data['Email']['returnUrl']));
答案 1 :(得分:0)
$this->redirect($this->referer(array('action' => 'index')));
如果引用链接不存在,index
是您的默认操作。
例如,您的用户在content/view/my-content
,他点击emails/add
,填写他的详细信息,提交。引用页面是content/view/my-content
,因此他(应该)被重定向回那里。
答案 2 :(得分:0)
我将此添加到app_controller.php:
public function sendEmail($to,$from,$subject,$body,$headers=array(),$save_to_db=true)
{
if($save_to_db == true)
{
//do model crap here
$this->Email->create();
$data = array(
'email' => $to,
'subject' => $subject,
'email_text' => $body,
'user_from' => $from,
// map fields here
);
if($this->Email->save($data) == false)
{
$this->log("Email to '$to' from '$from' with subject '$subject' failed to save into the database!");
}
} // end save to db
$email = new EmailComponent();
//$email->startup($this);
//reeset email component
$email->reset();
/* SMTP Options */
$email->smtpOptions = array(
'port'=>'25',
'timeout'=>'30',
'host' => 'smtp.gmail.com',
'username'=>'###########',
'password'=>'###########',
);
/* Set delivery method */
//$email->delivery = 'smtp';
$email->from = $from;
$email->to = $to;
$email->subject = $subject;
$email->replyTo = $from;
$email->from = '############ <' . $subject . '>';
$email->sendAs = 'html'; //Send as 'html', 'text' or 'both' (default is 'text')
$success = $email->send($this->data['Email']['email_text']);
if($success == false)
{
$this->log("Email to '$to' from '$from' with subject '$subject' failed to send!");
}
return true;
} // end sendEmail
然后在我的电子邮件控制器中设置变量后调用该函数。 (例如$ to,$ from,$ subject,$ body,$ headers)