Using helper function inside a Core Class in CodeIgniter - codeigniter

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.

Related

Do I have to use get_instance() in every helper function that needs access to native resources?

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).

Basic on Codeigniter controller

I am new in codeigniter framework.I have some question.When we write some controller class then we call constructor.I am loading some library and some helper.I want to know what is the perfect way for loading this helper and library under construct class or under other functions.If i load everything in construct class then what is the disadvantages for it?if i use more controller class for one project then it is good or bad.Like i want to use some controller class for ajax functionalities,some for form submission,some for other sector.My english is not so good.Please help me.
For common libraries and helpers used by all functions of your controller try to load it in constructor or autoload. Except that for specific libraries e.g. payment gateway load inside the function.
How you use or how many controllers you are using depends upon your needs. But I would suggest you to build separate controllers for separate functions. Like admin for admin functions, users for user related functions, and so on. It build a user friendly URL too.
Well! everything depends on your requirements.
If you need any library , model or helper globally you can autoload it autoload.php.
It means the loading will be done on each request.
if you need any library , model or helper throughout your controller methods you can load them in constructor.
It means loading will be done on each method of controller.
if you need any library , model or helper for specific method you can load them in method.
It means loading will be done in method only.
For example let suppose you nees session and database library throughout your application so you can autoload it.
For a specific controller you need priviliges library so load it in constructor.
For email you need to send load email library in function.
These are only examples. You can explore SO to find more.
Loading takes time and performance. Not noticeable time by human standards, but time regardless. In order to cut of some slack to your server in the future, you do it a favor by only loading whatever is it you require for each page/controller method.
Used (almost) always
You can use application/config/autoload.php for things you almost always use. I usually include the helper url here.
Used (almost) always inside specific controller
Load it inside controller __construct. Don't forget to include parent::__construct();.
Used inside specific page
Load it inside method/function.
Used inside specific function
Load it at beginning or inside if-statement in function. You can load it numerous of times, it will only execute first time.

Which is better in codeigniter? Adding a function in a helper or adding a function in an extended base class

In a codeigniter project i have to do some set of stuff in one than one controller.
I code all that stuff in a function and now i need to call whenever necessary.
i think Writing this function in more than one controller is not good.
i have 2 options,
create a helper and write these function in that and include the helper in necessary controllers.
Since i have extended CI base controller (My_Controller) and most of my controllers are extended that controller, i can write this function to my base controller also.
I have confused which one is better, right way?
Which one will speed up the process?
Is the second way slows the site?
They are identical for all intents and purposes.
Using a helper allows you to make the code portable, so you can use it in other projects, or to be called from anywhere in the code base, in the case of a formatting function for example
If you were planning to put it in a controller, then MY_Controller is best bet
Just to help you on you endeavor what i do is: (this is just me)
If i need to use something in the views, i use a helper a custom one or built in.
If i want to do something on a controller that other controller will be using too and don't want it to mess up or crowd my controller i use a library (pretty much you can use a helper but i chose to use a library)
If i want to load lets say a method, to affect Globally or some of the controller i use the base controller. (you could also use helper or library)
The key is you are not restricted to one, choose the best that suits you, as the saying goes, there are many ways to skin a cat, but please don't skin a cat..

CodeIgniter: what is the scope of load->helper

In a controller class I have this function:
public function index(){
$this->load->helper('url');
$data['title'] = 'News archive';
$this->load->view('news/index', $data);
}
I load helper url because I'm using anchor() in news/index. So seems like it's enough to load helper in the parent function, and I don't have to load it inside news/index.
So my question is what's going on underneath CI that lets me do this? Is load->view a function, or is it pasting the result of executing news/index on $data? How is load->view aware of helper url having been loaded in index? I'm still trying to make sense of how the CI framework works.
Also what would be the best place to load helper, in the constructor, or in each function as we need it?
Also what would be the best place to load helper, in the constructor, or in each function as we need it?
As a rough rule of thumb;
If you use the helper once in a controller - place it in that specific function
If you use the helper in multiple places in a controller - place it in that controllers constructor
If you use the helper in multiple places in multiple controllers - place it in the 'autoload' section once.
you can get all the answer if u go through the Loader Class in codeigniter...
path >> system/core/loader.php
everything that is done is here....
and for ut last question . according to the user guide
http://ellislab.com/codeigniter/user-guide/libraries/loader.html
loader, as the name suggests, is used to load elements. These elements can be libraries (classes) View files, Helpers, Models, or your own files.
so since it just loads the elements...
i usually(prefer) loading it in each function where needed. (unless i need the same elements in other functions too)

CodeIgniter Model Call to Model

Im using CodeIgniter 2.0.2 and I noticed while calling a Model from within a Model, you dont need to load it.
For instance, in a Controller you need to write
$this->load->model('my_model');
$this->my_model->my_function();
But in a Model it can load just like this
$this->my_model->my_function();
Should i avoid writing my code like this, or is this safe?
I would avoid writing my code like this, but for a different reason.
Models are generally loaded from controllers, so it seems strange that you would need one model to call another one. Are you sure that there is not a better way to structure your code, such as having a model base class or using a helper for common functionality?

Resources