Codeigniter,扩展类方法的问题
我已经有一段时间没有使用 CI 了,我开始对此产生怀疑。
编辑:
class MY_Controller extends CI_Controller {
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');
}
}
发生的情况是用户会话无效,它将加载登录视图,但就像在我的Home控制器构造函数方法中调用视图home< /em>,它在同一页面上加载两个视图。
It’s been a while since I don’t use CI and I’m with a starter doubt.
EDIT:
class MY_Controller extends CI_Controller {
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要将其放在钩子中,而是将其放在
__construct()
方法中的MY_Controller
中。http://codeigniter.com/user_guide/general/core_classes.html
:
示例 确保在要运行此代码的控制器中扩展
MY_Controller
而不是CI_Controller
。如果您必须更改所有这些,那就这样吧。更新:您也可以尝试
post_controller_constructor
但我仍然更喜欢 MY_Controller 方法,因为它更灵活。
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:
Just make sure that you extend
MY_Controller
instead ofCI_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
But I would still prefer the MY_Controller method, as it is more flexible.