How to define a dynamic global variable in code-igniter framework.
I am using Geo-Location tracking api, which tracks the country of the user based on user's IP address.
I need to set the value of this global variable dynamically depending on the country code returned from the API.
what is the best way to achieve this ?
In your case, You can store the value in session like this
$newdata = array(
'country' => 'Nepal',
);
$this->session->set_userdata($newdata);
And again retrieve the session value like this
$country = $this->session->userdata('country');
Have a look at their website (see the Config Class), it is written in great detail there.
Related
I need to set global variables in Laravel that I can modify whenever I want. Suppose that I have an admin area where I want to be able to modify colors, ads ... etc.
How can I do that, without storing values in the database?
You can use the config.
For the example, I am using Jenssegers\Agent\Agent to detect is the client desktop and generate different views and different queries. For that, I have created config/globalVars.php:
use Jenssegers\Agent\Agent;
$agent = new Agent();
return [
'isDesktop' => $agent->isDesktop()
];
Now, I can use config('globalVars.isMobile') everywhere.
In your case, you want and to change these vars:
var_dump(config('globalVars.isDesktop')); // true/false
config(['globalVars' => ['isDesktop' => 'fake info']]);
var_dump(config('globalVars.isDesktop')); // fake info
In the old laravel 4 Code we use an constants.php to define global vars.
What is the best practices to use define vars in Laravel 5?
We wouldn't store it in the .env file or the routes.php
I just want to improve a little bit #Adam's answer. Suppose an online shopping cart. Then, you want a global var to store the currency symbol in order to be displayed at views. Because this value can be changed across the time, you can specify it at App\Providers\ConfigServiceProvider like so:
public function register()
{
$currency = 'whatever you want';
config([
'currency' => $currency
]);
}
The $currency = config('currency') var is global for use at entire application even the request is not yet arrived at controllers (you can read more about middleware classes).
If you definitively want to use constanst (values never change) you could consider set the values at config files direclty as below:
# create a new config file config/customs.php
# like content, you could start with:
return [
'currency' => '$'
];
Finally, you grap the value in your app with config('customs.currency').
Depends on what sort of variables you are trying to store. If they are related to the model they should be stored in the model, if they are related to the environment they should be stored in .env. What sort of variables are you trying to store?
I am new to codeigniter , In my program i want a variable need to be accessed by multiple controllers,
It's not a constant variable, value of variable changes ,
Sorry , My mistake
I want to store a JSON object to be precise
Pls help me to figure this out.
Thanks in advance.
You can create a base controller with an attribute for your variable, then have your controllers extend that base controller.
Option 1
Since you are using CodeIgniter and sessions then something like this could work out for you:
set it
$someJSONobject = 'JSON';
$this->session->set_userdata('GLOBAL_JSON', $someJSONobject);
retrieve it
$someJSONobject = $this->session->userdata('GLOBAL_JSON');
echo $someJSONobject->subitem;
Make sure you are storing sessions in a DB if you go with this option because Cookie space is VERY limited
Option 2
Even if you are not using CodeIgniters' session implementation then you can do something quite similar in native PHP:
$someJSONobject = 'JSON';
$_SESSION['GLOBAL_JSON'] = $someJSONobject;
Appending on Rooneyl's solution you may want to save that value on session which is easier to access from all end
Session docs
I am trying to pass a variable from a view (of mobile model) to a different controller (of inventory model), using the chtml:button with this code
echo CHtml::button(
'Sell It',
array('submit' => array('inventory/create', array('id'=>$data->id)))
);
Now how do I access the $id variable in the Inventory controller, so that I can prepopulate the create view with details corresponding to the passed 'id' variable of the mobile model.
In your inventory/create controller action do checking before getting the id like this :-
if (isset($_REQUEST['id'])) {
$id = $_REQUEST['id'];
$this->render('create',array('model'=>$inventory, 'id'=>$id));
}
else{
$this->render('create',array('model'=>$inventory);
}
If you are trying to come up an update/edit form with the values prefilled based on the Id passed then you should have to go through the CRUD options available within YII.. This is much better way to handle record updation and its easy too . See this topic for furhter info..
http://www.yiiframework.com/doc/guide/1.1/en/quickstart.first-app
In your inventory/create controller action do a test for $_GET['id'] something like:
$id = (#$_GET['id']) ? : DEFAULT_VALUE;
$this->render('create',array('model'=>$inventory, 'id'=>$id));
and then you pass the data through to the view by passing an array of variables you want to make available.
(you would want to filter the input better, this is just a sample -- using filter_input or some other method and define a default value and/or some test for it being null/invalid)
in your controller you can get the variable by giving an argument to your controller method like this:
public function actionCreate($id){
$id = isset($id)?$id:NULL; // Or whatever, you can access it like this.
}
You don't have to use $_GET, and yii has already done some security checks on the value.
I am trying to use a third party script and extract the logged in users userid. I am aware the CodeIgniter uses some sort of encrypted sessions. Can you please suggest how to get the userid. A simple $_SESSION does not seem to work.
I am basically running a separate script and i just want the session details i.e. the userid. But I do not want to modify this script as MVC model. I want to modify it as minimal as possible.
Sorry I am very new to CodeIgniter. Thank you for your time.
Depending if you auto-load the session library or not, we will need to include:
$this->load->library('session');
Then you should be able to use:
$session_id = $this->session->userdata('SessionID');
Does this get you what you need?
Code Igniter session
If you want to set a session variable, for example:
$this->session->set_userdata('some_name', 'some_value');
or use the array. It's quite easy if you read the docs. And the docs in Code Igniter are great!
$newdata = array(
'username' => 'johndoe',
'email' => 'johndoe#some-site.com',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);