Codeigniter environments custom javascript inclusion
-
13-04-2021 - |
سؤال
Codeigniter has its 'ENVIRONMENT' setting such that when in a production environment, by changing the setting to 'production', all of the production configuration files are included.
A large amount of my website utilizes javascript. As such I want to have some config such as the below which changes dependent on whether i'm in a local or production environment.
<base href="http://localhost/site/" />
<script type="text/javascript">
var base_url = 'http://localhost/site/';
</script>
Does anyone know how i can make codeigniter include config/production/custom-config.php And if I could, surely that wouldn't be a suitable place for such configuration?
Many Thanks
المحلول
CodeIgniter includes as base_url
configuration variable located in application/config/config.php
. You could do something like this:
<base href="<?php echo base_url(); ?>" />
<script type="text/javascript">
var base_url = '<?php echo base_url(); ?>';
</script>
Does anyone know how i can make codeigniter include config/production/custom-config.php And if I could, surely that wouldn't be a suitable place for such configuration?
You can easily create custom config files. Create a file in the application/config/
folder (or the appropriate environment folder). For example:
*application/config/production/custom_config.php:*
<?php
$config['custom_var'] = 'custom value';
$config['another_var'] = 'another value';
// ...
Then you can load your new configuration file using:
$this->load->config('custom_config');
And access your configuration variables using:
$this->config->item('custom_var');
Finally, take a look at the documentation on the Config Class.