CodeIgniter: what is the scope of load->helper - codeigniter

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)

Related

How to use $this in another file on Codeigniter?

I have a file named fn.php in my view, then I create a function to post data, like this
function post($input_name){
return $this->input->post($input_name);
}
In my controller I call it like this,
public function myFunc(){
$this->input->load('fn.php');
// then I use the function that I have created like this
post('myinputname');
}
But... I got error, how I can solve this problem? thank you so much
You should take some time to try and understand the MVC architecture. It is, after all, one of the main points of using a framework.
You can't put functions in a view and expect to load them somehow and access them. You can put functions in a model, controller, library or helper. In you case I would suggest a helper:
application/helpers/some_file_helper.php
function post($input_name){
$CI = &get_instance();
return $CI->input->post($input_name);
}
The get_instance() part is only used when $this (CI context) isn't available. This only happens in helpers and libraries. In views, controllers, and models $this is always available.
Model or controller:
$this->load->helper('some_file');
print_r(post('somevar'));
However if all you want to do is access the post variable use $this->input->post('somevar') directly and don't introduce an extra layer.

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

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.

Using helper function inside a Core Class in 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.

Resources