简体   繁体   English

访问发送到Auth.redirect的帖子数据

[英]Accessing the post data sent to Auth.redirect

I am facing an issue with Auth.redirect functionality in Cakephp 2.5.3. 我在Cakephp 2.5.3中遇到Auth.redirect功能的问题。 Following is the Scenario: 以下是方案:

Scenario 脚本

There is an action (controller=>TalkComments, action=>add) which is login protected. 有一个受登录保护的操作(控制器=> TalkComments,操作=> add)。 The data is submitted to this action using POST HTML Method. 数据使用POST HTML方法提交给该操作。 Now, if the user is not logged in, it takes the user to login view. 现在,如果用户尚未登录,它将带用户登录视图。 Once the user logs in, it successfully redirects the user to the required controller and action. 用户登录后,它将成功将用户重定向到所需的控制器和操作。 The problem is, it does not send the POST data as well, because of which the add() function is not executed. 问题是,它也不会发送POST数据,因此不会执行add()函数。

How can I make the Auth.redirect to also store my post data and send it once the redirectUrl is called. 我如何使Auth.redirect也存储我的帖子数据,并在调用redirectUrl之后发送它。

Following is my code: 以下是我的代码:

UsersController.php UsersController.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 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;
}
}

Please do let me know if anything else is also required from my end. 如果我还有其他要求,请告诉我。

Thanks in advance :) 提前致谢 :)

The only way to make this work fine is to use Session Component ! 使这项工作正常的唯一方法是使用会话组件

AuthComponent won't let you access to the add action before you get Authenticated. 在获得身份验证之前,AuthComponent不允许您访问添加操作。 So we'll use beForeFilter function in our AppController, like that's documented right here , as this: 因此,我们将使用我们的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);   
    }
  }

Now the time to use our Session stored data to make add automatically a record ! 现在是时候使用我们的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'));
    }
  }

this should work for you as a charm. 这应该对您很有魅力。


If you have more than just one Add you need to make a DynamicAdd action like this: 如果您有多个添加,则需要执行以下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);
}

Let's make an example: 让我们举个例子:

we suppose that we have two controllers that have adds method, TestOnesController, TestTwosController ! 我们假设我们有两个具有添加方法的控制器,TestOnesController和TestTwosController! Then the login method will be like this 然后登录方法将像这样

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'));
  }
}

In beforeFilter action at AppController ! 在AppController的beforeFilter操作中!

Also a dynamic writing in the Session with: 会话中的动态写作还包括:

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);   
}
}

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

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