I found this tutorial (http://www.sitepoint.com/multi-language-support-in-codeigniter/) to add multilanguage support to my website. I followed it step by step. However on changing the language I get the following error code:
Fatal error: Call to a member function userdata() on null in application/hooks/LanguageLoader.php on line 9
Obviously the saved language in my cookie can not be received? How do I fix this? I appreciate any help!
The error indicates the that class that has the function userdata() is not initialized - NULL. In this case userdata() is part of the Session class.
I don't see that class being loaded anywhere in the tutorial so try this version for LangSwitch.php controller
<?php
class LangSwitch extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->helper('url');
}
function switchLanguage($language = "") {
$language = ($language != "") ? $language : "english";
$this->session->set_userdata('site_lang', $language);
redirect(base_url());
}
}
Related
I want to load model using
$this->load->model('apotek_data');
function __construct()
{
parent::__construct();
$this->load->model('apotek_data');
$this->template->write_view('sidenavs', 'template/default_sidenavs', true);
$this->template->write_view('navs', 'template/default_topnavs.php', true);
$this->load->database();
}
But when I put it ($this->load->model) in my code, the view doesn't show, and display an error message.
Fatal error: Class 'CI_Template' not found in
D:\wamp64\www\apotek\system\core\Common.php on line 196
What should I do?
Load the template library:
$this->load->library('template');
OR:
In config/autoload.php add template in the libraries array.
I don't know the name of your library so I am assuming it is template if it is not - change it with the name of the library.
For any other person experiencing a 'CI_Template not found' error:
In my case, I was loading a library (in the constructor) with the same name as the controller that was being accessed.
class Authld extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('Authld');
}
}
I had to change the name of the controller for the issue to be resolved.
class Authentication extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('Authld');
}
}
Your Template.php maybe looks like this:
class Template extends MX_Controller{
If so, change the MX to CI (CI_Controller).
I am new to Codeigniter and still trying to understand some basics.
I am building a registration/login system. everything good for now.
I am creating a controller to show the user info. Before that I want to check if the session existis and if user is logged in, if not I want to redirect to the site root.
class Myprofile extends CI_Controller {
function __construct()
{
$user_data = $this->session->userdata('user_data');
$user_data['logged_in'] = isset($user_data['logged_in']) ? $user_data['logged_in'] : null;
if($user_data['logged_in'] != 1){
redirect();
}
}
public function index(){
redirect("myprofile/info");
}
public function info(){
echo"here I'll ave my info";
}
}
The problem is the error I am getting. It looks like I it can't see the sessions in the construct part of the script.
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Myprofile::$session
Filename: controllers/myprofile.php
Line Number: 6
This would be the very best solution, otherwise I need to put the same code on all the functions.
Hope to hear some feedback help. Thanks
Perhaps the session library is not loaded. Add session class on the autoload array() located in application/config/autoload.php
eg.
$autoload['libraries'] = array('database', 'session', 'output');
class Myprofile extends CI_Controller {
function __construct()
{
$this->load->library('session');
$user_data = $this->session->userdata('user_data');
$user_data['logged_in'] = isset($user_data['logged_in']) ? $user_data['logged_in'] : null;
if($user_data['logged_in'] != 1){
redirect();
}
}
public function index(){
redirect("myprofile/info");
}
public function info(){
echo"here I'll ave my info";
}
}
My custom core Class in application/core/MY_Controller.php
class MY_Controller extends CI_Controller{
protected $data = array();
function __construct(){
parent::__construct();
}
function rander_page($view){
//do this to don't repeate in every controller
$this->load->view('includes/header');
$this->load->view('top_menus');
$this->load->view($view, $this->data);
$this->load->view('includes/footer');
}
}
Index Controller in application/controllers
class Index extends MY_Controller{
function __construct(){
parent::__construct();
}
function index(){
$this->render_page('index');
}
}
Error is :Fatal error: Call to undefined method Index::render_page() in D:\wamp\www\ci\application\controllers\index.php on line 10
i am trying to use one controller for all pages help plz
Yes, the error is the clue itself, it has mentioned the function is undefined. You have not defined render_page function. Instead, you misspelled and named it as rander_page().
May be you are trying to write:
function render_page()
as you have called it like :
$this->render_page('index');
You have misspelled the function name. Call $this->rander_page('index');
This question already has answers here:
Call to a member function on a non-object [duplicate]
(8 answers)
Closed 9 years ago.
I'm using CI 2.1.3 and I'm having issues with loading models and referring to the CodeIgniter "super object".
For example:
I'm trying to log in using my Login controller:
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Users_model');
}
public function validate_credentials() {
$this->load->library('form_validation');
// Some form validation here...
$query = $this->Users_model->validate();
if($query) { // if the user's credentials validated...
// something
} else {
// something
}
}
And when it gets to my Users_model:
class Users_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function validate()
{
$this->db->where('active', 1);
$this->db->where('email', $this->input->post('email'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('user');
if($query->num_rows == 1)
{
return $query;
}
}
}
I get an error "Fatal error: Call to a member function where() on a non-object in users_model.php on line XX" referring to the first line of validate() function.
I can get it working by using double colon (::) operator in Login controller like Users_model::validate() but I think I shouldn't need that.
I can also get it working by writing something like:
$ci = get_instance();
$ci->db->where...
at the start of the Users_model->validate() function, but I think I shouldn't need to do that either.
The database class is being loaded in autoload.php.
So the problem is that $this in my models refers to the model class itself rather than the "super object" it's supposed to. I have no moves left and I'm guessing it's about something very simple but I just can't see it. Please help me.
Please try by loading CI library like this
$this->load->library('database');
in __construct() function before loading model:
or load this library from autoload file like:
$autoload['libraries'] = array('database','form_validation', 'session');
i try to use custom 404 page from this tutorial http://maestric.com/doc/php/codeigniter_404
my controller error.php :
class Error extends Controller{
function error_404()
{
$CI =& get_instance();
$CI->output->set_status_header('404');
echo "error bro";
}
}
when i open link localhost/mading/admin/blablabla , where there is not function blablabla() in controoler admin. the output : “error bro”
i try change line in method error_404() become the code below,
class Error extends Controller{
function error_404()
{
$CI =& get_instance();
$CI->output->set_status_header('404');
$data['title'] = "404 Page Not Found";
$data['body'] = $CI->load->view('web/404','',true);
$CI->load->view('web/web_page',$data);
}
}
but, when i open link localhost/mading/admin/blablabla , where there is not function blablabla in controller admin. the output : blank page.
the function error_404 not load the view . why the 404 page not load when i open controller admin/blablabla ??
thanks
Reading the comments of that tutorial, many people are having the same problem with that code. Did you change parent::CI_Router() on line 10 to parent::__construct()?
But why not just set the $route['404_override'] variable?
Just set a proper route in your config/routes.php:
$route['404_override'] = 'your_controller/show_404';
And the corresponding controller would look like that:
class Your_controller extends CI_Controller {
function __construct() {
parent::__construct();
}
function show_404() {
$this->output->set_header("HTTP/1.1 404 Not Found");
$this->load->view ('common/errors/404'); //wherever your view is
}
}
That should do the trick for you.