Search code examples
phpcodeigniter-2

Setting Constant In CodeIgniter Not Working


I am setting the constant for the raw http post data in .application/config/constants.php as follows:

define('POST_DATA', json_decode($GLOBALS["HTTP_RAW_POST_DATA"], true));

So I can adding sending the constant POST_DATA to the model from the controller as follows:

$data['data'] = $this->logins_model->signup(POST_DATA);

But I am getting the following error...

Severity: Notice

Message: Use of undefined constant POST_DATA - assumed 'POST_DATA'

Filename: controllers/logins.php

However I checked and sending the post data to the model worked when I did it like this:

$data['data'] = $this->logins_model->signup(json_decode($GLOBALS["HTTP_RAW_POST_DATA"], true));

Any ideas on what I am doing wrong? Am I using the constants file incorrectly?


Solution

  • You cannot set constant as an array . json_deocde produces an array, so the constant will not be set.

    Constants may only evaluate to scalar values
    

    The main point of constants is to make something that can't be altered.

    But if you want to set the data then you can serialize the data and set to the constant and when you need that data unserialize the constant value

    define('POST_DATA', serialize(json_decode($GLOBALS["HTTP_RAW_POST_DATA"], true)));
    
    $post_data     = unserialize (POST_DATA);
    $data['data'] = $this->logins_model->signup($post_data);