【问题标题】:CakePHP Multiple Named Parameters in Link链接中的 CakePHP 多个命名参数
【发布时间】:2012-01-14 07:47:41
【问题描述】:

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

    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

【问题讨论】:

    标签: php email cakephp


    【解决方案1】:

    我的建议是使用以下方法构建完整的返回 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 因为下划线很乱
    【解决方案2】:

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

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

    read up here

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

    【讨论】:

    • 他们将来自的动作是视图,所以我应该用视图替换索引吗?
    【解决方案3】:

    我将此添加到 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)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      • 2011-03-17
      • 2011-01-03
      • 2017-05-28
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      相关资源
      最近更新 更多