2015-07-11 113 views
0

在我的CakePHP 3应用程序,一个控制器重定向后,我得到这个错误:CakePHP的3重定向器

Error: [LogicException] Controller action can only return an instance of Response 
Request URL: /mycontroller/ 
Stack Trace: 
#0 /var/www/vhosts/example.com/httpdocs/vendor/cakephp/cakephp/src/Routing/Dispatcher.php(87): Cake\Routing\Dispatcher->_invoke(Object(App\Controller\SigninsController)) 
#1 /var/www/vhosts/example.com/httpdocs/webroot/index.php(37): Cake\Routing\Dispatcher->dispatch(Object(Cake\Network\Request), Object(Cake\Network\Response)) 
#2 {main} 

关于把CakePHP 2 controller documentation重定向是这样的:

$this->redirect('/orders/thanks'); 
$this->redirect('http://www.example.com'); 
return $this->redirect(
     array('controller' => 'orders', 'action' => 'confirm') 
    ); 

但在CakePHP中3 documentation似乎是这样的:

return $this->redirect('/orders/thanks'); 
return $this->redirect('http://www.example.com'); 
return $this->redirect(
      ['controller' => 'Orders', 'action' => 'thanks'] 
     ); 

当我添加在$this->redirect之前3210字,错误得到解决。这是否会造成问题?因为我看不到cakephp 3 migration guide中的返回重定向部分。迁移指南只提到第三个参数被删除。

+1

引用[迁移指南](http://book.cakephp.org/3.0/en/appendices/3-0-migration-guide.html#id2):“该方法不能再发送响应并退出脚本,而是返回一个带有适当头文件的Response实例。“ – ADmad

回答

0

看看CakePHP中的3个应用程序this

Response provides an interface to wrap the common response-related tasks such as: Sending headers for redirects.

this

You should return the response instance from your action to prevent view rendering and let the dispatcher handle actual redirection.

2

返回时使用要重定向到其他网页,或者我们可以说,其他控制器的动作/方法或者即使您想重定向到第三方链接(如您已指定): -

return $this->redirect('/orders/thanks'); 
return $this->redirect('http://www.example.com'); 
return $this->redirect(['controller' => 'Orders', 'action' => 'thanks']); 

如果你想重定向到其他方法在同一个控制器,那么你可以离开返回并使用setAction如下,但这里的URL将保持不变。

$this->setAction('thanks'); 

返回是更好的方法,如果您使用下面的代码,URL将会重定向。

return $this->redirect(['action' => 'thanks']);