我在cakephp控制器中有这个方法。在这里,我尝试根据用户是否已经注册到站点,将用户订阅数据保存到订阅表。我有两个名为Name
和Email
的表单字段。因此,如果用户登录并单击订阅按钮,则表单弹出窗口会显示他的姓名和电子邮件已填入框中,如果他提交,则他将被订阅。如果用户未注册且未登录但只想订阅,则他将填写一份空的订阅表单。
后来我添加了一些修改,如果任何已注册或未注册的用户想要使用相同的电子邮件订阅,弹出窗口将显示:“您已经注册”。到目前为止,我已经这样做了。它工作正常,但有很多if和else条件。任何关于如何最小化这个的想法都会有所帮助。我是cakephp和所有这些东西的新手。我的控制器方法代码如下:
function subscription_add() {
if(!empty($this->data)){
if($this->Session->check('User')){
$is_subscribed = $this->Subscription->find('count', array('conditions'=>array('Subscription.email' => $this->data['Subscription']['email'])));
if($is_subscribed > 0){
$this->Session->setFlash('You are already Subscribed !','default',array(),'E');
$this->redirect(array('action' => 'index'));
}
else{
$this->data['Subscription']['user_type'] = 1;
$this->data['Subscription']['user_id'] = $this->Session->read('User.id');
$this->Subscription->create();
if ($this->Subscription->save($this->data)) {
$this->Session->setFlash('Congrats ! You are Subscribed ', 'default', array(), 'S');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('You are not subscribed. Please, try again.', 'default',array(),'E');
}
}
}
else{
$subscribed = $this->Subscription->find('count', array('conditions'=>array('Subscription.email' => $this->data['Subscription']['email'])));
if($subscribed > 0){
$this->Session->setFlash('You are already Subscribed !','default',array(),'E');
$this->redirect(array('action' => 'index'));
}else{
$this->Subscription->create();
if ($this->Subscription->save($this->data)) {
$this->Session->setFlash('Congrats ! You are Subscribed ', 'default', array(), 'S');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('You are not subscribed.. Please, try again.', 'default',array(),'E');
}
}
}
}
}