Welcome Guest, Not a member yet? Register   Sign In
static model/helper
#1

[eluser]ferus[/eluser]
Hi, i would like to create static model/helper (mainly - class) with set of constants that i will be using across all my models/controllers, i.e.

Code:
const ERROR_CODE_WRONG_FILE = 123;

When i was creating applications in pure PHP i was just creating new class with only constants, then i was reffering to this by including_once class file and using it i.e.
Code:
echo Initializer::ERROR_CODE_WRONG_FILE;

Would you like to help me with solving my problem? Any help will be appreciated.

Cheers,
Maciej
#2

[eluser]InsiteFX[/eluser]
You can just add your constants to CI constants file and they will be autoloaded!

./application/config/constants.php
#3

[eluser]ferus[/eluser]
There is any onther option to do it outside config? I am using git repositories and config directory is one of the ignored ones, beacuse my co-workers works on other databases/email accounts etc.
#4

[eluser]ferus[/eluser]
I need to bump this thread beacuse i've haven't got my reply yet.
Still - there is any way to create class with constants and refer to them like i posted it above?
#5

[eluser]Noobigniter[/eluser]
Hi,

You can create a core/MY_Controller.php and put this:

Code:
interface Constants
{
   const FOOBAR = 'Hello, World.';
   const BARFOO = 'World, Hello.';
}

class MY_Controller extends CI_Controller implements Constants{
    function __construct()
    {
        parent::__construct();
    }
    // more code here
}

Then, if you are in the class blog, for instance, just call it like this:

Code:
<?php
    echo Blog::FOOBAR;  
    echo Blog::BARFOO;
?>

Regards.
#6

[eluser]ferus[/eluser]
Do i need to use MY_Controller as my main controller parent in my all files to achieve this solution?

i.e.

Code:
class Games extends MY_Controller
{
//code goes here
}

#7

[eluser]Noobigniter[/eluser]
Yes (A little short as a response ^^)
#8

[eluser]ferus[/eluser]
And what if i want to use it within my models (i want to use one global file for that)?
#9

[eluser]Noobigniter[/eluser]
Example:
In your Controller :
Code:
$data['c'] = $this->blog_model->get_foobar();

In your model :
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Blog_model extends Crud {
    
    public function get_foobar() {
        
        try {

            return Blog::FOOBAR;

        } catch(Exception $e) {

            throw($e->getMessage());

        }

}

In view :
Code:
echo $c;
#10

[eluser]Aken[/eluser]
Why don't you just reconfigure the .gitignore to ignore certain config files instead of the entire directory? You're basically ruling out all the best ways to implement this by restricting any changes to the config directory. That's one of the most important parts of a CI installation.




Theme © iAndrew 2016 - Forum software by © MyBB