CodeIgniter customer Controller class says session is undefined - php

I'm using CodeIgniter (v1.7.2) and I've created a custom controller that incorporates authentication called MY_Controller (based on David Winter's blog post). When I load any controllers that use this base class, I get this error;
*Message: Undefined property: MY_Controller::$session*
Note that I am autoloading 'session' (and 'MY_controller' as a library) like so:
$autoload['libraries'] = array('database', 'session', 'MY_Controller');
Here is MY_Controller:
class MY_Controller extends Controller {
public function __construct() {
parent::__construct();
if (!$this->session->userdata('loggedin')) { <-- error is here
header('Location: /sessions/login');
exit();
}
}
}
Here's the controller that I'm trying to load:
class Welcome extends MY_Controller {
function __construct() {
parent::__construct();
}
function index() {
$this->load->view('header');
$this->load->view('welcome_message');
$this->load->view('footer');
}
}
When I var_dump $this->session above where the error occurs, I can see that it is NULL. Even putting $this->load->library('session'); in MY_Controller's constructor doesn't work. Why isn't it loading properly?
Thanks

Try taking MY_Controller out of the autoload.
$autoload['libraries'] = array('database', 'session');
You are extending the controller class which is automatically loaded by codeigniter as it is part of the core

Related

Codeigniter Controller, Config variables after upgrade

I am in the process of upgrading CI from 1.x to 3.x
From my application/controller/home.php which has a class:
class Home extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('app_controller');
}
...
And inside application/libraries/App_Controller.php
class App_Controller extends CI_Controller {
function __construct() {
parent::__construct();
$config_app = $this->config->load('Application');
...
From application/config/Application.php I am defining variables:
$config['env'] = 'dev';
$config['https'] = '//'.$_SERVER['HTTP_HOST'].'/';
$config['js'] = '/ui/js/';
But this returns an error
An uncaught Exception was encountered
Type: Error
Message: Class 'CI_App_controller' not found
Filename: C:\xampp\htdocs\myproject\system\core\Common.php
Any suggestions how to overcome this error and why App_Controller is not found.
Thank you!
your libraries shouldn't extend to CI_controller.
What you want to do in that case is move your App_controller into application/core/App_controller.php.
Then go to your application/config/config.php and change your $config['subclass_prefix'] = 'MY_'; to $config['subclass_prefix'] = 'App_';
Finally your Home controller should extend to App_controller instead like so:
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Home extends App_Controller {
function __construct() {
parent::__construct();
$this->load->library('app_controller');
}
}

Message: Undefined property: Material_model::$load Filename: libraries/Form_validation.php

I am a newbie to Codeigniter and trying to build a service registration page. Not just the page but the whole website has this error.
$autoload['model'] = array('User_model','Material_model');
$autoload['libraries'] = array('database','form_validation');
class services extends CI_Controller {
public function service1()
{
$this->form_validation->set_rules('type','Type',trim|required);
$this->form_validation->set_rules('area','Area',trim|required);
$this->form_validation->set_rules('quantity','Quantity',trim|required);
$this->form_validation->set_rules('details','Details',trim|required);
$this->form_validation->set_rules('name','Name',trim|required);
$this->form_validation->set_rules('number','Number',trim|required);
$this->form_validation->set_rules('location','Location',trim|required);
if($this->form_validation->run()==FALSE){
$data['main_view']="page_view";
$this->load->view('layouts/main',$data);
} else{
$this->user_model->create_user();
redirect('/');
}
I'm getting this error in user model from material model. Th
Actually, you should load the model in your parent construct.
For example:
class services extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('your_directory_model, 'your_initialize_model');
}
}
And then, if you want to use your model. you can build code like i write below in your method.
$this->your_initialize_model->your_model_method();

How to extend CodeIgniter's library?

So I have followed every rule there is on CI docs page but still get error
Call to undefined method CI_Form_validation::clear_field_data()
I placed the file MY_Form_validation.php in application/core directory:
class MY_Form_validation extends CI_Form_validation {
public function __construct()
{
parent::__construct();
}
public function clear_field_data() {
$this->_field_data = array();
return $this;
}
}
and then in my controller I load class as usual:
$this->load->library('form_validation');
and use my new method:
$this->form_validation->clear_field_data();
$this->load->view('product/create', $data);
Which are the mistakes in my attempt to extend the library?
Your own libraries must be in the folder: application/libraries
$this->load->library('form_validation');
$this->form_validation->clear_field_data();
$this->load->view('product/create', $data);
You're loading the wrong library? Your lib should also go in application/libraries.
$this->load->library('MY_Form_validation');
$this->MY_Form_validation->clear_field_data();
$this->load->view('product/create', $data);

CodeIgniter controller won't load a helper

I created a helper in CodeIgniter 3 which I called asset_helper.php
and this is the controller I use to call this helper:
<?php
class Index extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('asset');
}
function index()
{
$this->load->view('header');
$this->load->view('nav');
$this->load->view('title');
$this->load->view('stat1');
$this->load->view('footer');
}
}
but when I access to the controller I get this message:
Unable to load the requested file: helpers/asset_helper.php
How can I solve this problem?
Load like this
$this->load->helper('asset_helper');
If you use like this $this->load->helper('asset'); it will never works.B Bcz there is no file call asset

CI > Custom Base Controller cannot find Model

I have this file structure:
application
controllers
home.php
core
MY_Controller.php
models
users.php
These are the codes for each file:
home.php
class Home extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
echo "Hello World!";
}
}
MY_Controller.php
class MY_Controller extends CI_Controller {
var $user_model = null;
public function __construct() {
parent::__construct();
$this->user_model = new Users();
}
}
users.php
class Users extends CI_Model {
public function __construct() {
parent::__construct();
}
}
When I load the webpage on a browser, I get this error:
Fatal error: Class 'Users' not found in C:\%path%\application\core\MY_Controller.php on line 7.
Please help me make my custom base Controller to find my Model. Thank you!
NOTE:
The cases are as I have provided them (I heard there might be issues with case-sensitivities).
EDIT:
When I opened the log files, I found this.
ERROR - 2014-02-17 01:02:05 --> Severity: 8192 --> mysql_pconnect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead C:\%path%\system\database\drivers\mysql\mysql_driver.php 91
Nevermind, I got it to work by either going in the config.php file and autoloading the model or by doing $this->load->model('Users');
Upper or lower case U seems to work.

Categories