Welcome Guest, Not a member yet? Register   Sign In
Where is the best place to include inc file??
#1

[eluser]OKIEWARDOYO[/eluser]
I have a code like below
Note that $options is a variable in file options.inc.php

Code:
class Welcome extends CI_Controller {

function __construct()
{
  parent::__construct();
  
  
}

function index()
{
  include "options.inc.php";
  return $options['status_index'];
}

function read()
{
  include "options.inc.php";
  return $options['status_read'];
}

function write()
{
  include "options.inc.php";
  return $options['status_write'];
}
}

I'm confusing, where is the best place to place the line of code include "options.inc.php";

i want the code like below
Code:
class Welcome extends CI_Controller {

function __construct()
{
  parent::__construct();
  
  
}

function index()
{
  global $options;
  return $options['status_index'];
}

function read()
{
  global $options
  return $options['status_read'];
}

function write()
{
  global $options
  return $options['status_write'];
}
}

thanks
#2

[eluser]OKIEWARDOYO[/eluser]
So here is my story,
i'm creating an application like structure below

my_cms/
application/
system/
inc/

i have placed options.inc.php in inc folder
this file contain main array of all my_cms configuration

<?
$options = array(
'status_index' => 0,
'status_read' => 1,
'status_write => 2,
);

?>

on index.php, i have define a path for this file,
define('INCPATH', CURRDIR.'inc/');

Now, where should i put this include "options.inc.php" so i can make a global variable for $options from every function in model or controller page??

should i include this file on index.php?
#3

[eluser]PhilTem[/eluser]
You may include it in any file you want, but you must be sure to not include it within any class or any class' method.

I'd suggest you create a MY_Controller in ./application/core/ and put the 'include' line before you start the description of your MY_Controller class:

Code:
<?php
include('options.inc.php');

class MY_Controller extends CI_Controller {

}

/* File location: ./application/core/MY_Controller.php /*

However, it is much more MVC-/CI-style to create a config file of your configs, put it into ./application/config/your_config.php and have the config file being autoloaded within ./application/config/autoload.php in $autoload['config']. That way it will also be global put you do it in a better way.
You can then access your config-items with

Code:
config_item('status_read');
#4

[eluser]OKIEWARDOYO[/eluser]
Hem, thanks for reply, what about if i have another example like below,
this is my project structure for example,

root/
__application/
____models/
______mymodellang.php

__system/
__lang/
____en.php

__index.php
__.htaccess


look, i have an en.php there,
this en.php contain the code like below, it's an array
Code:
<?
//root/lang/en.php
//language file
$langs = array(
'_Home'=> 'Home',
'_People'=> 'People',
'_News'=> 'News',
'_Blogs'=> 'Blogs',
'_Register'=> 'Register',
'_Login'=> 'Login',
'_Email'=> 'Email',
);
?>

and, i have a mymodellang.php as the model of project,
Code:
<?php
//root/application/models/mymodellang.php

class Mymodellang extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }

    function translate($lang_key){
        @include(LANGPATH.'en.php');

        //global $langs; //i want this global varialbes
        if(in_array($lang_key, array_keys($langs)))
                return $langs[$lang_key];
        else
            return $lang_key;
    }
}
?>

My problem is on my model, my translate function above contain @include(LANGPATH.'en.php');
I want to delete this line of code from the translate function, place it somewhere that can be access by global $langs.

but i don't know where to placed.
i have tried putting like below but no luck,

Code:
<?php
//root/application/models/mymodellang.php
@include(LANGPATH.'en.php');
class Mymodellang extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
    function translate($lang_key){
      
        global $langs; //i want this global varialbes
        if(in_array($lang_key, array_keys($langs)))
                return $langs[$lang_key];
        else
            return $lang_key;
    }

i want this en.php stay on folder lang/
i don't want to create my_controller or another extra config,
can somebody help me??
thanks
#5

[eluser]OKIEWARDOYO[/eluser]
anyone?
#6

[eluser]PhilTem[/eluser]
I know this may sound stupid again, but why don't you stick with the load of classes that CI provides you? I'd recommend you put your languages in one file in ./application/language/english/your_lang.php and load it via

Code:
$this->lang->load('your_lang', 'english');

Then you can access any language line with
Code:
$this->lang->line('_Home');

Otherwise it must work if you add the include before declaring the model-class (as long as your LANGPATH constant is defined well, it will work. Try to remove the @ before the include and see if you get any errors prompted)




Theme © iAndrew 2016 - Forum software by © MyBB