Welcome Guest, Not a member yet? Register   Sign In
how to create some common function in controller
#1

[eluser]quals_jack[/eluser]
Hi, all

I have some common function in controllers used by many views with Ajax. How do I save those common functions in one place instead of coding in every controller? Thank you in advance!

Jack
#2

[eluser]Pascal Kriete[/eluser]
Either create a parent controller (put it in libraries/MY_Controller.php) and extend that, or make a library and autoload it.

If it's just one or two functions you might consider a helper as well.
#3

[eluser]quals_jack[/eluser]
Thank you inparo. You mean I create a MY_controller which extands Controller class and then my controllers extend My_controller. Would you please give me a simple template of this structure. Thank you again!
#4

[eluser]Pascal Kriete[/eluser]
Sure thing. The name of the class isn't really important (just have to extend it properly), but the file must be called MY_Controller.

./application/libraries/MY_Controller:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class App_Controller extends Controller {

    /**
     * Constructor
     *
     * @access    public
     */
    function App_Controller()
    {
        parent::Controller();
    }
    // --------------------------------------------------------------------

    /**
     * A function
     *
     * @access    public
     */
    function fun_function()
    {
        $this->load->library('fun');
        $this->fun->cookie_monster('eat');
    }
}
// END App_Controller class

/* End of file MY_Controller.php */
/* Location: ./application/libraries/MY_Controller.php */

Usage:
Some Controller:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Prefs extends App_Controller {

    /**
     * Constructor
     *
     * @access    public
     */
    function Prefs()
    {
        parent::App_Controller();
    }

    // --------------------------------------------------------------------
    
    /**
     * Controller Default Function
     *
     * @access    public
     */
    function index()
    {
        // Have fun
        $this->fun_function();

        // Do other stuff...
        $this->layout->gen_crumb(array(
                                'preferences/'        => 'Preferences'
                                ));
        
        $this->layout->render('utilities/prefs');
    }
}
// END Prefs class

/* End of file prefs.php */
/* Location: ./application/controllers/utilities/prefs.php */
#5

[eluser]quals_jack[/eluser]
Thank you very much, Inparo!
#6

[eluser]quals_jack[/eluser]
I've got problem to call functions in My_Controller. It reported as undefined function. I saved it in libraries under application folder.
#7

[eluser]quals_jack[/eluser]
I mean some functions in My_Controller are working like utilites called by funtionfs in Prefs, in your examples. Should I declare the utility functions as public?
#8

[eluser]Pascal Kriete[/eluser]
Could you show us a small example of how you're calling them - I'm not following.

Also, have you explored the idea of using a library or helper? It sounds like you're trying to put very, very general functions in the base controller. One or two of those are ok, but it gets messy pretty fast.
#9

[eluser]quals_jack[/eluser]
at first, I forgot $this-> when calling functions in my_controller. That's why they became undefined functions. And yes, as you said if I put very general functions in base controller, it's messy. So I put those functions in a helper. Thank you so much, Inparo!

Jack




Theme © iAndrew 2016 - Forum software by © MyBB