Welcome Guest, Not a member yet? Register   Sign In
Problem with $this in child Class extend of MX_Controllers
#1

(This post was last modified: 01-31-2020, 10:59 AM by avidal.)

Hi!

I am having an error with the $ this reserved variable in my daughter classes inherited from MX_Controllers.

It is because the & get_intance () does not arrive completely. Can you help me find the error please.

PHP Version: 7.2


Error: 
Fatal error: Uncaught Error: Using $this when not in object context in D:\Proyectos\OEFA\SGSA\application\modules\admin\controllers\login.php:14 Stack trace: #0 D:\Proyectos\OEFA\SGSA\application\third_party\MX\Controller.php(170): login::index() #1 D:\Proyectos\OEFA\SGSA\system\core\CodeIgniter.php(326): MX_Controller->_remap('index', Array) #2 D:\Proyectos\OEFA\SGSA\index.php(202): require_once('D:\\Proyectos\\OE...') #3 {main} thrown in D:\Proyectos\OEFA\SGSA\application\modules\admin\controllers\login.php on line 14


My Class:

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
login extends MX_Controller {

    public function 
__construct()
    {
        
parent::__construct();
        
                
    
}
    
    
    function 
index()
    {    
        
$redirect    $this->auth->is_logged_in(falsefalse);
        if(
$redirect)
        {
            
redirect('admin/dashboard');
        }
        
        
        
$this->load->helper('form');
        
$data['redirect']    = $this->session->flashdata('redirect');
        
$submitted             $this->input->post('submitted');
        if (
$submitted)
        {
            
$username    $this->input->post('username');
            
$password    $this->input->post('password');
            
$remember   $this->input->post('remember');
            
$redirect    $this->input->post('redirect');
            
$login        $this->auth->login_admin($username$password$remember);
           
            $redirect 
site_url('admin/dashboard');
            if (
$login)
            {
                if (
$redirect == '')
                {
                    
$redirect site_url('admin/dashboard');
                }
                
redirect($redirect);
            }
            else
            {
                
//this adds the redirect back to flash data if they provide an incorrect credentials
                
$this->session->set_flashdata('redirect'$redirect);
                
$this->session->set_flashdata('error'lang('authenication_failed'));
                
redirect('admin/login');
            }
        }
        
$this->load->view('login/login'$data);
    }
?>


MX_Controller:

PHP Code:
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

/** load the CI class for Modular Extensions **/
require dirname(__FILE__).'/Base.php';

/**
 * Modular Extensions - HMVC
 *
 * Adapted from the CodeIgniter Core Classes
 * @link    http://codeigniter.com
 *
 * Description:
 * This library replaces the CodeIgniter Controller class
 * and adds features allowing use of modules and the HMVC design pattern.
 *
 * Install this file as application/third_party/MX/Controller.php
 *
 * @copyright    Copyright (c) 2011 Wiredesignz
 * @version     5.4
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 **/
class MX_Controller extends CI_Controller
{
    public 
$autoload = array();
    
    public function 
__construct() 
    {                        

        
$this->load->helper('language');
            
//echo '<pre>'; print_r($this->session->all_userdata());die;
        
        
if($this->uri->segment(2)!="login" && $this->uri->segment(1)!="register" && $this->uri->segment(1)!="forgot"){
            
$this->auth->check_session();
        }
        if(
$this->session->userdata('lang')!="")
        {
            
$this->lang->load('admin',$this->session->userdata('lang'));
        }else{
            
$this->lang->load('admin''english');
        }
        
        
$class str_replace(CI::$APP->config->item('controller_suffix'), ''get_class($this));
        
log_message('debug'$class." MX_Controller Initialized");
        
Modules::$registry[strtolower($class)] = $this;    
        
        
/* copy a loader instance and initialize */
        
$this->load = clone load_class('Loader');
        
$this->load->initialize($this);    
        
$this->load->model("setting_model");
        
        
        
        
/* autoload module items */
        
$this->load->_autoloader($this->autoload);
        
$this->settings $this->setting_model->get_setting();
        if(!empty(
$this->settings->timezone)){
            
date_default_timezone_set($this->settings->timezone);
        }
        if (!
function_exists('check_user_role'))
        {
                
                function 
check_user_role($action_id){
                    
$CI = &get_instance();
                    $ci_admin=$CI->session->userdata('admin');
                    
$access    $ci_admin['user_role'];
                    
                    
if($access!=1){    
                        $CI
->db->where('action_id',$action_id);
                        
$CI->db->where('role_id',$access);
                    
    $result $CI->db->get('rel_role_action')->row();
                        if(
count($result) > 0){
                            return 
1;
                        }else{
                            return 
0;
                        }
                    }else{
                        return 
1;
                    }
                }
        }    
        
        
    
    }


    public function 
_remap($action$arguments)
    {    
        
$this->load->helper('url');
        
$this->load->model("user_role_model");
        
$allowed false;
        $ci_admin=$this->session->userdata('admin');
            if(
$ci_admin['user_role'] != OR $ci_admin['user_role'] !=2){
            
                
$controller strtolower($this->router->class);                
                
$parent_id $this->user_role_model->get_action_parent_id($controller);
                
$depart_id $ci_admin['user_role'];
                
$user_id $ci_admin['id'];    
                
//echo $depart_id.'/'.$user_id.'/'.$parent_id ;exit;
                
if(empty($depart_id)){
                    
$allowed=true;
                }elseif(
$depart_id==1){
                    
$allowed=true;    
                }elseif(!
is_array($parent_id) && $parent_id!=''){
                    if(
$action=='index'){
                        
$res $this->user_role_model->get_action_id_by_name_parent($controller); 
                        
//echo '<pre>'; print_r($res);die;
                        
$always_allowed $res->row('always_allowed');
                        
$action_id $res->row('id');
                        
                        if(!
$always_allowed) {
                         
$is_allowed $this->user_role_model->check_is_allowed($depart_id$action_id);
                            if(
$is_allowed){
                                
$allowed=true;
                            }else{
                                
$allowed=false;
                            
                            
                            }
                        }else{
                        
                            
$allowed=true;
                        }
                        
                    }elseif(
$res $this->user_role_model->get_action_id_by_name_parent($action$parent_id)) {
                        
                        
$always_allowed $res->row('always_allowed');
                    
                        
$action_id $res->row('id');
                        if(!
$always_allowed) {
                            
$is_allowed $this->user_role_model->check_is_allowed($depart_id$action_id);
                            if(
$is_allowed){
                                
$allowed=true;
                            }else{
                                    
                                
$allowed=false;
                            }
                        }else{
                            
$allowed=true;
                        }
                    }
                }else{
                    
$allowed=true;
                }
                

                if(@$this->session->userdata['admin']['user_role']==&& $action=='send_message'
                    $allowed=true;
                
                
if($allowed){
                    if (
method_exists($this$action))
                    {
                        
call_user_func_array(array($controller$action), $arguments);
                    } else {
                        
show_404();
                    }
                } else {                    
                    
$this->session->set_flashdata('error'lang('no_access'));
                    
redirect('admin/dashboard''refresh');
                }
                
            
}//END USSER ROLE FOR != 1/2
    
}    
    
    
    
    public function 
__get($class) {
        return 
CI::$APP->$class;
    }
    



Thanks-


Attached Files
.php   login.php (Size: 1.52 KB / Downloads: 2)
.php   Controller.php (Size: 5.73 KB / Downloads: 3)
Reply
#2

(This post was last modified: 02-01-2020, 04:14 AM by ajmeireles.)

As I can see the object $this->auth really isn't recognized by Login class. Where is this object, on Base.php?

If this object is from other controller or lib I recommend create an lib or helper and subsequetly create a superglobal. On my latest project based on CI3 I have a superglobal ci that's instancied on BaseController, like this:

PHP Code:
class BaseController extends CI_Controller
{
    public 
$ci;
    public function 
__construct()
    {
        
$this->ci &= get_instance();
        
$this->ci->global->superglobal();
    }
}

/* Global is a library */
class Global
{
    public 
$panel;
    public function 
__construct()
    {
        
$this->ci &= get_instance();
    }
    public function 
Superglobal()
    {
        
$this->panel "abc";
    }



Then I can use $this->ci->panel to show "abc" where I want: controller or views.
(it's necessary running Global with part of autoloader)
Reply
#3

(This post was last modified: 02-01-2020, 04:24 AM by InsiteFX.)

If this is just the downloaded version of HMVC then there are all kinds of fixes
on the forum topics here. Do a forum search for HMVC.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(02-01-2020, 04:14 AM)ajmeireles Wrote: As I can see the object $this->auth really isn't recognized by Login class. Where is this object, on Base.php?

If this object is from other controller or lib I recommend create an lib or helper and subsequetly create a superglobal. On my latest project based on CI3 I have a superglobal ci that's instancied on BaseController, like this:

PHP Code:
class BaseController extends CI_Controller
{
    public 
$ci;
    public function 
__construct()
    {
        
$this->ci &= get_instance();
        
$this->ci->global->superglobal();
    }
}

/* Global is a library */
class Global
{
    public 
$panel;
    public function 
__construct()
    {
        
$this->ci &= get_instance();
    }
    public function 
Superglobal()
    {
        
$this->panel "abc";
    }



Then I can use $this->ci->panel to show "abc" where I want: controller or views.
(it's necessary running Global with part of autoloader)

Thanks, but I don't want to replace all my classes and files that have $ this-> with $ this-> ci-> are many files. Why doesn't the father's constructor inherit?

Any other solution?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB