4

is it possible to declare constants in /application/config/constant.php from a DB table?

I'm trying to do something like this.

$result = $this->db->select( 'attr, value' )->from( 'app_conf')->where( 'city', 53 )->or_where( 'city', -1 )->order_by( 'city' )->get();
$app_conf = $result->result_array();
// var_dump( $app_conf );
foreach( $app_conf as $row )
{
    define( "_{$row['attr']}", $row['value'] );
}

but right now I have to do it in every controller I create, so duplicating code and requiring to do changes all over if needed, which seems unnecessary!

3 Answers 3

5

Extend the CI Controller and place it in the application/core directory as MY_Controller. In the extended controller's constructor place the above code. Now you can extend it from your controllers to execute the same code. So you are not required to write this code again and again.

Sign up to request clarification or add additional context in comments.

Comments

4

Make a new controller by extending the CI_Controller and use this controller as your main controller (use it to create new controllers instead of CI_Controller)

<?php 
class MY_Controller extends CI_Controller{
    function __construct()
    {
        parent::__construct();
        // Assuming that you have loaded all required library in autoload
        $result = $this->db->select( 'attr, value' )->from( 'app_conf')->where( 'city', 53 )->or_where( 'city', -1 )->order_by( 'city' )->get();
        $app_conf = $result->result_array();
        foreach( $app_conf as $row )
        {
            define( "_{$row['attr']}", $row['value'] );
        }
    }
}

create a new controller (a normal controller from MY_Controller instead of CI_Controller)

class Somecontroller extends MY_Controller {
    function __construct()
    {
        parent::__construct();
        // ...
    } 

    // other functions/mechods
 }

Comments

2

Either use hooks or MY_Controller to solves this matter. Or if you're lazy, you can autoload a helper which does this for you.

Then it will be defined in every call.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.