• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a correct way of using Flashdata? The doc seems confusing.

#1
Hi,

I'm confused with the documentation about flashdata.

First the doc shows how to use flashdata http://codeigniter.com/userguide3/librar...#flashdata

Then at Class Reference, all flashdata methods have this message "This is a legacy method kept only for backwards compatibility with older applications."

So my question is, if using flashdata is a legacy, what is the current/best way of doing it?

Thank you,
Castle
Reply

#2
Personally I'm using it like this.

PHP Code:
// Helper function
if ( ! function_exists('get_flashdata'))
{
    function 
get_flashdata($name NULL$type 'success'$destroy TRUE)
    {
        if( ! empty(
$name) && isset($_SESSION[$name]) )
        {
            
$tmp $_SESSION[$name];

            if( ! 
in_array($type, ['danger','info','warning','success']) )
                
$type 'success';
            if( 
$destroy )
                unset(
$_SESSION[$name]);

            return 
'<div class="alert alert-' html_escape($type) . ' no-print">' html_escape($tmp) . '</div>';
        }
    }


Setting

PHP Code:
$_SESSION['flashdata'] = 'Whatever'

Getting

PHP Code:
<?= get_flashdata('flashdata'); ?>
Reply

#3
(07-22-2020, 04:39 AM)jreklund Wrote: Personally I'm using it like this.

PHP Code:
// Helper function
if ( ! function_exists('get_flashdata'))
{
    function 
get_flashdata($name NULL$type 'success'$destroy TRUE)
    {
        if( ! empty(
$name) && isset($_SESSION[$name]) )
        {
            
$tmp $_SESSION[$name];

            if( ! 
in_array($type, ['danger','info','warning','success']) )
                
$type 'success';
            if( 
$destroy )
                unset(
$_SESSION[$name]);

            return 
'<div class="alert alert-' html_escape($type) . ' no-print">' html_escape($tmp) . '</div>';
        }
    }


Setting

PHP Code:
$_SESSION['flashdata'] = 'Whatever'

Getting

PHP Code:
<?= get_flashdata('flashdata'); ?>

Thank you. It's a nice approach.

So, is the documentation outdated?
Reply

#4
The code are still there in CodeIgniter 3, but as the documentation states that it's deprecated I choose to implement my own. Using $_SESSION as it stated instead. That code aren't using the Session Library.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.