Reputation: 105
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.
Upvotes: 0
Views: 68
Reputation: 19882
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.
Upvotes: 1
Reputation: 10996
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.
You can use application/config/autoload.php
for things you almost always use. I usually include the helper url
here.
Load it inside controller __construct
. Don't forget to include parent::__construct();
.
Load it inside method/function.
Load it at beginning or inside if
-statement in function. You can load it numerous of times, it will only execute first time.
Upvotes: 1
Reputation: 7475
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.admin
for admin functions, users
for user related functions, and so on. It build a user friendly URL too.Upvotes: 2