链接中的CakePHP多个命名参数


CakePHP Multiple Named Parameters in Link

我正在使用一个链接来命名下一页表单中需要的参数。这是链接代码:

    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

我的建议是使用构建一个完整的返回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']));

$this->redirect($this->referer(array('action' => 'index')));

其中,如果引用链接不存在,index是默认操作。

在这里阅读

例如,您的用户在content/view/my-content上,他单击emails/add,填写详细信息,然后提交。引用页面是content/view/my-content,所以他(应该(被重定向回那里。

我将其添加到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(