CodeIgniter扩展多个控制器? [英] CodeIgniter Extending Multiple Controllers?

查看:195
本文介绍了CodeIgniter扩展多个控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找不到办法做这个,可能是因为有另一种方法这样做?



我的一些控制器扩展AdminLayout,其中一些扩展ModLayout但我还需要这些页面来扩展LoggedIn控制器。

  class Profile extends AdminLayout,LoggedIn {

但是看着没有办法做这个很好。有没有解决方法?

解决方案

假设你使用Codeigniter 2,这可以通过将所有扩展控制器类

/ application / core 中创建一个名为 MY_Controller.php 的文件在第109行的 config.php 中检查子类前缀)



在这里你可以添加所有控制器类来扩展。例如:

 <?php if(!defined('BASEPATH'))exit ); 
/ **
* MY_Controller类
*
*
* @package项目名称
* @subpackage控制器
* /
class MY_Controller extends CI_Controller {

public function __construct(){
parent :: __ construct();
$ this-> form_validation-> set_error_delimiters('< div class =form-error>','< / div>');
}
}

类LoggedIn extends MY_Controller {

public function __construct(){
parent :: __ construct
if(is_logged_in()== FALSE){
$ this-> session-> set_userdata('return_to',uri_string());
$ this-> session-> set_flashdata('message','您需要登录。
redirect('/ home');
}
}
}

class AdminLayout extends LoggedIn {

public function __construct(){
parent :: __ construct ();
//做某事
}
}

类ModLayout扩展LoggedIn {

public function __construct(){
parent ::__构造();
//做某事
}
}

/ *文件结束* /
/ *位置:./application/core/ * /

然后当按照正常方式创建控制器时,只需选择基本控制器类来扩展。示例;

  class Foo extends AdminLayout {

public function __construct(){
parent ::__构造();
if(is_logged_in()== FALSE){
$ this-> session-> set_userdata('return_to',uri_string());
$ this-> session-> set_flashdata('message','您需要登录。
redirect('/ home');
}
}
}

  class Bar extends ModLayout {

public function __construct(){
parent :: __ construct
if(is_logged_in()== FALSE){
$ this-> session-> set_userdata('return_to',uri_string());
$ this-> session-> set_flashdata('message','您需要登录。
redirect('/ home');
}
}
}


Can't find a way to do this, possibly because there is another way to do this?

Some of my controllers extend AdminLayout and some of them extend ModLayout but I also need these pages to extend a LoggedIn Controller.

class Profile extends AdminLayout, LoggedIn {

However looking into there is no way to do this nicely. Is there a workaround?

解决方案

Assuming that you are using Codeigniter 2, this can be done by putting all you extended controller classes in the same file.

In /application/core create a file called MY_Controller.php (don't forget to check the subclass prefix in config.php around line 109)

In here you can add all you controller classes to extend. For example;

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * MY_Controller Class
 *
 *
 * @package Project Name
 * @subpackage  Controllers
 */
class MY_Controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->form_validation->set_error_delimiters('<div class="form-error">', '</div>');
    }
}

class LoggedIn extends MY_Controller {

    public function __construct() {
        parent::__construct();
        if (is_logged_in() == FALSE) {
            $this->session->set_userdata('return_to', uri_string());
            $this->session->set_flashdata('message', 'You need to log in.');
            redirect('/home');
        }
    }
}

class AdminLayout extends LoggedIn {

    public function __construct() {
        parent::__construct();
        // do something
    }
}

class ModLayout extends LoggedIn {

    public function __construct() {
        parent::__construct();
        // do something
    }
}

/* End of file  */
/* Location: ./application/core/ */

Then when you create your controllers as per normal, just choose the base controller class to extend. Example;

class Foo extends AdminLayout {

    public function __construct() {
        parent::__construct();
        if (is_logged_in() == FALSE) {
            $this->session->set_userdata('return_to', uri_string());
            $this->session->set_flashdata('message', 'You need to log in.');
            redirect('/home');
        }
    }
}

or

class Bar extends ModLayout {

    public function __construct() {
        parent::__construct();
        if (is_logged_in() == FALSE) {
            $this->session->set_userdata('return_to', uri_string());
            $this->session->set_flashdata('message', 'You need to log in.');
            redirect('/home');
        }
    }
}

这篇关于CodeIgniter扩展多个控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆