简体   繁体   English

重定向仅适用于某些 controller 操作

[英]Redirect only working on some controller actions

A redirect, called by the private function r() , in the edit function works, but not in the delete .由私有 function r()调用的重定向在edit function 中有效,但在delete中无效。

Even commenting out the allowMethod doesn't help.即使注释掉allowMethod也无济于事。

Any ideas why that would be?任何想法为什么会这样?

/**
    * Edit method
    *
    * @param string|null $id Task id.
    * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
    * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
    */
public function edit($id = null)
{
    //get task
    $task = $this->Tasks->get($id, [
        'contain' => ['Users'],
    ]);

    $this->r();
    ...
}

/**
    * Delete method
    *
    * @param string|null $id Task id.
    * @return \Cake\Http\Response|null Redirects to index.
    * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
    */
public function delete($id = null)
{
    $this->request->allowMethod(['post', 'delete']);

    //get task
    $task = $this->Tasks->get($id, [
        'contain' => ['Users'],
    ]);

    $this->r();
    ...
}

private function r()
{
    $this->Flash->error(__('Not enough permissions to edit this task.')); 
    return $this->redirect(['controller' => 'tasks', 'action' => 'view',687]);
}

I modified the Controller class in /vendor to output the $response object.我将/vendor中的Controller class修改为output $response object。

Debug of headers following edit redirect: edit重定向后的标头调试:

CORE\src\Controller\Controller.php (line 695)
object(Cake\Http\Response) id:0 {
'status' => (int) 302
'contentType' => 'text/html'
'headers' => [
'Content-Type' => [
(int) 0 => 'text/html; charset=UTF-8',
],
'Location' => [
(int) 0 => 'http://localhost:9000/spt/tasks/view/687',
],
]
'file' => null
'fileRange' => [ ]
'cookies' => object(Cake\Http\Cookie\CookieCollection) id:1 {
protected cookies => [ ]
}
'cacheDirectives' => [ ]
'body' => ''
}

Debug of headers following delete redirect: delete重定向后的标头调试:

CORE\src\Controller\Controller.php (line 695)
object(Cake\Http\Response) id:0 {
'status' => (int) 302
'contentType' => 'text/html'
'headers' => [
'Content-Type' => [
(int) 0 => 'text/html; charset=UTF-8',
],
'Location' => [
(int) 0 => 'http://localhost:9000/spt/tasks/view/687',
],
]
'file' => null
'fileRange' => [ ]
'cookies' => object(Cake\Http\Cookie\CookieCollection) id:1 {
protected cookies => [ ]
}
'cacheDirectives' => [ ]
'body' => ''
}

Edit #1编辑 #1

Prior to this post, $this->r(), had logic to determine if it needed to redirect or not:在这篇文章之前,$this->r() 具有确定是否需要重定向的逻辑:

//code evaluating the need to redirect

//redirect if not editable by user
if($isEditable === false)
{
    $this->Flash->error(__('Not enough permissions to edit this task.')); 
    return $this->redirect(['controller' => 'tasks', 'action' => 'view',$task->id]);
}
else
{
    return null;
}

Edit #2编辑 #2

This seems to work and offer some reusability:这似乎有效并提供了一些可重用性:

   /**
    * Edit method
    *
    * @param string|null $id Task id.
    * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
    * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
    */
public function edit($id = null)
{
    //get task
    $task = $this->Tasks->get($id, [
        'contain' => ['Users'],
    ]);

    if($this->r() === false)
    {
        $this->Flash->error(__('Not enough permissions to edit this task.')); 
        return $this->redirect(['controller' => 'tasks', 'action' => 'view',687]);
    }
    
    //rest of controller:action code
}

/**
    * Delete method
    *
    * @param string|null $id Task id.
    * @return \Cake\Http\Response|null Redirects to index.
    * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
    */
public function delete($id = null)
{
    $this->request->allowMethod(['post', 'delete']);

    //get task
    $task = $this->Tasks->get($id, [
        'contain' => ['Users'],
    ]);

    if($this->r() === false)
    {
        $this->Flash->error(__('Not enough permissions to edit this task.')); 
        return $this->redirect(['controller' => 'tasks', 'action' => 'view',687]);
    }
    
    //rest of controller:action code
}

private function r()
{
    //logic to determine $isEditable
    return $isEditable
}

Returning null, stops the processing of the action, the view no longer gets the variables passed and fails.返回null,停止动作的处理,视图不再获取传递的变量而失败。

Assuming that I now have to move the conditional check in the controller instead of the $this->r() and redirect accordingly.假设我现在必须在 controller 而不是$this->r()中移动条件检查并相应地重定向。

What's working now.现在有什么工作。 Essentially, function returns boolean that is used to redirect.本质上,function 返回用于重定向的 boolean。

 /**
    * Edit method
    *
    * @param string|null $id Task id.
    * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
    * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
    */
public function edit($id = null)
{
    //get task
    $task = $this->Tasks->get($id, [
        'contain' => ['Users'],
    ]);

    if($this->r() === false)
    {
        $this->Flash->error(__('Not enough permissions to edit this task.')); 
        return $this->redirect(['controller' => 'tasks', 'action' => 'view',687]);
    }
    
    //rest of controller:action code
}

/**
    * Delete method
    *
    * @param string|null $id Task id.
    * @return \Cake\Http\Response|null Redirects to index.
    * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
    */
public function delete($id = null)
{
    $this->request->allowMethod(['post', 'delete']);

    //get task
    $task = $this->Tasks->get($id, [
        'contain' => ['Users'],
    ]);

    if($this->r() === false)
    {
        $this->Flash->error(__('Not enough permissions to edit this task.')); 
        return $this->redirect(['controller' => 'tasks', 'action' => 'view',687]);
    }
    
    //rest of controller:action code
}

private function r()
{
    //logic to determine $isEditable
    return $isEditable
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM