显示从Admin_Controller到任何视图的错误消息

时间:2015-03-15 05:49:32

标签: codeigniter

我希望能够在核心文件夹core / Admin_Controller.php中显示来自我的Admin_Controller的数据消息,然后显示在我的登录视图中。

我似乎只能使用会话闪存数据,但不想使用闪存数据。

那么从我的Admin_Controller获取$data['error_warning']消息以便能够在我的Login控制器和视图上工作的最佳方法是什么。

<?php 

class Admin_Controller extends MX_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('user');

        Modules::run('admin/error/permission/check');

        $ignore = array(
            'login',
            'logout'
        );

        if (!in_array($this->router->fetch_class(), $ignore)) {
            if ($this->session->userdata('user_id') == FALSE) {
                $data['error_warning'] = 'You have tried to directly access controller without logging on! Please login.';
                redirect('admin');
            }
        }
    }
}

登录控制器:

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

class Login extends Admin_Controller {

    private $error = array();

    public function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
    }

    public function index() {        
        $data['title'] = 'Administration';

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        $username = $this->input->post('username');

        if (isset($username)) {
            $data['username'] = $username;
        } else {
            $data['username'] = '';
        }

        $password = $this->input->post('password');

        if (isset($password)) {
            $data['password'] = $password;
        } else {
            $data['password'] = '';
        }

        $this->form_validation->set_rules('username', 'Username', 'required|callback_validate');
        $this->form_validation->set_rules('password', 'Password', 'required');

        if ($this->form_validation->run($this) == FALSE) {

            $this->load->view('template/common/login.tpl', $data);

        } else {

            redirect('admin/dashboard'.'/'.$token);

        }

    }

    public function validate() {

        $this->load->library('user');

        if ($this->user->login() == FALSE) {

            $this->form_validation->set_message('validate', '<i class="fa fa-exclamation-triangle"></i> Does not match any of our database records!');

            return false;

        } else {

            return true;
        }
    }
}

登录视图

<?php echo Modules::run('admin/common/header/index');?>
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-8 col-md-offset-2 col-sm-10 col-sm-offset-1 col-xs-12">
<div class="panel panel-default" style="margin-top: 12.5%;">
<div class="panel-heading"><strong><i class="fa fa-key"></i> Enter Details To Login </strong></div>
<div class="panel-body">

    <?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>

    <?php $data = array('class' => 'form-horizontal');?>
    <?php echo form_open('admin', $data);?>

    <?php if ($error_warning) { ?>
        <div class="alert alert-danger text-center"><i class="fa fa-exclamation-triangle"></i> <?php echo $error_warning; ?>
        <button type="button" class="close" data-dismiss="alert">&times;</button>
        </div>
    <?php } ?>

    <?php if ($this->session->flashdata('error')) { ?>
        <div class="alert alert-danger text-center"><i class="fa fa-exclamation-triangle"></i> <?php echo $this->session->flashdata('error'); ?>
        <button type="button" class="close" data-dismiss="alert">&times;</button>
        </div>
    <?php } ?>

    <div class="form-group">
    <?php $data = array('class' => 'col-sm-2 control-label');?>
    <?php echo form_label('Username', 'username', $data);?>
    <div class="col-sm-10">
    <?php 
    $data_username = array(
    'id' => 'username', 
    'name' => 'username', 
    'class' => 'form-control', 
    'placeholder' => 'Username',
    'value' => $username
    )
    ;?>
    <?php echo form_input($data_username);?>
    </div>
    </div>

    <div class="form-group">
    <?php $data = array('class' => 'col-sm-2 control-label');?>
    <?php echo form_label('Password', 'password', $data);?>
    <div class="col-sm-10">
    <?php 
    $data_password = array(
    'id' => 'password', 
    'name' => 'password', 
    'class' => 'form-control',
    'placeholder' => 'Password', 
    'value' => $password
    )
    ;?>
    <?php echo form_password($data_password);?>
    </div>
    </div>

    <div class="text-right">
    <button type="submit" class="btn btn-primary">Login Now</button>
    </div>
    </div>
    <div class="panel-footer text-right">
    Not registered on admin ?
    <a href="<?php echo base_url();?>admin/register" target="_blank">click here </a>
    <?php echo form_close();?>
    </div>
</div>
</div>
</div>
<?php echo Modules::run('admin/common/footer/index');?>

1 个答案:

答案 0 :(得分:1)

尝试在Admin_Controller中定义类属性,如下所示:

<?php 

class Admin_Controller extends MX_Controller {
    // Change here
    protected $ignore;
    protected $data;        

    public function __construct() {
        parent::__construct();
        $this->load->library('user');

        Modules::run('admin/error/permission/check');

        $this->ignore = array(
            'login',
            'logout'
        );

        if (!in_array($this->router->fetch_class(), $ignore)) {
            if ($this->session->userdata('user_id') == FALSE) {
                $this->data['error_warning'] = 'You have tried to directly access controller without logging on! Please login.';
                redirect('admin');
            }
        }
    }
}

此外,如上所述,将对$data$ignore的每个引用更改为$this->data$this->ignore。您还需要在Login课程中执行此操作。总是在引用类属性(即在类中的方法外声明的变量)时,$this->PROPERTY_NAME执行此操作。