如何在codeigniter中调用另一个控制器中的一个控制器功能 [英] How to call one controller function in another controller in codeigniter

查看:1077
本文介绍了如何在codeigniter中调用另一个控制器中的一个控制器功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 home.php 的控制器,其中有一个名为 podetails 的函数。我想在另一个控制器 user.php 中调用此函数。

是否可以这样做?我已经阅读了在CI中的 HMVC ,但我想知道是否可以不使用hmvc?

I have one controller named home.php in which a function named podetails is there. I want to call this function in another controller user.php.
Is it possible to do so? I have read about HMVC in CI, but I want to know is it possible to do without using hmvc?

推荐答案

要扩展控制器,请按照 tutorial 或查看下面的一些代码。

To extend controller please either follow this tutorial or see some code below.

private / public / protected

/ application / core / 命名为 MY_Controller.php

在该文件中有一些代码如

Within that file have some code like

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

class MY_Controller extends CI_Controller {

    protected $data = Array(); //protected variables goes here its declaration

    function __construct() {

        parent::__construct();
        $this->output->enable_profiler(FALSE); // I keep this here so I dont have to manualy edit each controller to see profiler or not        
        $this->load->model('some_model'); //this can be also done in autoload...
        //load helpers and everything here like form_helper etc
    }

    protected function protectedOne() {

    }

    public function publicOne() {

    }

    private function _privateOne() {

    }

    protected function render($view_file) {

        $this->load->view('header_view');
        if ($this->_is_admin()) $this->load->view('admin_menu_view');

        $this->load->view($view_file . '_view', $this->data); //note all my view files are named <name>_view.php
        $this->load->view('footer_view');

    }

    private function _isAdmin() {

        return TRUE;

    }

}

您现有的任何控制器只需编辑第一行或第二行

and now in any of yours existing controllers just edit 1st or 2nd line where

class <controller_name> extends MY_Controller {

并完成

也请注意,所有您要在视图中使用的变量都在此变量(array)$ this-> data

also note that all your variables that are meant to be used in view are in this variable (array) $this->data

MY_Controller扩展的控制器示例

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

class About extends MY_Controller {

    public function __construct() {

        parent::__construct();

    }

    public function index() {
        $this->data['today'] = date('Y-m-d'); //in view it will be $today;
        $this->render('page/about_us'); //calling common function declared in MY_Controller
    }

}

这篇关于如何在codeigniter中调用另一个控制器中的一个控制器功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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