重定向到同一控制器中的操作不起作用 - CakePHP 3.x
Redirecting to an action in the same controller doesn't work - CakePHP 3.x
CakePHP 3.5.13
根据 documentation:
If you need to forward the current action to a different action on the same controller, you can use Controller::setAction()
to update the request object, modify the view template that will be rendered and forward execution to the named action:
// From a delete action, you can render the updated
// list page.
$this->setAction('index');
但这似乎不起作用。
例如,在我的控制器中我有 2 个函数,index()
和 apply_filters()
。我想在 apply_filters()
执行后将用户重定向回 index()
。
所以我这样做了:
public function apply_filters()
{
$this->autoRender = false;
// code for applying filters
$this->setAction('index');
}
public function index()
{
debug('index');
}
apply_filters()
没有视图,所以 $this->autoRender = false;
当用户在 URL /apply-filters
上时,它只是停留在那里而不会重定向回 index()
。
如果我将 $this->setAction('index')
替换为 return $this->redirect(['controller' => 'Front', 'action' => 'index']);
,它绝对可以正常工作
有什么想法吗?
转发(内部重定向) 与 HTTP 重定向.
不同
这两种方法做两件不同的事情,一个按照描述转发调用,改变应用程序状态,使调用被转发到的操作认为它是开始调用的那个,另一个创建 HTTP 重定向响应。
如果你想要后者,那么redirect()
就是你要找的,而不是setAction()
。
另见
CakePHP 3.5.13
根据 documentation:
If you need to forward the current action to a different action on the same controller, you can use
Controller::setAction()
to update the request object, modify the view template that will be rendered and forward execution to the named action:
// From a delete action, you can render the updated
// list page.
$this->setAction('index');
但这似乎不起作用。
例如,在我的控制器中我有 2 个函数,index()
和 apply_filters()
。我想在 apply_filters()
执行后将用户重定向回 index()
。
所以我这样做了:
public function apply_filters()
{
$this->autoRender = false;
// code for applying filters
$this->setAction('index');
}
public function index()
{
debug('index');
}
apply_filters()
没有视图,所以 $this->autoRender = false;
当用户在 URL /apply-filters
上时,它只是停留在那里而不会重定向回 index()
。
如果我将 $this->setAction('index')
替换为 return $this->redirect(['controller' => 'Front', 'action' => 'index']);
有什么想法吗?
转发(内部重定向) 与 HTTP 重定向.
不同这两种方法做两件不同的事情,一个按照描述转发调用,改变应用程序状态,使调用被转发到的操作认为它是开始调用的那个,另一个创建 HTTP 重定向响应。
如果你想要后者,那么redirect()
就是你要找的,而不是setAction()
。
另见