I have to show message after insert some data in database. I'm using Kohana. Is there a way to do that with flash messages? It's better than header refresh.
我必须在数据库中插入一些数据后显示消息。我正在使用Kohana。有没有办法用flash消息做到这一点?它比标题刷新更好。
2 个解决方案
#1
2
Well sort of. You could use the Session::get_once()
function. But this only let you retrieve a variable once, and you cannot use it again in the same request. While you want a flash message to persist a full request cycle. To manage that you'll need a wrapper class, something like this.
好吧。您可以使用Session :: get_once()函数。但这只允许您检索一次变量,并且不能在同一请求中再次使用它。虽然您希望Flash消息持续完整的请求周期。要管理它,你需要一个包装类,就像这样。
class Flash {
private $session;
private $messages = array();
private static $_instance; // Singleton object
public static function instance() {
if ( ! isset( self::$_instance ) ) {
self::$_instance = new Flash();
}
return self::$_instance;
}
private function __construct() {
$this->session = Session::instance();
$this->messages['current'] = $this->session->get_once('flash');
if( ! is_array($this->messages['current'] ) ) {
$this->messages['current'] = array();
}
}
public function add( $key, $message === null ) {
if ( is_null( $message ) ) {
$message = $key;
$key = null;
}
$this->messages['new'][$key] = $message;
$this->session->set('flash', $this->messages['new'] );
return true;
}
public function get( $item = null ) {
if( $item === null ) {
return $this->messages['current'];
}
if( ! array_key_exists($item, $this->messages['current']) ) {
return null;
}
return $this->messages['current'][$item];
}
}
Usage:
用法:
$flash = Flash::instance(); $flash->add('A random message'); $flash->add('some_key', 'Some message'); $flash->get(); // array( 0 => 'A random message', 'some_key' => 'Some message') $flash->get('some_key'); // 'A Random message'
What it does basically is on initialization it retrieves the current message from the session, using the get_once()
function. The variable is nou out of the Session object, so it will only last this request. Everytime you add a variable, it will immediately persisted to the Session object.
它的作用基本上是在初始化时,它使用get_once()函数从会话中检索当前消息。该变量不在Session对象之外,因此它只会持续此请求。每次添加变量时,它都会立即保存到Session对象中。
There is just one problem; if you are using ajax calls, the messages will only be available on the initial php request, not on subsequent ajax calls. And there is also no restriction whatsoever on what kind of variable you are storing (but it must be serializable). You'll have to build in some checks for that too.
只有一个问题;如果您正在使用ajax调用,则消息将仅在初始php请求中可用,而不是在后续的ajax调用上。对于存储的变量类型也没有任何限制(但必须是可序列化的)。你也必须建立一些检查。
warning: the class is not tested, so it would surprise me if you do not get a syntax error ;)
警告:该类未经过测试,因此如果您没有收到语法错误,我会感到惊讶;)
And to go a step further: you would need an extra refresh anyway. The request flow should be like this imo:
更进一步:无论如何你还需要额外的刷新。请求流应该像这样imo:
Request 1: User is presented form Request 2: User posts the form, which is processed. Data is inserted in database. When done, user is redirected Request 3: A confirmation page is shown (can be "thank you", or the detail page, whatever).
请求1:用户呈现表单请求2:用户发布表单,该表单已处理。数据插入数据库中。完成后,重定向用户请求3:显示确认页面(可以是“谢谢”,或详细页面,无论如何)。
You would set the flash message in request 2, and show it in 3. I would not directly show the thank you page on request 2, because when the user refreshes, the form will be posted again.
您可以在请求2中设置flash消息,并在3中显示。我不会直接在请求2上显示感谢页面,因为当用户刷新时,表单将再次发布。
#1
2
Well sort of. You could use the Session::get_once()
function. But this only let you retrieve a variable once, and you cannot use it again in the same request. While you want a flash message to persist a full request cycle. To manage that you'll need a wrapper class, something like this.
好吧。您可以使用Session :: get_once()函数。但这只允许您检索一次变量,并且不能在同一请求中再次使用它。虽然您希望Flash消息持续完整的请求周期。要管理它,你需要一个包装类,就像这样。
class Flash {
private $session;
private $messages = array();
private static $_instance; // Singleton object
public static function instance() {
if ( ! isset( self::$_instance ) ) {
self::$_instance = new Flash();
}
return self::$_instance;
}
private function __construct() {
$this->session = Session::instance();
$this->messages['current'] = $this->session->get_once('flash');
if( ! is_array($this->messages['current'] ) ) {
$this->messages['current'] = array();
}
}
public function add( $key, $message === null ) {
if ( is_null( $message ) ) {
$message = $key;
$key = null;
}
$this->messages['new'][$key] = $message;
$this->session->set('flash', $this->messages['new'] );
return true;
}
public function get( $item = null ) {
if( $item === null ) {
return $this->messages['current'];
}
if( ! array_key_exists($item, $this->messages['current']) ) {
return null;
}
return $this->messages['current'][$item];
}
}
Usage:
用法:
$flash = Flash::instance(); $flash->add('A random message'); $flash->add('some_key', 'Some message'); $flash->get(); // array( 0 => 'A random message', 'some_key' => 'Some message') $flash->get('some_key'); // 'A Random message'
What it does basically is on initialization it retrieves the current message from the session, using the get_once()
function. The variable is nou out of the Session object, so it will only last this request. Everytime you add a variable, it will immediately persisted to the Session object.
它的作用基本上是在初始化时,它使用get_once()函数从会话中检索当前消息。该变量不在Session对象之外,因此它只会持续此请求。每次添加变量时,它都会立即保存到Session对象中。
There is just one problem; if you are using ajax calls, the messages will only be available on the initial php request, not on subsequent ajax calls. And there is also no restriction whatsoever on what kind of variable you are storing (but it must be serializable). You'll have to build in some checks for that too.
只有一个问题;如果您正在使用ajax调用,则消息将仅在初始php请求中可用,而不是在后续的ajax调用上。对于存储的变量类型也没有任何限制(但必须是可序列化的)。你也必须建立一些检查。
warning: the class is not tested, so it would surprise me if you do not get a syntax error ;)
警告:该类未经过测试,因此如果您没有收到语法错误,我会感到惊讶;)
And to go a step further: you would need an extra refresh anyway. The request flow should be like this imo:
更进一步:无论如何你还需要额外的刷新。请求流应该像这样imo:
Request 1: User is presented form Request 2: User posts the form, which is processed. Data is inserted in database. When done, user is redirected Request 3: A confirmation page is shown (can be "thank you", or the detail page, whatever).
请求1:用户呈现表单请求2:用户发布表单,该表单已处理。数据插入数据库中。完成后,重定向用户请求3:显示确认页面(可以是“谢谢”,或详细页面,无论如何)。
You would set the flash message in request 2, and show it in 3. I would not directly show the thank you page on request 2, because when the user refreshes, the form will be posted again.
您可以在请求2中设置flash消息,并在3中显示。我不会直接在请求2上显示感谢页面,因为当用户刷新时,表单将再次发布。