我正在使用cakephp 3.5,而我要使用的两种方法是弃用的,我找不到替代方案。
方法是:
$this->response->send();
$this->response->stop();
我想重定向到另一个页面,并停止执行当前方法。我尝试在重定向后打电话给die()
,但它不起作用。
根据迁移指南,方法已过时。
有什么想法吗?
编辑:
我正在尝试重定向用户,而无需访问某些页面。这是在控制器中的initialize()
方法中。
if ($allowedAccess) {
$this->Flash->error("Insufficient rights to access that location");
$this->redirect($this->referer());
// FIXME - find alternative to deprecated methods
return $this->response;
$this->response->send();
$this->response->stop();
}
您是否在控制器中尝试此操作?只需从控制器方法返回响应对象:
public function index() {
// Some code
return $this->response;
}
send()只是PHPS Exit()周围的包装器。如果需要的地方,请使用exit()。
返回响应时会发生什么是ActionDisPatcher处理返回值以及是否是响应对象。请参阅__invoke()方法。
响应将通过中间软件层,并最终由服务器使用的响应发言称。检查您的Webroot/index.php以查看它:
// Bind your application to the server.
$server = new Server(new Application(dirname(__DIR__) . '/config'));
// Run the request/response through the application
// and emit the response.
$server->emit($server->run());