Codeigniter, problem with extended class methods
It’s been a while since I don’t use CI and I’m with a starter doubt.
EDIT:
class MY_Controller extends CI_Con开发者_JAVA技巧troller {
public function __construct() {
parent::__construct();
if(!$this->session->userdata('usuario')) {
$this->load->view('login');
}
}
}
class Home extends MY_Controller {
public function __construct() {
parent::__construct();
Template::set('title', 'Login');
Template::set('view', 'home');
}
public function index() {
$this->load->view('template');
}
}
What happens is that is the user session is invalid, it will load the login view but as in my Home controller contructor method is calling the view home, its loading both views on the same page.
Don't put this in a hook, put it in MY_Controller
in the __construct()
method.
http://codeigniter.com/user_guide/general/core_classes.html
Example:
// file application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
// your code here
}
}
Just make sure that you extend MY_Controller
instead of CI_Controller
in the controllers you want to run this code in. If you have to change all of them, so be it.
UPDATE: You could also try a post_controller_constructor
post_controller_constructor
Called immediately after your controller is instantiated, but prior to any method calls happening.
But I would still prefer the MY_Controller method, as it is more flexible.
精彩评论