【问题标题】:Flash() method shows the message but not redirecting in CakePHP 2Flash() 方法在 CakePHP 2 中显示消息但不重定向
【发布时间】:2015-07-29 06:29:48
【问题描述】:

在控制器中

public function add(){
    $this->loadModel('User'); //load model      
    if($this->request->is('post')){ 
        $filename=$this->User->checkFileUpload($this->request->data);
        $this->User->set($this->request->data); //set data to model                         
        if ($this->User->validates()){
            $datas = array(
                    'User' => array(
                                'name' => $this->request->data['User']['name'],
                                'email'=>$this->request->data['User']['email'],
                                'password'=>$this->request->data['User']['password'],
                                'image'=>$filename
                        )
                    );  
            $pathToUpload=  WWW_ROOT . 'upload/';           
            move_uploaded_file($this->request->data['User']['image']['tmp_name'],$pathToUpload.$filename);                      
            // prepare the model for adding a new entry
            $this->User->create();
            // save the data
            if($this->User->save($datas)){
                //$this->Session->setFlash('User Information has been saved!');
                return $this->Flash('User Information has been saved!',array('action' => 'index'));
                //return $this->redirect(array('action' => 'index'));
            }
        } else {
            $errors = $this->User->validationErrors; //handle errors    
        }   
    }
    //$this->layout = NULL;
    $this->viewpPath='Users';
    $this->render('add');
}

在上面的代码中,我使用 flash() 方法在操作后将用户引导到新页面。此方法显示消息但不在给定 url 中重定向。 请帮我。在 flash() 方法的帮助下重定向我在这里做错了什么?

【问题讨论】:

标签: php cakephp-2.0 flash-message


【解决方案1】:

flash() 不会重定向,它会呈现。和render()函数很相似,会继续执行脚本,不像redirect()函数。

但如果你还想用这个

您应该在配置文件中使用以下内容。

Configure::write('debug', 0);

更新 将其添加到 main.php 后,像

一样使用
$this->flash(__("Some message for the user here..."), array("action" => "index"));

它会完美地工作。关注thisforrefrence

【讨论】:

  • 先生,我说的是 Controller::flash(string $message, string|array $url, integer $pause, string $layout) 。我阅读了 CakePhp 2.x 文档,我想使用 flash 方法只是为了练习,但这不起作用。请查看链接:book.cakephp.org/2.0/en/controllers.html。打开此链接后,请搜索“Controller::flash”。我想知道如何使用这个方法。
  • 感谢您的回复,但如果我将核心文件中的调试设置从 2 更改为 0,您的更新代码将正常工作。
  • 这只有在核心文件中将调试参数从 2 更改为 0 时才有效
  • 我终于得到了为什么我必须在 flash 方法的调试参数中设置 0 的答案。 CakePHP 调试参数说: * 生产模式: * 0:不显示错误消息、错误或警告。 Flash 消息重定向。 * * 开发模式: * 1:显示错误和警告,刷新模型缓存,停止闪存消息。 * 2:与 1 相同,但也带有完整的调试消息和 SQL 输出。 * 3:与 2 相同,但也包含完整的控制器转储。
【解决方案2】:

渲染 != 重定向

如果您需要重定向到引用页面,您可以使用:

$this->redirect($this->referer());

如果你想重定向到不同的控制器:

$this->redirect(('controller' => 'YOURCONTROLLER', 'action' => 'YOURACTION'));

或者如果您想重定向到同一控制器中的不同操作:

$this->redirect(('action' => 'YOURACTION'));

【讨论】:

  • 嗨,Javi,我知道这也可以通过 redirect() 方法实现,但我想通过 flash() 方法做同样的工作。
  • 如果我想显示带有重定向 url 的消息,首先,我必须为消息设置 setFlash() 方法,然后我必须使用 redirect() 方法。而不是设置消息,然后使用 redirect() 方法,我想在单行中使用 flash() 方法做同样的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多