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

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:

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

Implementation of set_temporary_message:
//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);

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:

$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:
class Globals {

function __construct($config = array()) {

  if (is_array($config)) {
   $CI =& get_instance();

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:
$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?


I believe its not a good solution. I could just call
echo $this->session->flashdata('temp_msg');
directly from view. Moderators, feel free to delete the thread...

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.


Hence, i just moved all my globals to MY_Controller's constructor:

$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');

I was trying to find away not to repeat

LATEST: I just changed my globals to defined constants. I moved
into helper.


Theme © iAndrew 2016 - Forum software by © MyBB