簡體   English   中英

重定向僅適用於某些 controller 操作

[英]Redirect only working on some controller actions

由私有 function r()調用的重定向在edit function 中有效,但在delete中無效。

即使注釋掉allowMethod也無濟於事。

任何想法為什么會這樣?

/**
    * 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]);
}

我將/vendor中的Controller class修改為output $response object。

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' => ''
}

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' => ''
}

編輯 #1

在這篇文章之前,$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;
}

編輯 #2

這似乎有效並提供了一些可重用性:

   /**
    * 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
}

返回null,停止動作的處理,視圖不再獲取傳遞的變量而失敗。

假設我現在必須在 controller 而不是$this->r()中移動條件檢查並相應地重定向。

現在有什么工作。 本質上,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