CodeIgniter Forums
Class not found - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Class not found (/thread-11294.html)

Pages: 1 2


Class not found - El Forum - 09-02-2008

[eluser]gtipete[/eluser]
im trying to do some simple authentication by extending the Controller class. Here is my code.

Code:
<?php
class MY_Auth extends Controller {
    
    function MY_Auth()
    {
        parent::Controller();
        if(!isset($this->session->userdata('logged_in')) || $this->session->userdata('logged_in') != TRUE)
        {
            redirect('user/login/', 'refresh');
            exit;
        }
    }
}
?>

This is called MY_Auth.php and located in the libraries folder. When try to extend this class, i get the following error:

Fatal error: Class 'MY_Auth' not found in C:\xampp\htdocs\gallery\system\application\controllers\gallery.php on line 2


Class not found - El Forum - 09-03-2008

[eluser]Unknown[/eluser]
Hi !

before your class definition, insert :

Code:
require_once APPPATH.'/libraries/MY_Auth.php';

Pierre


Class not found - El Forum - 09-03-2008

[eluser]Sumon[/eluser]
And make sure you have not change this line of code in config\config.php

Code:
$config['subclass_prefix'] = 'MY_'; //By default it's MY_



Class not found - El Forum - 09-03-2008

[eluser]gtipete[/eluser]
i thought that classes with the same prefix defined in the config would be automatically loaded?


Class not found - El Forum - 09-03-2008

[eluser]drewbee[/eluser]
You almost have it. Custom libraries are automatically extended from the core libraries.

In this case you are trying to extend the default controller, so you would need:

Code:
MY_Controller extends CI_Controller
{

}

Which, I do not think this is what you are trying to go for. It looks like you have a library you want to run:

/system/application/libraries/Auth.php
Code:
Auth extends Controller
{

}

Now, in our controller:

Code:
$this->load->library('auth');



Class not found - El Forum - 09-03-2008

[eluser]phpoet[/eluser]
I often put simple authentication checks in the constructor of my Controller. For example, if I were coding a blog, I might do something like this.

Code:
class Blog extends Controller {

  /**
   * Check authentication before executing any actions
   */
  public function __construct() {
    parent::__construct();
    $this->_check_authentication();
  }

  /**
   * Only allow unauthenticated access to a
   * the sign_in and register actions
   */
  private function _check_authentication() {
    $exceptions = array("sign_in", "register");
    $action = $this->uri->segment(2);
    if(!in_array($action, $exceptions)) {
      if(!$this->session->userdata('logged_in')) {
        $this->session->sess_destroy();
        redirect('blog/sign_in');
      }
    }
  }

}



Class not found - El Forum - 09-03-2008

[eluser]gtipete[/eluser]
[quote author="drewbee" date="1220480177"]It looks like you have a library you want to run:

/system/application/libraries/Auth.php
Code:
Auth extends Controller
{

}

Now, in our controller:

Code:
$this->load->library('auth');
[/quote]

What i actually want to do is extend the controller. So then in my other controllers (which need authorisation) i can do
Code:
class Gallery extends MY_Auth{

    function Gallery()
    {
        parent::My_Auth();    
    }
}

Then i dont have to put the authorisation code in every controller.

Thanks


Class not found - El Forum - 09-03-2008

[eluser]drewbee[/eluser]
Ah. As far as I know, you can only auto extend it two classes deep, and you are trying to go three. Controller > Auth > Gallery.

One thing I don't really understand is why you need to extend the authentication class for yoru Gallery class? Your actually breaking the rules of MVC and object orientation. Auth has nothing to do with Galleries. IE Gallery is not a 'type of' Auth. Sure, they should definitely interact, but extension I wouldn't do.

Here is what I would do.

Code:
// Everything your authentication class does. Put any auto-executing code in the __construct() function
class Auth extends Controller
{
     function __construct()
     {
          parent::__construct();
     }

     function isAllowed($action)
     {
         // return true if user is allowed to do action
     }
}

// our gallery class
class Gallery extends Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->library('auth');
    }

    function showImage($image)
    {
         if ($this->auth->isAllowed('view_image'))
         {
             echo $image;
         }
    }

}

Do you see how you do not need inheritance, yet you can keep your auth on every single page that you need it by simply loading it. Also, you can also auto-load it on every page if you do not want to manually load it by using the autoload configuration directive.


Class not found - El Forum - 09-03-2008

[eluser]Pascal Kriete[/eluser]
I've done what you're trying to do, and still do it a lot, just not for authentication.

You were doing it correctly, but it has to be in a file (class name is irrelevant) called MY_Controller.php (not MY_Auth) as that is what CI includes when it instantiates the base Controller class (which, btw is only called Controller, not CI_Controller).

@drewbee: why is your library extending the controller??


Class not found - El Forum - 09-03-2008

[eluser]drewbee[/eluser]
Sorry. I mixed up a library and a controller.

Unfortunately I have biased myself against extending the controller class because of building against the default controller. Unfortunately any extension to it would require me to go back through all of my other controllers to adjust for the new controller class name.