CodeIgniter Forums
accessing other controller methods - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: accessing other controller methods (/showthread.php?tid=1606)



accessing other controller methods - vinay.amaraneni - 03-25-2015

Hi,

Please any one can tell me how to access the methods of one controller ito another controller methods without Extends


RE: accessing other controller methods - rk128 - 03-25-2015

I think you can't access a controller method in another controller in CI. but you can do it define a library.

1. create MyFunctions.php in to application/libraries folder.

<?php
/*MyFunction.php*/
defined( 'BASEPATH' ) or exit( 'No direct script access allowed' );

class MyFunctions {

public function __construct( $config = array() ) {
// initialize
}

public function function1() {
// your code here
}
}

2. setting autoload.php in application/config folder to auto load your library.

$autoload['libraries'] = array('database', 'email', 'session', 'MyFunctions', 'table', 'upload' );

3. call your methods everywhere.

$this->myfunctions->function1();

thats it.


RE: accessing other controller methods - josetrindade - 03-25-2015

(03-25-2015, 05:06 AM)[email protected] Wrote: Hi,

Please any one can tell me how to access the methods of one controller ito another controller methods without Extends

Hi,

What you want is HMVC, meaning Hierarchical Model-View-Controller.

Codeigniter will not do that out of the box, but you can always install a third-party HMVC extension, like the one Wiredesigns has on their bitbucket: https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc.

Then all you have to do is add modules::run('controller_name/method_name', $params_array) inside your controller.


RE: accessing other controller methods - CroNiX - 03-25-2015

Or use a library as suggested earlier. You can use it in as many controllers as you need.


RE: accessing other controller methods - sv3tli0 - 03-25-2015

Just try to separate what "Use" means in your case..
Usually Controller methods are used as Actions and they care about some special request..
For usage you usually need some library , model or something as that..