I am using codeigniter with sqlite and I have created a new library for connecting the sqlite database. The sqlite database will connect only if the session is set.
LIBRARY (userdb)
$CI =& get_instance();
if( $CI->session->userdata('user') ){
$userId = $CI->session->userdata('user');
$this->conDb = new PDO("sqlite:" . BASEPATH . "sqlitedb/" . $userId . "/data.db");
}else{
$userId = '';
}
And I execute queries by using $query = $this->userdb->query($sql);
It works fine but the problem is that query only works if the page is refreshed after session is created otherwise it returns Call to a member function query() on a non-object error.
CONTROLLER
$sessiondata = array('user' => $userid);
$this->session->set_userdata($sessiondata);
$insert = $this->user_model->insert($data);
All this seems legit, if you set your userdata while the page/controller is loading it means that the librairies and other assets have been loaded already. So when your userdb library is loaded there is no sessions set yet.
You have two ways to fix this : Try to Load your library after loading your controller but for a db library this would be better to be in your config file so that it is auto loaded.
The other way would be to reload or redirect your current PAGE after the session isset. So that you can use your db library
Hope that helps You
NwK
Related
<?php
$request = request();
// if (empty($request)) return false; .// That does not work
$loggedUserAccessGroups = $request->session()->get('loggedUserAccessGroups');
$logged_user_ip = $request->session()->get('logged_user_ip');
In my Laravel 5.7 application, I want to check if the user has the right access level in the session. It works ok but I made automatic tests and got the error:
local.ERROR: Session store not set on request.
I added checks to see if the session is set and it fails to return false.
Which is the correct way? Thanks!
You may also use the global session PHP function to retrieve and store data in the session as outlined here:
// Retrieve a piece of data from the session with the global session helper...
$loggedUserAccessGroups = session('loggedUserAccessGroups');
$logged_user_ip = session('logged_user_ip');
// Store a piece of data in the session...
session(['key' => 'value']);
For more information look at the section of The Global Session Helper in the official documentation.
I am trying to set session in views/errors/html/error_404.php session library already load in autoload file.
$tech_error = array('is_error_404' => 'yes','is_error_404_data' => $content);
$this->session->set_userdata($tech_error);
Its throwing error, Fatal error: Call to a member function set_userdata() on null
can't we access CodeIgniter session in error views?
Try this
$ci = & get_instance();
$ci->session->set_data('is_error_404','yes');
$ci->session->set_data('is_error_404_data',$content);
then retrieve each session data ? using
$this->session->userdata('is_error_404');
I am having problems with storing values in a session. I am using laravel 5.3 on xampp local host. I have tried the following but when I try to access the session is empty:
//using global session helper method
$variable = session(['exam'=>$exam]);
$exam = session('exam');
echo $exam; //exam is always empty
//I have also tried this
public function myControllermethod(Request $request)
{
$exam = $request->session()->put('exam',$exam);
//get the session
$exam = $request->session()->get('exam');
echo $exam // this approach returns the following exception
}
RuntimeException in Request.php line 905:
Session store not set on request.
I have tried both the file and database drivers without lack. Sessions are actually not being registered at all. Session::get('key') also do not work,
How can I start sessions in laravel 5.3. Any Help?
I'm attempting to create dynamic routing in Laravel for my controllers - I know this can be done in Kohana, but I've been unsuccessful trying to get it working with Laravel.
This is what I have right now:
Route::get('/{controller}/{action?}/{id?}'...
So I would like to call controller/method($id) with that.
Ideally this is what I would like to do:
Route::get('/{controller}/{action?}/{id?}', $controller . '#' . $action);
And have it dynamically call $controller::$action.
I've tried doing this:
Route::get('/{controller}/{action?}/{id?}', function($controller, $action = null, $id = null)
{
$controller = new $controller();
$controller->$action();
});
But I get an error message: Class Controller does not exist.
So it appears that Laravel is not including all the necessary files when the controller extends the BaseController.
If I use $controller::$action() it tells me I can't call a non-static function statically.
Any ideas for how to make this work?
You can auto register all controllers in one fell swoop:
Route::controller( Controller::detect() );
If you're using Laravel 4 (as your tag implies), you can't use Controller::detect() anymore. You'll have to manually register all the controllers you want to use.
After reading that Laravel doesn’t support this anymore, I came up with this solution:
$uri = $_SERVER['REQUEST_URI'];
$results = array();
preg_match('#^\/(\w+)?\/?(\w+)?\/?(\w+)?\/?#', $_SERVER['REQUEST_URI'], $results);
// set the default controller to landing
$controller = (empty($results[1])) ? 'landing' : $results[1];
// set the default method to index
$method = (empty($results[2])) ? 'index' : $results[2];
Route::get('{controller?}/{action?}/{id?}', $controller . '#' . $method);
// now we just need to catch and process the error if no controller#method exists.
I've got an issue connecting to the database with CodeIgniter.
In the model, if I do
$this->load->database();
then a query such as
$query = $this->db->get('articles', 10);
works and returns data. However, when I remove the load->database line and try autoloading the database library, using
$autoload['libraries'] = array('database');
in application/config/autoload.php, the above query fails. As it works when I explicitly load the library, it's not a problem with my database connection. I'm using CodeIgniter 2.0.2 (the current latest release).
As most of my pages use the database I'd like to autoload it to avoid having to load it anually in each model. Have I misunderstood the documentation regarding loading the database library or am I doing something wrong?
I know I am probably just answering a dead question but i had the same problem.
if you are using eclipse pdt or similar IDE and modified the system>core>model.php by adding some variables just before the constructor to get code completion then the autoload doesnt work
i dont know why but the fact is it doesnt work. I restored the core>model.php to original and autoload works fine. I dont get autoload now and its really a bad experience for new coder to CI.
If you have a workaround please comment so.
This is just an alternative if you still can't resolve the issue. Just extend the CI_Model and auto load the database library in the constructor. Then you can just make all other models inherit from that base class.
Example:
<?php
class My_Model extends CI_Model{
public function __construct(){
parent::__construct();
$this->load->database();
}
}
And the all others will start like :
<?php
class Some_Model extends MY_Model{}
This method has the other benefit that you don't have to include the loading code line in every model you create. Also you can easily push shared functionalities among models into the parent MY_Model class.
Hope it helps!
This is my database.php from my application/config/ directory
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'mydbhost';
$db['default']['username'] = 'myusername';
$db['default']['password'] = 'mypassword';
$db['default']['database'] = 'mydatabase';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
Check yours looks like this and rerun through the user guide at http://codeigniter.com/user_guide/database/configuration.html and reread the comments in the autoload.php file in the config directory.
The relevant line in mine reads:
$autoload['libraries'] = array('database', 'form_validation', 'session');
You should be loading the db library like this:
$this->load->library('database');
The only thing I can think of is in your application/config/database.php file, the following line is set to false, instead of true.
//['autoinit'] Whether or not to automatically initialize the database.
$db['default']['autoinit'] = TRUE;
Everything else looks ok.
Shababhsiduque,
Just you we're right, at least in my case!
The workaround to getting the ide to work is creating the reference variables in ANOTHER CI project.
That is leave the system model as it is in the current project but modify it in another project.
Then change the php buildpath (project->properties->phpbuildpath) to the project with the variables.
Note this are settings for Aptana Studio 3.
Worked for me.
You could try PHP Autoload Class Magic function in this case.
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
#include_once( APPPATH . 'libraries/'. $class . '.php' );
}
}
This code must copied inside the config.php (after the last line) within Application folder.
This code will automatically load libraries for you when its needed.
It might work. Happy coding.
In my case the problem was because there was another occurrence of $autoload['libraries'] inside the same file and it reset the array.