0

我在 Cakephp 2.5.3 中遇到了 Auth.redirect 功能的问题。以下是场景:

设想

有一个受登录保护的操作(控制器=>TalkComments,操作=>添加)。使用 POST HTML 方法将数据提交到此操作。现在,如果用户未登录,则将用户带到登录视图。用户登录后,它成功地将用户重定向到所需的控制器和操作。问题是,它也没有发送 POST 数据,因此 add() 函数没有被执行。

如何使 Auth.redirect 也存储我的帖子数据并在调用 redirectUrl 后发送它。

以下是我的代码:

用户控制器.php

class UsersController extends AppController {

//put your code here

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('add', 'login');
}

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Session->setFlash(__('Invalid username or password, try again'));
    }
}

public function logout() {
    return $this->redirect($this->Auth->logout());
}
public function isAuthorized() {
    return true;
}
}

TalkCommentsController.php

class TalkCommentsController extends AppController {

//put your code here
public function add() {
    if ($this->request->is('post')) {
        $this->TalkComment->create();
        if ($this->TalkComment->save($this->request->data)) {
            $this->Session->setFlash(__('Comment Saved'));
            return $this->redirect($this->referer());
        }
        $this->Session->setFlash(__('Could not post comment'));
    }
}

public function isAuthorized() {
    return true;
}
}

请让我知道我是否还需要其他任何东西。

提前致谢 :)

4

1 回答 1

1

使这项工作正常的唯一方法是使用会话组件

AuthComponent 在您获得身份验证之前不会让您访问添加操作。因此,我们将在 AppController 中使用 beForeFilter 函数,就像这里记录的那样,如下所示:

  public function beforeFilter() {
    //i used strtolower function the test is a case sensitive & i do not know the output of $this->params['controller'] and $this->params['action'] in your application but in this case it will work fine !
    if((strtolower($this->params['controller']) == 'talkcomments') && (strltolower($this->params['action']) == 'add') && ($this->is('post')){
       //do not forget to use Session Component in AppController before to use!
       $this->Session->write('data',$this->data);   
    }
  }

现在是时候使用我们的 Session 存储数据来自动添加记录了!

  public function login() {

    if ($this->request->is('post')) {
      if ($this->Auth->login()) {
        $comment = $this->Session->read('data');
        if(isset($comment) && !empty($comment)){
          $this->loadModel('TalkComment');
          $this->TalkComment->set($comment);
          if($this->TalkComment->validate()){
            if($this->TalkComment->save($comment))
              $this->Session->setFlash(__('Comment Saved'));
            else
              $this->Session->setFlash(__('Could not post comment'));
          }
          else
            $this->Session->setFlash(__('Could not post comment'));
        }
        return $this->redirect($this->Auth->redirectUrl());
      }
      $this->Session->setFlash(__('Invalid username or password, try again'));
    }
  }

这对你来说应该是一种魅力。


如果您有多个 Add,则需要像这样进行 DynamicAdd 操作:

private function DYnamicAdd($model, $data, $success_message, $error_message){
  $record = $data;
    if(isset($record) && !empty($record)){
      App::uses($model,'model');
      $this->$model->set($record);
      if($this->$model->validate()){
        if($this->$model->save($data))
          $this->Session->setFlash($success_message);
        else
          $this->Session->setFlash($error_message);
      }
      else
        $this->Session->setFlash($error_message);
}

让我们举个例子:

我们假设我们有两个具有添加方法的控制器,TestOnesController,TestTwosController!那么登录方式会是这样的

public function login(){
  if ($this->request->is('post')) {
    if ($this->Auth->login()) {

      //in the case of TestOnesController
      $this->DynamicAdds('TestOne', $this->Session->read('TestOne'), 'Testone Saved', 'Could not post Testone');

      //in the case of TestTwosController
      $this->DynamicAdds('TestTwo', $this->Session->read('TestTwo'), 'Testtwo Saved', 'Could not post Testtwo');

      return $this->redirect($this->Auth->redirectUrl());
    }
    $this->Session->setFlash(__('Invalid username or password, try again'));
  }
}

在 AppController 的 beforeFilter 操作中!

在 Session 中也有动态写作:

public function beforeFilter(){
  //C1 C2 C3 C4 are the controllers you may have a Add method in
  if(in_array(strtolower($this->params['controller']), array('C1','C2','C3','C4')) && (strltolower($this->params['action']) == 'add') && ($this->is('post')){
   //ucfirst and singularize to extract the name of the model from controller name !
   $this->Session->write(ucfirst(Inflector::singularize($this->params['controller'])),$this->data);   
}
}
于 2014-10-13T11:42:31.577 回答