php 在 CodeIgniter 中调用另一个控制器中的控制器函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6647112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:54:30  来源:igfitidea点击:

Calling a Controller function in another Controller in CodeIgniter

phpcodeigniterclass-visibilitycodeigniter-classloader

提问by Roman

I have a controller "user" in my codeigniter application. This controller has a function called logged_user_only():

我的 codeigniter 应用程序中有一个控制器“用户”。这个控制器有一个函数叫做logged_user_only()

public function logged_user_only()
    {
        $is_logged = $this -> is_logged();
        if( $is_logged === FALSE)
        {
            redirect('user/login_form');
        }
    }

As this function calls another function called is_logged(), which just checks if the session is set, if yes it returns true, else returns false.

由于此函数调用另一个名为 的函数is_logged(),该函数仅检查会话是否已设置,如果是,则返回 true,否则返回 false。

Now if i place this function in the begining of any function within same controller, it will check if the user is not logged, it will redirect to login_formotherwise continue. This works fine. For example,

现在,如果我将此函数放在同一控制器内任何函数的开头,它将检查用户是否未登录,它将重定向到login_form否则继续。这工作正常。例如,

public function show_home()
    {
        $this -> logged_user_only();
        $this->load->view('show_home_view');
    }

Now I would like to call this logged_user_only()function in a function of another controller to check if the user is logged in or not?

现在我想logged_user_only()在另一个控制器的函数中调用这个函数来检查用户是否登录?

PS. If this can not be done, or is not recommended, where should i place this function to access in multiple controllers? Thanks.

附注。如果这不能完成,或者不推荐,我应该把这个功能放在哪里才能在多个控制器中访问?谢谢。

回答by simnom

Why not extend the controllers so the login method is within a MY controller (within the core folder of your application) and all your other controllers extend this. For example you could have:

为什么不扩展控制器,以便登录方法位于 MY 控制器内(在您的应用程序的核心文件夹内),而您的所有其他控制器都扩展它。例如你可以有:

class MY_Controller extends CI_Controller {
    public function is_logged()
    {
        //Your code here
    }
}

and your main controllers could then extend this as follows:

然后您的主控制器可以按如下方式扩展它:

class Home_Controller extends MY_Controller {
    public function show_home()
    {
         if (!$this->is_logged()) {
           return false;
         }
    }
}

For further information visit: Creating Core System Classes

欲了解更多信息,请访问: 创建核心系统类

New link is here: https://www.codeigniter.com/user_guide/general/core_classes.html?highlight=core%20classes

新链接在这里:https: //www.codeigniter.com/user_guide/general/core_classes.html?highlight=core%20classes

回答by fire

Calling a controller from another is not possible with CI and not recommended.

使用 CI 无法从另一个控制器调用控制器,也不推荐使用。

Either move your logged_user_onlyinto a helper or even better a core controller that you extend all of your controllers from (MY_Controller) see http://codeigniter.com/wiki/MY_Controller_-_how_to_extend_the_CI_Controller/

要么将您移动logged_user_only到帮助程序中,或者甚至更好地将您从 ( MY_Controller)扩展所有控制器的核心控制器移动到http://codeigniter.com/wiki/MY_Controller_-_how_to_extend_the_CI_Controller/

回答by upendra allu

just load user controller as library from your controller

只需从您的控制器加载用户控制器作为库

function __construct(){
     parent::__construct();
     $this->load->library('../controllers/user');
}

Now, call this function of user controller any where in your controller,

现在,在控制器中的任何位置调用用户控制器的这个函数,

$this->user->logged_user_only();

回答by Muhammad Nashrullah

Function login in Controller Login

控制器登录中的功能登录

$data =array('username' => $this->input->post('username'), 'password' => $this->input >post('password')) $query = $this->db->get_where('usertable',$data)
    if ($query->num_rows() == 1) {
        $data = array(
            'username' => $this->input->post('username'),
            'logged_in' => TRUE,
            'role' => "user");
        $this->session->set_userdata($data);
        redirect('home');
    } 

Function Construct in Controller user

控制器用户中的函数构造

if ($this->session->userdata('logged_in') == TRUE && $this->session->userdata('role') == "user") {} else {redirect('home');}