I am struggling to apply session basis time zone in Codeigniter(2.0).
Scenario:
Once user logged in the system I use the lat/lon to detect the time zone ID and set it to a session variable and then call set_timezone() into every controller's constructor.
Ex a helper file which is auto loaded:
function set_timezone($timeZoneId='')
{
if($timeZoneId!="")
{
date_default_timezone_set($timeZoneId);
mysql_query("SET SESSION time_zone = '".$timeZoneId."'");
}
}
My problem is I don't want to call set_timezone() in to every controller's constructor. What I want is to call globally instead of every controller's constructor.
You should create a parent controller called MY_Controller that all your other controllers extend. In the constructor of that class you can call set_timezone. With this solution the timezone will be set automatically in all child controllers.
Your MY_Controller should be stored in application/core and should look something like this:
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->set_timezone();
}
function set_timezone($timeZoneId='')
{
// Your code
}
}
Now all your other controllers should extend MY_Controller instead of CI_Controller. E.g.:
class WelcomeController extends MY_Controller
{
function __construct()
{
// This will call the constructor of MY_Controller,
// which in turn sets the timezone:
parent::__construct();
}
// Rest of your functions...
}
The another way is,
You can define it inside Library. You can create a library and define this function inside libraries and call it anywhere in the application. Don't forget to load the library in AutoLoad.
As Nikitas said create a library file like below and include the library file in autoload.phpb
class Timezoneconvert
{
function set_timezone($timeZoneId='') {
$this->obj =& get_instance();
$this->obj->load->database();
if($timeZoneId!="")
date_default_timezone_set($timeZoneId);
$this->obj->db->query("SET SESSION time_zone = '".$timeZoneId."'");
}
}
}
in autoload.php
$autoload['libraries'] = array('Timezoneconvert');
I have got a solution by using hooks.
1.enable the hooks in config
2.set it into hooks.php
class HookMyHandler {
function post_controller_constructor() {
$this->ci = & get_instance();
//my method name
}
}
Related
I have created the session in login function. Now I want to use the created session on all other functions too if a user login then the created session should be applied to all other functions.
Thanks
I think you need to understand session first.
How do Sessions work?
Sessions will typically run globally with each page load, so the Session class should either be initialized in your controller constructors, or it can be auto-loaded by the system. For the most part the session class will run unattended in the background, so simply initializing the class will cause it to read, create, and update sessions when necessary.
To initialize the Session class manually in your controller constructor, use the $this->load->library() method:
$this->load->library('session');
Once loaded, the Sessions library object will be available using:
$this->session
Session data is simply an array associated with a particular session ID (cookie).
Visit CI Documentation of Session For More detail
See it live here: Session
In your login section
$this->db->where('email',$email);
$this->db->where('password',$pass);
$query = $this->db->get('admin');
$data= $query->result_array();
if($data){
$this->session->set_userdata('sessionVariable', $data);
redirect('controller_name');
}
Open autoload.php from application/config/autoload.php
$autoload['libraries'] = array('session');
OR
load session libraries in __construct() of your controller
public function __construct()
{
parent::__construct();
$this->load->library('session');
}
To get session data
$sessionData = $this->session->userdata('sessionVariable');
You can autoload the sessions in config.php
$autoload['libraries'] = array('database','Session','email');
OR
You can create a Base Controller in Core folder and extend all your other controller to that base controller.
Like this
<?php
class MY_Controller extends CI_Controller {
public $data = array();
function __construct() {
parent::__construct();
$this->data['errors'] = array();
$this->data['site_name'] = config_item('site_name');
$this->load->library('session');
}
}
Now All Your other Controller should be extended to your Base Controller instead of CI_Controller
In Your Controller
Controller 1:
class Login extends MY_Controller
{
function __construct() {
parent::__construct();
}
}
Controller 2:
class Dashboard extends MY_Controller
{
function __construct() {
parent::__construct();
}
}
So You will get just need to load your library is your base controller and get all the goodness of base controller in the child controllers. This will give you better hierarchy, code management and security
Why do I need to use the parent::__construct(); constructor, what does it have I need?
//CONTROLLER
class users_ctrl extends CI_Controller {
function __construct() {
parent::__construct(); //Why do I need to include it?
$this->load->model('select_model');
}
public function index()
{
$data['user_list'] = $this->select_model->get_all_users();
$this->load->view('show_users', $data);
}
}
//MODEL
class select_model extends CI_Model{
function __construct() {
parent::__construct();
}
function get_all_users()
{
$query = $this->db->get('students');
return $query->result();
}
}
In your given examples, you are calling the load class that the base controller class loads. Without the parent's constructor, you would have to load an instance of it yourself.
As for your model, you would have to manually load your db object.
Remove it and you should get something like called to undefined propery $class::load
CodeIgniter aside, it really is a basic fundamental of object-oriented programming.
If you make a class that extends another, and you declare a new constructor in the child class, the parent constructor will never run - since you've overridden it, and since CodeIgniter base controller does (most likely) a lot of things behind the scenes, if you do not run parent constructor, your controller most likely won't be injected in CI's container.
I need to set additional configs defined in the database. My idea is to extend CI config in pre_controller hook.
The hook is not a problem, but how to extend CI config in the right way?
Can someone explain it to me with some example if possible?
Besides that, if I hook it in pre_controller, will it check configs and values from the database on every request with caching, because I need to check the table on every request?
dont need to use hooks necessarily
in application/core/ create your base application controller ex: AppController
class AppController extends CI_Controller
{
public function __construct()
{
parent::__construct();
$config = $this->db->get('config')->result();
foreach ($config as $config_item)
{
$this->config->set_item($config_item->name, $config_item->value);
}
}
}
and all your application controllers extends from AppController
class Main extends AppController
{
// other functions
}
My codeigniter site autoloads sessions. I have an XML API page that I created but I'm getting a session error because of that autoload. I would prefer not to load sessions on this controller but I don't want to have to load sessions manually on all of my other controllers. Can that be done?
Use a base controller to load the session class rather than autoload.php and have your controllers extend it. More information here: http://ellislab.com/codeigniter/user-guide/general/core_classes.html
// application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('session');
}
}
// you may add additional base controller classes here
You must extend this controller with the ones that you want to have access to the session class, so unfortunately you will have to make some edits to your existing controllers:
class UserController extends MY_Controller {
public function index()
{
// session class is loaded
}
}
Other controllers can continue to extend CI_Controller and the session class won't be loaded.
I use this method for all my CI projects and rarely use the autoloader.php, it allows much more flexibility.
I have this question: how to load function in codeIgniter in every page and get data or variables from it to show in view?
I have many controllers and each one has many function. Each function load master view as a template and load its own view. Master view has dynamic data I push it from database.
So I need in master view to show some data from database automatically. How to do it?
You can use this. change it according to your
function index()
{
$data['list'] = $this->some_model->show_data();
$data1['content'] = $this->load->view('some_view',$data,true);
$this->load->view('template',$data1);
}
content is in your main template where u will pass your data to show in template
I am not sure what exactly you ask but if you want a function to be loaded automatically to any controller
then create a Mycontroller.php in application/core
class Mycontroller extends CI_Controller
{
function __construct()
{
parent::__construct();
// here goes your function
}
}
and then extend every controller you have to Mycontroller instead of CI_Controller
if you want to use your function inside views. use CI Helpers
Codeigniter Helpers
create your helper function
function get_data() {
// and use CI instance
$CI = & get_instance();
// now you can call your models, libraries. etc. in the helper
$data = $CI->your_model->your_model_funcion();
return $data;
}
and you can call your helper function in controllers, views, models. etc...
get_data();