Welcome Guest, Not a member yet? Register   Sign In
Where to place general global variables?
#11

[eluser]Ki[/eluser]
Further note: if you do decide to create the variables in a config/model/library, you would have to load them in every controller in order to reference them. This may be a little bit difficult to maintain in the long run.
#12

[eluser]chefnelone[/eluser]
[quote author="samota" date="1266802447"][quote author="chefnelone" date="1266802139"][quote author="samota" date="1266715168"]I don't mean to be repeating what I mentioned earlier, but if you load info from database, why don't you use My_controller to load everything you need for every page? It seems to be the common solution judging by the posts...[/quote]
I think that since a controller can't be autoloaded it isn't a good option for what I need.
I need the variables available everytime/everypage.[/quote]

If you create My_controller, will just extend it every page instead of the Controller. This way you do not have to load library/model in every controller. Best of luck![/quote]
I think now I see what you mean, Can you leave me a link to a post/wiki that show me how to create My_controller
thanks
#13

[eluser]Ki[/eluser]
it is VERY easy... I have mentioned this solution to you because I deeply believe it will be the best match for your needs as I had same issue. and there is NO WIKI needed. It is built into CI!

In your system/application/libraries create a file called MY_Controller.php. It will look like this:

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

class  MY_Controller extends Controller{

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

{you can call db here or define variables, load libraries/models etc}
define('COMPANY_EMAIL','[email protected]'); // you just defined a global variable to use anywhere you want
$this->data['company_email']='[email protected]';

}
function custom(){
{do something else here, if you need another function}
}        
      

}

The above library will allow you to controll everythign you need for ALL of your site in one location. To use it in your controllers simply

Code:
<?php
class Your_controller extends MY_Controller{
    
    function __construct(){
        parent::__construct();
    }

And in all controller you end up extending My_controller, which loads all of CI stuff in Controller + anything you define to load in My_controller


Let me know if you need more help
#14

[eluser]chefnelone[/eluser]
[quote author="samota" date="1266803971"]it is VERY easy... I have mentioned this solution to you because I deeply believe it will be the best match for your needs as I had same issue. and there is NO WIKI needed. It is built into CI!

In your system/application/libraries create a file called MY_Controller.php. It will look like this:

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

class  MY_Controller extends Controller{

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

{you can call db here or define variables, load libraries/models etc}
define('COMPANY_EMAIL','[email protected]'); // you just defined a global variable to use anywhere you want
$this->data['company_email']='[email protected]';

}
function custom(){
{do something else here, if you need another function}
}        
      

}

The above library will allow you to controll everythign you need for ALL of your site in one location. To use it in your controllers simply

Code:
<?php
class Your_controller extends MY_Controller{
    
    function __construct(){
        parent::__construct();
    }

And in all controller you end up extending My_controller, which loads all of CI stuff in Controller + anything you define to load in My_controller


Let me know if you need more help[/quote]
many thanks samota. I'll go for your suggestion..
Just one more question. It seems to me that using hooks is not recommended. Why is that?
#15

[eluser]Ki[/eluser]
Hooks are great if you want to "hack" CI permanently. My_controller allows you to ADD to CI + gives you the flexibility to change everything you want any time you want. If you build your My_controller into most of your controller, you know that whenever you need to change something globally, you have one place to do that.
I personally see hooks as a little bit more 'hidden' solution.
I use hooks when I have a deleted page or method and I want to permanently (forever) redirect users to another page.
And generally, My_controller is easier to manage and maintain because it ends up being the core of your website if you construct it properly.
#16

[eluser]chefnelone[/eluser]
[quote author="samota" date="1266805189"]Hooks are great if you want to "hack" CI permanently. My_controller allows you to ADD to CI + gives you the flexibility to change everything you want any time you want. If you build your My_controller into most of your controller, you know that whenever you need to change something globally, you have one place to do that.
I personally see hooks as a little bit more 'hidden' solution.
I use hooks when I have a deleted page or method and I want to permanently (forever) redirect users to another page.
And generally, My_controller is easier to manage and maintain because it ends up being the core of your website if you construct it properly.[/quote]

Thanks for the explanation. ;-)




Theme © iAndrew 2016 - Forum software by © MyBB