Welcome Guest, Not a member yet? Register   Sign In
How to Call $this->session->flashdata Globally
#1

[eluser]fjamal[/eluser]
I found a solution that calls $this->session->flashdata('temp_msg') globally. However, i'm not sure if its the right solution or not.

1. The function add within home controller i.e. http://...../ci_folder/home/add has the following code:

Code:
$this->general_lib->set_temporary_message('Inserted Successfully');

Implementation of set_temporary_message:
Code:
//Holds a message to be shown after redirect. For example, a success message as "Good Job! Registered Successfully".
public function set_temporary_message($msg, $redirect_location = '') {
  $this->session->set_flashdata('temp_msg', $msg);
  redirect($redirect_location);
}

Hence, home controller extends MY_Controller

2. Once page get redirected to the default page, the message shows up. $this->session->flashdata('temp_msg') is in a custom config file as shown in step 3.

3. Write custom config file, called globals.php:

Code:
$CI =& get_instance();
$config['TEMPORARY_MSG'] = $CI->session->flashdata('temp_msg');
$config['TITLE'] = 'Sample Site';
$config['CSS'] = base_url().'rescources/css/style.css';

4. The library below set the config variables as globals, surely, i autoload the library:
Code:
class Globals {

function __construct($config = array()) {

  if (is_array($config)) {
  
   $CI =& get_instance();
   $CI->load->vars($config);
  
  }
  
}
}

5. I call $TEMPORARY_MSG anywhere within the view. It either has a message or empty string.

6. As you can see, whenever i need to set flashdata, all i have to do is setup my message:
Code:
$this->general_lib->set_temporary_message('Any Message You Want');
Page will be redirect automatically and message is shown to the user.

Is this a good idea?


#2

[eluser]fjamal[/eluser]
I believe its not a good solution. I could just call
Code:
echo $this->session->flashdata('temp_msg');
directly from view. Moderators, feel free to delete the thread...
#3

[eluser]skunkbad[/eluser]
If all you want to do is display a confirmation, why not just append a query string variable to your URL that you are redirecting to.

Code:
?conf=1
#4

[eluser]fjamal[/eluser]
Hence, i just moved all my globals to MY_Controller's constructor:

Code:
$my_globals['title'] = 'Sample Site';
  $my_globals['css'] = base_url().'rescources/css/style.css';
  $my_globals['server_root'] = $_SERVER['DOCUMENT_ROOT'];
  $my_globals['temporary_msg'] = $this->session->flashdata('temp_msg');
  $this->load->vars($my_globals);

I was trying to find away not to repeat
Code:
$this->session->flashdata(....);
#5

[eluser]fjamal[/eluser]
LATEST: I just changed my globals to defined constants. I moved
Code:
$this->session->flashdata('temp_msg');
into helper.

Thanks




Theme © iAndrew 2016 - Forum software by © MyBB