Welcome Guest, Not a member yet? Register   Sign In
can I use $this->config->site_url() in index.php
#1

[eluser]bill19[/eluser]
Hi everyone,

I am trying to modify my program to detect the environment it is in and load configuration files accordingly. Obviously when you open index.php you see the following:

Code:
if (defined('ENVIRONMENT'))
{
switch (ENVIRONMENT)
{
  case 'development':
   error_reporting(E_ALL);
  break;

  case 'testing':
  case 'production':
   error_reporting(0);
  break;

  default:
   exit('The application environment is not set correctly.');
}
}

So I have placed:

Code:
$environment=$this->config->site_url()
before this. I am getting the error:

Quote:Fatal error: Using $this when not in object context
. Can someone explain what the problem is? if this doesn't work here what is the best way to distinguish between my production server and my wamp development server?

Thanks,

Bill
#2

[eluser]TunaMaxx[/eluser]
You are trying to set a variable with a non-existant (at the moment) value. What needs to happen is to define() a named constant with 'devlopment' or 'production' etc. So instead of the default define() statement at about line 21 of index.php:

Code:
define('ENVIRONMENT', 'development');

...you need to do something like this:

Code:
if ($_SERVER['SERVER_ADDR'] == '192.168.0.1' || $_SERVER['HTTP_HOST'] == 'localhost')
{
define('ENVIRONMENT', 'development');
}
else
{
define('ENVIRONMENT', 'production');
}

NOTE: The if statement can be whatever reliably describes your development server(s).

Also, you are getting:

Quote:Fatal error: Using $this when not in object context

...because $this does not exist yet, and won't until sometime after the following code at the end of index.php:

Code:
/*
* --------------------------------------------------------------------
* LOAD THE BOOTSTRAP FILE
* --------------------------------------------------------------------
*
* And away we go...
*
*/
require_once BASEPATH.'core/CodeIgniter.php';
#3

[eluser]bill19[/eluser]
Thanks.
#4

[eluser]PhilTem[/eluser]
Actually, $this does not exist until line 308 of CodeIgniter.php

Code:
// Mark a start point so we can benchmark the controller
$BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');

/*
  * This is where $this is created, since $class represents your controller
  * which is the one object you're referring to with $this ;)
  */
$CI = new $class();
#5

[eluser]TunaMaxx[/eluser]
[quote author="PhilTem" date="1354022286"]Actually, $this does not exist until line 308 of CodeIgniter.php[/quote]

Yes, which is why I said
Quote:...and won’t until sometime after the following code at the end of index.php:

The exact line number of the included file where $this is set is really not relevant. The fact that it does not exist until later is. Thanks for hunting that down though.
#6

[eluser]Aken[/eluser]
$this is never available in index.php. $this is a special variable reserved for referencing an object's self. http://www.php.net/manual/en/language.oop5.basic.php

To definitively answer your question, no, you can never use config variables within index.php, because index.php is the very first thing to be processed - zero other CI resources are available at that point. You can use standard PHP logic, or control your environment elsewhere (like in an .htaccess file).
#7

[eluser]TunaMaxx[/eluser]
[quote author="Aken" date="1354086809"]To definitively answer your question, no, you can never use config variables within index.php, because index.php is the very first thing to be processed - zero other CI resources are available at that point...[/quote]
This is not true. While you may not be able to use $this, config variables and "CI resources" are available in index.php, albeit after the require_once BASEPATH.'core/CodeIgniter.php'; at the end of the file. Although you should probably never do something like this, try adding the following to the end of index.php to see what I mean:

Code:
die($CI->config->item('base_url'));

However, this thread stopped being valuable to the OP about 4 posts ago. In hindsight, I should have just said "You can't use '$this' in index.php" instead of being purposefully vague about the timing/availability of the reference. We could continue to banter about CI and OOP minutiae, but it doesn't add to the answer of the original question. I'm out. Wink
#8

[eluser]TunaMaxx[/eluser]
[quote author="bill19" date="1353995308"]Thanks.[/quote]

By the way, you are welcome! Hope it worked for you.
#9

[eluser]Aken[/eluser]
True, "never" is a strong word. But for his purpose, it's not feasible, since the environment variable will determine what config items are loaded anyway.

And regardless of if this info is value to the OP specifically, it's helpful info, especially for people who are not familiar with the way CI resources are loaded / handled.
#10

[eluser]wiredesignz[/eluser]
@TunaMaxx, Please explain your point for using the $CI object after the entire application has finished running and output has been sent.




Theme © iAndrew 2016 - Forum software by © MyBB