I am using CodeIgniter I would like to use helper, my helper function would be need to call another methods that belong to other controllers. Do I possible to do that?
Helpers are not written in an Object Oriented format. They are simple, procedural functions. Each helper function performs one specific task, with no dependence on other functions.
So if your helper function would need to call another method of a controller, then there is no point in creating it as a helper function.
http://ellislab.com/codeigniter%20/user-guide/general/helpers.html
Related
I've been looking around for best practices on where to put some helper methods that do all this calculation / sorting for me but I haven't found a definitive answer yet and was wondering if someone had some good insight for me.
Basically I have an action method that takes a string user input, finds similar words to that string, does a bunch of string manipulation, and then ordering to return an array.
I don't know whether I should have module in /lib, make a controller helper module, ... Where I'm looking for some feedback!
But essentially I just want to:
POST a word to a controller action method
Call a helper method or execute some logic on the word outside of the controller
Have that helper method or wherever that logic will be, return to me the result
Just put that method in your application helper, wherever it is called from it will return the resulting values after processing your logic, you should not over complicate things so the good old application helper is a good place to put the common method used in views and in your controllers
I wouldn't call a helper method from your controller. Personally I would use either a module/plain old ruby object or a concern to handle calculations. The controller stores the object in an instance variable so it can be used througout your views.
Also worth noting are decorators, you should take a look at draper: https://github.com/drapergem/draper
I have many helper functions that require access to get_instance(). For example, here is a function to get a list of members for use in a dropdown:-
function get_members_list()
{
$EE =& get_instance();
$EE->load->model('member_model');
// rest of my code
return $members_list;
}
I put it in a helper because there's a lot of extra code in here, something I would not consider suitable for model, but more for a controller. And then because several libraries/controllers are accessing it, I thought move it a helper.
So my question is, is it OK to use get_instance() and load helpers, within helper functions? I have get_instance() used many times in my helper file, because each function needs to call it.
Or is there a way for helpers to access get_instance() that is already loaded in the controller/library that is loading the helper?
Thanks
I would put it in a library.
How you organize things is really up to personal preference, but having a library would make the most sense. You can group it by data type, or just have a single library class for key/value data (for use in drop downs).
I wrote a three cascade dropDownLists that its listData are generated from the database models.
The lists are generated with an Ajax call to action in the controller based.
I want to reuse this code and to share it with more pages.
I tried to do the following:
Write it as a Custom Widget.
currently i use 'createurl' function that calls a function in the matching controller.
I cant write JavaScript since i want to use the existing db models.
In this case i need to write the action functions in an independent file - so should i write a controller? where should i place it?
Write it as a part of a module - but it seems overkill.
any suggestions, i am sure that there is a right and simple way to do it.
You could create it as a helper. A helper is just a class in the components which has no direct action in the M->C->V action flow but can be used in any controller, model, view, component, module, etc...
I would write a helper method to call it from the controller.
Another suggestion could be to extend CController to your own base controller and have your actual controllers, extend from your custom base controller. That way you can make it easily available in every controller, and then you just set some members that contain the models to use which you set in the actual controller.
If you need more help on this, find me on freenode #yii
There is a helper which I would like to use inside a core class, CI_Router (MY_Router, to be more accurate). In this custom router, I made some modifications to the original code, in order to be able to insert hyphens into my urls.
I have defined the helper on the autoload.php file, as usual, but it seems that I canĀ“t invoque a helper function inside a class other than a view or controller.
Any ideas about how to handle this? My initial approach was to use a helper, so I can reuse it on any place I want.
TYVM.
Helpers are not instantiated until after the core, thus why it does not work.
You will either have to:
Duplicate the function in your MY_Router class, or,
Rethink why you might be using the same function in the Router that you use in a standard controller or view.
Option 1 is obviously easier, but might not be preferable depending on how bad your OCD is.
You could try getting the instance of the main CI object and setting it to a variable, then load the helper using that. Ex:
$ci =& get_instance();
$ci->load->helper('date');
I know that works in other areas, not 100% sure about any of the router classes.
I was wondering if its possible to have one or more functions within a funciton like in jQuery? I need to add an inline function that performs a simple task but I really don't need it anywhere except the function I'm calling it from.
Not possible, but you could create a class with the functions you need.