2017-10-01 61 views
0

我有以下功能subscribe

public function subscribe() { 
    $plans = Configure::read('Stripe.american_plans'); 
    $plans['premium_american_monthly'] = __('Premium Monthly') . ' - 4.99'; 
    $plans['premium_american_annually'] = __('Premium Annually') . ' - 49.99'; 

    if ($this->request->is('post')) { 
     $user = $this->Users->get($this->Auth->user('id')); 

     if ($this->Users->save($user)) { 
      $this->Flash->success(__('Your account has been subscribed')); 
      debug($this->redirect(['controller' => 'Users', 'action' => 'edit'])); 
      die(); 
     } else { 
      $this->Flash->error('User could not be subscribed.'); 
     } 
    } 

    $this->set(compact('plans')); 
} 

我做操作$之前,我將它保存在實際功能的用戶對象,但是這是最簡單的,我可以使功能和頁面仍然加載和錯誤仍然發生。

周圍重定向調試語句以檢查它重定向用戶,而且吐出:

object(Cake\Http\Response) { 

'status' => (int) 302, 
'contentType' => 'text/html', 
'headers' => [ 
    'Content-Type' => [ 
     (int) 0 => 'text/html; charset=UTF-8' 
    ], 
    'Location' => [ 
     (int) 0 => 'https://localhost/users/subscribe' 
    ] 
], 
'file' => null, 
'fileRange' => [], 
'cookies' => [ 
    'cookies go here' 
    ] 
], 
'cacheDirectives' => [], 
'body' => '' 

} 

正如你可以看到它不是重定向到那裏我希望它。事實上,無論我在重定向語句中放置什麼,它總是重定向到相同的函數subscribe

我不知道我在做什麼錯,因爲這段代碼看起來與其他函數的功能是一致的。有誰知道如何使這個重定向到我想要重定向的地方?

+0

'Controller :: redirect()'只會設置'Location'頭,如果它還沒有被設置,那麼檢查'$ this-> response'是什麼樣的。還要注意'Controller.beforeRedirect'可以在這裏產生干擾並修改響應,這樣'redirect()'不會改變它。 – ndm

回答

0

感謝@ndm的評論,讓我找到了一個解決方案。這感覺像一個骯髒的解決方案,因爲我不認爲我正在解決實際問題,但它確實有效。我重定向之前,我已經添加了以下內容:

$url = Router::url(['controller' => Users', 'action' => 'edit']); 
$this->response = $this->response->withLocation($url); 

的問題是發生,因爲我的地方頭部已經被設置與不正確的位置。我不確定發生了什麼,因爲我們其他的重定向邏輯都沒有被擊中,我們也沒有在我們的代碼中的任何地方實現beforeRedirect

但是,正如我所說,這工作,並確實解決了問題。

+0

如果不知道代碼庫,我仍然會在Controller.beforeRedirect事件上賭錢,它可能是一個插件,但是誰知道。我建議在Response :: location(),Response :: withLocation()和MessageTrait :: withHeader()中設置斷點,以確定變化源於哪裏。 – ndm