Does helper loads all functions on load in CodeIgniter - codeigniter

In my helper there is a function ie abc which involves database operations. While we are loading a helper using
$this->load->helper('common_helper');
will it load all the functions, ie will it perform the db operation in function abc automatically. Or it will perform the db operation while calling the function
abc();

It will load all the functions inside your helper. You can call any method without loading this helper again.
Yes, you need to call specific method for each tasks you have defined in your method.

No, helper not loads all function on load, its just link up with helper file.
when you call a helper function then load this function.

Related

Trying to understand the view method and general method calling in laravel

I am a new to laravel and trying to understand where the view method comes from and what mechanism allows it to be shown in the web.php folder in laravel.
For example :
Route::get('/', function () { return view('welcome'); })
I guess the view function is defined in some class. Bu which class is it and where is that class made reference to in order to access its method?
Thanks a lot if you can help me understanding this!
In most IDEs you can hold CTRL and left-click the function to view it's definition. view() is not defined in a class. It comes from a file called helpers.php.
This file is included at the beginning, so its functions can be used afterwards.
PHP is not only object oriented. Procedural and object oriented programming can be mixed together.
What I do usually in these cases is to search in the whole project (and remember to include vendor directory in your search) for: "function YOUR_FUNCTION_NAME" because somewhere in PHP there must be that function declared, whether is in a class or in a simple .php file.
view() method is a helper method inside src/Illuminate/Foundation/helpers.php. All the methods that declare here will be available everywhere inside Laravel application. You can check view() method here

How to call method from another controller to helper function?

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

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.

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)

Resources