我在Cakephp 2.5.3中遇到了Auth.redirect功能的问题。以下是场景:
方案
有一个受登录保护的操作(controller => TalkComments,action => add)。使用POST HTML方法将数据提交给此操作。 现在,如果用户未登录,则用户需要登录视图。用户登录后,会成功将用户重定向到所需的控制器和操作。 问题是,它也没有发送POST数据,因为没有执行add()函数。
如何让Auth.redirect也存储我的帖子数据,并在调用redirectUrl后发送它。
以下是我的代码:
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
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;
}
}
如果我还需要其他任何东西,请告诉我。
提前致谢:)
答案 0 :(得分:1)
使这项工作正常的唯一方法是使用会话组件!
在获得身份验证之前,AuthComponent不允许您访问添加操作。所以我们将在AppController中使用beForeFilter函数,就像右边here所记载的那样,如下所示:
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'));
}
}
这对你来说应该是一种魅力。
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);
}
我们假设我们有两个带有add方法的控制器,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'));
}
}
还在会话中使用动态写作:
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);
}
}