【问题标题】:how to use multiple controllers in codeigniter如何在codeigniter中使用多个控制器
【发布时间】:2016-02-04 13:14:27
【问题描述】:

我目前正在使用 codeigniter 进行一个项目。我想为每个功能分开我的控制器。

例子,

controller_for_login.php
controller_for_redirecting_to_other_views.php
controller_for_CRUD.php
controller_for_others.php

有没有办法让它变成这样?这样我的代码就会井井有条。谢谢。

【问题讨论】:

标签: php codeigniter controllers


【解决方案1】:

很简单

1)首先在控制器文件夹中创建一个新文件,例如Classname.php

2) 编辑该文件

class Classname Extends CI_Controller
{
}

3) 把你的新函数放在这个类文件中

将类文件放入库文件夹 像这样的文件

class Authenticate {

    public function __construct()
    {
        $this->ci =& get_instance();

    }
  public function is_logged_in()
    {
        $sessionid = $this->ci->session->userdata('moderId');
        if($sessionid)
        {
        return isset($sessionid);
        }
         else if(!$sessionid) {
      redirect(base_url() . 'moderator');
 }
    }
}

在控制器文件中,像这样调用该类函数

class B2bcategory extends CI_Controller {    
    function __construct() {
        parent::__construct();
        $this->authenticate->is_logged_in();
    }
}

【讨论】:

  • 我已经这样做了。问题是,我不能从另一个控制器调用另一个类。
  • 在库文件夹中创建该类。并在你的控制器中调用它 $this->authenticate->is_logged_in();
  • 嗨,kev_m,请将类文件放入库文件夹,我编辑了我的答案,检查一下
猜你喜欢
  • 2015-09-13
  • 2013-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
  • 2018-01-09
  • 2019-01-16
相关资源
最近更新 更多