Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter extends core class

I want to have class that checks login on all controllers that I specified. Codeigniter version is 2.1.0 and I have php 5.3.10

Hier is how I would set it up: I look at https://www.codeigniter.com/user_guide/general/core_classes.html and I set it up like this: in the /application/core/MY_Main.php

class MY_Main extends CI_Controller {

function __construct()
{
    parent::__construct();
}
}

In my controller I have welcome.php

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

class Welcome extends MY_Main {

    function __construct()
    {
            parent::__construct();
    }
    public function index()
    {
            $this->load->view('welcome_message');
    }
}

So if I place login check in MY-Main it should get work, but I cant get it work anyone???

like image 375
Dakadaka Avatar asked Feb 19 '12 10:02

Dakadaka


3 Answers

I needed to add the following code to /application/config/config.php before I got the extenson of core classes working as described in the CI manual.

Code taken from here http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY

function __autoload($class)
{
    if(strpos($class, 'CI_') !== 0)
    {
        @include_once( APPPATH . 'core/'. $class . EXT );
    }
}
like image 88
Chris Aelbrecht Avatar answered Oct 03 '22 20:10

Chris Aelbrecht


You logic is correct, that should work. It's exactly what I do on all my codeigniter sites. My code is a bit more complex as my login check is being called from a library (so I have to call $CI =& get_instance(); and then $CI in place of $this) but something like below should work for you. logged_in is just a name given to an item of session data set when the user logs in.

class MY_Main extends CI_Controller {

    function __construct()
    {
        parent::__construct();

        $session_data = $this->session->all_userdata();

        if(!isset($session_data['logged_in']))
            redirect('/login');
    }
}

In regards to your comment above (http 500), not really sure what's going on there. The code you have pasted shouldnt be throwing errors like that so something else is probably going on. Try turning on codeigniters built in logging functionality.

http://codeigniter.com/user_guide/general/errors.html

like image 45
leejmurphy Avatar answered Oct 03 '22 20:10

leejmurphy


You should create a library class and put it inside your library folder and load it as auto_load or inside your controllers. create functions inside your library for example:

  /**
  * 
  * @return boolean check if a user is logged in or not
  */
  function notLogin()
  {
       if (!$this->is_logged_in()){
           //echo "pelase <a href='login'><b>login</b></a> to continue ";
           redirect('home/login','refresh'); exit;
       }
       return true;
  }

and call it inside your controller constructor or any functions you want like this:

 class Main extends CI_Controller
 {
     private $POST = array();
     private $ci_form;

     function __construct()
     {
         parent::__construct();
         //check if user is logged in or not
         $this->m_auth->notLogin();
         $this->load->library('form_validation');
         $this->load->library('ajax_pagination');
     }
}
like image 35
MJ X Avatar answered Oct 03 '22 21:10

MJ X