http://www.girishgaurav.com/js/ga_social_tracking.js

«

»

Jul 07

Loading library/model classes from your views in CodeIgniter




Hi,

When I was new to CodeIgniter, I need some data manipulation using a library function in my view classes, I simply loaded the library and called the library function, as I always do….

$this->load->library(‘some_library’);

$this->some_library->some_library_function(some_parameters);

but when run this code, I surprised by the error:

Before this I had used library functions many times in the code, and it always works fine, and after checking the code thoroughly I didn’t find any mistake in the code.

So, what happened with that code?

then I realize that I always use the library functions in the Controllers, this is the first time I’m using it in the views.

then I found the problem! You can do almost everything in the view, even when you shouldn’t, but loading library, models etc. has to be done in the controller. After searching and reading many articles on the net I found a solution to use library functions anywhere in the application with the help of get_instance() method.

get_instance() method returns the CodeIgniter super object. this is used to access CodeIgniter native resources from within your custom classes. Normally, to call the codeigniter’s member function we use $this object i.e. $this->load->library(‘some_library_name’); or $this->load->helper(‘some_helper_name’);

but if you want to use your these functions from views, or any other custom class you have to use &get_instance() method to get the codeigniter’s object.

$ci_obj = & get_instance();
$ci_obj->load->library('some_library');
$ci->some_library->some_library_function();

or you can use the given helper function to load library from anywhere in the code:

function load_library($library_name) {

$ci = & get_instance();
$ci->load->library($library_name);
return $ci->$library_name;

}

it will return the library object and you can call any library function using that object in anywhere in the views or any other custom class.

 

77 comments

Skip to comment form

  1. Zachary Shishido

    We appreciate you all of your time & operate. As a Newcomer, I will be consistently performing a search online relating to content material articles that could be involving aid me.

  2. floor tape

    I do not even know how I ended up here, but I thought this post was great. I don’t know who you are but certainly you’re going to a famous blogger if you are not already 😉 Cheers!

  3. Mirtha Alfaro

    I like this weblog so much, saved to my bookmarks.

  4. Lawanda

    Fanatastic post! Makes me change my embargo on commenting

  5. buy email lists

    I was just looking for this information for some time. After 6 hours of continuous Googleing, at last I got it in your website. I wonder what is the lack of Google strategy that do not rank this kind of informative websites in top of the list. Usually the top sites are full of garbage.

  6. Rayford

    comments really should often be made whenever you read some thing as very good as this, thanks a million

  7. pro flight simulator demo download

    Made my day once again, wonderful post..two thumbs up!:)

  8. play slots for free

    A well written story. I’m always looking for news like this.

  9. Carry Blazier

    As a site owner I believe the material here is reallyfantastic. I appreciate it for your efforts. You should keep it up forever! Best of luck.

  10. url

    Heya, I just hopped over to your webpage via StumbleUpon. Not somthing I might typically browse, but I appreciated your thoughts none the less. Thank you for making something well worth reading through.

  11. Visit Site

    How did you make a blog site look this awesome. Email me if you get the chance and share your wisdom. Id be appreciative.

  12. extreme couponing

    There are some interesting points on time in this article nonetheless I don’t know merely see every one of them center to help heart. There exists some quality but I most certainly will take have opinion until eventually I investigate it further more. Good write-up , thanks therefore we want much more! Added to FeedBurner also

  13. Dawfydd

    I just need to notify you that I’m extremely new to blogging and seriously savored you might be webpage. Possible I’m possible to bookmark your site . You actually have remarkable reports. Thank you a bunch for sharing with us your webpage.

  14. Gale Alesse

    I will immediately grab your rss feed to stay informed of any updates.

  15. sciatica

    You need to really control the remarks listed here

  16. 5s

    Wonderful goods from you, man. I have understand your stuff previous to and you’re just too fantastic. I actually like what you have acquired here, certainly like what you’re stating and the way in which you say it. You make it enjoyable and you still take care of to keep it smart. I cant wait to read far more from you. This is really a terrific web site.

  17. vps hosting

    Hi my family member! I wish to say that this post is amazing, great written and come with approximately all vital infos. I would like to look more posts like this .

  18. download free movies

    Good day! Do you use Twitter? I’d like to follow you if that would be ok. I’m definitely enjoying your blog and look forward to new updates.

  19. trampolineavecfilet

    Some genuinely select articles on this site, saved to bookmarks .

  20. Fred Viejo

    I really like what you are saying, and am glad I found your website. Please keep it up.

  21. reverse cell phone number lookup

    Very great visual appeal on this internet site , I’d rate it 10 10.

  22. turbotax free 2011

    I love your wp format, where did you get a hold of it?

  23. cell phone directory

    I like this blog very much, Its a really nice place to read and obtain info .

  24. Scoliosis

    I believe other website proprietors should take this website as an example , very clean and excellent user genial design .

  25. HVAC training

    Brilliant, thank you, I will bookmark you now!

  26. robot multifonction

    I gotta favorite this web site it seems very useful very useful

  27. sarayın incisi

    Amazing article admin thank you. I observed what i used to be looking for here. I’ll review complete of posts in this time of day

  28. Locksmith Toronto

    Locating dependable locksmith professional professional is an excellent means of preserving your current locks.

  29. VICTOR

    Whenever I originally commented I clicked the

  30. Teena Gehr

    Spot on with this write-up, I truly think this website needs far more consideration. I’ll most likely be once more to learn way more, thanks for that info.

  31. robot menager

    Howdy very cool web site!! Man .. Excellent .. Superb .. I’ll bookmark your blog and take the feeds additionally…I am glad to search out so many helpful information here in the post, we’d like work out extra techniques in this regard, thank you for sharing.

  32. emt basic certification

    woh I am glad to find this website through google.

  33. bottes pas cher

    As soon as I observed this internet site I went on reddit to share some of the love with them.

  34. Free Stuff

    Normally I do not read article on blogs, however I wish to say that this write-up very pressured me to take a look at and do it! Your writing style has been surprised me. Thank you, very nice post.

  35. sacoche homme

    I envy your work , thanks for all the good blog posts.

  36. appareil photo bridge pas cher

    I truly enjoy looking at on this internet site , it contains good content .

  37. zakłady sportowe

    Thanks for sharing excellent information. Your web site is so cool. I’m impressed by the details that you have on this blog. It reveals how nicely you perceive this subject. Bookmarked this web page, will come back for extra articles.

  38. plagiarism tester

    Thanks for this distinct blog; this is the kind of thing that keeps me on track through my day. Been looking around for your blog after being sent to it from a buddy and was thrilled when I stumbled on it after googling for a while. As a dedicated blogger, I’m excited to see authors like you taking initivative and contributing to the community. Just commenting to show my appreciation for your work as it’s quite inspiring, and many bloggers do not get acceptance they deserve. I am sure I’ll visit again and will recommend to my friends.

  39. Kathaleen Hollack

    Aw, this was a really nice post. In thought I want to put in writing like this moreover – taking time and precise effort to make a very good article… but what can I say… I procrastinate alot and on no account seem to get one thing done.

  40. Seksikauppa

    Have you thought about adding some differing opinions to your article? I think it might enhance everyones understanding.

  41. michigan personal injury attorney

    Brilliant, thanks, I will visit again soon!

  42. kasyno

    Good web site! I truly love how it is easy on my eyes it is. I’m wondering how I could be notified when a new post has been made. I’ve subscribed to your RSS feed which may do the trick? Have a nice day!

  43. Tonda Tilmon

    Can I simply say what a aid to search out someone who actually is aware of what theyre talking about on the internet. You positively know tips on how to bring an issue to gentle and make it important. Extra people need to learn this and understand this side of the story. I cant believe youre not more widespread because you definitely have the gift.

  44. kasyno

    Just wish to say your article is as astounding. The clarity in your post is just cool and i could assume you are an expert on this subject. Fine with your permission let me to grab your RSS feed to keep up to date with forthcoming post. Thanks a million and please carry on the gratifying work.

  45. Xeriscaping

    Thank you very a lot for writing this post. I enjoyed it.

  46. Film Schools in California

    Definitely, what a fantastic site and educative posts, I surely will bookmark your website.All the Best!

  47. Karrie Mech

    I’d forever want to be update on new blog posts on this web site , saved to fav! .

  48. Cassondra Wilbanks

    Heya i am for the first time here. I found this board and I find It truly useful & it helped me out much. I hope to give something back and help others like you aided me.

  49. Youlanda Stoppel

    Hello there! Would you mind if I share your blog with my myspace group? There’s a lot of folks that I think would really enjoy your content. Please let me know. Thank you

  50. Myung Ballestas

    Hello there, I found your website via Google while looking for a related topic, your site came up, it looks great. I have bookmarked it in my google bookmarks.

  51. disneys magical express

    This is a great blog for finding articles on this topic. I know this information exists elsewhere, but you have done a great job condensing it down for all.

  52. gafas arnette

    There are some exciting points in time in this article nonetheless I don’t know merely see these center for you to heart. There may be some credibility but Let me take carry opinion right until I explore it further more. Good post , thanks so we want additional! Added to FeedBurner in addition

  53. Emmett Tamaro

    I just want to mention I am just newbie to weblog and actually enjoyed this web-site. Almost certainly I’m likely to bookmark your website . You definitely come with fabulous articles and reviews. Bless you for sharing with us your blog.

  54. debt

    Awesome post! I will keep an on eye on your blog.

  55. Osvaldo Niedermayer

    You have raised quite a terrific details, respect to the write-up.

  56. natural food diet

    Love your blog!

  57. cross stitch

    Love your blog!

  58. westfield new jersey real estate

    Great post!

  59. new jersey tax appeal

    Love your blog!

  60. breeding dogs

    Great post!

  61. How To Get Rid Of Cold Sores

    This is by far my favorite blog! Keep up the good work!

  62. Over 40 Skin

    Awesome writing style!

  63. corporate gifts

    Finally! A blog worth reading! I bookmarked your blog!

  64. printing press

    Love your blog!

  65. Kansas dui lawyer

    Love your blog!

  66. Useful internet marketing tips

    Love your blog!

  67. Boston shows and concerts

    Great work! I wish I could write this well!

  68. lose weight

    Ones web page is definitely specifically valuable for myself. I bookmarked your site!

  69. pepeluca

    Hey There. I found your page searching with yahoo. This is an extremely well written article. I will be sure to bookmark it and return to read more of your useful info. Thank you for sharing it. I will absolutely be back again.

  70. Xochitl Wichterman

    Love your blog!

  71. svchost.exe

    cool story bro

  72. concord replica watches

    I like reading blog posts, and when I stumbled upon to this weblog, it just blew me away! Hey there I mean it! Your contents are rich and I discover them very useful!

  73. replica watches

    Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information.

  74. Replicaonline

    This is the most influential thing I read all day.

  75. mantar hastalığı

    Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to more added agreeable from you! However, how can we communicate?

    1. Girish Gaurav Agarwal

      you can contact me blog@girishgaurav.com

  76. Varun Dublish

    Hey Gaurav…. Remember Facing this problem a lot of times. And yes the getinstance method was really really helful.

Leave a Reply

%d bloggers like this: