2011-12-06 18 views
1

我正在使用一個鏈接來命名我在下一頁的表單中需要的參數。這裏是鏈接代碼:CakePHP鏈接中的多個命名參數

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

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

其中index是你的默認操作,如果參考鏈接不存在。

read up here

e.g你的用戶是content/view/my-content,他點擊emails/add,填補了他的細節,提交。引用頁面是content/view/my-content,所以他(應該)重新定向到那裏。

+0

他們來自的操作應該是視圖,那麼我應該用索引替換視圖嗎? – jwg2s

+0

剛剛在該重定向上添加了一個錯誤,可能有所幫助? – jwg2s

2

我有什麼建議,是利用構築了充分的回報URI:

$this->params['controller'] 
$this->params['action']; 
$this->params['pass']; 

因此,這將是這個樣子:

在add.ctp
$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')); 

echo $this->Form->hidden('returnUrl', array('value' => $this->params['named']['returnUrl'])); 

並在電子郵件的控制器中

$this->redirect('/' . str_replace('_', '/', $this->data['Email']['returnUrl'])); 
+0

加1,因爲下劃線的岩石 – YAMM

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)