2017-10-01 65 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