[eluser]Unknown[/eluser]
Hey all my first post on here and have used Codeigniter a bit in the past so hope I am not being a dumb-ass but here is my query.
I would like to make my controllers cleaner in terms of code by creating custom classes for common methods/functions that I call within them. For instance having an if statement within every controller that checks if a user is logged in could be done on a single line as a method call to a custom class.
So instead of this as a controller...
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Members_only extends CI_Controller {
public function index()
{
if($this->some_class->is_logged_in())
{
$this->load->view('welcome_message');
}
else
{
redirect('authentication/admin');
}
}
}
I would rather do this as a controller...
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Members_only extends CI_Controller {
public function index()
{
$this->some_class->is_logged_in('welcome_message', 'authentication/admin');
}
}
You can see I would like to pass the page names as parameters to my custom classes method, it is within this method that I would then like to load the view but it would seem that Codeigniter does not like the idea of me trying to load a view from within my own class - do I need to extend my class as part of a core Codeigniter object in order to inherit this ability (bearing in mind I have already tried extending my class as part of the CI_Controller and it did not work) or is there something else I need to do??
My custom class looks like so...
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Some_class{
public function __construct($allowed = NULL, $disallowed = NULL)
{
$this->allowed = $allowed;
$this->disallowed = $disallowed;
}
public function is_logged_in()
{
// PUT get_instance() IN THE FUNCTION/METHOD IF USING PHP 4
// ELSE USE WITHIN THE CONTRUCTOR
$CI =& get_instance();
$CI->load->library('session');
if($CI->session->userdata('is_logged_in'))
{
$this->load->view($this->allowed);
}
else
{
redirect($this->disallowed);
}
}
}
So essentially is it possible to use $this->load->view('abc') from within a custom class??