CodeIgniter Forums
Call controller method within library without redirect - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Call controller method within library without redirect (/showthread.php?tid=68145)



Call controller method within library without redirect - Russ_AB - 06-01-2017

Hey!

I'm building a library for CI and I need to run the callback after my logic.

How do i call a controller method within library?

I tried different ways and nothing worked Sad

PHP Code:
$default_controller_callback 'Welcome';
$default_method_callback 'custom_message';

// ERROR:  Undefined property: Welcome::$load
$controller = new $default_controller_callback();
call_user_func(array($controller $default_method_callback));

// ERROR: Got Strict standards: call_user_func() expects parameter 1 to be a valid callback, non-static method 
// Welcome::custom_message() should not be called statically
call_user_func(array($default_controller_callback $default_method_callback)); 

Here is the full library code: https://github.com/russ-ab/CodeIgniter-Custom-Exception-Handler/blob/master/libraries/Custom_Exception.php#L192

I want to change the redirect to real callback calling.

Thanks!


RE: Call controller method within library without redirect - PaulD - 06-01-2017

I have not looked at your full code (sorry) but I believe you cannot do what you ask (ie call controller method from somewhere else) without HMVC or similar. (I am not 100% on this though)

However there is no reason why your callback in, say, controller A, cannot call a common function (say 'common_callback_function') from a model or a library to get a result, just like any library or model call works. It is just a function after all. Then you are free to reuse that common_callback_function in any other library, or from where ever you want. Keeping everything DRY.

I hope that makes sense.

Paul.