CakePHP3 - 组件Flash无法正常工作

时间:2016-04-20 04:50:11

标签: cakephp flash-message

在我的app控制器中:

class AppController extends Controller
{
    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('Flash');
        $this->loadComponent('RequestHandler');
        $this->loadComponent('Cookie');
    }

}

在提供者控制器功能审核中:

public function review() {
        if ($this->request->is('post')) {
            $userId = $this->request->session()->read('Auth.User.UserId');
            $partnerId = $this->request->data('PartnerId');
            $content = $this->request->data('Content');
            $commentTable = TableRegistry::get('Comment');
            $comment = $commentTable->newEntity();
            $comment->UserId = $userId;
            $comment->PartnerId = $partnerId;
            $comment->Content = $content;
            $comment->CreatedBy = $userId;
            $comment->UpdatedBy = $userId;
            $comment->Source = $this->request->session()->read('Auth.User.LoginBy');
            if ($commentTable->save($comment)) {
                $this->Flash->success('Thank you for review!');
            } else {
                $this->Flash->error('So Sorry your review was failed! Please notify for us to fixed this problem!');
            }
            $this->redirect('provider/' . $partnerId);
        }
    }
  • 当用户提交评论成功但闪光灯未显示时。但是,我尝试创建另一个项目,请关注youtube(https://www.youtube.com/watch?v=eASSNS1f3V4)中的教程,以便在此项目中进行测试和Flash工作。
  • 对不起我的英文,请帮帮我。非常感谢你!

2 个答案:

答案 0 :(得分:0)

这是我的错,因为我不明白蛋糕。我改变了蛋糕的defaut布局,所以当Flash在文件夹Element / Flash / **。ctp中调用HTML而没有蛋糕样式css。因此消息框不会出现。

答案 1 :(得分:0)

我认为这不是您问题的答案,但以下是一些改进代码的提示:

    public function review(){
        if($this->request->is('post')){
            $commentTable = TableRegistry::get('Comment');
            $comment = $CommentTable->newEntity();
            $userId = $this->Auth->user('id');
            $comment->patchEntity($comment, $this->request->data);      
            $comment->patchEntity($comment, ['CreatedBy' => $userId, 'UserId' => $userId, 'UpdatedBy' => $userId, 'Source' => $userId]);
            if($commentTable->save($comment)){
                //something
            } else {
                //something
            }
        }
    }

如果您使用相同的列名称作为表单输入,则可以将所有表单变量一次性放入实体中:

$comment->patchEntity($comment, $this->request->data);  

如果要访问控制器中的用户会话变量,可以使用:

$userId = $this->Auth->user('id');

然后,如果您想手动将其他变量放入您的实体,您可以这样做:

$comment->patchEntity($comment, ['CreatedBy' => $userId, 'UserId' => $userId, 'UpdatedBy' => $userId, 'Source' => $userId]);

希望它能帮到你