无法在PHP中的if($\u POST)条件内使用flash消息
我在我的登录控制器中有一个用于侦听$\u POST数据的文件:无法在PHP中的if($\u POST)条件内使用flash消息,php,phalcon,phalcon-routing,Php,Phalcon,Phalcon Routing,我在我的登录控制器中有一个用于侦听$\u POST数据的文件: <?php use \Phalcon\Tag; class SigninController extends BaseController { public function indexAction() { Tag::setTitle('Login'); // if submit if ($_POST) { $user = Use
<?php
use \Phalcon\Tag;
class SigninController extends BaseController {
public function indexAction()
{
Tag::setTitle('Login');
// if submit
if ($_POST) {
$user = Users::findFirst([
"email = :email: AND password = :password:",
"bind" => [
"email" => $this->request->getPost('email'),
"password" => $this->request->getPost('password')
]
]);
if ($user) {
$this->session->set('id', $user->id);
$this->session->set('role', $user->role);
$this->response->redirect("account");
} else {
$this->flash->error('Wrong credentials!');
$this->response->redirect('signin');
}
}
}
}
知道为什么flash消息在我的if$\u POST条件下不起作用吗?双向测试
1.引导文件
$di->set('flash', function(){
return new Phalcon\Flash\Session(array(
'error' => 'alert alert-error',
'success' => 'alert alert-success',
'notice' => 'alert alert-info',
));
});
$di->set('flash', function(){
return new Phalcon\Flash\Direct(array(
'error' => 'alert alert-error',
'success' => 'alert alert-success',
'notice' => 'alert alert-info',
));
});
控制:
$this->flash->error('Some Message');
$this->response->redirect('signin/index');
$this->flash->error('Jump to other page');
$this->dispatcher->forward(array('controller' => 'signin', 'action' => 'index'));
视图:
控制:
$this->flash->error('Some Message');
$this->response->redirect('signin/index');
$this->flash->error('Jump to other page');
$this->dispatcher->forward(array('controller' => 'signin', 'action' => 'index'));
视图:
我也有同样的问题 事实上,我们的问题与if$\u POST语句无关 是我们的观点造成了这个问题。要解决此问题,只需在设置flash消息之前禁用视图即可:
$this->view->disable();
$this->flash->error('Wrong credentials!');
$this->response->redirect('signin');
两种方法:
使用$this->flash->error'Some Message';
视图:
getContent;?>
使用$this->flashSession->error'Some Message';
视图:
flashSession->output?>您应该使用带重定向的flashSession,而不是flash。我的引导文件中有:$di->设置'flash',函数{$flash=new\Phalcon\flash\Session['错误'=>'警报危险','成功'=>'警报成功','通知'=>'警报信息','警告'=>'警报警告',];返回$flash;}@Phantom知道它为什么不工作吗?尝试用内部转发替换重定向:$this->dispatcher->forwardarray'controller'=>'controller\u name','action'=>'action\u name';
<?php
echo $this->getContent();
?>
$this->view->disable();
$this->flash->error('Wrong credentials!');
$this->response->redirect('signin');