My custom library function is not working - php

I am gettig a error on my library
Undefined property: Authenticate::$ci
Here is my custom library function
function is_logged_in() {
$sessionid = $this->ci->session->userdata('moderId');
if($sessionid) {
return isset($sessionid);
} else if(!$sessionid) {
redirect(base_url() . 'moderator');
}
}
Here is my controller
class B2bcategory extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('moderator/b2bcategory_model');
$this->authenticate->is_logged_in();
}
}

I did not see if you had loaded the get_instance() in your construct area of library, Try some thing like this in your authenticate library
Filename: Authenticate.php
<?php
class Authenticate {
public function __construct() {
$this->CI =& get_instance();
}
function is_logged_in() {
$sessionid = $this->CI->session->userdata('moderId');
if($sessionid) {
return isset($sessionid);
} else if(!$sessionid) {
redirect(base_url() . 'moderator');
}
}
}
I have found that seems to work better with $this->CI on your library with get_instance.
To load library on controller if you have not autoload it use.
Filename: B2bcategory.php
<?php
class B2bcategory extends CI_Controller {
public function __construct() {
parent::__construct();
// Or Auto load it
$this->load->library('authenticate');
$this->load->model('moderator/b2bcategory_model');
$this->authenticate->is_logged_in();
}
}

$this->load->model('moderator/b2bcategory_model', 'authenticate');
$this->load->model('REAL_MODEL_PATH', 'PROPERTY_IN_CONTROLLER')
USAGE $this->PROPERTY_IN_CONTROLLER->library_method()
but best way is in application/core create ModeratorController.php
ModeratorController extends CI_Controller
{
public function __construct()
{
parent::__construct();
if($this->session->userdata('moder_id') === false)
{
redirect('site/moder_login');
}
}
}
And all moder controllers extend from this controller

Related

Problems with the constructor of a class when extending from another I can not use methods in the constructor

i have two class, one(controller class) extend from another, then in the controller class, I define a variable "load" (in the construct), but when i extend from another class i can't invoke this variable from the constructor, any ideas? (Apologies for my bad english).
Class Controller:
<?php
class Controller {
protected $load;
public function __construct() {
$this->load = new Loader();
if($_GET && isset($_GET['action']))
{
$action = $_GET['action'];
if(method_exists($this, $action))
$this->$action();
else
die('Method not found.');
} else {
if(method_exists($this, 'index'))
$this->index();
else
die('Index method not found.');
}
}
}
Class home ( Where does it extend):
<?php
class Home extends Controller
{
function __construct() {
parent::__construct();
$this->load->model("HomeModel");// this line doesn't work
}
public function index() {
$articles = new HomeModel();
$articles = $articles->getData();
$nombres = ['jona', 'juan', 'jose'];
$view = new Views('home/home', compact("nombres", "articles"));
}
}
Loader Class:
<?php
class Loader
{
function __construct() {
}
public function model($model) {
require('./models/'.$model.'.php');
}
}
The error "'HomeModel' not found" would lead me to believe that you are not requiring the file that contains the 'HomeModel' class in the 'Home' class file.

Can not load url helper in codeigniter

I tried to load base_url() in controller, but codeigniter does not load the helper('url'). I also call helper from autoload and the constructor both in the hook, but it's still not working and showing an error "Trying to get property of non-object".
Any idea how can I redirect?
My code:
if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
class Auth_hook {
protected $CI;
public function __construct() {
$this->CI =& get_instance();
$this->CI->load->helper('url');
}
public function index(){
redirect(base_url('auth/login'));
print_r("hello!!");
if(isset($_SESSION['name']) == 'TRUE'){
redirect(base_url('auth/admin'));
}
else {
redirect(base_url('auth/login'));
}
}
}
How about this:
class Auth_hook {
protected $CI;
public function __construct() {
$this->CI =& get_instance();
}
public function index(){
// can communicate back with CI by using $this->CI
$this->CI->load->helper('url');
redirect(base_url('auth/login'));
print_r("hello!!");
if(isset($_SESSION['name']) == 'TRUE'){
redirect(base_url('auth/admin'));
}
else {
redirect(base_url('auth/login'));
}
}
}

Use controller from another controller CodeIgniter and HMVC

I want to load a function from another controller. This is my structure:
- modules
--orderpages
---controllers
----WebshopCore.php
----WebshopController.php
My function insertItemInCart in WebshopController.php is called. But when i want to execute a function from another controller it crashes.
class WebshopController extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->module('orderPages/WebshopCore');
}
function insertItemInCart(){
$partId = $this->input->post('partId');
$quantity = $this->input->post('quantity');
$output = $this->WebshopCore->getPickLocations($partId,$quantity);
}
}
My WebshopCore:
class WebshopCore extends MX_Controller {
public function __construct() {
parent::__construct();
}
public function getPickLocations($partId,$amount){
$result = "test";
return $result;
}
}
What goes wrong? I don't get it
The solution:
$output = modules::load('orderPages/WebshopCore/')->getPickLocations($partId,$quantity);
You should write a library in that case.
In application/libraries create Cart.php (or whatever you want)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cart
{
protected $ci;
public function __construct()
{
$this->ci =& get_instance();
}
public function getPickLocations($partId, $qty)
{
//Your stuff
}
}
And then in your controllers :
$this->load->library("cart");
$data = $this->cart->getPickLocations($this->input->post('partId'), $this->input->post('quantity'));

using external library classes in codeigniter

i have crated a custom library file for the login validation. if i call the custom library at before $this->load->library('form_validation');
class VerifyLogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('loginuser');
$this->load->library('validate_login','','session_validation');// where validate_login is the custom library class inside applications/libraries/validate_login.php
}
function index()
{
//my code
$this->load->library('form_validation');
}
}
This is the error i got $this->load->library('form_validation'); it works perfectley.
i just want to know Why this code works? Am i overwriting the default libraries?
Message: Undefined property: VerifyLogin::$form_validation
if i load the library after
class VerifyLogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('loginuser');
$this->load->library('form_validation');
$this->load->library('validate_login','','session_validation');// where validate_login is the custom library class inside applications/libraries/validate_login.php
}
function index()
{
//my code
}
}
It looks like you're loading your libraries wrong. Try one of these;
Multiple
$this->load->library(array('library1', 'library2'));
Single
$this->load->library('library1');
$this->load->library('library2');
#sobiaholic
this is validate_login
class Validate_login extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('loginuser');
$this->load->helper('url');
}
function is_logged()
{
if(isset($this->session->userdata['my_session_id']))
{
if(strlen($this->session->userdata['my_session_id']))
{
return TRUE;
}
else
{
return FALSE;
}
}
else
{
return FALSE;
}
}
function validate_login()
{
$session_id=$this->session->userdata['my_session_id'];
$this->db->select('last_activity,user_data');
$this -> db -> from('sessions');
$this->db->where('session_id',$session_id);
$this -> db -> limit(1);
$query = $this -> db -> get();
if($query -> num_rows() == 1)
{
$results=$query->result();
$active_session=$this->session_alive($results[0]->last_activity,$session_id);
if($active_session==TRUE)
{
return TRUE;
}
else
{
$this->verifylogin->logout();
}
}
else
{
return false;
}
}
function session_alive($valid_till,$session_id)
{
$time_limit=$this->config->item('sess_expiration');
if (time() - $valid_till > $time_limit)
{
return FALSE;
}
else
{
if($this->update_session($session_id)==TRUE)
{
return TRUE;
}
else
{
return FALSE;
}
}
}
function update_session($session_id)
{
$new_time=time();
$data=array('last_activity'=>$new_time);
$this->db->where('session_id',$session_id);
$this->db->update('sessions',$data);
if($this->db->affected_rows())
{
return TRUE;
}
}
}

How to run method inside a Model | MVC

In the index file i have _autoload and load the libs and then i explode the url to get the wanted contoller and the model if exists. In the view i can see the model __construct() so the model is loaded but if i try to use $this->model->test(); i get
Call to a member function test() on a non-object
http://site.com/about
$this->request = about;
$controller = new $this->request;
$controller->loadModel($this->request);
Everething work ok
*Here is the Main controller *
class Conroller {
function __construct() {
// echo 'Main controller<br />';
$this->view = new View();
}
public function loadModel($name) {
$path = 'models/'.$name.'_model.php';
if (file_exists($path)) {
require 'models/'.$name.'_model.php';
$modelName = $name . '_model';
// **here i make the object**
$this->model = new $modelName();
}
}
}
Here is the About model
class about_model{
function __construct() {
echo 'test';
}
public function test() {
$test = 'test one';
}
}
Here is the About Conroller
class About extends Conroller {
function __construct(){
parent::__construct();
$this->model->test();
$this->view->render('/about');
}
}
You will need to call loadModel in your About controller before you refer to the model:
class About extends Conroller {
function __construct(){
parent::__construct();
$this->loadModel('about');
$this->about->test();
}
}

Categories